if( !(o instanceof ModelInstance) ) {
throw new SerializationException("ModelInstanceNodeJSONSerializer is unable to serialize object of type: " + o.getClass().getName());
}
try {
ModelInstance res = (ModelInstance)o;
result = new JSONObject();
result.put(MODEL_INST_ID, res.getId() );
result.put(PARENT_ID, res.getParentId() );
if(res.getModel() != null){
result.put(MODEL_ID, res.getModel().getId() );
Model model = DAOFactory.getModelDAO().loadModelWithoutChildrenById(res.getModel().getId() );
result.put(MODEL_TEXT, model.getCode() +" - "+model.getName() );
result.put(MODEL_CODE, model.getCode());
result.put(MODEL_NAME, model.getName() );
result.put(MODEL_DESCR, model.getDescription() );
result.put(MODEL_TYPE, model.getTypeName() );
result.put(MODEL_TYPEDESCR, model.getTypeDescription() );
}
//if no kpi instance --> fill data with kpi properties
//else with kpi instance properties
if(res.getKpiInstance() != null){
result.put(KPI_INST_ID, res.getKpiInstance().getKpiInstanceId() );
KpiInstance kpiInst = DAOFactory.getKpiInstanceDAO().loadKpiInstanceById(res.getKpiInstance().getKpiInstanceId());
if(kpiInst != null){
result.put(KPI_ID, kpiInst.getKpi());
if(kpiInst.getKpi() != null){
Kpi kpi = DAOFactory.getKpiDAO().loadKpiById(kpiInst.getKpi());
result.put(KPI_NAME, kpi.getKpiName());
result.put(KPI_CODE, kpi.getCode());
}
result.put(KPI_INST_CHART, kpiInst.getChartTypeId());
result.put(KPI_INST_PERIODICITY, kpiInst.getPeriodicityId());
result.put(KPI_INST_TARGET, kpiInst.getTarget());
result.put(KPI_INST_THR_ID, kpiInst.getThresholdId());
if(kpiInst.getThresholdId() != null){
Threshold thr = DAOFactory.getThresholdDAO().loadThresholdById(kpiInst.getThresholdId());
result.put(KPI_INST_THR_NAME, thr.getName());
}
result.put(KPI_INST_WEIGHT, kpiInst.getWeight());
result.put(KPI_INST_SAVE_HISTORY, kpiInst.isSaveKpiHistory());
}
}
result.put(NAME, res.getName() );
String text = res.getName() ;
if(text.length()>= 20){
text = text.substring(0, 19)+"...";
}
text = res.getModel().getCode()+" - "+ text;
result.put(TEXT, text );
result.put(LABEL, res.getLabel());
result.put(DESCRIPTION, res.getDescription() );
result.put(STARTDATE, res.getStartDate());
result.put(ENDDATE, res.getEndDate());
result.put(MODELUUID, res.getModelUUID() );
result.put(KPI_INST_ACTIVE, res.isActive());
if(res.getChildrenNodes() != null && !res.getChildrenNodes().isEmpty()){
result.put(LEAF, false );
}else{
result.put(LEAF, true );
}