You are on page 1of 24

// rawMaterialInfos = new ArrayList();

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

findViews();

// checkAvailableJson(lab_code);

btnSync();

textChange();

private void checkAvailableJson(String lab_code) {

SQLiteDatabase db = DataBaseSQlite.connectToDb(context);

try {

String query = "SELECT distinct lab_certifications_info from tbl_lab_survey_data where lab_code


= '" + lab_code + "'";

Cursor cur = db.rawQuery(query, null);

if (cur.getCount() == 1) {

while (cur.moveToNext()) {

jsonRetrived = cur.getString(cur.getColumnIndex("lab_certifications_info"));

} else {

Toast.makeText(context, "Problem!", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

e.printStackTrace();
Toast.makeText(this, "Exception: " + e.getCause(), Toast.LENGTH_LONG).show();

} finally {

db.close();

private void textChange() {

helper.TextChangeListenerLooper(extra_staff_container);

private void findViews() {

toolbar = (Toolbar) findViewById(R.id.toolbar);

try {

toolbar.setTitle("Certifications");

// toolbar.setSubtitle(lab_code+" ("+lab_name+")");

setSupportActionBar(toolbar);

} catch (Exception e) {

e.printStackTrace();

snackbarView = (View) findViewById(R.id.snackbarView);

surveyForm = (LinearLayout) findViewById(R.id.surveyForm);

sendSave = (Button) findViewById(R.id.sendSave);

extra_staff_container = (LinearLayout) findViewById(R.id.extra_staff_container);

sendSave.setOnClickListener(this);

}
public void btnSync() {

try {

fetchMISdata();

} catch (Exception e) {

e.printStackTrace();

private void fetchMISdata() {

SQLiteDatabase db = DataBaseSQlite.connectToDb(context);

try {

String query = "SELECT distinct lab_certifications_info from tbl_lab_details where lab_code = '" +
lab_code + "'";

Cursor cur = db.rawQuery(query, null);

int count = cur.getCount();

ArrayList<String> stringArrayList = new ArrayList<>();

if (count == 1) {

while (cur.moveToNext()) {

stringArrayList.add(cur.getString(cur.getColumnIndex("lab_certifications_info")));

} else {

Toast.makeText(context, "Problem!", Toast.LENGTH_SHORT).show();

arrSyncData.clear();
for (int i3 = 0; i3 < stringArrayList.size(); i3++) {

try {

JSONArray responseReturned = new JSONArray(stringArrayList.get(i3));

JSONObject jsonResponse = (JSONObject) responseReturned.get(0);

if (jsonResponse.getString("response").equalsIgnoreCase("true")) {

JSONObject jsonData = (JSONObject) responseReturned.get(1);

JSONArray jsonArray = jsonData.getJSONArray("data");

for (int i = 0; i < jsonArray.length(); i++) {

CertificationsData verticalData = new CertificationsData();

JSONObject jsonObject = (JSONObject) jsonArray.get(i);

verticalData.setCategory(jsonObject.getString("category"));

verticalData.setScore(jsonObject.getInt("score"));

JSONArray jsonDetailedArray = jsonObject.getJSONArray("innerData");

ArrayList<receivedCertificationsDetails> rPData = new ArrayList<>();

for (int x = 0; x < jsonDetailedArray.length(); x++) {

receivedCertificationsDetails usersModel = new receivedCertificationsDetails();

JSONObject jsonDetailedObject = (JSONObject) jsonDetailedArray.get(x);


usersModel.setId(jsonDetailedObject.getInt("id"));

usersModel.setCertification_lab_code(jsonDetailedObject.getString("certification_lab_code"));

usersModel.setUser_login(jsonDetailedObject.getString("user_login"));

usersModel.setCertification_type(jsonDetailedObject.getString("certification_type"));

usersModel.setOther_certification(jsonDetailedObject.getString("other_certification"));

usersModel.setCategory(jsonDetailedObject.getString("category"));

usersModel.setIssuance_date(jsonDetailedObject.getString("issuance_date"));

usersModel.setExpiry_date(jsonDetailedObject.getString("expiry_date"));

rPData.add(usersModel);

verticalData.setPublicationDetail(rPData);

arrSyncData.add(verticalData);

} else {

hidepDialogFetch();

JSONObject jsonData = (JSONObject) responseReturned.get(1);

JSONArray jsonArray = jsonData.getJSONArray("data");

JSONObject jsonObject = (JSONObject) jsonArray.get(0);

String message = jsonObject.getString("message");


helper.dialog(context, "" + message, "Alert!");

} catch (Exception e) {

hidepDialogFetch();

Toast.makeText(context, "No Record Found.", Toast.LENGTH_SHORT).show();

for (int i = 0; i < arrSyncData.size(); i++) {

Log.d("touch", "paste");

LayoutInflater layoutInflater = (LayoutInflater)


context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View addView = layoutInflater.inflate(R.layout.xx_certifications_info_row, null);

TextView qualificationHeading = (TextView)


addView.findViewById(R.id.tv_qualification_heading);

LinearLayout staffDetailedContainer = (LinearLayout)


addView.findViewById(R.id.staff_detailed_container);

final RadioGroup rg_meeting_requirements = (RadioGroup)


addView.findViewById(R.id.rg_meeting_requirements);

EditText et_comment = (EditText) addView.findViewById(R.id.et_comment);

EditText et_score = (EditText) addView.findViewById(R.id.et_score);

TextInputLayout til_score = (TextInputLayout) addView.findViewById(R.id.til_score);

String catType = arrSyncData.get(i).getCategory();

qualificationHeading.setText(catType.toUpperCase());
String TagString = catType.toLowerCase().replace(" ", "_");

rg_meeting_requirements.setTag("rg_cert_meeting---" + catType);

et_comment.setTag("et_cert_comment---" + catType);

et_score.setTag("et_cert_score---" + catType);

// rg_meeting_requirements.setTag("rg_cert_meeting_" + TagString);

// et_comment.setTag("et_cert_comment_" + TagString);

// et_score.setTag("et_cert_score_" + TagString);

package com.uu.epd_lir;

import android.app.ProgressDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.support.design.widget.Snackbar;

import android.support.design.widget.TextInputLayout;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;
import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.Toast;

import com.uu.epd_lir.database.DataBaseSQlite;

import com.uu.epd_lir.database.Querries;

import com.uu.epd_lir.helper.Helper;

import com.uu.epd_lir.helper.SeterGeter;

import com.uu.epd_lir.model.DataModels.CertificationsData;

import com.uu.epd_lir.model.ReceivedDetails.receivedCertificationsDetails;

import org.apache.http.conn.HttpHostConnectException;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.ArrayList;

import java.util.Iterator;

import static android.view.View.VISIBLE;

public class LabCertificationsInfo extends AppCompatActivity implements View.OnClickListener {


Context context;

private Helper helper;

private ProgressDialog progressDialog;

private SharedPreferences sharedPreferences;

private Toolbar toolbar;

private View snackbarView;

private LinearLayout surveyForm;

private LinearLayout extra_staff_container;

private Button sendSave;

private ArrayList<CertificationsData> arrSyncData = new ArrayList<>();

String lab_code, lab_name;

LayoutInflater inflater;

private int pk_id = 0;

private JSONObject json;

private boolean edEmptyCheck = false;

private boolean radioOk = false;

private String jsonRetrived = "NA";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_lir_certifications);

context = this;
helper = new Helper(context);

sharedPreferences = context.getSharedPreferences("MyData", Context.MODE_PRIVATE);

Intent intent = getIntent();

lab_code = intent.getExtras().getString("lab_code");

lab_name = intent.getExtras().getString("lab_name");

pk_id = intent.getExtras().getInt("pk_id");

progressDialog = new ProgressDialog(context);

progressDialog.setMessage("Please wait, fetching records...");

progressDialog.setCancelable(false);

// rawMaterialInfos = new ArrayList();

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

findViews();

// checkAvailableJson(lab_code);

btnSync();

textChange();

private void checkAvailableJson(String lab_code) {

SQLiteDatabase db = DataBaseSQlite.connectToDb(context);

try {

String query = "SELECT distinct lab_certifications_info from tbl_lab_survey_data where lab_code


= '" + lab_code + "'";
Cursor cur = db.rawQuery(query, null);

if (cur.getCount() == 1) {

while (cur.moveToNext()) {

jsonRetrived = cur.getString(cur.getColumnIndex("lab_certifications_info"));

} else {

Toast.makeText(context, "Problem!", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

e.printStackTrace();

Toast.makeText(this, "Exception: " + e.getCause(), Toast.LENGTH_LONG).show();

} finally {

db.close();

private void textChange() {

helper.TextChangeListenerLooper(extra_staff_container);

private void findViews() {

toolbar = (Toolbar) findViewById(R.id.toolbar);

try {

toolbar.setTitle("Certifications");

// toolbar.setSubtitle(lab_code+" ("+lab_name+")");

setSupportActionBar(toolbar);

} catch (Exception e) {
e.printStackTrace();

snackbarView = (View) findViewById(R.id.snackbarView);

surveyForm = (LinearLayout) findViewById(R.id.surveyForm);

sendSave = (Button) findViewById(R.id.sendSave);

extra_staff_container = (LinearLayout) findViewById(R.id.extra_staff_container);

sendSave.setOnClickListener(this);

public void btnSync() {

try {

fetchMISdata();

} catch (Exception e) {

e.printStackTrace();

private void fetchMISdata() {

SQLiteDatabase db = DataBaseSQlite.connectToDb(context);

try {

String query = "SELECT distinct lab_certifications_info from tbl_lab_details where lab_code = '" +
lab_code + "'";

Cursor cur = db.rawQuery(query, null);

int count = cur.getCount();


ArrayList<String> stringArrayList = new ArrayList<>();

if (count == 1) {

while (cur.moveToNext()) {

stringArrayList.add(cur.getString(cur.getColumnIndex("lab_certifications_info")));

} else {

Toast.makeText(context, "Problem!", Toast.LENGTH_SHORT).show();

arrSyncData.clear();

for (int i3 = 0; i3 < stringArrayList.size(); i3++) {

try {

JSONArray responseReturned = new JSONArray(stringArrayList.get(i3));

JSONObject jsonResponse = (JSONObject) responseReturned.get(0);

if (jsonResponse.getString("response").equalsIgnoreCase("true")) {

JSONObject jsonData = (JSONObject) responseReturned.get(1);

JSONArray jsonArray = jsonData.getJSONArray("data");

for (int i = 0; i < jsonArray.length(); i++) {

CertificationsData verticalData = new CertificationsData();


JSONObject jsonObject = (JSONObject) jsonArray.get(i);

verticalData.setCategory(jsonObject.getString("category"));

verticalData.setScore(jsonObject.getInt("score"));

JSONArray jsonDetailedArray = jsonObject.getJSONArray("innerData");

ArrayList<receivedCertificationsDetails> rPData = new ArrayList<>();

for (int x = 0; x < jsonDetailedArray.length(); x++) {

receivedCertificationsDetails usersModel = new receivedCertificationsDetails();

JSONObject jsonDetailedObject = (JSONObject) jsonDetailedArray.get(x);

usersModel.setId(jsonDetailedObject.getInt("id"));

String part2 = null;

String part1 = null;

if (jsonRetrived.length() > 10) {

JSONObject gsonObject = new JSONObject(jsonRetrived);

Iterator a = gsonObject.keys();

while (a.hasNext()) {

String key = (String) a.next();

// loop to get the dynamic key

String value = null;

try {

value = (String) gsonObject.get(key);

} catch (JSONException e) {

e.printStackTrace();
}

String[] parts = key.split("---");

part1 = parts[0];

part2 = parts[1];

if (part2.equalsIgnoreCase(catType)) {

String[] a1 = part1.split("_");

String subPart1 = a1[0];

String subPart3 = a1[2];

if (subPart1.equalsIgnoreCase("rg")) {

if (subPart3.equalsIgnoreCase("meeting")) {

if(value.equalsIgnoreCase("Yes")){

rg_meeting_requirements.check(R.id.rb_yes_meeting_requirements);

}else if(value.equalsIgnoreCase("No")){

rg_meeting_requirements.check(R.id.rb_no_meeting_requirements);

} else if (subPart1.equalsIgnoreCase("et")) {

if (subPart3.equalsIgnoreCase("comment")) {

et_comment.setText(value);

}else if (subPart3.equalsIgnoreCase("score")) {

et_score.setText(value);

}
Log.d("touch", "key :" + key);

Log.d("touch", "value :" + value);

til_score.setHint("Score (Out of "+arrSyncData.get(i).getScore()+")");

rg_meeting_requirements.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

RadioButton rbb = (RadioButton)


rg_meeting_requirements.getChildAt(rg_meeting_requirements.getChildCount() - 1);

rbb.setError(null);

});

// Toast.makeText(context, "Meeting Radio Group Tag:\n" +


rg_meeting_requirements.getTag().toString() + "\nComment Tag:\n" + et_comment.getTag().toString() +
"\nScore Tag:\n" + et_score.getTag().toString(), Toast.LENGTH_LONG).show();

ArrayList<receivedCertificationsDetails> staffDetails = arrSyncData.get(i).getPublicationDetail();

LayoutInflater layoutInflater2;

View subView;

for (int j = 0; j < staffDetails.size(); j++) {

Log.d("touch", "inner");
layoutInflater2 = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

subView = layoutInflater2.inflate(R.layout.yy_certifications_detailed_row, null);

TextView title = (TextView) subView.findViewById(R.id.tv_title);

TextView titleValue = (TextView) subView.findViewById(R.id.tv_title_value);

TextView categoryValue = (TextView) subView.findViewById(R.id.tv_category_value);

TextView issueValue = (TextView) subView.findViewById(R.id.tv_issue_date_value);

TextView expiryValue = (TextView) subView.findViewById(R.id.tv_expiry_date_value);

// if (catType.equalsIgnoreCase("Other")) {

// title.setVisibility(View.VISIBLE);

// titleValue.setVisibility(View.VISIBLE);

titleValue.setText(staffDetails.get(j).getOther_certification().toString());

// } else {

//

// title.setVisibility(View.GONE);

// titleValue.setVisibility(View.GONE);

// }

categoryValue.setText(staffDetails.get(j).getCategory().toString());

issueValue.setText(staffDetails.get(j).getIssuance_date().toString());

expiryValue.setText(staffDetails.get(j).getExpiry_date().toString());

staffDetailedContainer.addView(subView);

}
extra_staff_container.addView(addView);

hidepDialogFetch();

} catch (Exception e) {

e.printStackTrace();

Toast.makeText(this, "Exception: " + e.getCause(), Toast.LENGTH_LONG).show();

} finally {

db.close();

private void showpDialogFetch() {

if (!progressDialog.isShowing())

progressDialog.show();

private void hidepDialogFetch() {

if (progressDialog.isShowing())

progressDialog.dismiss();

public void getdata() {

json = new JSONObject();

edEmptyCheck = true;
for (int j = 0; j < extra_staff_container.getChildCount(); j++) {

LinearLayout surveyForm = (LinearLayout) extra_staff_container.getChildAt(j);

for (int i = 0; i < surveyForm.getChildCount(); i++) {

if (surveyForm.getChildAt(i) instanceof LinearLayout) {

LinearLayout r1 = (LinearLayout) surveyForm.getChildAt(i);

getAllLayouts(r1);

private void getAllLayouts(LinearLayout rl) {

for (int i = 0; i < rl.getChildCount(); i++) {

if (rl.getChildAt(i) instanceof TextInputLayout) {

TextInputLayout til = (TextInputLayout) rl.getChildAt(i);

EditText et = til.getEditText();

if (et.getVisibility() == VISIBLE) {

if (!(et.getId() == R.id.et_remarks)) {

if (helper.isEmpty(et)) {

edEmptyCheck = false;

til.setErrorEnabled(true);

til.setError("Please fill this field");

} else {

try {

json.put(et.getTag().toString(), et.getText().toString());

til.setErrorEnabled(false);

til.setError(null);

} catch (JSONException e) {
e.printStackTrace();

} else {

try {

if (helper.isEmpty(et)) {

json.put(et.getTag().toString(), "NA");

} else {

json.put(et.getTag().toString(), et.getText().toString());

} catch (JSONException e) {

e.printStackTrace();

} else {

try {

json.put(et.getTag().toString(), "NA");

} catch (JSONException e) {

e.printStackTrace();

if (rl.getChildAt(i) instanceof RadioGroup) {

RadioGroup chk = (RadioGroup) rl.getChildAt(i);

RadioButton rb = (RadioButton) findViewById(chk.getCheckedRadioButtonId());

// RadioButton rbb = (RadioButton) chk.getChildAt(0);

RadioButton rbb = (RadioButton) chk.getChildAt(chk.getChildCount() - 1);


if (chk.getVisibility() == VISIBLE) {

if (!(chk.getCheckedRadioButtonId() == -1)) {

try {

json.put(chk.getTag().toString(), rb.getText().toString());

} catch (Exception e) {

e.printStackTrace();

} else {

rbb.setError("Check Anyone");

radioOk = false;

} else {

try {

json.put(chk.getTag().toString(), "NA");

} catch (Exception e) {

e.printStackTrace();

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.save_report, menu);
return true;

@Override

public boolean onOptionsItemSelected(MenuItem item) {

if (item.getItemId() == R.id.save) {

functionSendDataFinal();

return super.onOptionsItemSelected(item);

@Override

public void onClick(View v) {

if (v.getId() == sendSave.getId()) {

functionSendDataFinal();

private void functionSendDataFinal() {

try {

radioOk = true;

getdata();

if (edEmptyCheck) {

if (radioOk) {

send();

} else {

helper.dialog(context, "Please check all radio buttons", "Alert!");

}
} else {

helper.dialog(context, "Please fill all empty fields", "Alert!");

} catch (Exception e) {

e.printStackTrace();

Snackbar.make(snackbarView, e.getMessage(), Snackbar.LENGTH_LONG).setAction("Action",


null).show();

@Override

public void onBackPressed() {

new AlertDialog.Builder(this)

.setTitle("Exit!")

.setCancelable(false)

.setMessage("Are you sure you want to leave?")

.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

LabCertificationsInfo.super.onBackPressed();

})

.setNegativeButton("No", null)

.show();

public void send() throws HttpHostConnectException {

try {
SeterGeter.setCertificationJson(json.toString());

Log.d("touch", json.toString());

if (!(json.toString().length() == 0)) {

boolean id = Querries.updateCertification(context, "0" ,pk_id);

if (id) {

InputMethodManager imm = (InputMethodManager)


getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(surveyForm.getWindowToken(), 0);

LabCertificationsInfo.super.onBackPressed();

} else {

helper.dialog(context, "DataBase connection problem, restart application.", "Alert!");

} else {

helper.dialog(context, "Your data cannot be saved, restart application.", "Alert!");

} catch (Exception e) {

Toast.makeText(context, "Error is" + e.getMessage(), Toast.LENGTH_LONG).show();

You might also like