public static Integer LIMIT_DEFAULT = 16;
@Override
public void doService() {
logger.debug("IN");
ISbiAlarmContactDAO contactDao;
try {
contactDao = DAOFactory.getAlarmContactDAO();
contactDao.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.equals(CONTACTS_LIST)){
try {
Integer start = getAttributeAsInteger( START );
Integer limit = getAttributeAsInteger( LIMIT );
if(start==null){
start = START_DEFAULT;
}
if(limit==null){
limit = LIMIT_DEFAULT;
}
Integer totalResNum = contactDao.countContacts();
List<SbiAlarmContact> contacts = contactDao.loadPagedContactsList(start, limit);
logger.debug("Loaded contacts list");
JSONArray contactsJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(contacts,locale);
JSONObject contactsResponseJSON = createJSONResponseContacts(contactsJSON, totalResNum);
writeBackToClient(new JSONSuccess(contactsResponseJSON));
} catch (Throwable e) {
logger.error(e.getMessage(), e);
try {
writeBackToClient("Exception occurred while retrieving contacts");
} catch (IOException e1) {
logger.error(e1.getMessage(), e1);
}
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving contacts", e);
}
}else if(serviceType != null && serviceType.equals(CONTACT_INSERT)){
String name = getAttributeAsString(NAME);
String email = getAttributeAsString(EMAIL);
String mobile = getAttributeAsString(MOBILE);
String resources = getAttributeAsString(RESOURCES);
String id = getAttributeAsString(ID);
SbiAlarmContact contact = new SbiAlarmContact();
contact.setEmail(email);
contact.setMobile(mobile);
contact.setName(name);
if(resources != null && !resources.equals(NO_RESOURCES_STR)){
contact.setResources(resources);
}else{
contact.setResources(null);
}
try {
if(id != null && !id.equals("") && !id.equals("0")){
contact.setId(Integer.valueOf(id));
contactDao.update(contact);
logger.debug("Contact "+id+" updated");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
}else{
Integer contactID = contactDao.insert(contact);
logger.debug("New Contact inserted");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
attributesResponseSuccessJSON.put("id", contactID);
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
}
} catch (JSONException e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while saving new contact",
e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while saving new contact",
e);
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(CONTACT_DELETE)) {
Integer id = getAttributeAsInteger(ID);
try {
contactDao.delete(id);
logger.debug("Contact deleted");
writeBackToClient( new JSONAcknowledge("Operation succeded") );
} catch (Throwable e) {
logger.error("Exception occurred while deleting role", e);