Package it.eng.spagobi.utilities.service

Examples of it.eng.spagobi.utilities.service.JSONSuccess


     
      try {
        JSONArray viewpointsJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize( viewpoints ,null);
        JSONObject results = new JSONObject();
        results.put("results", viewpointsJSON);
        writeBackToClient( new JSONSuccess( results ) );
      } catch (IOException e) {
        throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to write back the responce to the client", e);
      } catch (SerializationException e) {
        throw new SpagoBIServiceException(SERVICE_NAME, "Cannot serialize objects", e);
      } catch (JSONException e) {
View Full Code Here


      } catch (JSONException e) {
        throw new SpagoBIServiceException("Impossible to serialize response", e);
      }
     
      try {
        writeBackToClient( new JSONSuccess( responseJSON ) );
      } catch (IOException e) {
        throw new SpagoBIServiceException("Impossible to write back the responce to the client", e);
      }
     
    } finally {
View Full Code Here

        logger.debug("Loaded models list");
        JSONArray modelsListJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(modelRootsList,locale);
        JSONObject modelsResponseJSON = createJSONResponseModelsList(modelsListJSON,modelRootsList.size());

        writeBackToClient(new JSONSuccess(modelsResponseJSON));

      } 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_LIST)) {

      try

        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));

      } catch (Throwable e) {
        logger.error("Exception occurred while saving model instance root", e);
        throw new SpagoBIServiceException(SERVICE_NAME,
            "Exception occurred while saving model instance root", e);
View Full Code Here

        }
      }

      toReturn = (JSONArray) SerializerFactory.getSerializer("application/json").serialize(metaDataAndContents, null);

      writeBackToClient( new JSONSuccess( toReturn ) );
     
    } catch (Exception e) {
      throw new SpagoBIServiceException(SERVICE_NAME, "Exception occurred while retrieving metadata", e);
    } finally {
      logger.debug("OUT");
View Full Code Here

       
        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",
View Full Code Here

      logger.debug("Save domain");
     
      domainDao.saveDomain(domain);
      JSONObject response = new JSONObject();
      response.put("VALUE_ID", domain.getValueId());
      writeBackToClient(new JSONSuccess(response));
     
    } catch (Throwable e) {
      logger.error("Exception occurred while saving config data", e);
      throw new SpagoBIServiceException(SERVICE_NAME,
          "Impossible to save domain", e);
View Full Code Here

      logger.debug("Delete domain");
      Integer valueId = this.getAttributeAsInteger("VALUE_ID");
      domainDao.delete(valueId);
      JSONObject response = new JSONObject();
      response.put("VALUE_ID", valueId);
      writeBackToClient(new JSONSuccess(response));

    } catch (Throwable e) {
      logger.error("Exception occurred while retrieving domain data", e);
      throw new SpagoBIServiceException(SERVICE_NAME,
          "Impossible to delete domain", e);
View Full Code Here

          .getSerializer("application/json").serialize(domainList,
              this.getLocale());
      JSONObject response = new JSONObject();
      response.put("response", domainListJSON);

      writeBackToClient(new JSONSuccess(response));

    } catch (Throwable e) {
      logger.error("Exception occurred while retrieving domain data", e);
      throw new SpagoBIServiceException(SERVICE_NAME,
          "Exception occurred while retrieving domain data", e);
View Full Code Here

    try {
      JSONArray notesListJSON = new JSONArray();
      JSONObject results = new JSONObject();
      results.put("results", notesListJSON);
      results.put("totalCount", notesListJSON.length());
      writeBackToClient( new JSONSuccess( results ) );
    } catch (IOException e) {
      throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to write back the responce to the client", e);
    } catch (JSONException e) {
      throw new SpagoBIServiceException(SERVICE_NAME, "Cannot serialize objects into a JSON object", e);
    }
View Full Code Here

        notesListJSON = (JSONArray) SerializerFactory.getSerializer("application/json").serialize( updatedNotesList,null );
      }
      JSONObject results = new JSONObject();
      results.put("results", notesListJSON);
      results.put("totalCount", notesListJSON.length());
      writeBackToClient( new JSONSuccess( results ) )
    } catch (IOException e) {
      throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to write back the responce to the client", e);
    } catch (SerializationException e) {
      throw new SpagoBIServiceException(SERVICE_NAME, "Cannot serialize objects", e);
    } catch (JSONException e) {
View Full Code Here

TOP

Related Classes of it.eng.spagobi.utilities.service.JSONSuccess

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.