String parentId = (String)getAttributeAsString("modelInstId");
if(parentId == null || parentId.startsWith("xnode")){
writeBackToClient(new JSONSuccess("OK"));
return;
}
ModelInstance aModel = modelDao.loadModelInstanceWithChildrenById(Integer.parseInt(parentId));
logger.debug("Loaded model tree");
JSONArray modelChildrenJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(aModel.getChildrenNodes(), locale);
writeBackToClient(new JSONSuccess(modelChildrenJSON));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving model tree", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving model tree", e);
}
}else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_NODE_DETAILS)) {
try {
String node = (String)getAttributeAsString("modelInstId");
ModelInstance aModel = modelDao.loadModelInstanceWithChildrenById(Integer.parseInt(node));
logger.debug("Loaded model tree");
JSONObject modelChildrenJSON = (JSONObject) SerializerFactory.getSerializer("application/json").serialize(aModel, locale);
writeBackToClient(new JSONSuccess(modelChildrenJSON));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving model tree", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving model tree", e);
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_NODES_SAVE)) {
JSONArray nodesToSaveJSON = getAttributeAsJSONArray(NODES_TO_SAVE);
JSONArray droppedNodesToSaveJSON = getAttributeAsJSONArray(DROPPED_NODES_TO_SAVE);
JSONObject rootObj = getAttributeAsJSONObject(ROOT_TO_SAVE);
List<ModelInstance> modelNodes = null;
List<ModelInstance> modelNodesDD = null;
ModelInstance root = null;
Vector idsToRemove = new Vector();
if(nodesToSaveJSON != null || droppedNodesToSaveJSON != null){
JSONObject response = new JSONObject();
try {
modelNodesDD = deserializeNodesJSONArrayDD(droppedNodesToSaveJSON);
//clean nodes modified from DD ones
for(int i=0; i<modelNodesDD.size(); i++){
ModelInstance mi = modelNodesDD.get(i);
String guidToSkip = mi.getGuiId();
//if already present in modified nodes...
for(int k =0; k< nodesToSaveJSON.length(); k++){
JSONObject objMod = nodesToSaveJSON.getJSONObject(k);
String guiId = "";
try{
guiId = objMod.getString("id");
}catch(Throwable t){
logger.debug("Dropped node guiid doesn't exist");
}
if(guiId.equals(guidToSkip)){
idsToRemove.add(guidToSkip);
}
}
}
modelNodes = deserializeJSONArray(nodesToSaveJSON, idsToRemove);
//save DD nodes
if(rootObj != null){
root = deserializeJSONObjectDD(rootObj, new ArrayList<ModelInstance>());
modelNodesDD.add(root);
}
if(modelNodesDD != null && !modelNodesDD.isEmpty()){
if(!isTreeStructureOfDDNodes(droppedNodesToSaveJSON)){
response = saveModelNodeInstances(modelNodesDD);
}else{
response = recursiveStart(modelNodesDD, root, response);
}
}
try{
if( rootObj.getBoolean("toSave")){
//root node has been modified
modelNodes.add(root);
}
}catch(Throwable e){
logger.debug("Root node is not modified");
}
response = saveModelNodeInstances(modelNodes);
writeBackToClient(new JSONSuccess(response));
} catch (Exception e) {
logger.error(e.getMessage(), e);
try {
writeBackToClient(new JSONSuccess(response));
} catch (IOException e1) {
logger.error("Exception occurred while sending response", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while sending response", e);
}
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception saving model instance nodes", e);
}
}
} else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_NODE_DELETE)) {
Integer modelInstId = getAttributeAsInteger("modelInstId");
try {
boolean result = modelDao.deleteModelInstance(modelInstId);
logger.debug("Model instance node deleted");
writeBackToClient( new JSONSuccess("Operation succeded") );
} catch (Throwable e) {
logger.error("Exception occurred while retrieving model instance to delete", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving model instance to delete", e);
}
}else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_KPI_RESTORE)) {
Integer kpiId = getAttributeAsInteger("kpiId");
try {
Kpi kpiToRestore = DAOFactory.getKpiDAO().loadKpiById(kpiId);
logger.debug("Found kpi to restore");
JSONObject kpiJson = (JSONObject) SerializerFactory.getSerializer("application/json").serialize(kpiToRestore, locale);
writeBackToClient(new JSONSuccess(kpiJson));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving kpi to restore", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving kpi to restore", e);
}
}else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINST_RESOURCE_LIST)) {
try {
Integer modelInstId = null;
try{
modelInstId = getAttributeAsInteger("modelInstId");
}catch (Exception e) {
// TODO: handle exception
logger.debug("No model Instance Id");
}
Integer start = getAttributeAsInteger( START );
Integer limit = getAttributeAsInteger( LIMIT );
if(start==null){
start = START_DEFAULT;
}
if(limit==null){
limit = LIMIT_DEFAULT;
}
List<ModelResourcesExtended> modelResourcesExtenList = new ArrayList<ModelResourcesExtended>();
//extract resources
List<ModelResources> modelResources = new ArrayList<ModelResources>();
if(modelInstId != null){
modelResources = modelResourcesDao.loadModelResourceByModelId(modelInstId);
}
HashMap<Integer, ModelResources> modResourcesIds = new HashMap<Integer, ModelResources>();
if(modelResources != null){
for(int i =0;i<modelResources.size(); i++){
ModelResources mr = modelResources.get(i);
modResourcesIds.put(mr.getResourceId(), mr);
}
}
//extract all resources
Vector resourcesIds = new Vector<Integer>();
List<Resource> allResources = (List<Resource>)getSessionContainer().getAttribute("ALL_RESOURCES_LIST");
//if null than extract
if(allResources == null){
allResources = DAOFactory.getResourceDAO().loadPagedResourcesList(start,limit);
}
modelResourcesExtendedListCreate(modelResourcesExtenList, allResources, modResourcesIds);
logger.debug("Loaded model resources");
JSONArray modelsResourcesJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(modelResourcesExtenList,locale);
JSONObject modelsResourcesResponseJSON = createJSONResponsemodelsResourcesList(modelsResourcesJSON, modelResourcesExtenList.size());
writeBackToClient(new JSONSuccess(modelsResourcesResponseJSON));
} catch (Throwable e) {
logger.error("Exception occurred while retrieving model tree", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while retrieving model tree", e);
}
}else if(serviceType != null && serviceType.equalsIgnoreCase(MODELINST_RESOURCE_SAVE)){
JSONArray resToSaveJSON = getAttributeAsJSONArray("ids");
Integer modelId = getAttributeAsInteger("modelInstId");
try {
List ids = deserializeResourceJSONArray(resToSaveJSON);
List toAddIds = ids;
//loops over all model resources
List<ModelResources> mrs = modelResourcesDao.loadModelResourceByModelId(modelId);
for(int i=0; i< mrs.size(); i++){
ModelResources modelres = mrs.get(i);
if(!ids.contains(modelres.getResourceId())){
//to remove
modelResourcesDao.removeModelResource(modelId, modelres.getResourceId());
}else {
//already present so remove it from the list
toAddIds.remove(modelres.getResourceId());
}
}
//now adds new ones
for(int i=0; i< toAddIds.size(); i++){
Integer resourceId = (Integer)toAddIds.get(i);
modelResourcesDao.addModelResource(modelId, resourceId);
}
writeBackToClient(new JSONSuccess("Operation succeded"));
} catch (EMFUserError e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception saving resources", e);
} catch (JSONException e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception deserializing resources", e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception in response", e);
}
}else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_COPY_MODEL)) {
try {
//saves all model nodes hierarchy as model instance
JSONObject response = new JSONObject();
Integer modelId = (Integer)getAttributeAsInteger("modelId");
response = recurseOverModelTree(modelId, response, null);
logger.debug("Loaded model tree");
writeBackToClient(new JSONSuccess(response));
} catch (Throwable e) {
logger.error("Exception occurred while copying model tree", e);
throw new SpagoBIServiceException(SERVICE_NAME,
"Exception occurred while copying model tree", e);
}
}else if (serviceType != null && serviceType.equalsIgnoreCase(MODELINSTS_SAVE_ROOT)) {
try {
//saves all model nodes hierarchy as model instance
JSONObject response = new JSONObject();
Integer modelId = (Integer)getAttributeAsInteger("modelId");
Model model = DAOFactory.getModelDAO().loadModelWithoutChildrenById(modelId);
ModelInstance modelInstNode = new ModelInstance();
modelInstNode = fillModelInstanceByModel(model, modelInstNode, null);
modelInstNode = setProgressiveOnDuplicate(modelInstNode);
Integer miId = modelDao.insertModelInstanceWithKpi(modelInstNode);
response.append("root", miId);
response.append("rootlabel", modelInstNode.getLabel());
response.append("rootname", modelInstNode.getName());
String text = modelInstNode.getName() ;
if(text.length()>= 20){
text = text.substring(0, 19)+"...";
}
text = modelInstNode.getModel().getCode()+" - "+ text;
response.append("roottext", text);
logger.debug("Loaded model tree");
writeBackToClient(new JSONSuccess(response));