package org.opencps.dossiermgt.action.util;

import org.opencps.dossiermgt.model.Dossier;
import org.opencps.dossiermgt.model.DossierFile;
import org.opencps.dossiermgt.model.PaymentFile;

import com.liferay.document.library.kernel.model.DLFileVersion;
import com.liferay.document.library.kernel.service.DLAppLocalServiceUtil;
import com.liferay.document.library.kernel.service.DLFileVersionLocalServiceUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.repository.model.FileEntry;

public class DossierLogUtils {
	public static String createPayload(DossierFile dossierFile, PaymentFile paymentFile, Dossier dossier) {
		String fileType = "";
		String fileUrl = "";
		if (dossierFile != null){
			if (dossierFile.getDossierFileId() > 0) {
				try {
					FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(dossierFile.getFileEntryId());
					DLFileVersion dlFileVersion = DLFileVersionLocalServiceUtil
							.getLatestFileVersion(fileEntry.getFileEntryId(), true);

					fileType = dlFileVersion.getExtension();
					fileUrl = dlFileVersion.getTreePath();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		JSONObject obj = JSONFactoryUtil.createJSONObject();
		JSONArray arr = JSONFactoryUtil.createJSONArray();

		if (dossierFile != null) {
			JSONObject item = JSONFactoryUtil.createJSONObject();
			obj.put("jobposTitle", "");
			obj.put("briefNote", "");

			item.put("referenceUid", dossierFile.getReferenceUid());
			item.put("fileType", fileType);
			item.put("fileUrl", fileUrl);
			item.put("displayName", dossierFile.getDisplayName());
			item.put("status", "");
			arr.put(item);

			obj.put("dossierFiles", arr);
		}

		if (paymentFile != null) {
			JSONObject item = JSONFactoryUtil.createJSONObject();
			obj.put("jobposTitle", "");
			obj.put("briefNote", "");

			item.put("referenceUid", paymentFile.getReferenceUid());
			item.put("fileType", fileType);
			item.put("fileUrl", fileUrl);
			item.put("displayName", "");
			item.put("paymentRequestDate", paymentFile.getCreateDate());
			item.put("paymentStatus", paymentFile.getPaymentStatus());
			arr.put(item);

			obj.put("paymentFile", arr);
		}

		if (dossier != null) {
			JSONObject item = JSONFactoryUtil.createJSONObject();
			obj.put("jobposTitle", "");
			obj.put("briefNote", "");

			item.put("referenceUid", dossier.getReferenceUid());
			item.put("displayName", "");
			arr.put(item);

			obj.put("dossier", arr);
		}
		return obj.toString();
	}

}

@@ -616,16 +616,115 @@
 leEntry;
+%0Aimport com.liferay.portal.kernel.util.StringPool;%0Aimport com.liferay.portal.kernel.util.Validator;
 %0A%0Apublic
@@ -873,18 +873,32 @@
 eType = 
-%22%22
+StringPool.BLANK
 ;%0A%09%09Stri
@@ -914,17 +914,52 @@
 l = 
-%22%22;%0A%09%09if 
+StringPool.BLANK;%0A%0A%09%09if (Validator.isNotNull
 (dos
@@ -966,25 +966,19 @@
 sierFile
- != null)
+)) 
 %7B%0A%09%09%09if 
@@ -1413,11 +1413,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%09JS
@@ -1520,24 +1520,44 @@
 ay();%0A%0A%09%09if 
+(Validator.isNotNull
 (dossierFile
@@ -1552,32 +1552,25 @@
 (dossierFile
- != null
+)
 ) %7B%0A%09%09%09JSONO
@@ -1954,16 +1954,36 @@
 %7D%0A%0A%09%09if 
+(Validator.isNotNull
 (payment
@@ -1986,24 +1986,17 @@
 mentFile
- != null
+)
 ) %7B%0A%09%09%09J
@@ -2461,23 +2461,36 @@
 if (
-dossier != null
+Validator.isNotNull(dossier)
 ) %7B%0A

Fix DossierLogUtils