Skip to content

Commit

Permalink
#117 #148 Constraints class loading JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
hallahan committed May 13, 2016
1 parent 39b8340 commit fda469b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
71 changes: 71 additions & 0 deletions app/src/main/java/org/redcross/openmapkit/Constraints.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.redcross.openmapkit;

import com.spatialdev.osm.model.OSMElement;

import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import org.redcross.openmapkit.odkcollect.ODKCollectHandler;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Constraints {

// We're just keeping these guys static so they only get loaded up once.
private static JSONObject defaultConstraints;
private static Map<String, JSONObject> formConstraints = new HashMap<>();

private OSMElement osmElement;
private JSONObject activeFormConstraints;

boolean active = true;

public Constraints(OSMElement osmElement) {
this.osmElement = osmElement;

loadConstraintsJson();
}

private void loadConstraintsJson() {
if (defaultConstraints == null) {
try {
File defaultConstraintsFile = ExternalStorage.fetchConstraintsFile("default");
String defaultConstraintsStr = FileUtils.readFileToString(defaultConstraintsFile);
defaultConstraints = new JSONObject(defaultConstraintsStr);
} catch (Exception e) {
active = false;
}
}

if (!ODKCollectHandler.isODKCollectMode()) return;

String formId = ODKCollectHandler.getODKCollectData().getFormId();
activeFormConstraints = formConstraints.get(formId);
if (activeFormConstraints == null) {
try {
File formConstraintsFile = ExternalStorage.fetchConstraintsFile(formId);
String formConstraintsStr = FileUtils.readFileToString(formConstraintsFile);
JSONObject json = new JSONObject(formConstraintsStr);
formConstraints.put(formId, json);
} catch (Exception e) {
// do nothing
// We typically do not need a constraints specific to a given form,
// so this is normal.
}
}
}

/**
* If there is no default.json constraints file, we have nothing to work with,
* so the constraint functionality is disabled.
*
* @return boolean if the constraint is active
*/
public boolean isActive() {
return active;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.util.Log;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -359,7 +360,12 @@ private static void copyAssetsFileToExternalStorage(Context context, String file

}


public static File fetchConstraintsFile(String formName) {
File storageDir = Environment.getExternalStorageDirectory();
File appDir = new File(storageDir, APP_DIR);
File constraintsDir = new File(appDir, CONSTRAINTS_DIR);
return new File(constraintsDir, formName + ".json");
}



Expand Down

0 comments on commit fda469b

Please sign in to comment.