private static Logger logger = Logger.getLogger(ManageAttributesAction.class);
@Override
public void doService() {
logger.debug("IN");
ISbiAttributeDAO attrDao;
try {
attrDao = DAOFactory.getSbiAttributeDAO();
attrDao.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.contains(ATTR_LIST)) {
String name = null;
String description =null;
String idStr = null;
try {
BufferedReader b =httpRequest.getReader();
if(b!=null){
String respJsonObject = b.readLine();
if(respJsonObject!=null){
JSONObject responseJSON = deserialize(respJsonObject);
JSONObject samples = responseJSON.getJSONObject(SAMPLES);
if(!samples.isNull(ID)){
idStr = samples.getString(ID);
}
//checks if it is empty attribute to start insert
boolean isNewAttr = this.getAttributeAsBoolean(IS_NEW_ATTR);
if(!isNewAttr){
if(!samples.isNull(NAME)){
name = samples.getString(NAME);
if (GenericValidator.isBlankOrNull(name) ||
!GenericValidator.matchRegexp(name, ALPHANUMERIC_STRING_REGEXP_NOSPACE)||
!GenericValidator.maxLength(name, nameMaxLenght)){
logger.error("Either the field name is blank or it exceeds maxlength or it is not alfanumeric");
EMFValidationError e = new EMFValidationError(EMFErrorSeverity.ERROR, description, "9000","");
getHttpResponse().setStatus(404);
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", false);
attributesResponseSuccessJSON.put("message", "Either the field name is blank or it exceeds maxlength or it is not alfanumeric");
attributesResponseSuccessJSON.put("data", "[]");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
return;
}
}
if(!samples.isNull(DESCRIPTION)){
description = samples.getString(DESCRIPTION);
if (GenericValidator.isBlankOrNull(description) ||
!GenericValidator.matchRegexp(description, ALPHANUMERIC_STRING_REGEXP_NOSPACE) ||
!GenericValidator.maxLength(description, descriptionMaxLenght)){
logger.error("Either the field description is blank or it exceeds maxlength or it is not alfanumeric");
EMFValidationError e = new EMFValidationError(EMFErrorSeverity.ERROR, description, "9000","");
getHttpResponse().setStatus(404);
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", false);
attributesResponseSuccessJSON.put("message", "Either the field description is blank or it exceeds maxlength or it is not alfanumeric");
attributesResponseSuccessJSON.put("data", "[]");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
return;
}
}
SbiAttribute attribute = new SbiAttribute();
if(description!=null){
attribute.setDescription(description);
}
if(name!=null){
attribute.setAttributeName(name);
}
boolean isNewAttrForRes = true;
if(idStr!=null && !idStr.equals("")){
Integer attributeId = new Integer(idStr);
attribute.setAttributeId(attributeId.intValue());
isNewAttrForRes = false;
}
Integer attrID = attrDao.saveOrUpdateSbiAttribute(attribute);
logger.debug("Attribute updated");
ArrayList<SbiAttribute> attributes = new ArrayList<SbiAttribute> ();
attribute.setAttributeId(attrID);
attributes.add(attribute);
getAttributesListAdded(locale,attributes,isNewAttrForRes, attrID);
}
//else the List of attributes will be sent to the client
}else{
getAttributesList(locale,attrDao);
}
}else{
getAttributesList(locale,attrDao);
}
} catch (Throwable e) {
logger.error(e.getMessage(), e);
getHttpResponse().setStatus(404);
try {
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("message", "Exception occurred while saving attribute");
attributesResponseSuccessJSON.put("data", "[]");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
} catch (IOException e1) {
logger.error(e1.getMessage(), e1);
} catch (JSONException e2) {
logger.error(e2.getMessage(), e2);
}
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving attributes", e);
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(ATTR_DELETE)) {
String idStr = null;
try {
BufferedReader b =httpRequest.getReader();
if(b!=null){
String respJsonObject = b.readLine();
if(respJsonObject!=null){
JSONObject responseJSON = deserialize(respJsonObject);
idStr = responseJSON.getString(SAMPLES);
}
}
} catch (IOException e1) {
logger.error("IO Exception",e1);
e1.printStackTrace();
} catch (SerializationException e) {
logger.error("Deserialization Exception",e);
e.printStackTrace();
} catch (JSONException e) {
logger.error("JSONException",e);
e.printStackTrace();
}
if(idStr!=null && !idStr.equals("")){
Integer id = new Integer(idStr);
try {
attrDao.deleteSbiAttributeById(id);
logger.debug("Attribute deleted");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("message", "");
attributesResponseSuccessJSON.put("data", "[]");