@Override
public void doService() {
logger.debug("IN");
ISbiAlarmDAO alarmDao;
try {
alarmDao = DAOFactory.getAlarmDAO();
alarmDao.setUserProfile(getUserProfile());
} catch (EMFUserError e1) {
logger.error(e1.getMessage(), e1);
throw new SpagoBIServiceException(SERVICE_NAME, "Error occurred");
}
HttpServletRequest httpRequest = getHttpRequest();
Locale locale = getLocale();
String serviceType = this.getAttributeAsString(MESSAGE_DET);
logger.debug("Service type "+serviceType);
if (serviceType != null && serviceType.equalsIgnoreCase(ALARMS_LIST)) {
//loads kpi
try {
Integer start = getAttributeAsInteger( START );
Integer limit = getAttributeAsInteger( LIMIT );
if(start==null){
start = START_DEFAULT;
}
if(limit==null){
limit = LIMIT_DEFAULT;
}
Integer totalResNum = alarmDao.countAlarms();
List<SbiAlarm> alarms = alarmDao.loadPagedAlarmsList(start, limit);
logger.debug("Loaded users list");
JSONArray alarmsJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(alarms,locale);
JSONObject usersResponseJSON = createJSONResponseAlarms(alarmsJSON, totalResNum);
writeBackToClient(new JSONSuccess(usersResponseJSON));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving alarms", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving alarms", e);
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(ALARM_INSERT)) {
String id = getAttributeAsString(ID);
String name = getAttributeAsString(NAME);
String descr = getAttributeAsString(DESCRIPTION);
String label = getAttributeAsString(LABEL);
String modality = getAttributeAsString(MODALITY);
Boolean singleEvent = getAttributeAsBoolean(SINGLE_EVENT);
Boolean autoDisabled = getAttributeAsBoolean(AUTO_DISABLED);
String text = getAttributeAsString(TEXT);
String url = getAttributeAsString(URL);
Integer kpiInstId = getAttributeAsInteger(KPI);
Integer thresholdId = getAttributeAsInteger(THRESHOLD);
JSONArray contactsJSON = getAttributeAsJSONArray(CONTACTS);
SbiAlarm alarm = new SbiAlarm();
alarm.setAutoDisabled(autoDisabled);
alarm.setDescr(descr);
alarm.setLabel(label);
alarm.setName(name);
alarm.setSingleEvent(singleEvent);
alarm.setText(text);
alarm.setUrl(url);
try {
if(modality!=null){
SbiDomains dModality = DAOFactory.getDomainDAO().loadSbiDomainByCodeAndValue(DOMAIN_CD, modality);
alarm.setModality(dModality);
}
if(kpiInstId != null){
SbiKpiInstance sbiKpiInstance = DAOFactory.getKpiInstanceDAO().loadSbiKpiInstanceById(kpiInstId);
alarm.setSbiKpiInstance(sbiKpiInstance);
}
if(thresholdId != null){
SbiThresholdValue sbiThresholdValue = DAOFactory.getThresholdValueDAO().loadSbiThresholdValueById(thresholdId);
alarm.setSbiThresholdValue(sbiThresholdValue);
}
if(id != null && !id.equals("") && !id.equals("0")){
alarm.setId(Integer.valueOf(id));
}
Set<SbiAlarmContact> contactsList = null;
if(contactsJSON != null){
contactsList = deserializeContactsJSONArray(contactsJSON);
alarm.setSbiAlarmContacts(contactsList);
}
Integer idToReturn = alarmDao.update(alarm);
logger.debug("Alarm updated or Inserted");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
attributesResponseSuccessJSON.put("id", idToReturn);
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
} catch (EMFUserError e) {
logger.error("Exception occurred while saving alarm", e);
writeErrorsBackToClient();
throw new SpagoBIServiceException(SERVICE_NAME, "Exception occurred while saving alarm", e);
} catch (IOException e) {
logger.error("Exception occurred while writing response to client", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while writing response to client", e);
} catch (JSONException e) {
logger.error("JSON Exception", e);
e.printStackTrace();
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(ALARM_DELETE)) {
Integer id = getAttributeAsInteger(ID);
try {
alarmDao.delete(id);
logger.debug("Alarm deleted");
writeBackToClient( new JSONAcknowledge("Operation succeded") );
} catch (Throwable e) {
logger.error("Exception occurred while retrieving user to delete", e);