Hi Experts,
I created Odata service for integration for Hybris and C4C.
In Hybris there are two methods for saving data.
1.
public class OrderNotificationServiceImpl implements OrderNotificationService
{
private final static Logger LOGGER = Logger.getLogger(OrderNotificationServiceImpl.class);
//private Converter<SalesOrderTest, AbstractOrderData> orderNotificationConverter;
private Converter<AbstractOrderModel, DemoSalesOrders> c4cOrderNotificationConverter;
private ObjectMapper jacksonObjectMapper;
private RestTemplate restTemplate;
//private Converter<AbstractOrderModel, TicketNote> updateMessageConverter;
@Override
public void createOrderNotification(final AbstractOrderModel orderModel, final String siteId)
{
LOGGER.info("Sending request to: " + HybriscxodataConstants.URL + HybriscxodataConstants.ORDER_SUFFIX);
try
{
if (orderModel.getStatus() == OrderStatus.PENDING_APPROVAL)
{
orderModel.setStatus(OrderStatus.CREATED);
}
final HttpHeaders headers = getEnrichedHeaders(siteId);
final HttpEntity<String> entity = new HttpEntity<>(
jacksonObjectMapper.writeValueAsString(c4cOrderNotificationConverter.convert(orderModel)), headers);
final ResponseEntity<String> result = restTemplate
.postForEntity(HybriscxodataConstants.URL + HybriscxodataConstants.ORDER_SUFFIX, entity, String.class);
LOGGER.info("Response status: " + result.getStatusCode());
LOGGER.info("Response headers: " + result.getHeaders());
LOGGER.info("Response body: " + result.getBody());
//final ODataSingleResponseData responseData = jacksonObjectMapper.readValue(result.getBody(),
// ODataSingleResponseData.class);
// return orderNotificationConverter
// .convert(((ODataSingleResultsOrderNotificationData) responseData.getD()).getOrderResults());
//return orderData;
}
catch (final IOException e)
{
LOGGER.warn("Can't convert orderData: " + e);
}
catch (final RestClientException e)
{
LOGGER.warn("Can't send request " + e);
}
//return null; // or throw
}
2.
@Override
public void updateOrderStatus(final AbstractOrderModel orderModel, final String siteId)
{
try
{
//final HttpHeaders updateTicketHeaders = getEnrichedHeaders(siteId);
//updateTicketHeaders.set(HttpHeaders.CONTENT_TYPE, Customerticketingc4cintegrationConstants.MULTIPART_MIXED_MODE);
final HttpHeaders orderStatusUpdateHeaders = addBatchHeaders(
"PATCH ZSALESORDERSRootCollection('" + orderModel.getCode() + "') HTTP/1.1");
//final HttpHeaders messageUpdateHeaders = addBatchHeaders(
// "POST DemoSalesOrdersRootCollection('" + orderModel.getCode() + "')/Notes HTTP/1.1");
//final MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
//LOGGER.info(c4cOrderNotificationConverter.convert(orderModel));
final HttpEntity<String> orderStatusEntity = new HttpEntity<>(
jacksonObjectMapper.writeValueAsString(c4cOrderNotificationConverter.convert(orderModel)),
orderStatusUpdateHeaders);
//LOGGER.info(updateMessageConverter.convert(ticket));
//final HttpEntity<String> messageEntity = new HttpEntity<>(
//jacksonObjectMapper.writeValueAsString(updateMessageConverter.convert(ticket)), messageUpdateHeaders);
/*
* if (completedStatus.getId().equalsIgnoreCase(ticket.getStatus().getId())) { if
* (getTicket(ticket.getId()).getStatus().getId().equals(ticket.getStatus().getId())) // so status doesn't
* changed { throw new IllegalArgumentException(
* "You can not add a message to a completed ticket. Please, reopen the ticket"); }
*
* parts.add("message", messageEntity); parts.add("status", statusEntity); } else {
*/
//parts.add("status", statusEntity);
//parts.add("message", messageEntity);
//}
//final HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity(parts, updateTicketHeaders);
final URI uri = UriComponentsBuilder.fromHttpUrl(HybriscxodataConstants.URL + HybriscxodataConstants.BATCH_SUFFIX)
.build().encode().toUri();
LOGGER.info("Result uri for status update: " + uri);
final ResponseEntity<MultiValueMap> result = restTemplate.exchange(uri, HttpMethod.POST, orderStatusEntity,
MultiValueMap.class);
LOGGER.info("Response status: " + result.getStatusCode());
LOGGER.info("Response headers: " + result.getHeaders());
LOGGER.info("Response body: " + result.getBody());
/*
* if (result.getBody().containsKey(Customerticketingc4cintegrationConstants.MULTIPART_HAS_ERROR)) {
* LOGGER.error("Error happend!"); if (null !=
* result.getBody().get(Customerticketingc4cintegrationConstants.MULTIPART_ERROR_MESSAGE)) {
* LOGGER.error(result.getBody().get(Customerticketingc4cintegrationConstants.MULTIPART_ERROR_MESSAGE)); }
*
* return null; }
*/
//return getTicket(ticket.getId());
}
catch (final IOException e)
{
LOGGER.warn("Can't convert ticketData: " + e);
}
catch (final RestClientException e)
{
LOGGER.warn("Can't send request " + e);
}
//return null; // or throw ?
}
Using method 1, we can create new entry in my custom table but when we try to update using method 2.
we getbelow error.
"
INFO [TaskExecutor-master-179-ProcessTask [8796356477878]] [OrderNotificationServiceImpl] Result uri for status update: http
s://my304456.crm.ondemand.com/sap/c4c/odata/cust/v1/demosalesorders/$batch
WARN [TaskExecutor-master-179-ProcessTask [8796356477878]] [OrderNotificationServiceImpl] Can't send request org.springframe
work.web.client.HttpClientErrorException: 403 Forbidden"
Could you please help me.
Thanks & Regards,
Niranjan Patel
+91-9619115758