private JSONObject saveModelNodes(List<Model> nodesToSave) throws JSONException { JSONArray errorNodes = new JSONArray(); JSONObject respObj = new JSONObject(); // loop over nodes and order them ascending TreeMap<Integer, Model> treeMap = new TreeMap<Integer, Model>(); for (int i = 0; i < nodesToSave.size(); i++) { Model model = (Model) nodesToSave.get(i); // loads all nodes guiid with type error respObj.put(model.getGuiId(), "OK"); respObj.put("label", model.getLabel()); if (model.getParentId() != null) { // look up for its id: if null --> newly created node Integer id = model.getId(); if (id == null) { treeMap.put(Integer.valueOf("-" + i + 1), model); } else { // else to modify node treeMap.put(model.getId(), model); } } else { // root node --> save first try { if (model.getId() != null) { modelDao.modifyModel(model); respObj.put(model.getGuiId(), model.getId()); respObj.put(model.getId() + "", model.getLabel()); } else { Integer index = modelDao.insertModel(model); respObj.put(model.getGuiId(), index); respObj.put(index + "", model.getLabel()); } } catch (Exception e) { // send error!!! respObj.put(model.getGuiId(), "KO"); } } } Set set = treeMap.entrySet(); // Get an iterator Iterator it = set.iterator(); // loop again over treemap while (it.hasNext()) { Map.Entry orderedEntry = (Map.Entry) it.next(); // check that parent exists Model orderedNode = (Model) orderedEntry.getValue(); // GET JSON OBJECT VALUE Integer parentId = orderedNode.getParentId(); try { Model parent = modelDao.loadModelWithoutChildrenById(parentId); if (parent != null) { // if parent exists--> save // if node id is negative --> insert if (orderedNode.getId() == null) { Integer newId = modelDao.insertModel(orderedNode); if (newId != null) { orderedNode.setId(newId); respObj.put(orderedNode.getGuiId(), newId); respObj.put(newId + "", orderedNode.getLabel()); } else { respObj.put(orderedNode.getGuiId(), "KO"); } } else { // else update modelDao.modifyModel(orderedNode); respObj.put(orderedNode.getGuiId(), orderedNode.getId()); respObj.put(orderedNode.getId() + "", orderedNode.getLabel()); } } } catch (Exception e) { // if parentId != null but no parent node stored on db --> exception respObj.put(orderedNode.getGuiId(), "KO"); } } return respObj; }
private List<Model> deserializeNodesJSONArray(JSONArray rows) throws JSONException { List<Model> toReturn = new ArrayList<Model>(); HashMap<String, Model> labels = new HashMap<String, Model>(); for (int i = 0; i < rows.length(); i++) { JSONObject obj = (JSONObject) rows.get(i); Model model = new Model(); // always present guiId String guiId = obj.getString("id"); model.setGuiId(guiId); try { model.setId(obj.getInt("modelId")); } catch (Throwable t) { // nothing model.setId(null); } try { model.setParentId(obj.getInt("parentId")); } catch (Throwable t) { // nothing model.setParentId(null); } try { model.setCode(obj.getString("code")); try { model.setDescription(obj.getString("description")); } catch (Throwable t) { // nothing model.setDescription(null); } String labelKey; try { labelKey = obj.getString("label"); } catch (Throwable t) { labelKey = java.util.UUID.randomUUID().toString(); } model.setLabel(labelKey); if (!labels.containsKey(labelKey)) { labels.put(labelKey, model); } else { // skip it continue; } model.setName(obj.getString("name")); model.setTypeCd(obj.getString("type")); model.setTypeId(obj.getInt("typeId")); try { model.setTypeDescription(obj.getString("typeDescr")); } catch (Throwable t) { // nothing model.setTypeDescription(null); } try { model.setKpiId(obj.getInt("kpiId")); } catch (Throwable t) { // nothing model.setKpiId(null); } // add the udpValues to Model Instance Definition, that will be serialized List<UdpValue> udpValues = new ArrayList<UdpValue>(); JSONArray jsonArray = null; try { jsonArray = obj.getJSONArray("udpValues"); } catch (Throwable t) { jsonArray = new JSONArray(); } logger.debug("found udpValues Array containing number of Udp " + jsonArray.length()); for (int j = 0; j < jsonArray.length(); j++) { JSONObject objJS = (JSONObject) jsonArray.get(j); // only label and value information are retrieved by JSON object String labelJ = objJS.getString("name"); String value = objJS.getString("value"); UdpValue udpValue = new UdpValue(); // reference id is the kpi id udpValue.setLabel(obj.getString("label")); udpValue.setValue(value); udpValue.setReferenceId(model.getId()); // get the UDP to get ID (otherwise could be taken in js page) Udp udp = DAOFactory.getUdpDAO().loadByLabelAndFamily(labelJ, "MODEL"); Domain familyDomain = DAOFactory.getDomainDAO().loadDomainById(udp.getFamilyId()); logger.debug( "Udp value assigning value " + value + " to UDP with label " + udp.getLabel() + " and Model Instance with label " + model.getLabel()); udpValue.setLabel(udp.getLabel()); udpValue.setName(udp.getName()); udpValue.setFamily(familyDomain != null ? familyDomain.getValueCd() : null); udpValue.setUdpId(udp.getUdpId()); udpValues.add(udpValue); } model.setUdpValues(udpValues); String value = obj.getString("toSave"); } catch (Throwable t) { logger.debug("Deserialization error on node: " + guiId); } toReturn.add(model); } return toReturn; }