package org.opencps.dossiermgt.action.impl;

import java.util.List;

import javax.naming.AuthenticationException;

import org.opencps.dossiermgt.action.MenuConfigActions;
import org.opencps.dossiermgt.model.MenuConfig;
import org.opencps.dossiermgt.service.MenuConfigLocalServiceUtil;
import org.opencps.dossiermgt.service.MenuRoleLocalServiceUtil;
import org.opencps.usermgt.model.JobPos;
import org.opencps.usermgt.service.JobPosLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
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.StringPool;

import backend.auth.api.BackendAuthImpl;

public class MenuConfigActionsImpl implements MenuConfigActions {

	Log _log = LogFactoryUtil.getLog(MenuConfigActionsImpl.class);

	@Override
	public MenuConfig addMenuConfig(long userId, long groupId, String menuGroup, String menuName, Integer order,
			Integer menuType, String queryParams, String tableConfig, String buttonConfig, ServiceContext serviceContext)
			throws PortalException, AuthenticationException {

		BackendAuthImpl authImpl = new BackendAuthImpl();

		if (authImpl.hasResource(serviceContext, StringPool.BLANK, StringPool.BLANK)) {
			MenuConfig object = null;

			object = MenuConfigLocalServiceUtil.addMenuConfig(userId, groupId, menuGroup, menuName, order, menuType,
					queryParams, tableConfig, buttonConfig);

			return object;
		} else {
			throw new AuthenticationException();
		}

	}

	@Override
	public MenuConfig updateMenuConfig(long actionCodePK, long userId, long groupId, String menuGroup, String menuName,
			Integer order, Integer menuType, String queryParams, String tableConfig, String buttonConfig,
			ServiceContext serviceContext) throws PortalException, AuthenticationException {

		BackendAuthImpl authImpl = new BackendAuthImpl();

		if (authImpl.hasResource(serviceContext, StringPool.BLANK, StringPool.BLANK)) {
			MenuConfig object = null;

			object = MenuConfigLocalServiceUtil.updateMenuConfig(actionCodePK, userId, groupId, menuGroup, menuName,
					order, menuType, queryParams, tableConfig, buttonConfig);

			return object;
		} else {
			throw new AuthenticationException();
		}

	}

	@Override
	public void deleteMenuConfig(Long menuConfigId, ServiceContext serviceContext)
			throws PortalException, AuthenticationException, AuthenticationException {

		BackendAuthImpl authImpl = new BackendAuthImpl();

		if (authImpl.hasResource(serviceContext, StringPool.BLANK, StringPool.BLANK)) {
			MenuConfigLocalServiceUtil.removeMenuConfig(menuConfigId);
		} else {
			throw new AuthenticationException();
		}

	}

	@Override
	public long updateMenuConfigDB(long userId, long groupId, String menuGroup, String menuName, Integer order,
			Integer menuType, String queryParams, String tableConfig, String buttonConfig) {

		MenuConfig menuConfig = MenuConfigLocalServiceUtil.updateMenuConfigDB(userId, groupId, menuGroup, menuName, order,
				menuType, queryParams, tableConfig, buttonConfig);
		if (menuConfig != null) {
			return menuConfig.getMenuConfigId();
		}
		return 0;
	}

	//LamTV_ Process all list MenuConfig
	@Override
	public boolean deleteAllMenuConfig(long groupId, long userId, ServiceContext serviceContext) {
		boolean flag = false;
		List<MenuConfig> menuList = MenuConfigLocalServiceUtil.getByGroupId(groupId);
		if (menuList != null && menuList.size() > 0) {
			for (MenuConfig menuConfig : menuList) {
				MenuConfigLocalServiceUtil.deleteMenuConfig(menuConfig);
				flag = true;
			}
		} else {
			flag = true;
		}
		return flag;
	}

	@Override
	public void updateMenuRoles(long groupId, long menuConfigId, String roles) {

		JobPos job = JobPosLocalServiceUtil.getByJobCode(groupId, roles);
		if (job != null) {
			long roleId = job.getMappingRoleId();
			if (roleId > 0) {
				MenuRoleLocalServiceUtil.updateMenuRoleDB(menuConfigId, roleId);
			}
		}
	}

}

@@ -704,16 +704,115 @@
 ingPool;
+%0Aimport com.liferay.portal.kernel.util.StringUtil;%0Aimport com.liferay.portal.kernel.util.Validator;
 %0A%0Aimport
@@ -3847,16 +3847,133 @@
 les) %7B%0A%0A
+%09%09if (Validator.isNotNull(roles)) %7B%0A%09%09%09String%5B%5D roleArr = StringUtil.split(roles);%0A%09%09%09for (String role: roleArr) %7B%0A%09%09
 %09%09JobPos
@@ -4028,20 +4028,21 @@
 Id, role
-s
 );%0A
+%09%09
 %09%09if (jo
@@ -4054,16 +4054,18 @@
 null) %7B%0A
+%09%09
 %09%09%09long 
@@ -4100,16 +4100,18 @@
 d();%0A%09%09%09
+%09%09
 if (role
@@ -4116,24 +4116,26 @@
 leId %3E 0) %7B%0A
+%09%09
 %09%09%09%09MenuRole
@@ -4191,16 +4191,29 @@
 oleId);%0A
+%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

update menu config