baseservice.java

来自「一个很好的开源项目管理系统源代码」· Java 代码 · 共 130 行

JAVA
130
字号
package net.java.workeffort.service;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import net.java.workeffort.service.dao.IBaseDao;import net.java.workeffort.service.support.IllegalHierarchyException;import net.java.workeffort.service.support.InvalidHierarchyException;/** * Base service which all other services extend * @author Antony Joseph */public class BaseService {    // This is to avoid infinite recursion if for some reason we have    // circular parent/child relationship while validating hierarchies.    private static int MAX_DEPTH = 10;    /** The target database. Used as prefixes for namespaces in sqlmaps     * Set in setDao();     */    protected String dbPrefix;    /** the dao */    protected IBaseDao dao;    /**     * @return Returns the dao.     */    public IBaseDao getDao() {        return dao;    }    /**     * Sets also the variable 'dbPrefix' in this object.     * @param dao The dao to set. Set using spring IOC     */    public void setDao(IBaseDao dao) {        this.dao = dao;        if (dao.getDatabase() == null || dao.getDatabase().length() == 0)            throw new IllegalStateException(                    "The 'database' property not available");        this.dbPrefix = dao.getDatabase()+"-";    }    /**     * Returns a date object which is the midnight of the input date.     * @param date The input date     * @return date The midnight date.     */    protected Date getDateMidnight(Date date) {        if (date == null)            return null;        Calendar cal = Calendar.getInstance();        cal.setTime(date);        cal.add(Calendar.DAY_OF_YEAR, 1);        cal.add(Calendar.SECOND, -1);        return cal.getTime();    }    /**     * Validates a party code     * @param partyCd The party code     * @return whether the party code is valid or not     */    protected boolean isPartyValid(String partyCd) {        if (partyCd != null && partyCd.length() > 0) {            Map paramMap = new HashMap();            paramMap.put("partyCd", partyCd);            if (dao.queryForObject("Party.getParty", paramMap) == null)                return false;            else                return true;        }        else            return true;    }    protected boolean isFacilityValid(String facilityCd) {        if (facilityCd != null && facilityCd.length() > 0) {            Map paramMap = new HashMap();            paramMap.put("facilityCd", facilityCd);            if (dao.queryForObject("Facility.getFacility", paramMap) == null)                return false;            else                return true;        }        else            return true;    }    /**     * Validates the parent-child hierarchy. Goes up the hierarchy to make sure     * that the child entered is not a parent somewhere up in the hierarchical     * chain.     * @param obj The object id/code     * @param parent The parent object id/code     * @param level The level in the hierarchy     * @param statement The statement to get the parents     */    public void validateHierarchy(Object obj, Object parent, String statement,            int level) throws InvalidHierarchyException {        if (level > MAX_DEPTH)            throw new IllegalStateException("Level=" + level                    + " went beyond max depth " + MAX_DEPTH);        if (obj.equals(parent)) {            throw new IllegalHierarchyException(                    "Circular relationship found. object " + obj                            + " parent object " + parent + " at level=" + level);        }        // Make sure that obj is not a parent in the hierarchical chain        // creating a circular relationship        List list = dao.queryForList(statement, parent);        for (int i = 0; i < list.size(); i++) {            if (obj.equals(parent))                throw new IllegalHierarchyException(                        "Circular relationship found. object " + obj                                + " parent object " + parent + " at level="                                + level);            validateHierarchy(obj, list.get(i), statement, level + 1);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?