customizesetaction.java

来自「jetspeed源代码」· Java 代码 · 共 1,280 行 · 第 1/4 页

JAVA
1,280
字号
/*
 * Copyright 2000-2004 The Apache Software Foundation.
 * 
 * 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 org.apache.jetspeed.modules.actions.portlets;

// Jetspeed imports
import org.apache.jetspeed.portal.PortletSet;
import org.apache.jetspeed.portal.PortletSkin;
import org.apache.jetspeed.portal.PortletController;
import org.apache.jetspeed.portal.PortletSetController;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.Profiler;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.PortalToolkit;
import org.apache.jetspeed.services.TemplateLocator;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.jetspeed.om.BaseSecurityReference;
import org.apache.jetspeed.om.SecurityReference;
import org.apache.jetspeed.om.profile.Profile;
import org.apache.jetspeed.om.profile.ProfileLocator;
import org.apache.jetspeed.om.profile.ProfileException;
import org.apache.jetspeed.om.profile.QueryLocator;
import org.apache.jetspeed.om.registry.RegistryEntry;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.registry.PortletInfoEntry;
import org.apache.jetspeed.om.registry.base.BaseCategory;
import org.apache.jetspeed.om.profile.Portlets;
import org.apache.jetspeed.om.profile.psml.PsmlPortlets;
import org.apache.jetspeed.om.profile.Entry;
import org.apache.jetspeed.om.profile.psml.PsmlEntry;
import org.apache.jetspeed.om.profile.Reference;
import org.apache.jetspeed.om.profile.psml.PsmlReference;
import org.apache.jetspeed.om.profile.Skin;
import org.apache.jetspeed.om.profile.psml.PsmlSkin;
import org.apache.jetspeed.om.profile.MetaInfo;
import org.apache.jetspeed.om.profile.psml.PsmlMetaInfo;
import org.apache.jetspeed.om.profile.Controller;
import org.apache.jetspeed.om.profile.psml.PsmlController;
import org.apache.jetspeed.om.profile.Control;
import org.apache.jetspeed.om.profile.psml.PsmlControl;
import org.apache.jetspeed.om.profile.PSMLDocument;
import org.apache.jetspeed.om.security.JetspeedUser;
import org.apache.jetspeed.services.customlocalization.CustomLocalization;
import org.apache.jetspeed.services.idgenerator.JetspeedIdGenerator;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.security.PortalResource;
import org.apache.jetspeed.util.AutoProfile;
import org.apache.jetspeed.util.PortletSessionState;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.jetspeed.services.statemanager.SessionState;
import org.apache.jetspeed.services.resources.JetspeedResources;

// Turbine stuff
import org.apache.turbine.util.DynamicURI;
import org.apache.turbine.util.RunData;


// Velocity Stuff
import org.apache.velocity.context.Context;

// Java imports
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.Collections;
import java.util.Comparator;
import java.text.MessageFormat;

/**
 * This action implements the default portletset behavior customizer 
 * 
 * <p>Don't call it from the URL, the Portlet and the Action are automatically
 * associated through the registry PortletName
 * 
 * @author <a href="mailto:raphael@apache.org">Rapha雔 Luta</a>
 * @version $Id: CustomizeSetAction.java,v 1.51 2004/02/23 02:56:58 jford Exp $
 */
public class CustomizeSetAction extends VelocityPortletAction
{

    private static final String USER_SELECTIONS = "session.portlets.user.selections";
    private static final String UI_PORTLETS_SELECTED = "portletsSelected";
    private static final String PORTLET_LIST = "session.portlets.list";
    private static final String ALL_PORTLET_LIST = "session.all.portlets.list";
    private static final String PORTLET_LIST_PAGE_SIZE = "session.portlets.page.size";
    private static final String HIDE_EMPTY_CATEGORIES = "customizer.hide.empty.categories";
    
    public static final String FILTER_FIELDS = "filter_fields";
    public static final String FILTER_VALUES = "filter_values";

    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(CustomizeSetAction.class.getName());    
    
    /** 
     * Subclasses must override this method to provide default behavior 
     * for the portlet action
     */
    protected void buildNormalContext(VelocityPortlet portlet, 
                                      Context context,
                                      RunData rundata) throws Exception
    {
        JetspeedRunData jdata = (JetspeedRunData) rundata;
        SessionState customizationState = jdata.getPageSessionState();
        Profile profile = jdata.getCustomizedProfile();
        String mediaType = profile.getMediaType ();

        // set velocity variable of mediatype (displayed in the customizer menu)
        context.put("mtype", profile.getMediaType());

        // make the list of already used panes/portlets available through the 'runs' reference
        context.put("runs", AutoProfile.getPortletList(rundata));
        
        // we should first retrieve the portlet to customize
        PortletSet set = (PortletSet) (jdata).getCustomized();

        //identify the portlet submode and build the appropriate subt-template path
        String mode = rundata.getParameters().getString("mode");
        if (mode == null)
        {
          mode = (String) customizationState.getAttribute("customize-mode");
          if ((mode == null) || (mode.equalsIgnoreCase("addset")) || (mode.equalsIgnoreCase("general")))
          {
            mode = "layout";
          }
          
        }
        else
        {
          if ((mediaType.equalsIgnoreCase("wml")) && (!mode.equalsIgnoreCase("add")))
          {
            mode = "layout";
          }

          customizationState.setAttribute("customize-mode", mode);
        }

        String template = (String) context.get("template");

        if (template != null)
        {
            int idx = template.lastIndexOf(".");
            
            if (idx > 0)
            {
                template = template.substring(0, idx);
            }
            
            StringBuffer buffer = new StringBuffer(template);
            buffer.append("-").append(mode).append(".vm");
            
            template = TemplateLocator.locatePortletTemplate(rundata, buffer.toString());
            context.put("feature", template);

        }
    
        if (set == null)
        {
            return;
        }

        // get the customization state for this page
        String customizedPaneName = (String) customizationState.getAttribute("customize-paneName");
        if (customizedPaneName == null) 
        {
            customizedPaneName = "*";
        }

        // generic context stuff
        context.put("panename", customizedPaneName);
        context.put("skin", set.getPortletConfig().getPortletSkin());
        context.put("set", set);
        context.put("action", "portlets.CustomizeSetAction");        
        context.put("controllers", buildInfoList(rundata, Registry.PORTLET_CONTROLLER, mediaType));
        //context.put("skins", buildList(rundata, Registry.SKIN));
        //context.put("securitys", buildList(rundata, Registry.SECURITY));
        context.put("customizer", portlet);
    
        String controllerName = set.getController().getConfig().getName();
        context.put("currentController", controllerName);

        context.put("currentSecurityRef", set.getPortletConfig().getSecurityRef());

       /** 
        * Special handling for wml profiles
        * no skins, no properties menuentry, no panes 
        * --------------------------------------------------------------------------
        * last modified: 12/10/01
        * Andreas Kempf, Siemens ICM S CP OP, Munich
        * mailto: A.Kempf@web.de
        */

        if (mediaType.equalsIgnoreCase("wml"))
        {
          context.put("currentSkin", "Not for wml!");
          context.put("allowproperties", "false");
        }
        else
        {
          if (set.getPortletConfig().getSkin() != null)
          {
            context.put("currentSkin", set.getPortletConfig().getPortletSkin().getName());
          }
          context.put("allowproperties", "true");
        }


        context.put("allowpane", "false");
        
        // do not allow panes for wml profiles
        if ((!mediaType.equalsIgnoreCase("wml")) && (set.getController() instanceof PortletSetController))
        {
          if (customizedPaneName != null)
          {
            context.put("allowpane", "true");
          }
        }
        else
        {
            context.put("allowportlet", "true");
        }
        // --------------------------------------------------------------------------
            


        if ("add".equals(mode)) // build context for add mode
        {
            int start = rundata.getParameters().getInt("start", -1);
            if (start < 0)
            {
                //System.out.println("Clearing session variables");
                start = 0;
                PortletSessionState.clearAttribute(rundata, USER_SELECTIONS);
                PortletSessionState.clearAttribute(rundata, PORTLET_LIST);
            }
            
            ArrayList allPortlets = new ArrayList();
            List portlets  = buildPortletList(rundata, set, mediaType, allPortlets);
            Map userSelections = getUserSelections(rundata); 
            // Build a list of categories from the available portlets
            List categories = buildCategoryList(rundata, mediaType, allPortlets);
            context.put("categories", categories);
            
            context.put("parents", PortletFilter.buildParentList(allPortlets));
            addFiltersToContext(rundata, context);
            
            int size = getSize(portlet);               
            int end = Math.min(start + size, portlets.size());
                
            if (start > 0)
            {
                context.put("prev", String.valueOf(Math.max(start - size, 0)));
            }
                
            if (start + size < portlets.size())
            {
                context.put("next", String.valueOf(start + size));
            }
                
            context.put("browser", portlets.subList(start, end));
            context.put("size", new Integer(size));
            context.put(UI_PORTLETS_SELECTED, userSelections);
            
			context.put("portlets", portlets);
        }
        else if ("addref".equals(mode))
        {
            Iterator psmlIterator = null;
            psmlIterator = Profiler.query(new QueryLocator(QueryLocator.QUERY_ALL));
            
            // Set Start and End
            int start = rundata.getParameters().getInt("start", 0);                
            int size = getSize(portlet);
 

            // Only include entries in compatibale with the Media-type/Country/Language
            List psmlList = new LinkedList();
            Profile refProfile = null;
            int profileCounter = 0;
            while (psmlIterator.hasNext())
            {
                refProfile = (Profile) psmlIterator.next();
                
                if (refProfile.getMediaType() != null)
                {
                    if (profile.getMediaType().equals(refProfile.getMediaType()) == false)
                    {
                        continue;
                    }
                }
                
                if (profile.getLanguage() != null)
                {
                    if (refProfile.getLanguage() != null)
                    {
                        if (profile.getLanguage().equals(refProfile.getLanguage()) == true)
                        {
                            if (profile.getCountry() != null)
                            {

⌨️ 快捷键说明

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