net.kamhon.ieagle.function.user.service.impl.MenuFrameworkServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.function.user.service.impl.MenuFrameworkServiceImpl.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kamhon.ieagle.function.user.service.impl;

import java.util.List;

import net.kamhon.ieagle.application.Application;
import net.kamhon.ieagle.datagrid.DatagridModel;
import net.kamhon.ieagle.exception.DataException;
import net.kamhon.ieagle.exception.ValidatorException;
import net.kamhon.ieagle.function.user.dao.UserMenuFrameworkDao;
import net.kamhon.ieagle.function.user.service.MenuFrameworkService;
import net.kamhon.ieagle.function.user.vo.UserMenu;
import net.kamhon.ieagle.util.CollectionUtil;
import net.kamhon.ieagle.util.ReflectionUtil;
import net.kamhon.ieagle.util.VoUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(MenuFrameworkService.BEAN_NAME)
public class MenuFrameworkServiceImpl implements MenuFrameworkService {
    @Autowired
    private UserMenuFrameworkDao userMenuDao;

    public UserMenu getUserMenu(Long menuId) {
        return userMenuDao.get(menuId);
    }

    public void saveUserMenu(UserMenu userMenu) {
        if (this.getUserMenu(userMenu.getMenuId()) != null) {
            throw new ValidatorException("Menu Id " + userMenu.getMenuId() + " already exists!");
        }

        if (userMenu.getParentId() != null) {
            UserMenu parentUserMenu = this.getUserMenu(userMenu.getParentId());

            userMenu.setTreeLevel(parentUserMenu.getTreeLevel() + 1);
            this.setTreeConfig(userMenu, parentUserMenu);
        } else {
            userMenu.setTreeLevel(1);
            userMenu.setTreeConfig("");
        }
        userMenuDao.save(userMenu);
    }

    public void updateUserMenu(UserMenu userMenu, Long orginalMenuId, Long version) {
        Long newMenuId = null;

        boolean isMenuIdChanged = false;
        boolean isParentIdChanged = false;

        userMenu.setVersion(version);

        UserMenu userMenuDb = this.getUserMenu(orginalMenuId);
        VoUtil.checkVoBaseVersion(userMenu, userMenuDb);

        if (!orginalMenuId.equals(userMenu.getMenuId())) {
            isMenuIdChanged = true;
            newMenuId = userMenu.getMenuId();
        }
        // check on parentId
        if (userMenuDb.getParentId() != null) {
            if (!userMenuDb.getParentId().equals(userMenu.getParentId())) {
                isParentIdChanged = true;
            }
        } else if (userMenu.getParentId() != null) {
            isParentIdChanged = true;
        }

        // can not copy/update menuId because can not update hibernate object's primary key
        String[] properties = { "parentId", "menuName", "menuDesc", "menuUrl", "isAuthNeeded", "accessCode",
                "createMode", "readMode", "updateMode", "deleteMode", "adminMode", "treeLevel", "menuTarget",
                "status" };
        ReflectionUtil.copyPropertiesWithPropertiesList(userMenu, userMenuDb, properties);

        // if parent id changed, treeConfig need to re-generate again
        if (isParentIdChanged) {
            if (userMenuDb.getParentId() == null) {
                userMenuDb.setTreeConfig("");
            } else {
                UserMenu parentUserMenu = this.getUserMenu(userMenuDb.getParentId());
                this.setTreeConfig(userMenuDb, parentUserMenu);
            }
        }

        userMenuDao.update(userMenuDb);

        if (isParentIdChanged) {
            recursiveUpdateChildUserMenu(userMenuDb);
        }

        if (isMenuIdChanged) {
            updateMenuId(orginalMenuId, newMenuId);
        }
    }

    public void doRegenerateTree() {
        List<Integer> treeLevels = userMenuDao.findTreeLevels();
        for (Integer level : treeLevels) {
            List<UserMenu> userMenus = this.findUserMenuByTreeLevel(level);
            for (UserMenu userMenu : userMenus) {
                this.setTreeConfig(userMenu, userMenu.getParentMenu());
                userMenuDao.update(userMenu);
            }
        }
    }

    public static void main(String[] args) {
        MenuFrameworkService menuFrameworkManager = (MenuFrameworkService) Application
                .lookupBean(MenuFrameworkService.BEAN_NAME);
        menuFrameworkManager.doRegenerateTree();
    }

    private void updateMenuId(Long orginalMenuId, Long newMenuId) {
        userMenuDao.updateMenuParentIdOnly(orginalMenuId, newMenuId);
        userMenuDao.updateMenuId(orginalMenuId, newMenuId);

        UserMenu userMenu = getUserMenu(newMenuId);
        setTreeConfig(userMenu, userMenu.getParentMenu());

        recursiveUpdateChildUserMenu(userMenu);
    }

    private void recursiveUpdateChildUserMenu(UserMenu userMenu) {
        if (CollectionUtil.isNotEmpty(userMenu.getChildMenus())) {
            for (UserMenu childMenu : userMenu.getChildMenus()) {
                childMenu.setTreeLevel(userMenu.getTreeLevel() + 1);
                this.setTreeConfig(childMenu, userMenu);
                userMenuDao.update(childMenu);

                recursiveUpdateChildUserMenu(childMenu);
            }
        }
    }

    private void setTreeConfig(UserMenu userMenu, UserMenu parentMenu) {
        int level = userMenu.getTreeLevel();
        if (level > 1 && parentMenu == null) {
            throw new DataException("parentMenu can't be blank for level > 1");
        }

        if (level == 1) {
            userMenu.setTreeConfig("");
        } else if (level == 2) {
            userMenu.setTreeConfig("" + parentMenu.getMenuId());
        } else {
            userMenu.setTreeConfig(parentMenu.getTreeConfig() + "|" + parentMenu.getMenuId());
        }
    }

    public void findUserMenuForListing(DatagridModel<UserMenu> datagridModel, Long menuId, Long menuId2,
            String menuName, String menuName2, Long parentMenuId, Long parentMenuId2, String parentMenuName,
            String parentMenuName2, String accessCode, String accessCode2, Integer treeLevel, Integer treeLevel2,
            String status, String status2, Long sortSeq2, String menuTarget2, String menuUrl2, String menuDesc2) {
        userMenuDao.findUserMenuForListing(datagridModel, menuId, menuId2, menuName, menuName2, parentMenuId,
                parentMenuId2, parentMenuName, parentMenuName2, accessCode, accessCode2, treeLevel, treeLevel2,
                status, status2, sortSeq2, menuTarget2, menuUrl2, menuDesc2);

    }

    public List<UserMenu> findUserMenuByTreeLevel(Integer treeLevel) {
        return userMenuDao.findUserMenuByTreeLevel(treeLevel);
    }

    // ---------------- GETTER & SETTER (START) ----------------

    public void setUserMenuDao(UserMenuFrameworkDao userMenuDao) {
        this.userMenuDao = userMenuDao;
    }

    // ---------------- GETTER & SETTER (END) ----------------
}