/** Refresh fields with values from tempModel or editedModel. */ private void refreshDisplay() { Session s = Session.current; Model tmp = s.tempModel; int structIndex = -1; for (int j = 0; j < this.table.getChildCount(); j++) { structIndex++; View v = this.table.getChildAt(j); if (!FormViewFactory.isFieldView(v)) { // Check if it is a x2many label Model field = this.view.getStructure().get(structIndex); if (field.hasAttribute("type")) { String type = field.getString("type"); if (type.equals("many2many") || type.equals("one2many")) { // This is the label of a x2many field // It is not present in structure and next // will be the true widget. // Get back in structure for next pass // to point on the x2many field (and not next one) structIndex--; } } continue; } Model field = this.view.getStructure().get(structIndex); FormViewFactory.setValue(v, field, this.view, tmp, s.editedModel, s.prefs, this); } }
/** Add a create call to the queue, update db and start a new record */ private void queueCreate() { Model newModel = Session.current.tempModel; Model queuedModel = new Model(newModel.getClassName()); queuedModel.merge(newModel); // Make it pending (also sets temp id for create) DelayedRequester.current.queueCreate(queuedModel, this.view, this); this.postCreate(queuedModel); }
/** 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(); }
/** Add a update call to the queue, update db and return */ private void queueUpdate() { Model newModel = Session.current.tempModel; Model oldModel = Session.current.editedModel; Model queuedModel = new Model(newModel.getClassName()); queuedModel.merge(oldModel); queuedModel.merge(newModel); // Make it pending DelayedRequester.current.queueUpdate(queuedModel, this.view, this); this.postUpdate(queuedModel); }
/** * 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 endNew(Model newModel) { if (Session.current.isEditingSub()) { // Create new record String linkToParent = Session.current.linkToParent; String linkToSelf = Session.current.linkToSelf; Session.current.finishEditing(); Session.current.editNewOne2Many(newModel.getClassName(), linkToParent, linkToSelf); } else { // Just create a new record Session.current.finishEditing(); Session.current.editNewModel(newModel.getClassName()); } TreeView.setDirty(); this.refreshDisplay(); }
/** Update temp model to current values */ private void updateTempModel() { Model tmp; Model origin; tmp = Session.current.tempModel; origin = Session.current.editedModel; int structIndex = -1; for (int j = 0; j < this.table.getChildCount(); j++) { structIndex++; View v = this.table.getChildAt(j); if (!FormViewFactory.isFieldView(v)) { // Check if it is a x2many label Model field = this.view.getStructure().get(structIndex); if (field.hasAttribute("type")) { String type = field.getString("type"); if (type.equals("many2many") || type.equals("one2many")) { // This is the label of a x2many field // It is not present in structure and next // will be the true widget. // Get back in structure for next pass // to point on the x2many field (and not next one) structIndex--; } } // Ignore continue; } Model field = this.view.getStructure().get(structIndex); Object value = FormViewFactory.getValue(v, field, Session.current.prefs); if (value != FormViewFactory.NO_VALUE) { tmp.set(field.getString("name"), value); } else { // If NO_VALUE (not null) the value is ignored // but still set null to create the field if necessary String fieldName = field.getString("name"); if (origin != null) { // Origin always has a value, it will be merged } else { // Origin doesn't have a value, set explicit null if (!tmp.hasAttribute(fieldName)) { tmp.set(fieldName, null); } } } } }
/** * Add a create call in the queue. It edits the id of the model to affect it a temporary negative * one. */ public void queueCreate(Model newModel, ModelView editView, Context ctx) { newModel.set("id", tempId); tempId--; this.updateRecName(newModel); // There must be a rec_name this.queue.add(new Command(CMD_CREATE, newModel, editView)); this.updateNotification(ctx); try { this.save(ctx); } catch (IOException e) { Log.w("Tryton", "Unable to save DelayedRequester", e); } }
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); } } }
/** 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); } } } } } }
/** 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); }
private void initView() { for (Model view : this.view.getStructure()) { Session s = Session.current; if (view.hasAttribute("type") && (view.getString("type").equals("many2many") || view.getString("type").equals("one2many"))) { // Special case of x2many: they embed their own title // Thus occupies a full line TextView label = new TextView(this); if (view.hasAttribute("string")) { label.setText(view.getString("string")); } else { label.setText(view.getString("name")); } this.table.addView(label); } View v = FormViewFactory.getView(view, this.view, s.editedModel, s.prefs, this); if (view.hasAttribute("name") && view.getString("name").equals(Session.current.linkToSelf)) { // Hide many2one parent field v.setVisibility(View.GONE); } this.table.addView(v); } }
/** 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); }
/** * Load the cached menu entries. If the cache is not set it will return null (beware of * NullPointerExceptions) */ @SuppressWarnings("unchecked") public static DelayedRequester load(Context ctx) throws IOException { FileInputStream fis = ctx.openFileInput(CACHE_ID); ObjectInputStream ois = new ObjectInputStream(fis); DelayedRequester req = new DelayedRequester(); try { String db = (String) ois.readObject(); if (!db.equals(Configure.getDatabaseCode(ctx))) { // The record is not for the current database ois.close(); return null; } int tempId = ois.readInt(); int size = ois.readInt(); for (int i = 0; i < size; i++) { int cmdCode = ois.readInt(); byte[] byteData = (byte[]) ois.readObject(); Model data = Model.fromByteArray(byteData); boolean hasView = ois.readBoolean(); ModelView view = null; if (hasView) { byteData = (byte[]) ois.readObject(); view = ModelView.fromByteArray(byteData); ArchParser p = new ArchParser(view); p.buildTree(); } Command cmd = new Command(cmdCode, data, view); req.queue.add(cmd); } req.updateNotification(ctx); } catch (ClassNotFoundException cnfe) { // Should never happen } catch (ClassCastException cce) { // Cache structure altered req = null; } ois.close(); return req; }
/** 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); } } }
private void postUpdate(Model updated) { // Save locally and continue DataCache db = new DataCache(this); db.storeData(updated.getClassName(), updated); this.endQuit(); }