Ejemplo n.º 1
0
        private static FormInstance newDataModel(System.String topTag)
        {
            FormInstance dm = new FormInstance();

            dm.addNode(ref_Renamed("/" + topTag));
            return(dm);
        }
Ejemplo n.º 2
0
 private void  memoize()
 {
     if (payload == null)
     {
         IStorageUtility instances = StorageManager.getStorage(FormInstance.STORAGE_KEY);
         try
         {
             FormInstance tree = (FormInstance)instances.read(recordId);
             payload = serializer.createSerializedPayload(tree);
         }
         catch (System.IO.IOException e)
         {
             //Assertion, do not catch!
             if (e is org.javarosa.core.io.StreamsUtil.DirectionalIOException)
             {
                 ((org.javarosa.core.io.StreamsUtil.DirectionalIOException)e).printStackTrace();
             }
             else
             {
                 SupportClass.WriteStackTrace(e, Console.Error);
             }
             //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
             throw new System.SystemException("ModelReferencePayload failed to retrieve its model from rms [" + e.Message + "]");
         }
     }
 }
Ejemplo n.º 3
0
 public virtual IDataPayload createSerializedPayload(FormInstance model, IDataReference ref_Renamed)
 {
     init();
     rootRef = org.javarosa.core.model.instance.FormInstance.unpackReference(ref_Renamed);
     if (this.serializer == null)
     {
         this.AnswerDataSerializer = new XFormAnswerDataSerializer();
     }
     model.accept(this);
     if (theXmlDoc != null)
     {
         //TODO: Did this strip necessary data?
         sbyte[] form = XFormSerializer.getUtfBytes(theXmlDoc);
         if (dataPointers.Count == 0)
         {
             return(new ByteArrayPayload(form, null, org.javarosa.core.services.transport.payload.IDataPayload_Fields.PAYLOAD_TYPE_XML));
         }
         MultiMessagePayload payload = new MultiMessagePayload();
         payload.addPayload(new ByteArrayPayload(form, "xml_submission_file", org.javarosa.core.services.transport.payload.IDataPayload_Fields.PAYLOAD_TYPE_XML));
         System.Collections.IEnumerator en = dataPointers.GetEnumerator();
         //UPGRADE_TODO: Method 'java.util.Enumeration.hasMoreElements' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationhasMoreElements'"
         while (en.MoveNext())
         {
             //UPGRADE_TODO: Method 'java.util.Enumeration.nextElement' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilEnumerationnextElement'"
             IDataPointer pointer = (IDataPointer)en.Current;
             payload.addPayload(new DataPointerPayload(pointer));
         }
         return(payload);
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 4
0
        public override System.Object eval(FormInstance model, EvaluationContext evalContext)
        {
            System.Object aval   = a.eval(model, evalContext);
            System.Object bval   = b.eval(model, evalContext);
            bool          result = false;

            //xpath spec says comparisons only defined for numbers (not defined for strings)
            aval = XPathFuncExpr.toNumeric(aval);
            bval = XPathFuncExpr.toNumeric(bval);

            double fa = ((System.Double)aval);
            double fb = ((System.Double)bval);

            switch (op)
            {
            case LT:  result = fa < fb; break;

            case GT:  result = fa > fb; break;

            case LTE:  result = fa <= fb; break;

            case GTE:  result = fa >= fb; break;
            }

            return(result);
        }
Ejemplo n.º 5
0
 public virtual System.String getConstraintMessage(EvaluationContext ec, FormInstance instance, System.String textForm)
 {
     if (xPathConstraintMsg == null)
     {
         //If the request is for getting a constraint message in a specific format (like audio) from
         //itext, and there's no xpath, we couldn't possibly fulfill it
         return(textForm == null?constraintMsg:null);
     }
     else
     {
         if (textForm != null)
         {
             ec.OutputTextForm = textForm;
         }
         try
         {
             System.Object value_Renamed = xPathConstraintMsg.eval(instance, ec);
             if (value_Renamed != (System.Object) "")
             {
                 return((System.String)value_Renamed);
             }
             return(null);
         }
         catch (System.Exception e)
         {
             Logger.exception("Error evaluating a valid-looking constraint xpath ", e);
             return(constraintMsg);
         }
     }
 }
Ejemplo n.º 6
0
 public virtual sbyte[] serializeInstance(FormInstance model, FormDef formDef)
 {
     //LEGACY: Should remove
     init();
     this.schema = formDef;
     return(serializeInstance(model));
 }
Ejemplo n.º 7
0
 public virtual void  setDestRef(QuestionDef q)
 {
     destRef = FormInstance.unpackReference(q.Bind).Clone();
     if (copyMode)
     {
         destRef.add(copyRef.NameLast, TreeReference.INDEX_UNBOUND);
     }
 }
Ejemplo n.º 8
0
        public static FormInstance createRootDataModel(Restorable r)
        {
            FormInstance inst = createDataModel(r);

            inst.schema = "http://openrosa.org/backup";
            addData(inst, "timestamp", System.DateTime.Now, Constants.DATATYPE_DATE_TIME);
            return(inst);
        }
Ejemplo n.º 9
0
        public static void templateChild(FormInstance dm, System.String prefixPath, TreeReference parent, Restorable r)
        {
            TreeReference childRef = (prefixPath == null ? parent : RestoreUtils.childRef(prefixPath, parent));

            childRef = org.javarosa.core.model.util.restorable.RestoreUtils.childRef(r.RestorableType, childRef);

            templateData(r, dm, childRef);
        }
Ejemplo n.º 10
0
        public virtual TreeElement resolveReference(TreeReference qualifiedRef)
        {
            FormInstance instance = this.MainInstance;

            if (qualifiedRef.InstanceName != null)
            {
                instance = this.getInstance(qualifiedRef.InstanceName);
            }
            return(instance.resolveReference(qualifiedRef));
        }
Ejemplo n.º 11
0
        public static TreeReference absRef(System.String refStr, FormInstance dm)
        {
            TreeReference ref_ = ref_Renamed(refStr);

            if (!ref_.isAbsolute())
            {
                ref_ = ref_.parent(topRef(dm));
            }
            return(ref_);
        }
Ejemplo n.º 12
0
        public static FormInstance createDataModel(Restorable r)
        {
            FormInstance dm = newDataModel(r.RestorableType);

            if (r is Persistable)
            {
                addData(dm, RECORD_ID_TAG, (System.Object)((Persistable)r).ID);
            }

            return(dm);
        }
Ejemplo n.º 13
0
        public static void  applyDataType(FormInstance dm, System.String path, TreeReference parent, int dataType)
        {
            TreeReference ref_Renamed = childRef(path, parent);

            System.Collections.ArrayList v = new EvaluationContext(dm).expandReference(ref_Renamed);
            for (int i = 0; i < v.Count; i++)
            {
                TreeElement e = dm.resolveReference((TreeReference)v[i]);
                e.DataType = dataType;
            }
        }
Ejemplo n.º 14
0
        public static void applyDataType(FormInstance dm, System.String path, TreeReference parent, int dataType)
        {
            TreeReference        ref_ = childRef(path, parent);
            List <TreeReference> v    = dm.expandReference(ref_);

            for (int i = 0; i < v.Count; i++)
            {
                TreeElement e = dm.resolveReference((TreeReference)v[i]);
                e.dataType = dataType;
            }
        }
Ejemplo n.º 15
0
 public virtual IDataPayload serializeInstance(FormInstance dm)
 {
     try
     {
         return((new XFormSerializingVisitor()).createSerializedPayload(dm));
     }
     catch (System.IO.IOException e)
     {
         return(null);
     }
 }
Ejemplo n.º 16
0
 public override System.Object eval(FormInstance model, EvaluationContext evalContext)
 {
     try
     {
         return(expr.eval(model, evalContext));
     }
     catch (XPathException e)
     {
         e.setSource("Relevant expression for " + contextRef.toString(true));
         throw e;
     }
 }
Ejemplo n.º 17
0
        /// <summary> deserialize a compact instance. note the retrieval of the template data instance</summary>
        //UPGRADE_TODO: Class 'java.io.DataInputStream' was converted to 'System.IO.BinaryReader' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioDataInputStream'"
        public virtual void  readExternal(System.IO.BinaryReader in_Renamed, PrototypeFactory pf)
        {
            int formID = ExtUtil.readInt(in_Renamed);

            instance = getTemplateInstance(formID).Clone();

            instance.ID        = ExtUtil.readInt(in_Renamed);
            instance.DateSaved = (System.DateTime)ExtUtil.read(in_Renamed, new ExtWrapNullable(typeof(System.DateTime)));
            //formID, name, schema, versions, and namespaces are all invariants of the template instance

            TreeElement root = instance.getRoot();

            readTreeElement(root, in_Renamed, pf);
        }
Ejemplo n.º 18
0
        public static void  importRMS(FormInstance dm, IStorageUtility storage, System.Type type, System.String path)
        {
            if (!typeof(Externalizable).IsAssignableFrom(type) || !typeof(Restorable).IsAssignableFrom(type))
            {
                return;
            }

            bool idMatters = typeof(Persistable).IsAssignableFrom(type);

            System.String childName = ((Restorable)PrototypeFactory.getInstance(type)).RestorableType;
            TreeElement   e         = dm.resolveReference(absRef(path, dm));

            System.Collections.ArrayList children = e.getChildrenWithName(childName);

            for (int i = 0; i < children.Count; i++)
            {
                FormInstance child = subDataModel((TreeElement)children[i]);

                Restorable inst = (Restorable)PrototypeFactory.getInstance(type);

                //restore record id first so 'importData' has access to it
                int recID = -1;
                if (idMatters)
                {
                    recID = ((System.Int32)getValue(RECORD_ID_TAG, child));
                    ((Persistable)inst).ID = recID;
                }

                inst.importData(child);

                try
                {
                    if (idMatters)
                    {
                        storage.write((Persistable)inst);
                    }
                    else
                    {
                        storage.add((Externalizable)inst);
                    }
                }
                catch (System.Exception ex)
                {
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Class.getName' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
                    throw new System.SystemException("Error importing RMS during restore! [" + type.FullName + ":" + recID + "]; " + ex.Message);
                }
            }
        }
Ejemplo n.º 19
0
        public static void templateData(Restorable r, FormInstance dm, TreeReference parent)
        {
            if (parent == null)
            {
                parent = topRef(dm);
                applyDataType(dm, "timestamp", parent, typeof(System.DateTime));
            }

            if (r is Persistable)
            {
                applyDataType(dm, RECORD_ID_TAG, parent, typeof(System.Int32));
            }

            r.templateData(dm, parent);
        }
Ejemplo n.º 20
0
 private FormInstance getTemplateInstance(int formID)
 {
     if (templateMgr != null)
     {
         return(templateMgr.getTemplateInstance(formID));
     }
     else
     {
         FormInstance template = loadTemplateInstance(formID);
         if (template == null)
         {
             throw new System.SystemException("no formdef found for form id [" + formID + "]");
         }
         return(template);
     }
 }
Ejemplo n.º 21
0
        public static void addData(FormInstance dm, System.String xpath, System.Object data, int dataType)
        {
            if (data == null)
            {
                dataType = -1;
            }

            IAnswerData val;

            switch (dataType)
            {
            case -1: val = null; break;

            case Constants.DATATYPE_TEXT: val = new StringData((System.String)data); break;

            case Constants.DATATYPE_INTEGER:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new IntegerData(ref new System.Int32[] { (System.Int32)data }[0]); break;

            case Constants.DATATYPE_LONG:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new LongData(ref new System.Int64[] { (System.Int64)data }[0]); break;

            case Constants.DATATYPE_DECIMAL:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new DecimalData(ref new System.Double[] { (System.Double)data }[0]); break;

            case Constants.DATATYPE_BOOLEAN: val = new StringData(((System.Boolean)data) ? "t" : "f"); break;

            case Constants.DATATYPE_DATE:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new DateData(ref new System.DateTime[] { (System.DateTime)data }[0]); break;

            case Constants.DATATYPE_DATE_TIME:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new DateTimeData(ref new System.DateTime[] { (System.DateTime)data }[0]); break;

            case Constants.DATATYPE_TIME:      //UPGRADE_NOTE: ref keyword was added to struct-type parameters. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1303'"
                val = new TimeData(ref new System.DateTime[] { (System.DateTime)data }[0]); break;

            case Constants.DATATYPE_CHOICE_LIST: val = (SelectMultiData)data; break;

            default: throw new System.ArgumentException("Don't know how to handle data type [" + dataType + "]");
            }

            TreeReference ref_Renamed = absRef(xpath, dm);

            if (dm.addNode(ref_Renamed, val, dataType) == null)
            {
                throw new System.SystemException("error setting value during object backup [" + xpath + "]");
            }
        }
Ejemplo n.º 22
0
        public override System.Object eval(FormInstance model, EvaluationContext evalContext)
        {
            System.Object aval = XPathFuncExpr.unpack(a.eval(model, evalContext));
            System.Object bval = XPathFuncExpr.unpack(b.eval(model, evalContext));
            bool          eq   = false;

            if (aval is System.Boolean || bval is System.Boolean)
            {
                if (!(aval is System.Boolean))
                {
                    aval = XPathFuncExpr.toBoolean(aval);
                }
                else if (!(bval is System.Boolean))
                {
                    bval = XPathFuncExpr.toBoolean(bval);
                }

                bool ba = ((System.Boolean)aval);
                bool bb = ((System.Boolean)bval);
                eq = (ba == bb);
            }
            else if (aval is System.Double || bval is System.Double)
            {
                if (!(aval is System.Double))
                {
                    aval = XPathFuncExpr.toNumeric(aval);
                }
                else if (!(bval is System.Double))
                {
                    bval = XPathFuncExpr.toNumeric(bval);
                }

                double fa = ((System.Double)aval);
                double fb = ((System.Double)bval);
                eq = System.Math.Abs(fa - fb) < 1.0e-12;
            }
            else
            {
                aval = XPathFuncExpr.toString(aval);
                bval = XPathFuncExpr.toString(bval);
                eq   = (aval.Equals(bval));
            }

            return(equal?eq:!eq);
        }
Ejemplo n.º 23
0
 public virtual System.Object evalRaw(FormInstance model, EvaluationContext evalContext)
 {
     try
     {
         return(XPathFuncExpr.unpack(expr.eval(model, evalContext)));
     }
     catch (XPathUnsupportedException e)
     {
         if (xpath != null)
         {
             throw new XPathUnsupportedException(xpath);
         }
         else
         {
             throw e;
         }
     }
 }
Ejemplo n.º 24
0
        public static void mergeDataModel(FormInstance parent, FormInstance child, TreeReference parentRef)
        {
            TreeElement parentNode = parent.resolveReference(parentRef);

            //ugly
            if (parentNode == null)
            {
                parentRef  = parent.addNode(parentRef);
                parentNode = parent.resolveReference(parentRef);
            }
            TreeElement childNode = child.getRoot();

            int mult = parentNode.getChildMultiplicity(childNode.getName());

            childNode.setMult(mult);

            parentNode.addChild(childNode);
        }
Ejemplo n.º 25
0
        //
        //	boolean nodeset = forceNodeset;
        //	if (!nodeset) {
        //		//is this a nodeset? it is if the ref contains any unbound multiplicities AND the unbound nodes are repeatable
        //		//the way i'm calculating this sucks; there has got to be an easier way to find out if a node is repeatable
        //		TreeReference repeatTestRef = TreeReference.rootRef();
        //		for (int i = 0; i < ref.size(); i++) {
        //			repeatTestRef.add(ref.getName(i), ref.getMultiplicity(i));
        //			if (ref.getMultiplicity(i) == TreeReference.INDEX_UNBOUND) {
        //				if (m.getTemplate(repeatTestRef) != null) {
        //					nodeset = true;
        //					break;
        //				}
        //			}
        //		}
        //	}

        public static System.Object getRefValue(FormInstance model, EvaluationContext ec, TreeReference ref_Renamed)
        {
            if (ec.isConstraint && ref_Renamed.Equals(ec.ContextRef))
            {
                //ITEMSET TODO: need to update this; for itemset/copy constraints, need to simulate a whole xml sub-tree here
                return(unpackValue(ec.candidateValue));
            }
            else
            {
                TreeElement node = model.resolveReference(ref_Renamed);
                if (node == null)
                {
                    //shouldn't happen -- only existent nodes should be in nodeset
                    throw new XPathTypeMismatchException("Node " + ref_Renamed.ToString() + " does not exist!");
                }

                return(unpackValue(node.isRelevant()?node.Value:null));
            }
        }
Ejemplo n.º 26
0
        /*
         * (non-Javadoc)
         * @see org.javarosa.core.model.utils.IInstanceSerializingVisitor#serializeDataModel(org.javarosa.core.model.IFormDataModel)
         */
        public virtual sbyte[] serializeInstance(FormInstance model, IDataReference ref_Renamed)
        {
            init();
            rootRef = org.javarosa.core.model.instance.FormInstance.unpackReference(ref_Renamed);
            if (this.serializer == null)
            {
                this.AnswerDataSerializer = new XFormAnswerDataSerializer();
            }

            model.accept(this);
            if (theXmlDoc != null)
            {
                return(XFormSerializer.getUtfBytes(theXmlDoc));
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 27
0
        /// <summary> Return the template model for the given form type. Serves the template out of the cache, if cached; fetches it
        /// fresh and caches it otherwise. If form types are restricted and the given form type is not allowed, throw an error
        /// </summary>
        public virtual FormInstance getTemplateInstance(int formID)
        {
            if (restrictFormTypes && !allowedFormTypes.contains((System.Int32)formID))
            {
                throw new System.SystemException("form ID [" + formID + "] is not an allowed form type!");
            }

            FormInstance template = templateCache.get_Renamed((System.Int32)formID);

            if (template == null)
            {
                template = CompactInstanceWrapper.loadTemplateInstance(formID);
                if (template == null)
                {
                    throw new System.SystemException("no formdef found for form id [" + formID + "]");
                }
                templateCache.put((System.Int32)formID, template);
            }
            return(template);
        }
Ejemplo n.º 28
0
 public virtual IDataPayload createSerializedPayload(FormInstance model, IDataReference ref_Renamed)
 {
     init();
     rootRef = org.javarosa.core.model.instance.FormInstance.unpackReference(ref_Renamed);
     if (this.serializer == null)
     {
         this.AnswerDataSerializer = new XFormAnswerDataSerializer();
     }
     model.accept(this);
     if (theSmsStr != null)
     {
         //UPGRADE_TODO: Method 'java.lang.String.getBytes' was converted to 'System.Text.Encoding.GetEncoding(string).GetBytes(string)' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangStringgetBytes_javalangString'"
         sbyte[] form = SupportClass.ToSByteArray(System.Text.Encoding.GetEncoding("UTF-16").GetBytes(theSmsStr));
         return(new ByteArrayPayload(form, null, org.javarosa.core.services.transport.payload.IDataPayload_Fields.PAYLOAD_TYPE_SMS));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 29
0
 /*
  * (non-Javadoc)
  * @see org.javarosa.core.model.utils.IInstanceSerializingVisitor#serializeInstance(org.javarosa.core.model.instance.FormInstance, org.javarosa.core.model.IDataReference)
  */
 public virtual sbyte[] serializeInstance(FormInstance model, IDataReference ref_Renamed)
 {
     init();
     rootRef = org.javarosa.core.model.instance.FormInstance.unpackReference(ref_Renamed);
     if (this.serializer == null)
     {
         this.AnswerDataSerializer = new XFormAnswerDataSerializer();
     }
     model.accept(this);
     if (theSmsStr != null)
     {
         //Encode in UTF-16 by default, since it's the default for complex messages
         //UPGRADE_TODO: Method 'java.lang.String.getBytes' was converted to 'System.Text.Encoding.GetEncoding(string).GetBytes(string)' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javalangStringgetBytes_javalangString'"
         return(SupportClass.ToSByteArray(System.Text.Encoding.GetEncoding("UTF-16BE").GetBytes(theSmsStr)));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 30
0
		/// <summary> Not for re-implementation, dispatches all of the evaluation</summary>
		/// <param name="instance">
		/// </param>
		/// <param name="evalContext">
		/// </param>
		/// <param name="f">
		/// </param>
		public void  apply(FormInstance instance, EvaluationContext parentContext, TreeReference context, FormDef f)
		{
			//The triggeringRoot is the highest level of actual data we can inquire about, but it _isn't_ necessarily the basis
			//for the actual expressions, so we need genericize that ref against the current context
			TreeReference ungenericised = originalContextRef.contextualize(context);
			EvaluationContext ec = new EvaluationContext(parentContext, ungenericised);
			
			System.Object result = eval(instance, ec);
			
			for (int i = 0; i < targets.size(); i++)
			{
				TreeReference targetRef = ((TreeReference) targets.elementAt(i)).contextualize(ec.ContextRef);
				System.Collections.ArrayList v = ec.expandReference(targetRef);
				for (int j = 0; j < v.Count; j++)
				{
					TreeReference affectedRef = (TreeReference) v[j];
					apply(affectedRef, result, instance, f);
				}
			}
		}