⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcdaoimpl.java

📁 ACEGI数据库保存 ACEGI数据库保存 ACEGI数据库保存
💻 JAVA
字号:
/*
 * Copyright 2004-2005 wangz.
 * Project shufe_newsroom
 */
package com.skyon.um.security.acegi.intercept.web;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;

import com.skyon.framework.util.ResultSetProxy;
import com.skyon.um.security.acegi.IConst;

/**
 * @since 2005-8-4
 * @author 王政
 * @version $Id: JdbcDaoImpl.java,v 1.5 2005/11/01 15:34:04 wangzheng Exp $
 */
public class JdbcDaoImpl extends JdbcDaoSupport implements ResourceMappingProvider {
    
    //~ Static fields/initializers =============================================
    
    public static final String DEF_ALL_RESOURCES_QUERY = " select distinct t.resourceId, t.resourceName, t.parentName, t.action, t.expression from Resource t where t.action is not null and t.layer = '" + IConst.WEB_LAYER + "' order by title";    
       
    public static final String DEF_RECIPENTS_RESOURCE_MAPPING_QUERY = " select distinct t.roleName, t.resourceId from Permission t ";
    
    private static final Log logger = LogFactory.getLog(JdbcDaoImpl.class);
    
    //~ Instance fields ========================================================
    
    protected MappingSqlQuery allResources;
    
    protected MappingSqlQuery recipentsResourceMapping;
    
    private String allResourcesQuery; 
    
    private String recipentsResourceMappingQuery;
    
    
       
    //~ Constructors ===========================================================
    
    public JdbcDaoImpl() {
        this.allResourcesQuery = DEF_ALL_RESOURCES_QUERY;
        this.recipentsResourceMappingQuery = DEF_RECIPENTS_RESOURCE_MAPPING_QUERY;
    }
     
    //~ Methods ================================================================
    
    /**
     * @see ResourceMappingProvider#getResourceMappings()
     */
    public ResourceMapping[] getResourceMappings() {
        List resourceMappings = new LinkedList();
        
        List allResource = getAllResources().execute();
        
        List recipentsResourceMapping = getRecipentsResourceMapping().execute();
        
        Map resourceRecipentsMap = new LinkedHashMap();
        
        for (Iterator iter = recipentsResourceMapping.iterator(); iter.hasNext(); ) {
        	RecipentsResourceMappingHolder mappingHolder = (RecipentsResourceMappingHolder) iter.next();
        	List recipents = (List) resourceRecipentsMap.get(mappingHolder.getResourceId());
        	if (recipents == null) {
        		recipents = new LinkedList();
        	}
        	if (! recipents.contains(mappingHolder.getRecipent())) {
        		recipents.add(mappingHolder.getRecipent());
        	}
        	resourceRecipentsMap.put(mappingHolder.getResourceId(), recipents);
        }
        
        for (Iterator iter = allResource.iterator(); iter.hasNext(); ) {          
            ResourceHolder holder = (ResourceHolder) iter.next();            
            String resourcePath = holder.getResourcePath();
            boolean expression = holder.isExpression();            
            
            if (StringUtils.isBlank(resourcePath)) {
                continue;
            }
            
            // 如果是模糊匹配, 加上 "*" 标志
            resourcePath = expression ? resourcePath + "*" : resourcePath;
            
            // 如果 url 前没有 "/", 自动加上
            if (resourcePath.indexOf(RESOURCE_PATH_PREFIX) != 0) {
            	resourcePath = RESOURCE_PATH_PREFIX + resourcePath;
            }
                
            ResourceMapping mapping = new ResourceMapping();                       
            mapping.setResourcePath(resourcePath);
            
            String resourceId = holder.getResourceId();
            List recipentsByResource = (List) resourceRecipentsMap.get(resourceId);
            if (recipentsByResource == null) {
            	recipentsByResource = new LinkedList();
            }
            
            String[] recipents = (String[]) recipentsByResource.toArray(new String[0]);
            mapping.setRecipients(recipents);
            resourceMappings.add(mapping);
        }
        
        if (allResource.isEmpty()) {
            return new ResourceMapping[] {new ResourceMapping("/dontuseme", new String[] {"dontuseme"})};
        }
        
        return (ResourceMapping[]) resourceMappings.toArray(new ResourceMapping[0]);
    }
    

    /**
     * @see org.springframework.dao.support.DaoSupport#initDao()
     */
    protected void initDao() throws Exception {
        if (StringUtils.isBlank(getAllResourcesQuery())) {
        	logger.info("None custom allResouceQuery find,  Using DAFAULT allRecourceQuery sql. ");
        }        
        if (StringUtils.isBlank(getRecipentsResourceMappingQuery())) {
        	logger.info("None custem recipentsResourceMappingQuery find, Using Default recipentsResourceMappingQuery sql.");
        }
        initMappingSqlQueries();
    }
    
    /**
     * Extension point to allow other MappingSqlQuery objects to be substituted
     * in a subclass
     */
    protected void initMappingSqlQueries() {
        setAllResources(new AllResourcesMapping(getDataSource()));
        setRecipentsResourceMapping(new RecipentsResourceMapping(getDataSource()));
    }
    
    
    protected final class AllResourcesMapping extends MappingSqlQuery {

        public AllResourcesMapping(DataSource dataSource) {
            super(dataSource, getAllResourcesQuery());
            compile();
        }

        protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        	ResultSetProxy proxy = new ResultSetProxy(rs);
            ResourceHolder holder = new ResourceHolder();
            holder.setResourceId(proxy.getString(1));
            holder.setResourceName(proxy.getString(2));
            holder.setParentName(proxy.getString(3));
            holder.setResourcePath(StringUtils.trim(proxy.getString(4)));
            holder.setExpression(true);
            return holder;
        }
        
    }
        
    protected final class RecipentsResourceMapping extends MappingSqlQuery {

        public RecipentsResourceMapping(DataSource dataSource) {
            super(dataSource, getRecipentsResourceMappingQuery());
            compile();
        }
    	
		protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			ResultSetProxy proxy = new ResultSetProxy(rs);
            String recipent = proxy.getString(1);
            String resourceId = proxy.getString(2);
            return new RecipentsResourceMappingHolder(recipent, resourceId);
		}
    	
    }
    
    
    protected final class RecipentsResourceMappingHolder {
    	
    	private String recipent;
    	
    	private String resourceId;
    
    	public RecipentsResourceMappingHolder() {
    	}
    	
    	
		/**
		 * @param recipent
		 * @param resourceId
		 */
		public RecipentsResourceMappingHolder(String recipent, String resourceId) {
			this.recipent = recipent;
			this.resourceId = resourceId;
		}
		
		
		/**
		 * @return Returns the recipent.
		 */
		public String getRecipent() {
			return recipent;
		}
		/**
		 * @param recipent The recipent to set.
		 */
		public void setRecipent(String recipent) {
			this.recipent = recipent;
		}
		/**
		 * @return Returns the resourceId.
		 */
		public String getResourceId() {
			return resourceId;
		}
		/**
		 * @param resourceId The resourceId to set.
		 */
		public void setResourceId(String resourceId) {
			this.resourceId = resourceId;
		}
    }
    
    
    protected final class ResourceHolder {
        
        private String resourceId;
        
        private String resourceName;
        
        private String parentName;
        
        private String resourcePath;
        
        private boolean expression;
        
        
        public ResourceHolder() {            
        }

        /**
         * @param name
         * @param id
         * @param name2
         * @param path
         */
        public ResourceHolder(String resourceId, String resourceName, String parentName, String resourcePath, boolean expression) {
            this.resourceId = resourceId;
            this.resourceName = resourceName;
            this.parentName = parentName;
            this.resourcePath = resourcePath;
            this.expression = expression;
        }

        /**
         * @return Returns the parentName.
         */
        public String getParentName() {
            return parentName;
        }

        /**
         * @param parentName The parentName to set.
         */
        public void setParentName(String parentName) {
            this.parentName = parentName;
        }

        /**
         * @return Returns the resourceId.
         */
        public String getResourceId() {
            return resourceId;
        }

        /**
         * @param resourceId The resourceId to set.
         */
        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }

        /**
         * @return Returns the resourceName.
         */
        public String getResourceName() {
            return resourceName;
        }

        /**
         * @param resourceName The resourceName to set.
         */
        public void setResourceName(String resourceName) {
            this.resourceName = resourceName;
        }

        /**
         * @return Returns the resourcePath.
         */
        public String getResourcePath() {
            return resourcePath;
        }

        /**
         * @param resourcePath The resourcePath to set.
         */
        public void setResourcePath(String resourcePath) {
            this.resourcePath = resourcePath;
        }

		/**
		 * @return Returns the expression.
		 */
		public boolean isExpression() {
			return expression;
		}

		/**
		 * @param expression The expression to set.
		 */
		public void setExpression(boolean expression) {
			this.expression = expression;
		}
        
        
        
    }
    
    
    /**
     * @return Returns the allResourcesQuery.
     */
    public String getAllResourcesQuery() {
        return allResourcesQuery;
    }

    /**
     * @param allResourcesQuery The allResourcesQuery to set.
     */
    public void setAllResourcesQuery(String allResourcesQuery) {
        this.allResourcesQuery = allResourcesQuery;
    }

    /**
     * @return Returns the allResources.
     */
    public MappingSqlQuery getAllResources() {
        return allResources;
    }

    /**
     * @param allResources The allResources to set.
     */
    public void setAllResources(MappingSqlQuery allResources) {
        this.allResources = allResources;
    }

    /**
	 * @return Returns the recipentsResourceMappingQuery.
	 */
	public String getRecipentsResourceMappingQuery() {
		return recipentsResourceMappingQuery;
	}
	/**
	 * @param recipentsResourceMappingQuery The recipentsResourceMappingQuery to set.
	 */
	public void setRecipentsResourceMappingQuery(String recipentsResourceMappingQuery) {
		this.recipentsResourceMappingQuery = recipentsResourceMappingQuery;
	}
	/**
	 * @return Returns the recipentsResourceMapping.
	 */
	public MappingSqlQuery getRecipentsResourceMapping() {
		return recipentsResourceMapping;
	}
	/**
	 * @param recipentsResourceMapping The recipentsResourceMapping to set.
	 */
	public void setRecipentsResourceMapping(MappingSqlQuery recipentsResourceMapping) {
		this.recipentsResourceMapping = recipentsResourceMapping;
	}
}



⌨️ 快捷键说明

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