Commit 7d68cfc1 authored by rahadi's avatar rahadi

Added Pemutakhiran Module

parent b9305b74
......@@ -2,7 +2,6 @@ package org.odk.collect.android.pemutakhiran.activities;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
......@@ -25,56 +24,60 @@ import java.util.List;
public class DsrtActivity extends DsrtMinImpActivity implements OnGenerateCompletedListener {
ProgressDialog progressDialog;
List<RutaModel> rutaModels;
private String formId;
private ProgressDialog progressDialog;
private List<RutaModel> rutaModels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: 21/02/2018 RAHADI: Dynamize
formId = "VSEN18K.v2";
RutaDao rutaDao = new RutaDao();
Cursor b = rutaDao.getRutaCursor(null, RutaProviderAPI.Columns.BS_UUID + "=?",
new String[]{getBsUuid()}, null);
rutaModels = rutaDao.getRutaListFromCursor(b);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Menggenerate Kuesioner");
progressDialog.setTitle("Generate Kuesioner");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setMessage("Harap Tunggu..");
progressDialog.setMax(rutaModels.size());
}
@Override
public void onGenerateMenuClicked(String bsUuid, String bsNks) {
public void onGenerateMenuClicked() {
progressDialog.show();
// TODO: 20/02/2018 RAHADI: dynamize
String formId = "VSEN18K.v2";
for (RutaModel r : rutaModels) {
String rutaUuid = r.getUuid();
String rutaName = r.getNamaKrt();
String filename = formId + "_" + bsNks + "_" + rutaName;
HashMap<String, KeyValueModel> keyValues = new HashMap<>();
keyValues.put(BSProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(BSProviderAPI.Columns.UUID, bsUuid));
keyValues.put(RutaProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(RutaProviderAPI.Columns.UUID, r.getUuid()));
HashMap<String, KeyValueModel> keyValues = new HashMap<>();
keyValues.put(BSProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(BSProviderAPI.Columns.UUID, getBsUuid()));
keyValues.put(RutaProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(RutaProviderAPI.Columns.UUID, rutaModels.get(0).getUuid()));
GenerateTask task = new GenerateTask(formId, rutaUuid, rutaName, filename, keyValues,
DsrtActivity.this);
task.execute();
}
GenerateTask task = new GenerateTask(formId, getBsNks(), rutaModels, keyValues,
DsrtActivity.this);
task.execute(0);
}
@Override
public void onGenerateSucceed(Uri instanceUri, String rutaUuid, String rutaName) {
int progress = progressDialog.getProgress() + 1;
progressDialog.setProgress(progress);
progressDialog.setMessage("Ruta: " + rutaName);
if (progress == progressDialog.getMax()) {
public void onGenerateSucceed(int currentIdx, String rutaName) {
if (currentIdx == rutaModels.size() - 1) {
progressDialog.dismiss();
Toast.makeText(this, "Generate Selesai", Toast.LENGTH_LONG).show();
} else {
currentIdx++;
progressDialog.setProgress(currentIdx);
progressDialog.setMessage("Ruta: " + rutaName);
HashMap<String, KeyValueModel> keyValues = new HashMap<>();
keyValues.put(BSProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(BSProviderAPI.Columns.UUID, getBsUuid()));
keyValues.put(RutaProviderAPI.Columns.CONTENT_TYPE, new KeyValueModel(RutaProviderAPI.Columns.UUID, rutaModels.get(0).getUuid()));
GenerateTask task = new GenerateTask(formId, getBsNks(), rutaModels, keyValues,
DsrtActivity.this);
task.execute(currentIdx);
}
}
......
package org.odk.collect.android.pemutakhiran.tasks;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
......@@ -10,12 +9,12 @@ import com.rahadi.vsen.daos.GenerateDao;
import com.rahadi.vsen.listeners.OnGenerateCompletedListener;
import com.rahadi.vsen.models.GenerateModel;
import com.rahadi.vsen.models.KeyValueModel;
import com.rahadi.vsen.models.RutaModel;
import com.rahadi.vsen.providers.GenerateProviderAPI;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.FormsDao;
import org.odk.collect.android.provider.FormsProviderAPI;
import org.odk.collect.android.provider.InstanceProviderAPI;
import org.odk.collect.android.utilities.FileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
......@@ -41,96 +40,120 @@ import timber.log.Timber;
* Created by Rahadi on 20/02/2018.
*/
public class GenerateTask extends AsyncTask<Void, Void, Void> {
public class GenerateTask extends AsyncTask<Integer, Void, Exception> {
private String filename, formId, rutaUuid, rutaName;
private int currentIdx;
private String formId, noNks;
private List<RutaModel> rutaModels;
private HashMap<String, KeyValueModel> keyValues;
private OnGenerateCompletedListener onGenerateCompletedListener;
public GenerateTask(String formId, String rutaUuid, String rutaName, String filename,
public GenerateTask(String formId, String noNks, List<RutaModel> rutaModels,
HashMap<String, KeyValueModel> keyValues,
OnGenerateCompletedListener onGenerateCompletedListener) {
this.filename = filename;
this.formId = formId;
this.rutaUuid = rutaUuid;
this.rutaName = rutaName;
this.noNks = noNks;
this.rutaModels = rutaModels;
this.keyValues = keyValues;
this.onGenerateCompletedListener = onGenerateCompletedListener;
}
@Override
protected Void doInBackground(Void... voids) {
protected Exception doInBackground(Integer... index) {
try {
this.currentIdx = index[0];
Cursor f = new FormsDao().getFormsCursor(
new String[]{FormsProviderAPI.FormsColumns.EMPTY_INSTANCE_FILE_PATH},
FormsProviderAPI.FormsColumns.JR_FORM_ID + "=?", new String[]{formId},
null);
String emptyInstancePath = f.getString(f.getColumnIndex(
FormsProviderAPI.FormsColumns.EMPTY_INSTANCE_FILE_PATH));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(emptyInstancePath));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
Cursor g = new GenerateDao().getGenerateCursor(null,
GenerateProviderAPI.Columns.FORM_ID + "=?", new String[]{formId}, null);
List<GenerateModel> gms = new GenerateDao().getGenerateListFromCursor(g);
for (GenerateModel gm : gms) {
String value = null;
Uri uri = Uri.parse(gm.getTableUri());
String type = Vsen.getInstance().getContentResolver().getType(uri);
KeyValueModel kv = keyValues.get(type);
Cursor c = Vsen.getInstance().getContentResolver().query(uri, new String[]{gm.getCursorName()},
kv.getKey() + "=?", new String[]{kv.getValue()}, null);
if (c != null) {
if (c.moveToFirst()) {
value = c.getString(c.getColumnIndex(gm.getCursorName()));
if (f != null) {
if (f.moveToFirst()) {
String emptyInstancePath = f.getString(f.getColumnIndex(
FormsProviderAPI.FormsColumns.EMPTY_INSTANCE_FILE_PATH));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(emptyInstancePath));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
Cursor g = new GenerateDao().getGenerateCursor(null,
GenerateProviderAPI.Columns.FORM_ID + "=?", new String[]{formId}, null);
List<GenerateModel> gms = new GenerateDao().getGenerateListFromCursor(g);
for (GenerateModel gm : gms) {
String value = null;
Uri uri = Uri.parse(gm.getTableUri());
String type = Vsen.getInstance().getContentResolver().getType(uri);
KeyValueModel kv = keyValues.get(type);
Cursor c = Vsen.getInstance().getContentResolver().query(uri, new String[]{gm.getCursorName()},
kv.getKey() + "=?", new String[]{kv.getValue()}, null);
if (c != null) {
if (c.moveToFirst()) {
value = c.getString(c.getColumnIndex(gm.getCursorName()));
}
c.close();
}
XPathExpression expr = xpath.compile(gm.getxPath());
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
node.setTextContent(value);
}
c.close();
}
XPathExpression expr = xpath.compile(gm.getxPath());
Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
node.setTextContent(value);
}
String path = Collect.INSTANCES_PATH + File.separator + filename + File.separator + filename + ".xml";
int i = 2;
File newFile = null;
if (FileUtils.createFolder(Collect.INSTANCES_PATH + File.separator + filename)) {
newFile = new File(path);
while (newFile.exists()) {
path = Collect.INSTANCES_PATH + File.separator + filename + File.separator + filename + "_" + i + ".xml";
newFile = new File(path);
i++;
String fileName = formId + "-" + noNks + "-" + rutaModels.get(index[0]).getNamaKrt();
fileName = fileName.replace(" ", "_");
String xmlPath = Collect.INSTANCES_PATH + File.separator + fileName
+ File.separator + fileName + ".xml";
int i = 2;
File newFile = null;
if (FileUtils.createFolder(Collect.INSTANCES_PATH + File.separator + fileName)) {
newFile = new File(xmlPath);
while (newFile.exists()) {
FileUtils.createFolder(Collect.INSTANCES_PATH + File.separator + fileName + "_" + i);
xmlPath = Collect.INSTANCES_PATH + File.separator + fileName + "_" + i
+ File.separator + fileName + "_" + i + ".xml";
newFile = new File(xmlPath);
i++;
}
// 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(newFile);
transformer.transform(source, result);
// ContentValues v = new ContentValues();
// v.put(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, noNks + "-" + rutaName);
// v.put(InstanceProviderAPI.InstanceColumns.JR_FORM_ID, formId);
// v.put(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH, newFile.getAbsolutePath());
// v.put(InstanceProviderAPI.InstanceColumns.STATUS, InstanceProviderAPI.STATUS_INCOMPLETE);
//
// Uri instanceUri = Collect.getInstance().getContentResolver().insert(InstanceProviderAPI.InstanceColumns.CONTENT_URI, v);
}
}
// 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(newFile);
transformer.transform(source, result);
ContentValues v = new ContentValues();
v.put(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME, filename);
v.put(InstanceProviderAPI.InstanceColumns.JR_FORM_ID, formId);
v.put(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH, newFile.getAbsolutePath());
v.put(InstanceProviderAPI.InstanceColumns.STATUS, InstanceProviderAPI.STATUS_INCOMPLETE);
Uri instaceUri = Collect.getInstance().getContentResolver().insert(InstanceProviderAPI.InstanceColumns.CONTENT_URI, v);
onGenerateCompletedListener.onGenerateSucceed(instaceUri, rutaUuid, rutaName);
f.close();
}
} catch (Exception e) {
Timber.e(e);
onGenerateCompletedListener.onGenerateFailed(e);
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception e) {
super.onPostExecute(e);
if (e != null) {
onGenerateCompletedListener.onGenerateFailed(e);
} else {
onGenerateCompletedListener.onGenerateSucceed(currentIdx, rutaModels.get(currentIdx).getNamaKrt());
}
}
}
......@@ -141,6 +141,7 @@ public class FormsProvider extends ContentProvider {
+ ((oldVersion != 3) ? ""
: (FormsColumns.BASE64_RSA_PUBLIC_KEY + ", "))
+ FormsColumns.JRCACHE_FILE_PATH
+ ", "
+ FormsColumns.EMPTY_INSTANCE_FILE_PATH
+ ") SELECT "
+ FormsColumns._ID
......@@ -174,7 +175,9 @@ public class FormsProvider extends ContentProvider {
+ ((oldVersion != 3) ? ""
: (FormsColumns.BASE64_RSA_PUBLIC_KEY + ", "))
+ FormsColumns.JRCACHE_FILE_PATH
+ FormsColumns.EMPTY_INSTANCE_FILE_PATH + " FROM "
+ ", "
+ FormsColumns.EMPTY_INSTANCE_FILE_PATH
+ " FROM "
+ FORMS_TABLE_NAME);
// risky failures here...
......@@ -203,7 +206,7 @@ public class FormsProvider extends ContentProvider {
+ FormsColumns.SUBMISSION_URI + ", "
+ FormsColumns.JR_VERSION + ", "
+ FormsColumns.BASE64_RSA_PUBLIC_KEY + ", "
+ FormsColumns.JRCACHE_FILE_PATH
+ FormsColumns.JRCACHE_FILE_PATH + ", "
+ FormsColumns.EMPTY_INSTANCE_FILE_PATH + ") SELECT "
+ FormsColumns._ID + ", "
+ FormsColumns.DISPLAY_NAME
......@@ -224,7 +227,7 @@ public class FormsProvider extends ContentProvider {
+ FormsColumns.SUBMISSION_URI + ", "
+ FormsColumns.JR_VERSION + ", "
+ FormsColumns.BASE64_RSA_PUBLIC_KEY + ", "
+ FormsColumns.JRCACHE_FILE_PATH
+ FormsColumns.JRCACHE_FILE_PATH + ", "
+ FormsColumns.EMPTY_INSTANCE_FILE_PATH + " FROM "
+ TEMP_FORMS_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TEMP_FORMS_TABLE_NAME);
......
......@@ -44,6 +44,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
import timber.log.Timber;
import static org.odk.collect.android.provider.InstanceProviderAPI.InstanceColumns;
import static org.odk.collect.android.provider.InstanceProviderAPI.LATEST_VERSION;
/**
* Background task for syncing form instances from the instances folder to the instances table.
......@@ -169,6 +170,7 @@ public class InstanceSyncTask extends AsyncTask<Void, String, String> {
? InstanceProviderAPI.STATUS_COMPLETE : InstanceProviderAPI.STATUS_INCOMPLETE);
values.put(InstanceColumns.CAN_EDIT_WHEN_COMPLETE, Boolean.toString(true));
values.put(InstanceColumns.INSTANCE_UUID, getInstanceIdFromInstance(candidateInstance));
values.put(InstanceColumns.LATEST, LATEST_VERSION);
// save the new instance object
Collect.getInstance().getContentResolver()
.insert(InstanceColumns.CONTENT_URI, values);
......
......@@ -82,7 +82,7 @@ public abstract class DsrtMinImpActivity extends AppCompatActivity {
if (menuId == android.R.id.home) {
onBackPressed();
} else if (menuId == R.id.generate_menu_item) {
onGenerateMenuClicked(bsUuid, bsNks);
onGenerateMenuClicked();
}
return true;
......@@ -103,6 +103,6 @@ public abstract class DsrtMinImpActivity extends AppCompatActivity {
return bsNks;
}
public abstract void onGenerateMenuClicked(String bsUuid, String bsNks);
public abstract void onGenerateMenuClicked();
}
......@@ -7,7 +7,7 @@ import android.net.Uri;
*/
public interface OnGenerateCompletedListener {
void onGenerateSucceed(Uri instanceUri, String rutaUuid, String rutaName);
void onGenerateSucceed(int currentIdx, String rutaName);
void onGenerateFailed(Exception e);
}
\ No newline at end of file
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