com.sap.ipe.ble.irp.handler.services.MessageService Maven / Gradle / Ivy
package com.sap.ipe.ble.irp.handler.services;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sap.cds.services.EventContext;
import com.sap.ipe.ble.irp.exceptions.ExtensionException;
import com.sap.ipe.ble.irp.handler.constants.Constants;
import com.sap.ipe.ble.irp.handler.messages.ExtensionMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sap.ipe.ble.irp.handler.messages.Messages;
import com.sap.ipe.ble.irp.utils.Translator;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Iterator;
/***
* MessageService is used to show the messages on UI
*/
@Service
public class MessageService {
private static final String CLASSNAME = "MessageService";
private final Logger log = LoggerFactory.getLogger(CLASSNAME);
/***
* This message is used to show the messages that are returned from the response of remote Handler on the UI
* @param context event context used
* @param response response from remote handler
* @throws ExtensionException custom exception class created
*/
public void showMessages(EventContext context, JSONObject response) throws ExtensionException {
try {
ExtensionMessage extMessageList = new ObjectMapper().readValue(response.toString(),
ExtensionMessage.class);
if (extMessageList.getMessages() != null) {
for (Iterator iterator = extMessageList.getMessages().stream()
.iterator(); iterator.hasNext(); ) {
Messages messages = iterator.next();
if (messages.getInfo() != null) {
context.getMessages().info(messages.getInfo());
} else if (messages.getError() != null) {
context.getMessages().error(messages.getError());
} else if (messages.getWarning() != null) {
context.getMessages().warn(messages.getWarning());
} else if (messages.getSuccess() != null) {
context.getMessages().success(messages.getSuccess());
}
}
}
}
catch (JsonProcessingException exception){
log.error(Constants.MESSAGE_PROCESSING_ERROR + exception.getLocalizedMessage());
throw new ExtensionException(Translator.toLocale(Constants.FAIL_ON_UNKNOWN_PROPERTIES));
}
}
}