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

📄 selectug_engine.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
package org.jahia.engines.users;import java.security.acl.Group;import java.security.Principal;import java.util.*;                                 // HashMapimport javax.servlet.http.*;                        // HttpSessionimport org.jahia.exceptions.*;                  // JahiaExceptionimport org.jahia.exceptions.JahiaSessionExpirationException;import org.jahia.utils.*;                       // JahiaConsole, GroupRoleUtilsimport org.jahia.data.*;                        // JahiaDataimport org.jahia.services.pages.JahiaPage;import org.jahia.data.fields.*;                 // JahiaField, FieldTypesimport org.jahia.data.containers.*;             // JahiaContainerimport org.jahia.params.*;                      // ParamBeanimport org.jahia.registries.*;                  // ServicesRegistryimport org.jahia.engines.*;                     // JahiaEngine interfaceimport org.jahia.engines.shared.*;              // SmallText, BigText, etc.import org.jahia.data.events.*;                 // JahiaEventimport org.jahia.engines.rights.*;              // ViewRightsimport org.jahia.services.acl.*;                // JahiaBaseACLimport org.jahia.services.usermanager.*;        // JahiaUser, JahiaGroup/** * <p>Title: An engine to do user selection.</p> * <p>Description: This engine is sort of like a "File Open" window under * Windows, where you can select multiple users. This should be generic * enough to be used both in ACL management and in the administration component * for example for group management.</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Jahia Ltd</p> * @author Serge Huber * @version 3.0 */public class SelectUG_Engine implements JahiaEngine {    private static  final String TEMPLATE_JSP     = "selectUG";    private static  final String CLOSE_JSP        = "selectusers_close";    private static SelectUG_Engine theObject   = null;    private String engineName = "selectUG";    private EngineToolBox toolBox;    /**     * Private constructor for singleton pattern     */    private SelectUG_Engine()    {        toolBox = EngineToolBox.getInstance();    } // end constructor    /**     * Returns the singleton instance     * @return SelectUG_Engine the singleton object instance.     */    public static synchronized SelectUG_Engine getInstance()    {        if (theObject == null) {            theObject = new SelectUG_Engine();        }        return theObject;    } // end getInstance    /**     * Check if we have the rights to view this engine     * @param jParams ParamBean object     * @return boolean if we are allowed to render this engine, false     * otherwise     */    public boolean authoriseRender( ParamBean jParams )    {        return toolBox.authoriseRender( jParams );    } // end authoriseRender    /**     * Renders a link to this engine.     * @param jParams ParamBean object to be used to generate URL.     * @param theObj     * @return     * @throws JahiaException     */    public String renderLink( ParamBean jParams, Object theObj )    throws JahiaException    {        String rightParams = (String) theObj;        String params = "";        params += "?mode=display&screen=searchGroups";        params += rightParams;        return jParams.composeEngineUrl( engineName, params );    } // end renderLink    /***        * needsJahiaData        *        */    public boolean needsJahiaData( ParamBean jParams )    {        return false;    } // end needsJahiaData    /***        * handles the engine actions        *        * @param        jParams             a ParamBean object        * @param        jData               a JahiaData object (not mandatory)        *        */    public void handleActions( ParamBean jParams, JahiaData jData )        throws  JahiaException,                JahiaSessionExpirationException    {        // initalizes the hashmap        HashMap engineMap = initEngineMap( jParams );        processLastScreen( jParams, engineMap );        processCurrentScreen( jParams, engineMap );        // displays the screen        toolBox.displayScreen( jParams, engineMap );    } // end handleActions    /***        * processes the last screen sent by the user        *        * @param        jParams             a ParamBean object        *        */    public void processLastScreen( ParamBean jParams, HashMap engineMap )    throws JahiaException    {        // gets engineMap values        String  theScreen   = (String) engineMap.get( "screen" );        if (theScreen == null) {            throw new JahiaException( "SelectUG_Engine.processLastScreen",                                      "Error in parameters",                                      JahiaException.PARAMETER_ERROR,                                      JahiaException.CRITICAL );        }        if ("searchUsers".equals(theScreen)) {            JahiaUserManagerService uMgr = ServicesRegistry.getInstance().getJahiaUserManagerService();            String searchIn = jParams.getRequest().getParameter("searchIn");            if (searchIn == null) {                searchIn = "allProps";            }            String searchString = jParams.getRequest().getParameter("searchString");            if (searchString == null) {                searchString = "*";            }            if ("".equals(searchString)) {                searchString = "*";            }            Properties searchParameters = new Properties();            if ("allProps".equals(searchIn)) {                searchParameters.setProperty("*", searchString);            } else if ("properties".equals(searchIn)) {                String[] searchInProps = jParams.getRequest().getParameterValues("properties");                if (searchInProps == null) {                    searchParameters.setProperty("*", searchString);                } else {                    for (int i = 0; i < searchInProps.length; i++) {                        searchParameters.setProperty(searchInProps[i], searchString);                    }                }            } else {                // invalid search in value !                throw new JahiaException( "SelectUG_Engine.processLastScreen",                                          "searchIn parameter has incorrect value of [" + searchIn +"]",                                          JahiaException.PARAMETER_ERROR,                                          JahiaException.CRITICAL );            }            Set searchResults = null;            String storedOn = jParams.getRequest().getParameter("storedOn");            if ("everywhere".equals(storedOn)) {                searchResults = uMgr.searchUsers(jParams.getSiteID(), searchParameters);            } else {                String[] providers = jParams.getRequest().getParameterValues("providers");                if (providers == null) {                    searchResults = uMgr.searchUsers(jParams.getSiteID(), searchParameters);                } else {                    for (int i = 0; i < providers.length; i++) {                        String curServer = providers[i];                        Set curSearchResults = uMgr.searchUsers(curServer, jParams.getSiteID(), searchParameters);                        if (curSearchResults != null) {                            if (searchResults == null) {                                searchResults = new HashSet();                            }                            searchResults.addAll(curSearchResults);                        }                    }                }            }            if (searchResults != null) {                ArrayList resultList = new ArrayList();                Iterator resultListEnum = searchResults.iterator();                while (resultListEnum.hasNext()) {                    JahiaUser user = (JahiaUser) resultListEnum.next();                    String provider = JahiaString.adjustStringSize(user.getProviderName(), 6) + " ";                    String usrname = JahiaString.adjustStringSize(user.getUsername(), 10);                    // Find a displayable user property                    String properties = "";                    String firstname = user.getProperty("firstname");                    String lastname = user.getProperty("lastname");                    if (firstname != null) {                        properties += firstname;                        if (firstname.length() < 20) properties += " ";                    }                    if (lastname != null) properties += lastname;                    if ("".equals(properties)) {                        String email = user.getProperty("email");                        if (email != null) properties += email;                    }                    properties = JahiaString.adjustStringSize(properties, 20);                    String resultValue = "10000000r-- " + usrname + "u" + user.getName();                    String result = " " + provider + usrname + " " + properties;                    resultList.add(JahiaTools.replacePattern(resultValue, " ", "&nbsp;"));                    resultList.add(JahiaTools.replacePattern(result, " ", "&nbsp;"));                }                engineMap.put("resultList", resultList);                engineMap.put("selectUGEngine", "selectUsers");            }            engineMap.put("screen", "edit");        } else if (theScreen.equals("searchGroups")) {            ArrayList resultList = new ArrayList();            JahiaGroupManagerService gMgr = ServicesRegistry.getInstance().getJahiaGroupManagerService();            Vector groupList = gMgr.getGroupList(jParams.getSiteID());            Enumeration groupListEnum = groupList.elements();            while (groupListEnum.hasMoreElements()) {                String groupKey = (String)groupListEnum.nextElement();                if (groupKey.indexOf("administrators") != -1) { // FIXME : no admin group has to be displayed                    continue;                }                JahiaGroup group = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(groupKey);                String provider = JahiaString.adjustStringSize(group.getProviderName(), 6) + " ";                // Construct a displayable groupname                String grpname = JahiaString.adjustStringSize(group.getGroupname(), 10);                // Find some group members for properties                Enumeration grpMembers = group.members();                String members = "(";                while (grpMembers.hasMoreElements()) {                    Object obj = (Object)grpMembers.nextElement();                    if (obj instanceof JahiaUser) {                        JahiaUser tmpUser = (JahiaUser)obj;                        members += tmpUser.getUsername();                    } else {                        JahiaGroup tmpGroup = (JahiaGroup)obj;                        members += tmpGroup.getGroupname();                    }                    if (members.length() > 20) break;                    if (grpMembers.hasMoreElements()) members += ", ";                }                members += ")";                members = JahiaString.adjustStringSize(members, 20);                String resultValue = "10000000r-- " + grpname + "g" + group.getName();                String result = " " + provider + grpname + " " + members;                resultList.add(JahiaTools.replacePattern(resultValue, " ", "&nbsp;"));                resultList.add(JahiaTools.replacePattern(result, " ", "&nbsp;"));            }            engineMap.put("resultList", resultList);            engineMap.put("selectUGEngine", "selectGroups");        } else if (theScreen.equals("save")) {            // Another way to paste selection to the ACL entries is used            // let's do storage stuff here... Most notably insert into engine            // map the result of the user selection.            /*String[] selectedUsers = jParams.getRequest().getParameterValues("selectedUsers");            Vector userKeyList = new Vector();            for (int i=0; i < selectedUsers.length; i++) {                userKeyList.add(selectedUsers[i]);            }            jParams.getSession().setAttribute("org.jahia.engines.selectusers.selectedUsers", userKeyList);*/        }    } // end processLastScreen    /***        * prepares the screen requested by the user        *        * @param        jParams             a ParamBean object        *        */    public void processCurrentScreen( ParamBean jParams, HashMap engineMap )        throws JahiaException    {        String  theScreen   = (String) engineMap.get( "screen" );        JahiaUserManagerService uMgr = ServicesRegistry.getInstance().getJahiaUserManagerService();        Vector providerList = uMgr.getProviderList();        engineMap.put("providerList", providerList);        jParams.getRequest().setAttribute( "jahia_session_engineMap", engineMap );    } // end processCurrentScreen    /***        * inits the engine map        *        * @param        jParams             a ParamBean object        *                                   (with request and response)        * @return       a HashMap object containing all the basic values        *               needed by an engine        *        */    private HashMap initEngineMap( ParamBean jParams )        throws  JahiaException,                JahiaSessionExpirationException    {        String theScreen = jParams.getRequest().getParameter( "screen" );        // gets session values        //HttpSession theSession = jParams.getRequest().getSession (true);        HttpSession theSession = jParams.getSession();        HashMap engineMap = (HashMap) theSession.getAttribute( "jahia_session_engineMap" );        if (engineMap == null) {            theScreen = "edit";            // init engine map            engineMap = new HashMap();        }        engineMap.put( "jParams", jParams );        engineMap.put( "renderType", new Integer(JahiaEngine.RENDERTYPE_FORWARD) );        engineMap.put( "engineName", engineName );        engineMap.put( "engineUrl", jParams.composeEngineUrl( engineName )  );        engineMap.put("selectUGEngine", "selectGroups");        theSession.setAttribute( "jahia_session_engineMap", engineMap );        if (theScreen == null) {            theScreen = "edit";        }        // sets screen        engineMap.put( "screen", theScreen );        if (theScreen.equals("cancel")) {            engineMap.put( "jspSource", CLOSE_JSP );        } else if (theScreen.equals("save")) {            engineMap.put( "jspSource", CLOSE_JSP );        } else {            engineMap.put( "jspSource", TEMPLATE_JSP );        }        // sets engineMap for JSPs        jParams.getRequest().setAttribute( "engineTitle", "Select Users" );        jParams.getRequest().setAttribute( "org.jahia.engines.EngineHashMap", engineMap );        return engineMap;    } // end initEngineMap}

⌨️ 快捷键说明

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