try {
daoGoal = DAOFactory.getGoalDAO();
} catch (Exception e1) {
logger.error(e1.getMessage(), e1);
throw new SpagoBIServiceException(SERVICE_NAME, "Error occurred");
}
String serviceType = this.getAttributeAsString(MESSAGE_DET);
if (serviceType != null && serviceType.equalsIgnoreCase(GOAL_NODE_CHILD)) {
logger.debug("Loading the children for the goal with id:");
try{
Integer nodeId = getAttributeAsInteger("nodeId");
logger.debug("nodeId:"+ nodeId);
List<GoalNode> children = daoGoal.getChildrenNodes(nodeId);
JSONArray ja = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(children, getLocale());
writeBackToClient(new JSONSuccess(ja));
} catch (NumberFormatException e) {
logger.debug("The goal node "+ getAttribute("nodeId") +" is not saved yet ");
try{
writeBackToClient(new JSONSuccess(new JSONArray()));
}catch (IOException io){
logger.debug("Error getting the child of the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error getting the child of the goal node",e);
}
} catch (Exception ee){
logger.debug("Error getting the child of the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error getting the child of the goal node",ee);
}
logger.debug("Loaded children");
}
if (serviceType != null && serviceType.equalsIgnoreCase(OU_GOAL_ROOT)) {
logger.debug("Loading the root node of the goal tree with:");
JSONObject jo;
Integer goalId = getAttributeAsInteger("goalId");
Integer ouId = getAttributeAsInteger("ouId");
logger.debug("goalId: "+goalId+" ,ouId "+ouId);
GoalNode root = daoGoal.getRootNode(goalId, ouId);
try{
if(root!=null){
jo = (JSONObject) SerializerFactory.getSerializer("application/json").serialize(root, getLocale());
}else{
jo = new JSONObject();
}
writeBackToClient(new JSONSuccess(jo));
} catch (Exception e){
logger.debug("Error getting the root of the goal");
throw new SpagoBIServiceException(SERVICE_NAME, "Error getting the root of the goal",e);
}
logger.debug("Goal root loaded");
}
if (serviceType != null && serviceType.equalsIgnoreCase(INSERT_GOAL_NODE)) {
logger.debug("Adding the goal node: ");
JSONObject JSONGoal = getAttributeAsJSONObject("goalNode");
Integer goalId = getAttributeAsInteger("goalId");
Integer ouId = getAttributeAsInteger("ouId");
logger.debug("JSONGoal: "+JSONGoal);
try{
GoalNode gn = insertGoalNode(JSONGoal, null, goalId, ouId);
JSONObject jo = (JSONObject) SerializerFactory.getSerializer("application/json").serialize(gn, getLocale());
writeBackToClient(new JSONSuccess(jo));
} catch (Exception e){
logger.debug("Error inserting the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error inserting the goal node",e);
}
logger.debug("Node added");
}
if (serviceType != null && serviceType.equalsIgnoreCase(INSERT_GOAL_DETAILS)) {
logger.debug("Adding the details of the goal node: ");
List<GoalKpi> kpisList = new ArrayList<GoalKpi>();
JSONObject JSONGoalDetails = getAttributeAsJSONObject("goalDetails");
JSONObject goalNode = JSONGoalDetails.optJSONObject("goalNode");
Integer goalNodeId = goalNode.optInt("id");
String goalDesc = goalNode.optString("goalDesc");
String goalName = goalNode.optString("name");
logger.debug(goalNode);
GoalNode gn = new GoalNode(goalName, "", goalDesc, null, null);
gn.setId(goalNodeId);
JSONArray kpis = JSONGoalDetails.optJSONArray("kpis");
try{
for(int i=0; i<kpis.length(); i++){
kpisList.add(deserializeKpiNode((JSONObject)kpis.get(i), goalNodeId));
}
daoGoal.ereseGoalKpis(goalNodeId);
daoGoal.insertGoalKpis(kpisList, goalNodeId);
daoGoal.updateGoalNode(gn);
writeBackToClient(new JSONAcknowledge());
} catch (Exception e){
logger.debug("Error adding the details to the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error adding the details to the goal node",e);
}
logger.debug("Added the goal details");
}
if (serviceType != null && serviceType.equalsIgnoreCase(UPDATE_GOAL_NAME)) {
logger.debug("Updating the name of the goal with id:");
Integer goalNode = getAttributeAsInteger("goalId");
logger.debug("id:"+goalNode);
String goalName = getAttributeAsString("newName");
daoGoal.updateGoalName(goalNode, goalName);
try{
writeBackToClient(new JSONAcknowledge());
} catch (IOException e){
logger.debug("Error sending the response after the erese of the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error sending the response after the erese of the goal node",e);
}
logger.debug("Goal name updated");
}
if (serviceType != null && serviceType.equalsIgnoreCase(ERESE_GOAL_NODE)) {
logger.debug("Removing the goal node with id:");
Integer goalNode = getAttributeAsInteger("id");
logger.debug("id:"+goalNode);
daoGoal.ereseGoalNode(goalNode);
try{
writeBackToClient(new JSONAcknowledge());
} catch (IOException e){
logger.debug("Error sending the response after the erese of the goal node");
throw new SpagoBIServiceException(SERVICE_NAME, "Error sending the response after the erese of the goal node",e);
}
logger.debug("Goal removed");
}
if (serviceType != null && serviceType.equalsIgnoreCase(KPI_GOAL_ROOT_NODE)) {
logger.debug("Loading the kpi model instances linked to the goal: ");
Integer grantId = getAttributeAsInteger("grantId");
Integer goalNodeId = getAttributeAsInteger("goalNodeId");
Integer ouNodeId = getAttributeAsInteger("ouNodeId");
logger.debug("grantId: "+grantId+", goalNodeId"+goalNodeId);
OrganizationalUnitGrant grant = DAOFactory.getOrganizationalUnitDAO().getGrant(grantId);
ModelInstance mi = grant.getModelInstance();
List<OrganizationalUnitNodeWithGrant> ousWithGrants = DAOFactory.getOrganizationalUnitDAO().getGrantNodes(ouNodeId, grantId);
List<OrganizationalUnitGrantNode> grants = new ArrayList<OrganizationalUnitGrantNode>();
for(int i=0; i<ousWithGrants.size(); i++){
grants.addAll(ousWithGrants.get(i).getGrants());
}
List<Integer> modelInstances = new ArrayList<Integer>();
for(int i=0; i<grants.size(); i++){
modelInstances.add(grants.get(i).getModelInstanceNode().getModelInstanceNodeId());
}
if(modelInstances.contains(mi.getId())){
mi.setActive(true);
}
try {
JSONObject modelInstanceJSON = (JSONObject) SerializerFactory.getSerializer("application/json").serialize( mi, null);
if(goalNodeId!=null){
List<GoalKpi> listGoalKpi = daoGoal.getGoalKpi(goalNodeId);
for(int i=0; i<listGoalKpi.size(); i++){
if(listGoalKpi.get(i).getModelInstanceId().equals( mi.getId())){
modelInstanceJSON.put("weight1", ""+listGoalKpi.get(i).getWeight1());
modelInstanceJSON.put("weight2", ""+listGoalKpi.get(i).getWeight2());
modelInstanceJSON.put("sign1", ""+listGoalKpi.get(i).getSign1());
if(listGoalKpi.get(i).getSign1()==110){
modelInstanceJSON.put("threshold1", "");
}else{
modelInstanceJSON.put("threshold1", ""+listGoalKpi.get(i).getThreshold1());
}
modelInstanceJSON.put("sign2", ""+listGoalKpi.get(i).getSign2());
if(listGoalKpi.get(i).getSign2()==110){
modelInstanceJSON.put("threshold2", "");
}else{
modelInstanceJSON.put("threshold2", ""+listGoalKpi.get(i).getThreshold2());
}
break;
}
}
}
writeBackToClient( new JSONSuccess( modelInstanceJSON ) );
} catch (Exception e) {
logger.debug("Impossible to serialize the responce to the client");
throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to serialize the responce to the client", e);
}
logger.debug("kpi model instances loaded.");
}
if (serviceType != null && serviceType.equalsIgnoreCase(GOAL_ERESE)) {
logger.debug("Removing the goal with id:");
Integer goalId = getAttributeAsInteger("goalId");
logger.debug(goalId);
logger.debug(goalId);
daoGoal.eraseGoal(goalId);
try{
writeBackToClient( new JSONAcknowledge() );
} catch (IOException e){
logger.debug("Error sending the response after the erese of the goal");
throw new SpagoBIServiceException(SERVICE_NAME, "Error sending the response after the erese of the goal",e);
}
logger.debug("Goal removed");
}
if (serviceType != null && serviceType.equalsIgnoreCase(GOAL_INSERT)) {
logger.debug("Adding the goal: ");
JSONObject goalJSON = getAttributeAsJSONObject(GOAL);
logger.debug(goalJSON);
try{
Goal goal = deserializeGoal(goalJSON);
daoGoal.insertGoal(goal);
writeBackToClient( new JSONAcknowledge() );
} catch (Exception e){
logger.debug("Error inserting the goal");
throw new SpagoBIServiceException(SERVICE_NAME, "Error inserting the goal",e);
}
logger.debug("Goal added");
}
if (serviceType != null && serviceType.equalsIgnoreCase(GOALS_LIST)) {
logger.debug("Getting the list of the goals");
try{
List<Goal> goals = daoGoal.getGoalsList();
JSONArray jo = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(goals, getLocale());
JSONObject grantsJSONObject = new JSONObject();
grantsJSONObject.put("rows",jo);
writeBackToClient(new JSONSuccess(grantsJSONObject));
} catch (Exception e){
logger.debug("Error getting the goals list");
throw new SpagoBIServiceException(SERVICE_NAME, "Error getting the goals list",e);
}
logger.debug("List loaded");
}
}