Commit db297e6a authored by rahadi's avatar rahadi

Added dependencies mechanism

parent 0dc7f21c
......@@ -216,7 +216,7 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
root.addChild(child);
for (int j = 0; j < 2; j++) {
TreeNode grandchild = new ModeTreeNode("Grand Child " + j, "grand_child_" + j, ModeTreeNode.QUEUED);
grandchild.setLevel(j+1);
grandchild.setLevel(j + 1);
grandchild.setItemClickEnable(false);
grandchild.setExpanded(true);
child.addChild(grandchild);
......@@ -832,7 +832,7 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
AlertDialog dialog = new AlertDialog.Builder(getContext())
.setTitle("Dependencies Detail")
.setView(treeView)
// .setMessage(b)
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
......@@ -847,7 +847,7 @@ public class CapiFormFragment extends Fragment implements FormListDownloaderList
AlertDialog dialog = new AlertDialog.Builder(getContext())
.setView(treeView)
.setTitle("Download Summary")
// .setMessage(b)
.setCancelable(false)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
......
package id.ac.stis.capi.lessthink.listeners;
import java.util.List;
import id.ac.stis.capi.lessthink.models.DependencyDetails;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public interface DependenciesParserListener {
void onDependenciesParsingCompleted(List<DependencyDetails> dependencyDetails);
}
package id.ac.stis.capi.lessthink.tasks;
import android.os.AsyncTask;
import java.io.File;
import java.util.List;
import id.ac.stis.capi.lessthink.listeners.DependenciesParserListener;
import id.ac.stis.capi.lessthink.models.DependencyDetails;
import id.ac.stis.capi.odk.dao.FormsDao;
import id.ac.stis.capi.odk.dto.Form;
import id.ac.stis.capi.odk.logic.FormDetails;
import id.ac.stis.capi.odk.utilities.FileUtils;
/**
* Author : Rahadi Jalu
* Email : 14.8325@stis.ac.id
* Company: Politeknik Statistika STIS
*/
public class DependenciesParserTask extends AsyncTask<FormDetails, String, List<DependencyDetails>> {
private FormsDao formsDao;
private DependenciesParserListener dependenciesParserListener;
/**
* Set the dependencies parser listener
*
* @param dependenciesParserListener listener
*/
public void setDependenciesParserListener(DependenciesParserListener
dependenciesParserListener) {
this.dependenciesParserListener = dependenciesParserListener;
}
@Override
protected List<DependencyDetails> doInBackground(FormDetails... lists) {
formsDao = new FormsDao();
FormDetails toParse = lists[0];
// FormDetails contains only some basic information.
// To get the file path, we need to get the corresponding Form
Form form = formsDao.getFormsFromCursor(formsDao.getFormsCursorForFormId(toParse.formID)).get(0);
File file = new File(form.getFormFilePath());
// Parse the dependencies and save it as a list
// return the list of dependencies parsed from the XML form definition
return FileUtils.parseFormDependencies(file);
}
@Override
protected void onPostExecute(List<DependencyDetails> result) {
super.onPostExecute(result);
if (dependenciesParserListener != null) {
dependenciesParserListener.onDependenciesParsingCompleted(result);
}
}
}
......@@ -29,6 +29,10 @@ import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
......@@ -48,6 +52,149 @@ import timber.log.Timber;
*/
public class XmlUtils {
public static boolean setValuesByMap(File file, Map<String, String> values) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(file);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
Set<String> xPathStringSet = values.keySet();
for (String xPathString : xPathStringSet) {
XPathExpression expr = xpath.compile(xPathString);
org.w3c.dom.Node node = (org.w3c.dom.Node) expr.evaluate(doc, XPathConstants.NODE);
node.setTextContent(values.get(xPathString));
}
// Commit changes
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new FileOutputStream(file));
transformer.transform(source, result);
} catch (Exception e) {
Timber.e(e);
return false;
}
return true;
}
public static Map<String, String> getInitValueRules(File file, String rootElement) {
Timber.d("Parsing dependencies from %s file", file.getAbsolutePath());
final InputStream is;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e1) {
Timber.e(e1);
throw new IllegalStateException(e1);
}
InputStreamReader isr;
try {
isr = new InputStreamReader(is, "UTF-8");
} catch (UnsupportedEncodingException uee) {
Timber.w(uee, "Trying default encoding as UTF 8 encoding unavailable");
isr = new InputStreamReader(is);
}
final Document doc;
try {
doc = XFormParser.getXMLDocument(isr);
} catch (IOException e) {
Timber.e(e, "Unable to parse XML document %s", file.getAbsolutePath());
throw new IllegalStateException("Unable to parse XML document", e);
} finally {
try {
isr.close();
} catch (IOException e) {
Timber.w("%s error closing from reader", file.getAbsolutePath());
}
}
final String html = doc.getRootElement().getNamespace();
final Element head = doc.getRootElement().getElement(html, "head");
Map<String, String> result = new HashMap<>();
Element model = getChildElement(head, "model");
List<Element> initValues = findAllElementsByAttrName(model, "initvalue");
for (Element initValue : initValues) {
String nodeset = null;
String init = null;
for (int i = 0; i < initValue.getAttributeCount(); i++) {
if (initValue.getAttributeName(i).equals("nodeset")) {
nodeset = initValue.getAttributeValue(i);
}
if (initValue.getAttributeName(i).equals("initvalue")) {
if (initValue.getAttributeValue(i).contains(rootElement)) {
init = initValue.getAttributeValue(i);
}
}
}
if (nodeset != null && init != null) {
result.put(nodeset, init);
}
}
return result;
}
public static Map<String, String> getInstanceValuesMap(File file, Map<String, String> xPaths) {
Map<String, String> values = new LinkedHashMap<>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(file);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
Set<String> xPathsKeySet = xPaths.keySet();
for (String xPathsKey : xPathsKeySet) {
XPathExpression expression = xPath.compile(xPaths.get(xPathsKey));
Object res = expression.evaluate(doc, XPathConstants.STRING);
String node = (String) res;
values.put(xPathsKey, node);
}
} catch (Throwable throwable) {
Timber.e(throwable);
}
return values;
}
public static String getInstanceValue(File file, String xpath) {
String value = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(file);
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expression = xPath.compile(xpath);
Object res = expression.evaluate(doc, XPathConstants.STRING);
value = (String) res;
} catch (Throwable throwable) {
Timber.e(throwable);
}
return value;
}
public static List<String> getInstanceXPathList(File file) {
InputStream is;
......@@ -153,25 +300,6 @@ public class XmlUtils {
}
}
}
// for (int i = 0; i < body.getChildCount(); i++) {
// if (body.isText(i)) {
// continue;
// }
//
// if (body.getType(i) == Node.ELEMENT) {
// Element curr = body.getElement(i);
//
// String ref = getAttributeValue(curr, "ref");
// if (ref != null) {
// if (xPaths.contains(ref)) {
// Element label = getChildElement(curr, "label");
// if (label != null) {
// titles.put(ref, getText(label));
// }
// }
// }
// }
// }
return new FormLabels(formId, titles);
}
......@@ -206,7 +334,6 @@ public class XmlUtils {
private static List<String> getXPathByElement(Element element) {
List<String> target = new ArrayList<>();
// getXPathToTarget(target, element, "/" + element.getName());
getXPathToTarget(target, element, "");
return target;
......@@ -411,6 +538,7 @@ public class XmlUtils {
if (tagName.equals(el.getName())) {
elementList.add(el);
}
for (int i = 0; i < el.getChildCount(); i++) {
if (el.getType(i) == Node.ELEMENT) {
Element elem = el.getElement(i);
......@@ -425,6 +553,7 @@ public class XmlUtils {
if (localName.equals(el.getName()) && nameSpaceURI.contains(el.getNamespace())) {
elementList.add(el);
}
for (int i = 0; i < el.getChildCount(); i++) {
if (el.getType(i) == Node.ELEMENT) {
Element elem = el.getElement(i);
......@@ -444,6 +573,7 @@ public class XmlUtils {
for (int i = 0; i < el.getAttributeCount(); i++) {
if (attrName.equals(el.getAttributeName(i))) {
elementList.add(el);
break;
}
}
......
......@@ -123,8 +123,7 @@ public class CapiFormAdapter extends RecyclerView.Adapter<CapiFormAdapter.ViewHo
private void seeListRespons(int position) {
Intent intent;
// FIXME: 12/07/2018 INVERT! Testing purpose so added negation (!)
if (!dataSet.get(position).get(FORM_TYPE_KEY).equalsIgnoreCase(FormsProviderAPI.FORM_TYPE_UPDATING)) {
if (dataSet.get(position).get(FORM_TYPE_KEY).equalsIgnoreCase(FormsProviderAPI.FORM_TYPE_UPDATING)) {
intent = new Intent(context, ListingInstanceActivity.class);
} else {
intent = new Intent(context, CapiInstanceActivity.class);
......
......@@ -36,8 +36,26 @@ import org.javarosa.form.api.FormEntryModel;
import org.javarosa.xform.parse.XFormParser;
import org.javarosa.xform.util.XFormUtils;
import org.javarosa.xpath.XPathTypeMismatchException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import au.com.bytecode.opencsv.CSVReader;
import id.ac.stis.capi.R;
import id.ac.stis.capi.lessthink.models.DependencyDetails;
import id.ac.stis.capi.lessthink.utils.XmlUtils;
import id.ac.stis.capi.odk.application.Collect;
import id.ac.stis.capi.odk.dao.InstancesDao;
import id.ac.stis.capi.odk.database.ItemsetDbAdapter;
import id.ac.stis.capi.odk.external.ExternalAnswerResolver;
import id.ac.stis.capi.odk.external.ExternalDataHandler;
......@@ -50,22 +68,9 @@ import id.ac.stis.capi.odk.listeners.FormLoaderListener;
import id.ac.stis.capi.odk.logic.FileReferenceFactory;
import id.ac.stis.capi.odk.logic.FormController;
import id.ac.stis.capi.odk.preferences.PreferenceKeys;
import id.ac.stis.capi.odk.provider.InstanceProviderAPI;
import id.ac.stis.capi.odk.utilities.FileUtils;
import id.ac.stis.capi.odk.utilities.ZipUtils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import au.com.bytecode.opencsv.CSVReader;
import timber.log.Timber;
/**
......@@ -73,7 +78,7 @@ import timber.log.Timber;
*
* @author Carl Hartung (carlhartung@gmail.com)
* @author Yaw Anokwa (yanokwa@gmail.com)
* edit Mahendri Dwicahyo
* edit Mahendri Dwicahyo
*/
public class FormLoaderTask extends AsyncTask<String, String, FormLoaderTask.FECWrapper> {
private static final String ITEMSETS_CSV = "itemsets.csv";
......@@ -112,6 +117,10 @@ public class FormLoaderTask extends AsyncTask<String, String, FormLoaderTask.FEC
String formHash = FileUtils.getMd5Hash(formXml);
File formBin = new File(Collect.CACHE_PATH + File.separator + formHash + ".formdef");
publishProgress(Collect.getInstance().getString(R.string.parsing_dependencies));
resolveAndExecuteDependencies(formXml);
publishProgress(
Collect.getInstance().getString(R.string.survey_loading_reading_form_message));
......@@ -338,6 +347,33 @@ public class FormLoaderTask extends AsyncTask<String, String, FormLoaderTask.FEC
}
private void resolveAndExecuteDependencies(File file) {
List<DependencyDetails> deps = FileUtils.parseFormDependencies(file);
for (DependencyDetails dep : deps) {
File instanceFile = new File(instancePath);
String onValue = XmlUtils.getInstanceValue(instanceFile, dep.getDependencyRule());
if (onValue != null && !onValue.trim().isEmpty()) {
InstancesDao instancesDao = new InstancesDao();
Cursor c = instancesDao.getInstancesCursor(InstanceProviderAPI.InstanceColumns.INSTANCE_UUID + "=?",
new String[]{onValue});
if (c != null) {
if (c.moveToFirst()) {
String readInstanceFilePath = c.getString(c.getColumnIndex(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH));
File readInstanceFile = new File(readInstanceFilePath);
Map<String, String> initValues = XmlUtils.getInitValueRules(file, dep.getFormRootElement());
Map<String, String> initValuesValue = XmlUtils.getInstanceValuesMap(readInstanceFile, initValues);
XmlUtils.setValuesByMap(instanceFile, initValuesValue);
}
c.close();
}
}
}
}
@SuppressWarnings("unchecked")
private void loadExternalData(File mediaFolder) {
// SCTO-594
......
......@@ -535,7 +535,7 @@ public class FileUtils {
String version = dependency.getAttributeValue(null, "version");
String on = dependency.getAttributeValue(null, "on");
DependencyDetails details = new DependencyDetails(name, id, version, on);
DependencyDetails details = new DependencyDetails(id, version, name, on);
result.add(details);
}
}
......
......@@ -11,56 +11,59 @@
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</android.support.v4.widget.NestedScrollView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/collectiva_item_form" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<ProgressBar
android:id="@+id/holder_progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/recycler_view"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/collectiva_item_form" />
<LinearLayout
android:id="@+id/holder_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/background"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:visibility="gone">
<ProgressBar
android:id="@+id/holder_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/recycler_view"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp" />
<ImageView
android:id="@+id/icon_message"
android:layout_width="50dp"
android:layout_height="70dp"
android:tint="@android:color/darker_gray"
app:srcCompat="@drawable/ic_sad_sorry" />
<LinearLayout
android:id="@+id/holder_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/background"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:visibility="gone">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorry, internal error." />
</LinearLayout>
<ImageView
android:id="@+id/icon_message"
android:layout_width="50dp"
android:layout_height="70dp"
android:tint="@android:color/darker_gray"
app:srcCompat="@drawable/ic_sad_sorry" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sorry, internal error." />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
<!--<TextView-->
<!--android:id="@android:id/empty"-->
......
......@@ -531,4 +531,5 @@
<string name="access_token">pk.eyJ1Ijoia2FkZGFmaSIsImEiOiJjajMwOWlmazkwMDZmMnhvaDA5NnZzbXE0In0.Y9TXg0rJjTybmb3T-z76qw</string>
<string name="fetching_dependencies">Resolving dependencies of %1$s.\n\nForm %2$s of %3$s form(s).</string>
<string name="parsing_dependencies">Resolving dependencies…</string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment