use of org.javarosa.core.model.FormDef in project collect by opendatakit.
the class ItemsetWidget method getSelectionArgs.
private String[] getSelectionArgs(List<String> arguments, String nodesetStr, FormController formController) {
// +1 is for the list_name
String[] selectionArgs = new String[arguments.size() + 1];
// parse out the list name, between the ''
String listName = nodesetStr.substring(nodesetStr.indexOf('\'') + 1, nodesetStr.lastIndexOf('\''));
// first argument is always listname
selectionArgs[0] = listName;
if (formController == null) {
Timber.w("Can't instantiate ItemsetWidget with a null FormController.");
return null;
}
// loop through the arguments, evaluate any expressions and build the query string for the DB
for (int i = 0; i < arguments.size(); i++) {
XPathExpression xpr;
try {
xpr = parseTool.parseXPath(arguments.get(i));
} catch (XPathSyntaxException e) {
Timber.e(e);
TextView error = new TextView(getContext());
error.setText(String.format(getContext().getString(R.string.parser_exception), arguments.get(i)));
addAnswerView(error);
break;
}
if (xpr != null) {
FormDef form = formController.getFormDef();
TreeElement treeElement = form.getMainInstance().resolveReference(formEntryPrompt.getIndex().getReference());
EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
Object value = xpr.eval(form.getMainInstance(), ec);
if (value == null) {
return null;
} else {
if (value instanceof XPathNodeset) {
value = ((XPathNodeset) value).getValAt(0);
}
selectionArgs[i + 1] = value.toString();
}
}
}
return selectionArgs;
}
use of org.javarosa.core.model.FormDef in project collect by opendatakit.
the class FormController method getQuestionPromptRequiredText.
public String getQuestionPromptRequiredText(FormIndex index) {
// look for the text under the requiredMsg bind attribute
String constraintText = getBindAttribute(index, XFormParser.NAMESPACE_JAVAROSA, "requiredMsg");
if (constraintText != null) {
XPathExpression xpathRequiredMsg;
try {
xpathRequiredMsg = XPathParseTool.parseXPath("string(" + constraintText + ")");
} catch (Exception e) {
// This is a string literal, so no need to evaluate anything.
return constraintText;
}
if (xpathRequiredMsg != null) {
try {
FormDef form = formEntryController.getModel().getForm();
TreeElement treeElement = form.getMainInstance().resolveReference(index.getReference());
EvaluationContext ec = new EvaluationContext(form.getEvaluationContext(), treeElement.getRef());
Object value = xpathRequiredMsg.eval(form.getMainInstance(), ec);
if (!value.equals("")) {
return (String) value;
}
return null;
} catch (Exception e) {
Timber.e(e, "Error evaluating a valid-looking required xpath ");
return constraintText;
}
} else {
return constraintText;
}
}
return null;
}
use of org.javarosa.core.model.FormDef in project collect by opendatakit.
the class FormController method getSubmissionDataReference.
/**
* Find the portion of the form that is to be submitted
*/
private IDataReference getSubmissionDataReference() {
FormDef formDef = formEntryController.getModel().getForm();
// Determine the information about the submission...
SubmissionProfile p = formDef.getSubmissionProfile();
if (p == null || p.getRef() == null) {
return new XPathReference("/");
} else {
return p.getRef();
}
}
use of org.javarosa.core.model.FormDef in project collect by opendatakit.
the class FormController method getSubmissionMetadata.
/**
* Get the OpenRosa required metadata of the portion of the form beng submitted
*/
public InstanceMetadata getSubmissionMetadata() {
FormDef formDef = formEntryController.getModel().getForm();
TreeElement rootElement = formDef.getInstance().getRoot();
TreeElement trueSubmissionElement;
// Determine the information about the submission...
SubmissionProfile p = formDef.getSubmissionProfile();
if (p == null || p.getRef() == null) {
trueSubmissionElement = rootElement;
} else {
IDataReference ref = p.getRef();
trueSubmissionElement = formDef.getInstance().resolveReference(ref);
// resolveReference returns null if the reference is to the root element...
if (trueSubmissionElement == null) {
trueSubmissionElement = rootElement;
}
}
// and find the depth-first meta block in this...
TreeElement e = findDepthFirst(trueSubmissionElement, "meta");
String instanceId = null;
String instanceName = null;
boolean audit = false;
if (e != null) {
List<TreeElement> v;
// instance id...
v = e.getChildrenWithName(INSTANCE_ID);
if (v.size() == 1) {
IAnswerData sa = v.get(0).getValue();
if (sa != null) {
instanceId = sa.getDisplayText();
}
}
// instance name...
v = e.getChildrenWithName(INSTANCE_NAME);
if (v.size() == 1) {
IAnswerData sa = v.get(0).getValue();
if (sa != null) {
instanceName = sa.getDisplayText();
}
}
// timing element...
v = e.getChildrenWithName(AUDIT);
if (v.size() == 1) {
audit = true;
IAnswerData answerData = new StringData();
answerData.setValue(AUDIT_FILE_NAME);
v.get(0).setValue(answerData);
}
}
return new InstanceMetadata(instanceId, instanceName, audit);
}
use of org.javarosa.core.model.FormDef in project collect by opendatakit.
the class InstanceGoogleSheetsUploader method getInstanceElement.
private TreeElement getInstanceElement(String formFilePath, File instanceFile) throws UploadException {
FormDef formDef;
try {
formDef = XFormUtils.getFormFromInputStream(new FileInputStream(new File(formFilePath)));
} catch (FileNotFoundException e) {
throw new UploadException(e);
}
FormLoaderTask.importData(instanceFile, new FormEntryController(new FormEntryModel(formDef)));
return formDef.getMainInstance().getRoot();
}
Aggregations