<commit_before>/**
 * OpenCPS is the open source Core Public Services software
 * Copyright (C) 2016-present OpenCPS community
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */

package org.opencps.backend.util;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.opencps.accountmgt.model.Business;
import org.opencps.accountmgt.model.Citizen;
import org.opencps.datamgt.service.DictItemLocalServiceUtil;
import org.opencps.dossiermgt.model.Dossier;
import org.opencps.dossiermgt.model.DossierFile;
import org.opencps.dossiermgt.model.FileGroup;
import org.opencps.dossiermgt.service.DossierFileLocalServiceUtil;
import org.opencps.dossiermgt.service.DossierLocalServiceUtil;
import org.opencps.dossiermgt.service.FileGroupLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;

/**
 * @author khoavd
 *
 */
public class AutoFillFormData {
	public static String dataBinding(String sampleData,Citizen ownerCitizen, Business ownerBusiness, long dossierId) {
		String result = StringPool.BLANK;
		String _subjectName = StringPool.BLANK;
	 	String _subjectId = StringPool.BLANK;
	 	String _address = StringPool.BLANK;
	 	String _cityCode = StringPool.BLANK;
	 	String _cityName = StringPool.BLANK;
	 	String _districtCode = StringPool.BLANK;
	 	String _districtName = StringPool.BLANK;
	 	String _wardCode = StringPool.BLANK;
	 	String _wardName = StringPool.BLANK;
	 	String _contactName = StringPool.BLANK;
	 	String _contactTelNo = StringPool.BLANK;
	 	String _contactEmail = StringPool.BLANK;
	 	if(Validator.isNotNull(ownerCitizen)){
	 		_subjectName = ownerCitizen.getFullName();
	 		_subjectId = String.valueOf(ownerCitizen.getCitizenId());
	 		_address = ownerCitizen.getAddress();
	 		_cityCode = ownerCitizen.getCityCode();
	 		try {
	 			_cityName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getCityCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_districtCode = ownerCitizen.getDistrictCode();
	 		try {
	 			_districtName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getDistrictCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_wardCode = ownerCitizen.getWardCode();
	 		try {
	 			_wardName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getWardCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_contactName = ownerCitizen.getFullName();
	 		_contactTelNo = ownerCitizen.getTelNo();
	 		_contactEmail = ownerCitizen.getEmail();
	 	}
		if(Validator.isNotNull(ownerBusiness)){
			_subjectName = ownerBusiness.getName();
	 		_subjectId = String.valueOf(ownerBusiness.getBusinessId());
	 		_address = ownerBusiness.getAddress();
	 		_cityCode = ownerBusiness.getCityCode();
	 		try {
	 			_cityName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getCityCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_districtCode = ownerBusiness.getDistrictCode();
	 		try {
	 			_districtName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getDistrictCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_wardCode = ownerBusiness.getWardCode();
	 		try {
	 			_wardName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getWardCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_contactName = ownerBusiness.getShortName();
	 		_contactTelNo = ownerBusiness.getTelNo();
	 		_contactEmail = ownerBusiness.getEmail();
	 	}
		
		try {
			if(Validator.isNull(sampleData)){
				sampleData = "{}";
			}
			JSONObject jsonSampleData = JSONFactoryUtil.createJSONObject(sampleData);
			Map<String, Object> jsonMap = jsonToMap(jsonSampleData);
			for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
//				System.out.println(entry.getKey() + ": " + entry.getValue());
				String value = String.valueOf(entry.getValue());
				if(value.startsWith("_")){
					if(value.equals("_subjectName")){
						jsonMap.put(entry.getKey(), _subjectName);
					}else if(value.equals("_subjectId")){
						jsonMap.put(entry.getKey(), _subjectId);
					}else if(value.equals("_address")){
						jsonMap.put(entry.getKey(), _address);
					}else if(value.equals("_cityCode")){
						jsonMap.put(entry.getKey(), _cityCode);
					}else if(value.equals("_cityName")){
						jsonMap.put(entry.getKey(), _cityName);
					}else if(value.equals("_districtCode")){
						jsonMap.put(entry.getKey(), _districtCode);
					}else if(value.equals("_districtName")){
						jsonMap.put(entry.getKey(), _districtName);
					}else if(value.equals("_wardCode")){
						jsonMap.put(entry.getKey(), _wardCode);
					}else if(value.equals("_wardName")){
						jsonMap.put(entry.getKey(), _wardName);
					}else if(value.equals("_contactName")){
						jsonMap.put(entry.getKey(), _contactName);
					}else if(value.equals("_contactTelNo")){
						jsonMap.put(entry.getKey(), _contactTelNo);
					}else if(value.equals("_contactEmail")){
						jsonMap.put(entry.getKey(), _contactEmail);
					}else if(value.equals("_ngayNopDon")){
						jsonMap.put(entry.getKey(), ngayNopDon());
					}else if(value.equals("_donViThucHien")){
						if(dossierId > 0){
							try {
								Dossier dossier = DossierLocalServiceUtil.fetchDossier(dossierId);
								jsonMap.put(entry.getKey(), dossier.getGovAgencyName());
							} catch (SystemException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}else if(value.equals("_thanhPhanHoSoCon")){
						if(dossierId > 0){
							try {
								List<FileGroup> fileGroups = FileGroupLocalServiceUtil.getFileGroupByDossierId(dossierId);
								JSONArray arrays = JSONFactoryUtil.createJSONArray();
								JSONObject obJsonObject = null;
								int i = 0;
								for (FileGroup fileGroup : fileGroups) {
									i++;
									obJsonObject = JSONFactoryUtil.createJSONObject();
									obJsonObject.put("stt", i);
									obJsonObject.put("tenSanPham", fileGroup.getDisplayName());
									arrays.put(obJsonObject);
								}
								jsonMap.put(entry.getKey(), arrays);
							} catch (SystemException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					
				}else if(value.startsWith("#") && value.contains("@")){
					String newString = value.substring(1);
					String[] stringSplit = newString.split("@");
					String variable = stringSplit[0];
					String paper = stringSplit[1];
					try {
						DossierFile dossierFile = DossierFileLocalServiceUtil.fetchByTemplateFileNoDossierId_First(dossierId, paper);
						if(Validator.isNotNull(dossierFile) && Validator.isNotNull(dossierFile.getFormData())){
							JSONObject jsonOtherData = JSONFactoryUtil.createJSONObject(dossierFile.getFormData());
							Map<String, Object> jsonOtherMap = jsonToMap(jsonOtherData);
							String myCHK = StringPool.BLANK;
								if(variable.contains(":")){
									String[] variableMuti = variable.split(":");
									for (String string : variableMuti) {
										myCHK += ", " + jsonOtherMap.get(string).toString();
									}
									myCHK = myCHK.replaceFirst(", ", "");
								}else{
									myCHK = jsonOtherMap.get(variable.replaceAll("_ok", "").replaceAll("_fail", "")).toString();
								}
							
							if(myCHK.startsWith("[{")){
								JSONArray orignJsonArray = (JSONArray)jsonOtherMap.get(variable.replaceAll("_ok", "").replaceAll("_fail", ""));
								if(!variable.contains("_ok") && !variable.contains("_fail")){
									jsonMap.put(entry.getKey(), orignJsonArray);
								}else{
									JSONArray orignJsonArrayOk = JSONFactoryUtil.createJSONArray();
									JSONArray orignJsonArrayFail = JSONFactoryUtil.createJSONArray();
									for (int i = 0; i < orignJsonArray.length(); i++) {
										JSONObject childObj = orignJsonArray.getJSONObject(i);
										String ketQuaTD = StringPool.BLANK;
										try {
											ketQuaTD = childObj.getString("ketQua");
										} catch (Exception e) {
											// TODO: handle exception
										}
										
										childObj.put("stt", i+1);
										if(ketQuaTD.equalsIgnoreCase("0")){
											orignJsonArrayFail.put(childObj);
										}else{
											orignJsonArrayOk.put(childObj);
										}
									}
									if(variable.contains("_ok")){
										jsonMap.put(entry.getKey(), orignJsonArrayOk);
									}else if(variable.contains("_fail")){
										jsonMap.put(entry.getKey(), orignJsonArrayFail);
									}
								}
								
							}else{
								jsonMap.put(entry.getKey(), myCHK.toString());
							}
						}
					} catch (SystemException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			jsonSampleData = JSONFactoryUtil.createJSONObject();
			for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
				jsonSampleData.put(entry.getKey(), entry.getValue()+"");
			}<commit_msg>edit autofill bilding data<commit_after>/**
 * OpenCPS is the open source Core Public Services software
 * Copyright (C) 2016-present OpenCPS community
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */

package org.opencps.backend.util;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.opencps.accountmgt.model.Business;
import org.opencps.accountmgt.model.Citizen;
import org.opencps.datamgt.service.DictItemLocalServiceUtil;
import org.opencps.dossiermgt.model.Dossier;
import org.opencps.dossiermgt.model.DossierFile;
import org.opencps.dossiermgt.model.FileGroup;
import org.opencps.dossiermgt.service.DossierFileLocalServiceUtil;
import org.opencps.dossiermgt.service.DossierLocalServiceUtil;
import org.opencps.dossiermgt.service.FileGroupLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;

/**
 * @author khoavd
 *
 */
public class AutoFillFormData {
	public static String dataBinding(String sampleData,Citizen ownerCitizen, Business ownerBusiness, long dossierId) {
		String result = StringPool.BLANK;
		String _subjectName = StringPool.BLANK;
	 	String _subjectId = StringPool.BLANK;
	 	String _address = StringPool.BLANK;
	 	String _cityCode = StringPool.BLANK;
	 	String _cityName = StringPool.BLANK;
	 	String _districtCode = StringPool.BLANK;
	 	String _districtName = StringPool.BLANK;
	 	String _wardCode = StringPool.BLANK;
	 	String _wardName = StringPool.BLANK;
	 	String _contactName = StringPool.BLANK;
	 	String _contactTelNo = StringPool.BLANK;
	 	String _contactEmail = StringPool.BLANK;
	 	if(Validator.isNotNull(ownerCitizen)){
	 		_subjectName = ownerCitizen.getFullName();
	 		_subjectId = String.valueOf(ownerCitizen.getCitizenId());
	 		_address = ownerCitizen.getAddress();
	 		_cityCode = ownerCitizen.getCityCode();
	 		try {
	 			_cityName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getCityCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_districtCode = ownerCitizen.getDistrictCode();
	 		try {
	 			_districtName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getDistrictCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_wardCode = ownerCitizen.getWardCode();
	 		try {
	 			_wardName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerCitizen.getWardCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_contactName = ownerCitizen.getFullName();
	 		_contactTelNo = ownerCitizen.getTelNo();
	 		_contactEmail = ownerCitizen.getEmail();
	 	}
		if(Validator.isNotNull(ownerBusiness)){
			_subjectName = ownerBusiness.getName();
	 		_subjectId = String.valueOf(ownerBusiness.getBusinessId());
	 		_address = ownerBusiness.getAddress();
	 		_cityCode = ownerBusiness.getCityCode();
	 		try {
	 			_cityName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getCityCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_districtCode = ownerBusiness.getDistrictCode();
	 		try {
	 			_districtName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getDistrictCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_wardCode = ownerBusiness.getWardCode();
	 		try {
	 			_wardName = DictItemLocalServiceUtil.getDictItemInuseByItemCode(1, ownerBusiness.getWardCode()).getItemName(Locale.getDefault());
			} catch (Exception e) {
				// TODO: handle exception
			}
	 		
	 		_contactName = ownerBusiness.getShortName();
	 		_contactTelNo = ownerBusiness.getTelNo();
	 		_contactEmail = ownerBusiness.getEmail();
	 	}
		
		try {
			if(Validator.isNull(sampleData)){
				sampleData = "{}";
			}
			JSONObject jsonSampleData = JSONFactoryUtil.createJSONObject(sampleData);
			Map<String, Object> jsonMap = jsonToMap(jsonSampleData);
			for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
//				System.out.println(entry.getKey() + ": " + entry.getValue());
				String value = String.valueOf(entry.getValue());
				if(value.startsWith("_")){
					if(value.equals("_subjectName")){
						jsonMap.put(entry.getKey(), _subjectName);
					}else if(value.equals("_subjectId")){
						jsonMap.put(entry.getKey(), _subjectId);
					}else if(value.equals("_address")){
						jsonMap.put(entry.getKey(), _address);
					}else if(value.equals("_cityCode")){
						jsonMap.put(entry.getKey(), _cityCode);
					}else if(value.equals("_cityName")){
						jsonMap.put(entry.getKey(), _cityName);
					}else if(value.equals("_districtCode")){
						jsonMap.put(entry.getKey(), _districtCode);
					}else if(value.equals("_districtName")){
						jsonMap.put(entry.getKey(), _districtName);
					}else if(value.equals("_wardCode")){
						jsonMap.put(entry.getKey(), _wardCode);
					}else if(value.equals("_wardName")){
						jsonMap.put(entry.getKey(), _wardName);
					}else if(value.equals("_contactName")){
						jsonMap.put(entry.getKey(), _contactName);
					}else if(value.equals("_contactTelNo")){
						jsonMap.put(entry.getKey(), _contactTelNo);
					}else if(value.equals("_contactEmail")){
						jsonMap.put(entry.getKey(), _contactEmail);
					}else if(value.equals("_ngayNopDon")){
						jsonMap.put(entry.getKey(), ngayNopDon());
					}else if(value.equals("_donViThucHien")){
						if(dossierId > 0){
							try {
								Dossier dossier = DossierLocalServiceUtil.fetchDossier(dossierId);
								jsonMap.put(entry.getKey(), dossier.getGovAgencyName());
							} catch (SystemException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}else if(value.equals("_thanhPhanHoSoCon")){
						if(dossierId > 0){
							try {
								List<FileGroup> fileGroups = FileGroupLocalServiceUtil.getFileGroupByDossierId(dossierId);
								JSONArray arrays = JSONFactoryUtil.createJSONArray();
								JSONObject obJsonObject = null;
								int i = 0;
								for (FileGroup fileGroup : fileGroups) {
									i++;
									obJsonObject = JSONFactoryUtil.createJSONObject();
									obJsonObject.put("stt", i);
									obJsonObject.put("tenSanPham", fileGroup.getDisplayName());
									arrays.put(obJsonObject);
								}
								jsonMap.put(entry.getKey(), arrays);
							} catch (SystemException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					
				}else if(value.startsWith("#") && value.contains("@")){
					String newString = value.substring(1);
					String[] stringSplit = newString.split("@");
					String variable = stringSplit[0];
					String paper = stringSplit[1];
					try {
						DossierFile dossierFile = DossierFileLocalServiceUtil.fetchByTemplateFileNoDossierId_First(dossierId, paper);
						if(Validator.isNotNull(dossierFile) && Validator.isNotNull(dossierFile.getFormData())){
							JSONObject jsonOtherData = JSONFactoryUtil.createJSONObject(dossierFile.getFormData());
							Map<String, Object> jsonOtherMap = jsonToMap(jsonOtherData);
							String myCHK = StringPool.BLANK;
								try {
									if(variable.contains(":")){
										String[] variableMuti = variable.split(":");
										for (String string : variableMuti) {
											myCHK += ", " + jsonOtherMap.get(string).toString();
										}
										myCHK = myCHK.replaceFirst(", ", "");
									}else{
										myCHK = jsonOtherMap.get(variable.replaceAll("_ok", "").replaceAll("_fail", "")).toString();
									}
								} catch (Exception e) {
									// TODO: handle exception
								}
								
							
							if(myCHK.startsWith("[{")){
								JSONArray orignJsonArray = (JSONArray)jsonOtherMap.get(variable.replaceAll("_ok", "").replaceAll("_fail", ""));
								if(!variable.contains("_ok") && !variable.contains("_fail")){
									jsonMap.put(entry.getKey(), orignJsonArray);
								}else{
									JSONArray orignJsonArrayOk = JSONFactoryUtil.createJSONArray();
									JSONArray orignJsonArrayFail = JSONFactoryUtil.createJSONArray();
									for (int i = 0; i < orignJsonArray.length(); i++) {
										JSONObject childObj = orignJsonArray.getJSONObject(i);
										String ketQuaTD = StringPool.BLANK;
										try {
											ketQuaTD = childObj.getString("ketQua");
										} catch (Exception e) {
											// TODO: handle exception
										}
										
										childObj.put("stt", i+1);
										if(ketQuaTD.equalsIgnoreCase("0")){
											orignJsonArrayFail.put(childObj);
										}else{
											orignJsonArrayOk.put(childObj);
										}
									}
									if(variable.contains("_ok")){
										jsonMap.put(entry.getKey(), orignJsonArrayOk);
									}else if(variable.contains("_fail")){
										jsonMap.put(entry.getKey(), orignJsonArrayFail);
									}
								}
								
							}else if(myCHK.startsWith("#")){
								jsonMap.put(entry.getKey(), "");
							}else{
								jsonMap.put(entry.getKey(), myCHK.toString());
							}
						}
					} catch (SystemException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
			jsonSampleData = JSONFactoryUtil.createJSONObject();
			for (Map.Entry<String, Object> entry : jsonMap.entrySet()) {
				jsonSampleData.put(entry.getKey(), entry.getValue()+"");
			}
{'fraction_non_alphanumeric': 0.12459694884258145, 'fraction_numerical': 0.0019250204533423168, 'mean_word_length': 3.4755545983200515, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6748436', 'n_tokens_mistral': 8385, 'n_tokens_neox': 6397, 'n_words': 1147}