customizeaction.java

来自「jetspeed源代码」· Java 代码 · 共 489 行 · 第 1/2 页

JAVA
489
字号
/*
 * Copyright 2000-2001,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 Stuff
import org.apache.jetspeed.modules.actions.portlets.CustomizeSetAction;
import org.apache.jetspeed.portal.Portlet;
import org.apache.jetspeed.portal.PortletConfig;
import org.apache.jetspeed.portal.PortletException;
import org.apache.jetspeed.portal.PortletSkin;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.persistence.PersistenceManager;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.rundata.JetspeedRunData;
import org.apache.jetspeed.om.BaseSecurityReference;
import org.apache.jetspeed.om.profile.Entry;
import org.apache.jetspeed.om.profile.Profile;
import org.apache.jetspeed.om.profile.Skin;
import org.apache.jetspeed.om.profile.psml.PsmlSkin;
import org.apache.jetspeed.om.SecurityReference;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.registry.Parameter;
import org.apache.jetspeed.om.registry.base.BaseParameter;
import org.apache.jetspeed.om.security.JetspeedUser;
import org.apache.jetspeed.services.JetspeedSecurity;
import org.apache.jetspeed.services.PortalToolkit;
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.portal.PortletInstance;
import org.apache.jetspeed.util.MetaData;
import org.apache.jetspeed.services.statemanager.SessionState;

// Turbine stuff
import org.apache.turbine.util.RunData;
import org.apache.turbine.modules.ActionLoader;

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

import java.util.Vector;
import java.util.Iterator;

/**
 * This action implements the default portlet 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>
 */
public class CustomizeAction extends VelocityPortletAction
{

    public static final String PARAM_NAMESPACE = "_param_";

    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(CustomizeAction.class.getName());    
    
    /**
     * Subclasses must override this method to provide default behavior
     * for the portlet action
     *
     * <table>
     * <tr><th>Context      </th><th> Description</th></tr>
     * <!-- ---------  ------------------------- -->
     * <tr><td>action       </td><td> Action to use</td></tr>
     * <tr><td>current_skin </td><td> Current skin for this portlet INSTANCE</td></tr>
     * <tr><td>params       </td><td> List of configurable parameters from the REGISTRY entry.</td></tr>
     * <tr><td>portlet      </td><td> Portlet, not the Portlet Instance!</td></tr>
     * <tr><td>skins        </td><td> List of skins</td></tr>
     * <tr><td>security     </td><td> List of security ref</td></tr>
     * <tr><td>security_ref </td><td> Current securityRef for this portlet INSTANCE</td></tr>
     * </table>
     */
    protected void buildNormalContext( VelocityPortlet portlet,
                                       Context context,
                                       RunData rundata )
    {

        // generic context stuff
        context.put("skins", CustomizeSetAction.buildList(rundata, Registry.SKIN));
        context.put("securitys", CustomizeSetAction.buildList(rundata, Registry.SECURITY));

        // we should first retrieve the portlet to customize
        Portlet p = ((JetspeedRunData)rundata).getCustomized();

        context.put("action", "portlets.CustomizeAction");

        PortletInstance instance = PersistenceManager.getInstance(p, rundata);
        context.put("portlet_instance", PersistenceManager.getInstance(p, rundata));

        if (p==null) return;

        // retrieve the portlet parameters
        PortletEntry entry = (PortletEntry)Registry.getEntry(Registry.PORTLET,p.getName());
        // save the entry in the session
        Vector params = new Vector();
        Iterator i = entry.getParameterNames();

        //System.out.println("==========================================");
        while(i.hasNext())
        {
            String name = (String)i.next();
            Parameter param = entry.getParameter(name);

            // filter some "system" and hidden parameters
            if (  (!param.isHidden()) && (name.charAt(0)!='_') )
            {
                // check the user role
                if (JetspeedSecurity.checkPermission((JetspeedUser)rundata.getUser(), new PortalResource( entry, param), JetspeedSecurity.PERMISSION_CUSTOMIZE))
                {
                    // Implementation of clone() is missing so we have do it "by hand"
                    Parameter clone = new BaseParameter();
                    clone.setName(param.getName());
                    clone.setTitle(param.getTitle());
                    clone.setDescription(param.getDescription());
                    clone.setType(param.getType());
                    if (instance.getAttribute(name, null) != null)
                    {
                        clone.setValue(instance.getAttribute(name));
                        //System.out.println("Adding value from instance [" + name + "] = [" + clone.getValue() + "]");
                    }
                    else if (p.getPortletConfig().getInitParameter(name) != null)
                    {
                        clone.setValue(p.getPortletConfig().getInitParameter(name));
                        //System.out.println("Adding value from init [" + name + "] = [" + clone.getValue() + "]");
                    }
                    else
                    {
                        clone.setValue(param.getValue());
                        //System.out.println("Adding value from registry [" + name + "] = [" + clone.getValue() + "]");
                    }
                    params.add(clone);
                }
            }
        }

        // get the customization state for this page
        SessionState customizationState = ((JetspeedRunData)rundata).getPageSessionState();
        customizationState.setAttribute("customize-parameters", params);

        // populate the customizer context
        context.put("parameters", params);
        context.put("portlet", p);
        context.put("customizer", portlet);

        if (p.getPortletConfig().getSecurityRef() != null)
          context.put("security_ref", p.getPortletConfig().getSecurityRef().getParent());
        if (p.getPortletConfig().getSkin() != null)
            context.put("current_skin", p.getPortletConfig().getPortletSkin().getName());

        Profile profile = ((JetspeedRunData)rundata).getCustomizedProfile();
        String currentTitle = profile.getDocument().getEntryById(p.getID()).getTitle();
        if (currentTitle == null && p.getPortletConfig().getMetainfo() != null)
        {
            currentTitle = p.getPortletConfig().getMetainfo().getTitle();
        }
        context.put("current_title", currentTitle);

    }

    /** Clean up the customization state */
    public void doCancel(RunData rundata, Context context)
    {
        ((JetspeedRunData)rundata).setCustomized(null);
        if (((JetspeedRunData)rundata).getCustomized()==null)
        {
            try
            {
                ActionLoader.getInstance().exec( rundata, "controls.EndCustomize" );
            }
            catch (Exception e)
            {
                logger.error("Unable to load action controls.EndCustomize ",e);
            }
        }
    }

    /**
     * Resets the portlet settings to default
     * 
     * @param rundata
     * @param context
     */
    public void doDefault(RunData rundata, Context context)
    {                    
        // we should first retrieve the portlet to customize and its parameters
        // definition
        Portlet p = ((JetspeedRunData) rundata).getCustomized();

        // Update paramaters
        try
        {
            PortletInstance instance = PersistenceManager.getInstance(p, rundata);

            instance.removeAllAttributes();

            try
            {
                ((JetspeedRunData) rundata).getCustomizedProfile().store();
            }
            catch (Exception e)
            {
                logger.error("Unable to save profile ",e);
            }

            //FIXME: this hack is due to the corrupted lifecycle of the portlet in the
            //current API when caching is activated
            try
            {
                org.apache.jetspeed.util.PortletSessionState.setPortletConfigChanged(p, rundata);
                p.init();
            }
            catch (PortletException e)
            {
                logger.error("Customizer failed to reinitialize the portlet "+p.getName(), e);
            }

            // we're done, make sure clean up the
            // session
            doCancel(rundata, context);
        }
        catch (Exception e)
        {
            logger.error("Exception", e);
        }
    }

⌨️ 快捷键说明

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