public static Integer LIMIT_DEFAULT = 16;
@Override
public void doService() {
logger.debug("IN");
ISbiUserDAO userDao;
try {
userDao = DAOFactory.getSbiUserDAO();
userDao.setUserProfile(getUserProfile());
} catch (EMFUserError e1) {
logger.error(e1.getMessage(), e1);
throw new SpagoBIServiceException(SERVICE_NAME, "Error occurred");
}
Locale locale = getLocale();
String serviceType = this.getAttributeAsString(MESSAGE_DET);
logger.debug("Service type "+serviceType);
if (serviceType != null && serviceType.equalsIgnoreCase(USERS_LIST)) {
try {
Integer start = getAttributeAsInteger( START );
Integer limit = getAttributeAsInteger( LIMIT );
if(start==null){
start = START_DEFAULT;
}
if(limit==null){
limit = LIMIT_DEFAULT;
}
Integer totalResNum = userDao.countUsers();
List<UserBO> users = userDao.loadPagedUsersList(start, limit);
logger.debug("Loaded users list");
JSONArray usersJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(users, locale);
JSONObject usersResponseJSON = createJSONResponseUsers(usersJSON, totalResNum);
writeBackToClient(new JSONSuccess(usersResponseJSON));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving users", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving users", e);
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(USER_INSERT)) {
Integer id = getAttributeAsInteger(ID);
String userId = getAttributeAsString(USER_ID);
String fullName = getAttributeAsString(FULL_NAME);
String password = getAttributeAsString(PASSWORD);
JSONArray rolesJSON = getAttributeAsJSONArray(ROLES);
JSONArray attributesJSON = getAttributeAsJSONArray(ATTRIBUTES);
if (userId != null) {
SbiUser user = new SbiUser();
user.setUserId(userId);
user.setFullName(fullName);
if(password != null && password.length() > 0){
try {
user.setPassword(Password.encriptPassword(password));
} catch (Exception e) {
logger.error("Impossible to encript Password", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Impossible to encript Password",e);
}
}
if(id!=null){
user.setId(id);
}
try {
HashMap<Integer, String> attrList = null;
if(attributesJSON != null){
attrList = deserializeAttributesJSONArray(attributesJSON);
}
List rolesList = null;
if(rolesJSON != null){
rolesList = deserializeRolesJSONArray(rolesJSON);
}
id = userDao.fullSaveOrUpdateSbiUser(user, rolesList, attrList);
logger.debug("User updated or Inserted");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
attributesResponseSuccessJSON.put("id", id);
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
} catch (EMFUserError e) {
logger.error("Exception occurred while saving new user", e);
writeErrorsBackToClient();
throw new SpagoBIServiceException(SERVICE_NAME, "Exception occurred while saving new user", 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{
logger.error("User name missing");
throw new SpagoBIServiceException(SERVICE_NAME, "Please enter user name");
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(USER_DELETE)) {
Integer id = getAttributeAsInteger(ID);
try {
userDao.deleteSbiUserById(id);
logger.debug("User deleted");
writeBackToClient( new JSONAcknowledge("Operation succeded") );
} catch (Throwable e) {
logger.error("Exception occurred while retrieving user to delete", e);