/** * A method for producing a vector of single strings - from the current * attribute vector of string [] arrays. * * @return */ public ArrayList getSingleStringAttributeVector() { ArrayList strings = new ArrayList(); if (attributes.Count == 0) { return(null); } else { for (int i = 0; i < this.attributes.Count; i++) { TreeElement attribute = attributes[i]; String value = getAttributeValue(attribute); if (attribute.namespace_ == null || attribute.namespace_ == "") { strings.Add(attribute.getName() + "=" + value); } else { strings.Add(attribute.namespace_ + ":" + attribute.getName() + "=" + value); } } return(strings); } }
private void addChild(TreeElement child, Boolean checkDuplicate) { if (!isChildable()) { throw new SystemException("Can't add children to node that has data value!"); } if (child.multiplicity == TreeReference.INDEX_UNBOUND) { throw new SystemException("Cannot add child with an unbound index!"); } if (checkDuplicate) { TreeElement existingChild = getChild(child.name, child.multiplicity); if (existingChild != null) { throw new SystemException("Attempted to add duplicate child!"); } } // try to keep things in order int i = children.Count; if (child.getMult() == TreeReference.INDEX_TEMPLATE) { TreeElement anchor = getChild(child.getName(), 0); if (anchor != null) { i = children.IndexOf(anchor); } } else { TreeElement anchor = getChild(child.getName(), (child.getMult() == 0 ? TreeReference.INDEX_TEMPLATE : child.getMult() - 1)); if (anchor != null) { i = children.IndexOf(anchor) + 1; } } children.Insert(i, child); child.setParent(this); child.setRelevant(isRelevant(), true); child.setEnabled(isEnabled(), true); }
//this method is for copying in the answers to an itemset. the template node of the destination //is used for overall structure (including data types), and the itemset source node is used for //raw data. note that data may be coerced across types, which may result in type conversion error //very similar in structure to populate() public void populateTemplate(TreeElement incoming, FormDef f) { if (this.isLeaf()) { IAnswerData value = incoming.value; if (value == null) { this.setValue(null); } else { Type classType = CompactInstanceWrapper.classForDataType(this.dataType); if (classType == null) { throw new SystemException("data type [" + value.GetType().Name + "] not supported inside itemset"); } else if (classType.IsAssignableFrom(value.GetType()) && !(value is SelectOneData || value is SelectMultiData)) { this.setValue(value); } else { String textVal = RestoreUtils.xfFact.serializeData(value); IAnswerData typedVal = RestoreUtils.xfFact.parseData(textVal, this.dataType, this.getRef(), f); this.setValue(typedVal); } } } else { for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); List <TreeElement> newChildren = incoming.getChildrenWithName(child.getName()); if (child.repeatable) { for (int k = 0; k < newChildren.Count; k++) { TreeElement template = f.Instance.getTemplate(child.getRef()); TreeElement newChild = template.deepCopy(false); newChild.setMult(k); this.children.Insert(i + k + 1, newChild); newChild.populateTemplate((TreeElement)newChildren[k], f); } i += newChildren.Count; } else { child.populateTemplate((TreeElement)newChildren[0], f); } } } }
// determine if nodes are homogeneous, meaning their descendant structure is 'identical' for repeat purposes // identical means all children match, and the children's children match, and so on // repeatable children are ignored; as they do not have to exist in the same quantity for nodes to be homogeneous // however, the child repeatable nodes MUST be verified amongst themselves for homogeneity later // this function ignores the names of the two nodes public static Boolean isHomogeneous(TreeElement a, TreeElement b) { if (a.isLeaf() && b.isLeaf()) { return(true); } else if (a.isChildable() && b.isChildable()) { // verify that every (non-repeatable) node in a exists in b and vice // versa for (int k = 0; k < 2; k++) { TreeElement n1 = (k == 0 ? a : b); TreeElement n2 = (k == 0 ? b : a); for (int i = 0; i < n1.getNumChildren(); i++) { TreeElement child1 = n1.getChildAt(i); if (child1.repeatable) { continue; } TreeElement child2 = n2.getChild(child1.getName(), 0); if (child2 == null) { return(false); } if (child2.repeatable) { throw new SystemException("shouldn't happen"); } } } // compare children for (int i = 0; i < a.getNumChildren(); i++) { TreeElement childA = a.getChildAt(i); if (childA.repeatable) { continue; } TreeElement childB = b.getChild(childA.getName(), 0); if (!isHomogeneous(childA, childB)) { return(false); } } return(true); } else { return(false); } }
public TreeElement processSaved(FormInstance template, FormDef f) { TreeElement fixedInstanceRoot = template.getRoot().deepCopy(true); TreeElement incomingRoot = root.getChildAt(0); if (!fixedInstanceRoot.getName().Equals(incomingRoot.getName()) || incomingRoot.getMult() != 0) { throw new SystemException("Saved form instance to restore does not match form definition"); } fixedInstanceRoot.populate(incomingRoot, f); return(fixedInstanceRoot); }
private List <TreeElement> getChildrenWithName(String name, Boolean includeTemplate) { List <TreeElement> v = new List <TreeElement>(); for (int i = 0; i < this.children.Count; i++) { TreeElement child = (TreeElement)this.children[i]; if ((child.getName().Equals(name) || name.Equals(TreeReference.NAME_WILDCARD)) && (includeTemplate || child.multiplicity != TreeReference.INDEX_TEMPLATE)) { v.Add(child); } } return(v); }
public void importData(FormInstance dm) { name = (String)RestoreUtils.getValue("name", dm); formId = ((int)RestoreUtils.getValue("form-id", dm)); dateSaved = (DateTime)RestoreUtils.getValue("saved-on", dm); schema = (String)RestoreUtils.getValue("schema", dm); Boolean sent = RestoreUtils.getBoolean(RestoreUtils .getValue("sent", dm)); TreeElement names = dm.resolveReference(RestoreUtils.absRef("namespace", dm)); if (names != null) { for (int i = 0; i < names.getNumChildren(); i++) { TreeElement child = names.getChildAt(i); String name_ = child.getName(); Object value = RestoreUtils.getValue("namespace/" + name_, dm); if (value != null) { namespaces.Add(name_, value); } } } ///////////// throw new SystemException("FormInstance.importData(): must be updated to use new transport layer"); // if (sent) { // ITransportManager tm = TransportManager._(); // tm.markSent(id, false); // } ///////////// // IStorageUtility forms = StorageManager.getStorage(FormDef.STORAGE_KEY); // FormDef f = (FormDef)forms.read(formId); // setRoot(processSavedDataModel(dm.resolveReference(RestoreUtils.absRef("data", dm)), f.getDataModel(), f)); }
public TreeElement getChild(String name, int multiplicity) { if (name.Equals(TreeReference.NAME_WILDCARD)) { if (multiplicity == TreeReference.INDEX_TEMPLATE || this.children.Count < multiplicity + 1) { return(null); } return((TreeElement)this.children[multiplicity]); //droos: i'm suspicious of this } else { for (int i = 0; i < this.children.Count; i++) { TreeElement child = (TreeElement)this.children[i]; if (name.Equals(child.getName()) && child.getMult() == multiplicity) { return(child); } } } return(null); }
//rebuilding a node from an imported instance // there's a lot of error checking we could do on the received instance, but it's // easier to just ignore the parts that are incorrect public void populate(TreeElement incoming, FormDef f) { if (this.isLeaf()) { // check that incoming doesn't have children? IAnswerData value = incoming.getValue(); if (value == null) { this.setValue(null); } else if (this.dataType == Constants.DATATYPE_TEXT || this.dataType == Constants.DATATYPE_NULL) { this.setValue(value); // value is a StringData } else { String textVal = value.ToString(); IAnswerData typedVal = RestoreUtils.xfFact.parseData(textVal, this.dataType, this.getRef(), f); this.setValue(typedVal); } } else { ArrayList names = new ArrayList(); for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); if (!names.Contains(child.getName())) { names.Add(child.getName()); } } // remove all default repetitions from skeleton data model (_preserving_ templates, though) for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); if (child.repeatable && child.getMult() != TreeReference.INDEX_TEMPLATE) { this.removeChildAt(i); i--; } } // make sure ordering is preserved (needed for compliance with xsd schema) if (this.getNumChildren() != names.Count) { throw new SystemException("sanity check failed"); } for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); String expectedName = (String)names[i]; if (!child.getName().Equals(expectedName)) { TreeElement child2 = null; int j; for (j = i + 1; j < this.getNumChildren(); j++) { child2 = this.getChildAt(j); if (child2.getName().Equals(expectedName)) { break; } } if (j == this.getNumChildren()) { throw new SystemException("sanity check failed"); } this.removeChildAt(j); this.children.Insert(i, child2); } } // java i hate you so much for (int i = 0; i < this.getNumChildren(); i++) { TreeElement child = this.getChildAt(i); List <TreeElement> newChildren = incoming.getChildrenWithName(child.getName()); if (child.repeatable) { for (int k = 0; k < newChildren.Count; k++) { TreeElement newChild = child.deepCopy(true); newChild.setMult(k); this.children.Insert(i + k + 1, newChild); newChild.populate((TreeElement)newChildren[k], f); } i += newChildren.Count; } else { if (newChildren.Count == 0) { child.setRelevant(false); } else { child.populate((TreeElement)newChildren[0], f); } } } } }