Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ android {
}

chfDev {
applicationId "org.openimis.imispolicies.chf"
applicationId "org.openimis.imispolicies.chfdev"
buildConfigField "String", "API_BASE_URL", '"http://chf-dev.swisstph-mis.ch/rest/"'
buildConfigField "boolean", "SHOW_PAYMENT_MENU", 'true'
buildConfigField "boolean", "SHOW_BULK_CN_MENU", 'true'
Expand All @@ -101,7 +101,7 @@ android {
chfProd {
applicationId "org.openimis.imispolicies.chf"
buildConfigField "String", "API_BASE_URL", '"http://chf-imis.or.tz/restapi/"'
buildConfigField "boolean", "SHOW_PAYMENT_MENU", 'true'
buildConfigField "boolean", "SHOW_PAYMENT_MENU", 'false'
buildConfigField "boolean", "SHOW_BULK_CN_MENU", 'true'
buildConfigField "String", "APP_DIR", '"IMIS-CHFPROD"'
resValue "string", "app_name_policies", "Policies CHF"
Expand Down
815 changes: 428 additions & 387 deletions app/src/bepha/res/values-fr/strings.xml

Large diffs are not rendered by default.

815 changes: 428 additions & 387 deletions app/src/bepha/res/values/strings.xml

Large diffs are not rendered by default.

815 changes: 428 additions & 387 deletions app/src/chf/res/values-sw/strings.xml

Large diffs are not rendered by default.

817 changes: 428 additions & 389 deletions app/src/chf/res/values/strings.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/src/main/assets/pages/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $(document).ready(function () {
if (!Android.isLoggedIn()) {
window.open("Login.html?s=0", "_self");
} else {
Android.UploadOfflineFeedbackRenewal('renewal');
Android.uploadRenewals();
}

break;
Expand Down
163 changes: 134 additions & 29 deletions app/src/main/java/org/openimis/imispolicies/ClientAndroidInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -99,21 +96,26 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.Set;
import java.util.regex.Pattern;

import org.openimis.imispolicies.Util.AndroidUtil;
import org.xmlpull.v1.XmlSerializer;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HttpsURLConnection;

import static android.database.sqlite.SQLiteDatabase.openOrCreateDatabase;
import static org.openimis.imispolicies.Util.JsonUtil.isStringEmpty;
import static org.openimis.imispolicies.Util.StringUtil.isEmpty;

public class ClientAndroidInterface {
private static final String LOG_TAG_RENEWAL = "RENEWAL";

private Context mContext;

private SQLHandler sqlHandler;
Expand Down Expand Up @@ -268,24 +270,14 @@ public String getControl(String ctl) {

@JavascriptInterface
public void ShowToast(String msg) {
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
AndroidUtil.showToast(mContext, msg);
}

@JavascriptInterface
public AlertDialog ShowDialog(String msg) {
return new AlertDialog.Builder(mContext)
.setMessage(msg)
.setCancelable(false)
.setPositiveButton(R.string.Ok, (dialogInterface, i) -> {
}).show();
return AndroidUtil.showDialog(mContext, msg);
}


// public int getDialogResult(final String msg){
// ShowDialogYesNo(msg);
// while (inProgress){}
// return DialogResult;
// }
@JavascriptInterface
public AlertDialog ShowDialogYesNo(final int InsureId, final int FamilyId, Boolean Activate, final int isOffline) {
return new AlertDialog.Builder(mContext)
Expand Down Expand Up @@ -4464,21 +4456,129 @@ public Boolean UploadOfflineFeedbackRenewal(final String ActivityName) {
return true;
}

@JavascriptInterface
public void uploadRenewals() {
final String path;
final int totalFiles;
final ProgressDialog pd;

path = global.getMainDirectory();
File[] files = GetListOfFiles(path, "Renewal", null);
File[] jsonFiles = GetListOfJSONFiles(path, "Renewal", null);

totalFiles = jsonFiles.length;

if (totalFiles == 0) {
ShowDialog(mContext.getResources().getString(R.string.NoDataAvailable));
DeleteRenewals();
return;
}

if (!global.isNetworkAvailable()) {
ShowDialog(mContext.getResources().getString(R.string.NoInternet));
return;
}

pd = AndroidUtil.showProgressDialog(mContext, R.string.Sync, R.string.SyncProcessing);

new Thread(() -> {
StringBuilder messageBuilder = new StringBuilder();
String messageFormat = "[%s] %s\n";
Map<Integer, String> messages = new HashMap<>();
messages.put(ToRestApi.RenewalStatus.ACCEPTED, mContext.getResources().getString(R.string.RenewalAccepted));
messages.put(ToRestApi.RenewalStatus.ALREADY_ACCEPTED, mContext.getResources().getString(R.string.RenewalAlreadyAccepted));
messages.put(ToRestApi.RenewalStatus.REJECTED, mContext.getResources().getString(R.string.RenewalRejected));
messages.put(ToRestApi.RenewalStatus.DUPLICATE_RECEIPT, mContext.getResources().getString(R.string.DuplicateReceiptNumber));
messages.put(ToRestApi.RenewalStatus.GRACE_PERIOD_EXPIRED, mContext.getResources().getString(R.string.GracePeriodExpied));
messages.put(ToRestApi.RenewalStatus.CONTROL_NUMBER_ERROR, mContext.getResources().getString(R.string.ControlNumberError));
messages.put(ToRestApi.RenewalStatus.UNEXPECTED_EXCEPTION, mContext.getResources().getString(R.string.UnexpectedException));

ToRestApi rest = new ToRestApi();
JSONObject obj;

int acceptedRenewals = 0;
for (int i = 0; i < jsonFiles.length; i++) {
String jsonText = global.getFileText(jsonFiles[i].getPath());
String renewalInsureeNo = "";

HttpResponse response;
int responseCode;
int uploadStatus = -1;

try {
obj = new JSONObject(jsonText).getJSONObject("Policy");
renewalInsureeNo = obj.getString("CHFID");

response = rest.postToRestApiToken(obj, "policy/renew");
if (response != null) {
responseCode = response.getStatusLine().getStatusCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
uploadStatus = Integer.parseInt(EntityUtils.toString(response.getEntity()));
} else {
if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new JSONException("Renewal file caused 400 BAD REQUEST");
} else if (responseCode == HttpsURLConnection.HTTP_UNAUTHORIZED) {
messageBuilder.append(String.format(messageFormat, renewalInsureeNo, mContext.getResources().getString(R.string.LoginFail)));
break;
} else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
throw new IOException("500 INTERNAL ERROR");
}
}
} else {
Log.e(LOG_TAG_RENEWAL, "Server not responding");
messageBuilder.append(String.format(messageFormat, renewalInsureeNo, mContext.getResources().getString(R.string.SomethingWrongServer)));
break;
}
} catch (JSONException e) {
Log.e(LOG_TAG_RENEWAL, "Invalid renewal json format", e);
messageBuilder.append(String.format(messageFormat, renewalInsureeNo, mContext.getResources().getString(R.string.InvalidRenewalFile)));
continue;
} catch (IOException e) {
Log.e(LOG_TAG_RENEWAL, "Error while sending renewal", e);
messageBuilder.append(String.format(messageFormat, renewalInsureeNo, mContext.getResources().getString(R.string.SomethingWrongServer)));
continue;
}


if (messages.containsKey(uploadStatus)) {
if (uploadStatus == ToRestApi.RenewalStatus.ACCEPTED || uploadStatus == ToRestApi.RenewalStatus.ALREADY_ACCEPTED) {
MoveFile(files[i], 1);
MoveFile(jsonFiles[i], 1);
acceptedRenewals++;
} else {
MoveFile(files[i], 2);
MoveFile(jsonFiles[i], 2);
messageBuilder.append(String.format(messageFormat, renewalInsureeNo, messages.get(uploadStatus)));
}
}
}

String successMessage = mContext.getResources().getString(R.string.BulkUpload);

String resultMessage;
if (acceptedRenewals != jsonFiles.length) {
resultMessage = successMessage + "\n" + messageBuilder.toString();
} else {
resultMessage = successMessage;
}

((Activity) mContext).runOnUiThread(() -> AndroidUtil.showDialog(mContext, resultMessage));

pd.dismiss();
}).start();
}

private File[] GetListOfFiles(String DirectoryPath, final String FileType, final String FileName) {
File Directory = new File(DirectoryPath);
final Pattern p = Pattern.compile("(RenPol_)");
FilenameFilter filter = new FilenameFilter() {

@Override
public boolean accept(File dir, String filename) {
if (FileType.equals("Image")) {
// return filename.equalsIgnoreCase(FileName);
return filename.endsWith(".jpg");
} else if (FileType.equals("Feedback")) {
return filename.startsWith("feedback_");
} else {
return filename.startsWith("RenPol_");
}
FilenameFilter filter = (dir, filename) -> {
if (FileType.equals("Image")) {
// return filename.equalsIgnoreCase(FileName);
return filename.endsWith(".jpg");
} else if (FileType.equals("Feedback")) {
return filename.startsWith("feedback_");
} else {
return filename.startsWith("RenPol_");
}
};
return Directory.listFiles(filter);
Expand Down Expand Up @@ -4541,14 +4641,19 @@ private void MoveFile(File file, int res) {
Rejected = "RejectedFeedback";
}

boolean status = false;
switch (res) {
case 1:
file.renameTo(new File(global.getSubdirectory(Accepted), file.getName()));
status = file.renameTo(new File(global.getSubdirectory(Accepted), file.getName()));
break;
case 2:
file.renameTo(new File(global.getSubdirectory(Rejected), file.getName()));
status = file.renameTo(new File(global.getSubdirectory(Rejected), file.getName()));
break;
}

if (!status) {
Log.w(Global.FILE_IO_LOG_TAG, "Moving file failed: " + file.getAbsolutePath());
}
}


Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/openimis/imispolicies/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@

public class Global extends Application {
private static Global GlobalContext;
private static final String PREF_NAME = "CMPref";
private static final String PREF_LOG_TAG = "PREFS";
private static final String FILE_IO_LOG_TAG = "FILEIO";
public static final String PREF_NAME = "CMPref";
public static final String PREF_LOG_TAG = "PREFS";
public static final String FILE_IO_LOG_TAG = "FILEIO";

private String OfficerCode;
private String OfficerName;
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/openimis/imispolicies/ToRestApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;

public class ToRestApi {
public static class UploadStatus {
Expand All @@ -24,6 +25,16 @@ public static class UploadStatus {
public static final int ERROR = 2;
}

public static class RenewalStatus {
public static final int ACCEPTED = 3001;
public static final int ALREADY_ACCEPTED = 3002;
public static final int REJECTED = 3003;
public static final int DUPLICATE_RECEIPT = 3004;
public static final int GRACE_PERIOD_EXPIRED = 3005;
public static final int CONTROL_NUMBER_ERROR = 3006;
public static final int UNEXPECTED_EXCEPTION = 3999;
}

public static class Headers {
public static final String CONTENT_TYPE = "Content-Type";
public static final String ACCEPT = "Accept";
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/org/openimis/imispolicies/Util.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.openimis.imispolicies;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -58,4 +62,38 @@ public static boolean isStringEmpty(JSONObject object, @NonNull String field, bo
}
}
}

public static class AndroidUtil {
public static ProgressDialog showProgressDialog(Context context, int titleResId, int messageResId) {
return ProgressDialog.show(
context,
context.getResources().getString(titleResId),
context.getResources().getString(messageResId)
);
}

public static void showToast(Context context, int messageResId) {
Toast.makeText(context, messageResId, Toast.LENGTH_SHORT).show();
}

public static void showToast(Context context, CharSequence message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}

public static AlertDialog showDialog(Context context, int messageResId) {
return new AlertDialog.Builder(context)
.setMessage(messageResId)
.setCancelable(false)
.setPositiveButton(R.string.Ok, (dialogInterface, i) -> {
}).show();
}

public static AlertDialog showDialog(Context context, CharSequence message) {
return new AlertDialog.Builder(context)
.setMessage(message)
.setCancelable(false)
.setPositiveButton(R.string.Ok, (dialogInterface, i) -> {
}).show();
}
}
}
Loading