Package it.eng.spagobi.kpi.model.bo

Examples of it.eng.spagobi.kpi.model.bo.ModelInstance


    biObjectToInsert=new ArrayList();

    //I want to insert the whole model instance tree, first of all I get the model instance root
    IModelInstanceDAO modInstDAO=DAOFactory.getModelInstanceDAO();
    ModelInstance miRoot=modInstDAO.loadModelInstanceRoot(mi);


    // insert the model (model instance root points to model root)
    logger.debug("Insert the model root and the tree");   

    // insert model tree starting from root.
    Model modelRoot=miRoot.getModel();
    insertModelTree(modelRoot, session);

    logger.debug("Insert the model Instance root and the tree");   

    //insert the Model Instanceroot
View Full Code Here


      Set modelInstanceChildren=new HashSet();
      logger.debug("insert current model instance children");
      // get the Model Instance children
      IModelInstanceDAO modelInstDao=DAOFactory.getModelInstanceDAO();
      ModelInstance modInstWithChildren=modelInstDao.loadModelInstanceWithChildrenById(mi.getId());
      List childrenList=modInstWithChildren.getChildrenNodes();
      if(childrenList!=null){
        for (Iterator iterator = childrenList.iterator(); iterator.hasNext();) {
          ModelInstance childNode = (ModelInstance) iterator.next();
          logger.debug("insert child "+childNode.getLabel());
          insertModelInstanceTree(childNode,session);       
          SbiKpiModelInst hibKpiModelInst = (SbiKpiModelInst) session.load(SbiKpiModelInst.class, childNode.getId());
          modelInstanceChildren.add(hibKpiModelInst);
        }
      }
      hibMi.setSbiKpiModelInsts(modelInstanceChildren)// serve?
View Full Code Here

        // biObjectToInsert keeps track of objects that have to be inserted beacuse related to Kpi

        if (modelInstanceLabel != null) {
          IModelInstanceDAO modelInstanceDao = DAOFactory.getModelInstanceDAO();
          ModelInstance modelInstance = modelInstanceDao.loadModelInstanceWithoutChildrenByLabel(modelInstanceLabel);
          if (modelInstance == null) {
            logger.warn("Error while exporting kpi with id " + biobj.getId() + " and label " + biobj.getLabel() + " : " +
                "the template refers to a Model Instance with label " + modelInstanceLabel + " that does not exist!");
          } else {
            objsToInsert=exporter.insertAllFromModelInstance(modelInstance, session);
View Full Code Here

        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));
View Full Code Here

    try {
      Model model =DAOFactory.getModelDAO().loadModelWithChildrenById(id);
      Integer modelInstId = null;
      if(id != null){
        ModelInstance modelInstNode = new ModelInstance();

        //save root first
        try {
          modelInstNode = fillModelInstanceByModel(model, modelInstNode, parentId);
          modelInstNode = setProgressiveOnDuplicate(modelInstNode);
          //save node as ModelInstance node
          modelInstId = modelDao.insertModelInstanceWithKpi(modelInstNode);
          modelInstNode.setId(modelInstId);

          if(parentId == null){
            response.append("root",modelInstId.intValue()+"");
            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);
          }
        } catch (EMFUserError e) {
          response.append("tree", "KO");
        }
View Full Code Here

    return response;
  }
  private JSONObject recurseOverTree(List<ModelInstance> modelInstList, ModelInstance modelInstance, Integer parentId, JSONObject response, boolean isToSave) throws JSONException{

    ModelInstance modInstToSave = modelInstance;
    //found  root child
    Integer oldId = null;
    if(modInstToSave.getGuiId() != null){
      try{
        oldId =Integer.valueOf(modInstToSave.getGuiId());//incorrect
      }catch(Throwable t){
        if(!isToSave && modInstToSave.getId() != null){
          oldId = modInstToSave.getId();
        }else
          oldId = null;
      }
    }
    modInstToSave.setParentId(parentId);       
    //save it
    try {
      Integer genId = modInstToSave.getId();
      if(isToSave){

        genId = modelDao.insertModelInstanceWithKpi(modInstToSave);
        modInstToSave.setId(genId);
      }

      response.append(modInstToSave.getGuiId(), "OK");

      List<ModelInstance> nodes = findNextNodes(modelInstList, oldId);

      if(nodes == null || nodes.isEmpty()){
        //try another way
        nodes = modInstToSave.getChildrenNodes();

      }
      if(nodes != null && !nodes.isEmpty()){
        for (int i=0; i< nodes.size(); i++){
          ModelInstance modInst = (ModelInstance)nodes.get(i);
          recurseOverTree(modelInstList, modInst, genId, response, true);
        }
      }

    } catch (EMFUserError e) {
View Full Code Here

  }
 
  private List<ModelInstance> findNextNodes(List<ModelInstance> modelInstList, Integer parentIDToSearch){
    List<ModelInstance> nodes = new ArrayList<ModelInstance>();
    for(int i=0; i< modelInstList.size(); i++){     
      ModelInstance modInstToSave = (ModelInstance)modelInstList.get(i);

      if(parentIDToSearch == null ){
        if(modInstToSave.getParentId() == parentIDToSearch
            && modInstToSave.getGuiId().matches("^\\d+$")){
          nodes.add(modInstToSave);
        }
      }else{
        if(modInstToSave.getParentId() != null
            &&(modInstToSave.getParentId().intValue() == parentIDToSearch.intValue())
            && modInstToSave.getGuiId().matches("^\\d+$")){
          nodes.add(modInstToSave);
        }
      }
    }
    return nodes;
View Full Code Here

    List<ModelInstance> toReturn = new ArrayList<ModelInstance>();

    for(int i=0; i< rows.length(); i++){

      JSONObject obj = (JSONObject)rows.get(i);
      ModelInstance modelInst = deserializeJSONObjectDD(obj, toReturn);
      toReturn.add(modelInst);
   
    return toReturn;
  }
View Full Code Here

    }
    return modelInst;
  }
  private ModelInstance deserializeJSONObjectDD (JSONObject obj, List<ModelInstance> nodeslist)throws JSONException{

    ModelInstance modelInst = new ModelInstance();

    fillModelInstance(obj, modelInst);
    //children
    JSONArray children ;
    try{
      children = obj.getJSONArray("children");
      List <ModelInstance> childrenMI = new ArrayList<ModelInstance>();
      for(int k=0; k<children.length(); k++){
        JSONObject jsonchild = (JSONObject)children.get(k);
        childrenMI.add(deserializeJSONObjectDD(jsonchild, nodeslist));
      }
      modelInst.setChildrenNodes(childrenMI);
    }catch(Throwable t){
      //nothing
      modelInst.setChildrenNodes(null);
    }
    nodeslist.add(modelInst);
    return modelInst;
  }
View Full Code Here

    for(int i=0; i< rows.length(); i++){

      JSONObject obj = (JSONObject)rows.get(i);

      ModelInstance modelInst = new ModelInstance();

      fillModelInstance(obj, modelInst);
      //skip root node if new one:
      if(!(modelInst.getParentId() == null && modelInst.getId() == null) &&
          (!idsToRemove.contains(modelInst.getGuiId()))){
        toReturn.add(modelInst);
      }

    } 
    return toReturn;
View Full Code Here

TOP

Related Classes of it.eng.spagobi.kpi.model.bo.ModelInstance

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.