on 05-17-2019 8:14 AM
package com.company.aifunctionalbot.javamodule;
//import com.fasterxml.jackson.databind.PropertyName;
import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
//import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.productmaster.OperationalAcctgDocItemCube;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.productmaster.ProductFluentHelper;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.productmaster.Product;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.ProductMasterService;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultProductMasterService;
import org.slf4j.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import com.sap.cloud.sdk.odatav2.connectivity.ODataException;
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = CloudLoggerFactory.getLogger(Product.class);
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Message");
try {
List<Product> productResult =
new DefaultProductMasterService().getAllProduct().select(Product.PRODUCT_TYPE,
Product.PRODUCT_GROUP,
Product.PRODUCT)
.filter(Product.IS_RELEVANT_FOR_HZDS_SUBSTANCES.eq(false))
.execute();
response.setContentType("application/json");
response.getWriter().write(new Gson().toJson(productResult));
} catch (final ODataException e) {
logger.error(e.getMessage(), e);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write(e.getMessage());
}
}
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
BufferedReader reader = request.getReader();
Gson gson = new Gson();
RecastData myData = gson.fromJson(reader, RecastData.class);
try {
Entities entities = myData.getNlp().getEntities();
String property_name;
TextResponse textResponse = new TextResponse();
if(entities.getPropertyName()==null){
textResponse.setContent("Mhm, I can't identify this customer");
} else {
property_name = entities.getPropertyName().get(0).getValue();
List<Product> productResult =
new DefaultProductMasterService().getAllProduct().select(
Product.PRODUCT_TYPE,
Product.PRODUCT_GROUP)
.filter(Product.PRODUCT.eq(property_name))
.execute();
if (productResult.size() > 0) {
Product oneDoc = productResult.get(0);
textResponse.setContent("There are uncleared documents. For example document " + oneDoc.getProductGroup() + " with company " + oneDoc.getProductType());
} else {
textResponse.setContent("There are no uncleared documents! That is great");
}
}
ResponseWrapper responseWrapper = new ResponseWrapper(textResponse);
response.setContentType("application/json");
response.getWriter().println(responseWrapper.getResponse());
} catch (final ODataException e) {
logger.error(e.getMessage(), e);
response.setContentType("application/json");
response.getWriter().write(e.getMessage());
}
}
static class RecastData {
@SerializedName("nlp")
@Expose
private Nlp nlp;
public Nlp getNlp() {
return nlp;
}
}
static class Nlp {
@SerializedName("entities")
@Expose
private Entities entities;
public Entities getEntities() {
return entities;
}
public void setEntities(Entities entities) {
this.entities = entities;
}
}
static class Entities {
@SerializedName("product_name")
@Expose
private List<PropertyName> property_name = null;
public List<PropertyName> getPropertyName() {
return property_name;
}
public void setProduct(List<PropertyName> property_name) {
this.property_name = property_name;
}
}
static class PropertyName {
@SerializedName("value")
@Expose
private String value;
@SerializedName("raw")
@Expose
private String raw;
@SerializedName("confidence")
@Expose
private String confidence;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getRaw() {
return raw;
}
public void setRaw(String raw) {
this.raw = raw;
}
public String getConfidence() {
return confidence;
}
public void setConfidence(String confidence) {
this.confidence = confidence;
}
}
static class TextResponse {
String type = "text";
String content;
public String getResponse() {
Gson gson = new Gson();
return gson.toJson(this);
}
public void setContent(String content) {
this.content = content;
}
}
static class ResponseWrapper {
TextResponse[] replies;
Conversation conversation;
public ResponseWrapper(TextResponse replies) {
this.replies = new TextResponse[]{replies};
this.conversation = new Conversation(new Memory());
}
public String getResponse() {
Gson gson = new Gson();
return gson.toJson(this);
}
public Memory getMemory() {
return conversation.getMemory();
}
public void setMemory(Memory memory) {
this.conversation.setMemory(memory);
}
}
static class Memory {
}
static class Conversation {
Memory memory;
public Conversation(Memory memory) {
this.memory = memory;
}
public Memory getMemory() {
return memory;
}
public void setMemory(Memory memory) {
this.memory = memory;
}
}
}
Hi Dennis,
when i try to run the above servlet code in Web IDE it is showing 404 not found error.
And also i used instead of AccountingDocument API i used ProductMaster.
Here i am attaching the error log.
dkdacf0p1sc96cienalbot-javamodule.cfapps.eu10.hana.ondemand.com - [2019-05-13T05:32:36.227+0000] "GET / HTTP/1.1" 404 0 0 "https://webidecp-p2001207457trial.dispatcher.hanatrial.ondemand.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-" "10.0.74.133:61016" x_forwarded_for:"-" x_forwarded_proto:"https" vcap_request_id:"92b822bc-5d7a-4b75-7e10-4219b6f7888d" response_time:0.183527498 app_id:"db6fcfbb-d387-4e9b-804c-e74dedb1e1a6" app_index:"0" x_correlationid:"-" x_b3_traceid:"a58f6c031f6cd07c" x_b3_spanid:"a58f6c031f6cd07c" x_b3_parentspanid:"-" b3:"a58f6c031f6cd07c-a58f6c031f6cd07c"
dkdacf0p1sc96cienalbot-javamodule.cfapps.eu10.hana.ondemand.com - [2019-05-13T05:58:59.389+0000] "GET / HTTP/1.1" 404 0 0 "https://webidecp-p2001207457trial.dispatcher.hanatrial.ondemand.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-" "10.0.74.133:61016" x_forwarded_for:"-" x_forwarded_proto:"https" vcap_request_id:"2a423dd7-742d-4af7-5829-df581cebcd49" response_time:0.00622926 app_id:"db6fcfbb-d387-4e9b-804c-e74dedb1e1a6" app_index:"0" x_correlationid:"-" x_b3_traceid:"aa1202e3401dee5f" x_b3_spanid:"aa1202e3401dee5f" x_b3_parentspanid:"-" b3:"aa1202e3401dee5f-aa1202e3401dee5f"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry for the delay in my response.
Looking at your logs it seems like you're trying to access the root of you application (https://webidecp-p2001207457trial.dispatcher.hanatrial.ondemand.com/).
However, in your code you have the annotation @WebServlet("/hello"). Therefore (assuming the root path of your servlet being the root path of your app), shouldn't the resulting URL be https://webidecp-p2001207457trial.dispatcher.hanatrial.ondemand.com/hello ?
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.