/** Update rec_name locally as it is computed on the server side. */ private void updateRecName(Model model) { if (model.hasAttribute("name")) { model.set("rec_name", model.get("name")); } else { for (String key : model.getAttributeNames()) { if (model.get(key) instanceof String) { model.set("rec_name", model.get(key)); break; } } } }
public void deleteOne2Many(Model m) { Model parent = (Model) this.editStack.get(this.editStack.size() - 8); Model tmpParent = (Model) this.editStack.get(this.editStack.size() - 7); // Make sure parent is up-to-date with the field ids to remove one if (tmpParent.get(this.linkToParent) == null) { if (parent != null && parent.get(this.linkToParent) != null) { @SuppressWarnings("unchecked") List<Integer> ids = (List<Integer>) parent.get(this.linkToParent); List<Integer> tmpIds = new ArrayList<Integer>(); tmpIds.addAll(ids); tmpParent.set(this.linkToParent, tmpIds); } } tmpParent.deleteOne2Many(this.linkToParent, m); }
/** Add a one2many to be saved along the parent */ public void addOne2Many() { Model parent = (Model) this.editStack.get(this.editStack.size() - 8); Model tmpParent = (Model) this.editStack.get(this.editStack.size() - 7); Model submodel = this.tempModel; // Make sure parent is up-to-date with the field ids to add the new one if (tmpParent.get(this.linkToParent) == null) { if (parent != null && parent.get(this.linkToParent) != null) { @SuppressWarnings("unchecked") List<Integer> ids = (List<Integer>) parent.get(this.linkToParent); List<Integer> tmpIds = new ArrayList<Integer>(); tmpIds.addAll(ids); tmpParent.set(this.linkToParent, tmpIds); } } tmpParent.addNewOne2Many(this.linkToParent, submodel); }
/** Update a one2many to be saved along the parent */ public void updateOne2Many() { Model parent = (Model) this.editStack.get(this.editStack.size() - 8); Model tmpParent = (Model) this.editStack.get(this.editStack.size() - 7); Model oldSubmodel = this.editedModel; Model submodel = this.tempModel; // Set the field in parent to mark it as dirty if (tmpParent.get(this.linkToParent) == null) { if (parent != null && parent.get(this.linkToParent) != null) { @SuppressWarnings("unchecked") List<Integer> ids = (List<Integer>) parent.get(this.linkToParent); List<Integer> tmpIds = new ArrayList<Integer>(); tmpIds.addAll(ids); tmpParent.set(this.linkToParent, tmpIds); } } tmpParent.editOne2Many(this.linkToParent, oldSubmodel, submodel); }
/** Set session to edit a many2many subrecord. */ public void editMany2Many(Model data, String parentField) { this.tempModel = new Model(data.getClassName()); this.tempModel.set("id", data.get("id")); this.editedModel = data; this.linkToParent = parentField; this.linkToSelf = null; this.pushStack(); }
/** Set session to edit a record. */ public void editModel(Model data) { this.editedModel = data; this.tempModel = new Model(data.getClassName()); this.tempModel.set("id", data.get("id")); this.linkToParent = null; this.linkToSelf = null; this.pushStack(); }
/** Replace a temporary id with a real one in data in the queue. */ @SuppressWarnings("unchecked") public void updateTempId(int tempId, int realId) { for (Command cmd : this.queue) { // As tempId is unique, make it brutal Model data = cmd.getData(); Integer tempIdInt = new Integer(tempId); // just for equality for (String key : data.getAttributeNames()) { Object val = data.get(key); if (tempIdInt.equals(val)) { data.set(key, realId); } else if (val instanceof List) { List ids = (List) data.get(key); for (int i = 0; i < ids.size(); i++) { if (tempIdInt.equals(ids.get(i))) { ids.remove(i); ids.add(i, realId); } } } } } }
/** Add a value for the edited Many2Many/One2Many parent field. */ @SuppressWarnings("unchecked") public void addToParent(int newId) { Model parent = (Model) this.editStack.get(this.editStack.size() - 8); Model tmpParent = (Model) this.editStack.get(this.editStack.size() - 7); if (tmpParent.get(this.linkToParent) == null) { // Get original id list and add the new to it List<Integer> ids = new ArrayList<Integer>(); if (parent.get(this.linkToParent) != null) { List<Integer> pIds = (List<Integer>) parent.get(this.linkToParent); ids.addAll(pIds); } if (!ids.contains(newId)) { ids.add(newId); tmpParent.set(this.linkToParent, ids); } } else { // Ids already set, just add it to the list List<Integer> ids = (List<Integer>) tmpParent.get(this.linkToParent); if (!ids.contains(newId)) { ids.add(newId); } } }
/** * Set session to edit a one2many subrecord. ParentField is the name of the field that links to * new model. */ public void editOne2Many(Model data, String parentField, String childField) { this.tempModel = new Model(data.getClassName()); if (data.hasAttribute("id")) { // When editing a new one2many record, the record has no id this.tempModel.set("id", data.get("id")); } if (this.editedModel != null && this.editedModel.hasAttribute("id")) { this.tempModel.set(childField, (Integer) this.editedModel.get("id")); } this.editedModel = data; this.linkToParent = parentField; this.linkToSelf = childField; this.pushStack(); }
private void postCreate(Model newModel) { // Save locally DataCache db = new DataCache(this); db.addOne(newModel.getClassName()); db.storeData(newModel.getClassName(), newModel); // Update parent if necessary if (Session.current.isEditingSub()) { // Add the id to parent int id = (Integer) newModel.get("id"); Session.current.addToParent(id); } if (!Session.current.isEditingTop() && Session.current.linkToParent == null) { // Quit when creating a new many2many this.endQuit(); } else { if (this.dirtyQuit) { this.endQuit(); } else { this.endNew(newModel); } } }