package org.opencps.usermgt.scheduler.utils;

import java.util.List;

import org.opencps.communication.exception.NoSuchNotificationQueueException;
import org.opencps.communication.model.NotificationQueue;
import org.opencps.communication.model.Notificationtemplate;
import org.opencps.communication.service.NotificationQueueLocalServiceUtil;
import org.opencps.communication.service.NotificationtemplateLocalServiceUtil;

import com.liferay.portal.kernel.dao.orm.QueryUtil;
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.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.StringUtil;

import backend.utils.SendMailUtils;

public class SchedulerUtilProcessing {

	public static void notificationByType(String[] notificationTypeList) {

		List<NotificationQueue> listQueue = NotificationQueueLocalServiceUtil.getNotificationQueues(QueryUtil.ALL_POS,
				QueryUtil.ALL_POS);

		ServiceContext serviceContext = new ServiceContext();

		for (NotificationQueue notificationQueue : listQueue) {
			
			serviceContext.setCompanyId(notificationQueue.getCompanyId());
			serviceContext.setUserId(notificationQueue.getUserId());
			serviceContext.setScopeGroupId(notificationQueue.getGroupId());
			
			long groupId = notificationQueue.getGroupId();

			String queueType = notificationQueue.getNotificationType();

			try {

				JSONObject payLoadData = JSONFactoryUtil.createJSONObject(notificationQueue.getPayload());

				for (String type : notificationTypeList) {

					if (queueType.equals(type)) {

						Notificationtemplate notificationtemplate = NotificationtemplateLocalServiceUtil
								.fetchByF_NotificationtemplateByType(groupId, type);

						JSONObject payLoad = JSONFactoryUtil.createJSONObject();

						payLoad.put("toName", notificationQueue.getToUsername());
						payLoad.put("toAddress", notificationQueue.getToEmail());
						payLoad.put("subject", notificationtemplate.getEmailSubject());
						payLoad.put("body",
								StringUtil.replace(notificationtemplate.getEmailBody(),
										new String[] { "[$USERNAME$]", "[$USEREMAIL$]", "[$PASSWORD$]", "[$PASSWORD_CODE$]", "[$USERSTATUS$]" },
										new String[] { payLoadData.getString("USERNAME"),
												payLoadData.getString("USEREMAIL"),
												payLoadData.getString("PASSWORD"),
												payLoadData.getString("PASSWORD_CODE"),
												payLoadData.getString("USERSTATUS") }));

						SendMailUtils.sendEmailNotification(payLoad, serviceContext);

						try {
							NotificationQueueLocalServiceUtil.deleteNotificationQueue(
									notificationQueue.getNotificationQueueId(), serviceContext);
						} catch (NoSuchNotificationQueueException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}

				}

			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}

	}

	private static Log _log = LogFactoryUtil.getLog(SchedulerUtilProcessing.class);
}

@@ -2448,25 +2448,22 @@
 String(%22
-USEREMAIL
+toName
 %22),%0A%09%09%09%09

replace toName = [$USERNAME$]