registryupdateaction.java

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

JAVA
1,199
字号
/*
 * 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;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.jetspeed.modules.actions.portlets.security.SecurityConstants;
import org.apache.jetspeed.om.BaseSecurityReference;
import org.apache.jetspeed.om.SecurityReference;
import org.apache.jetspeed.om.registry.CapabilityMap;
import org.apache.jetspeed.om.registry.ClientEntry;
import org.apache.jetspeed.om.registry.MediaTypeEntry;
import org.apache.jetspeed.om.registry.Parameter;
import org.apache.jetspeed.om.registry.PortletEntry;
import org.apache.jetspeed.om.registry.PortletInfoEntry;
import org.apache.jetspeed.om.registry.RegistryEntry;
import org.apache.jetspeed.om.registry.base.BaseCachedParameter;
import org.apache.jetspeed.om.registry.base.BaseParameter;
import org.apache.jetspeed.om.registry.base.BaseSecurity;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.Registry;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.util.template.JetspeedLink;
import org.apache.jetspeed.util.template.JetspeedLinkFactory;
import org.apache.turbine.util.DynamicURI;
import org.apache.turbine.util.RunData;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.security.EntityExistsException;
import org.apache.velocity.context.Context;

/**
 * An abstract base class with default actions for many of the common 
 * fields and parameters shared by the registry entries.  To add a new registry
 * update action, simply derive from this class and override the resetForm,
 * clearUserData, and updateRegistry functions.  If you need to provide more
 * actions that those that are provided, simply create them in your derived class.
 *
 * @author <a href="mailto:caius1440@hotmail.com">Jeremy Ford</a>
 * @version $Id: RegistryUpdateAction.java,v 1.10 2004/03/31 04:49:10 morciuch Exp $
 */
public abstract class RegistryUpdateAction extends SecureVelocityPortletAction
{
    protected String registryEntryName = "";
    protected String registry = "";
    protected String pane = "";

    private static final String REASON = "reason";

    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RegistryUpdateAction.class.getName());    
    
    protected void buildNormalContext(
        VelocityPortlet portlet,
        Context context,
        RunData rundata)
        throws Exception
    {
        String msgid =
            rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
        if (msgid != null)
        {
            int id = Integer.parseInt(msgid);
            if (id < SecurityConstants.MESSAGES.length)
                context.put(
                    SecurityConstants.PARAM_MSG,
                    SecurityConstants.MESSAGES[id]);
        }

        String mode =
            rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
        context.put(SecurityConstants.PARAM_MODE, mode);

        String reason = rundata.getParameters().getString(REASON);
        context.put(REASON, reason);
    }

    /**
     * Insert a registry entry into the registry
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     * @throws Exception
     */
    public void doInsert(RunData rundata, Context context) throws Exception
    {
        try
        {
            String entryName =
                rundata.getParameters().getString(registryEntryName);

            if (entryName == null || entryName.length() == 0)
            {
                DynamicURI duri =
                    redirect(
                        rundata,
                        SecurityConstants.PARAM_MODE_INSERT,
                        SecurityConstants.MID_INVALID_ENTITY_NAME);
                rundata.setRedirectURI(duri.toString());
                resetForm(rundata);
            }
            else
            {
                //check if profile to be created already exists
                RegistryEntry existingEntry =
                    Registry.getEntry(registry, entryName);
                if (existingEntry != null)
                {
                    throw new EntityExistsException(
                        "RegistryEntry: " + entryName + " Already Exists!");
                }

                RegistryEntry registryEntry = Registry.createEntry(registry);
                registryEntry.setName(entryName);

                updateRegistryEntry(rundata, registryEntry);

                Registry.addEntry(registry, registryEntry);

                clearUserData(rundata);
                
                rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry + ":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
            }
        }
        catch (EntityExistsException e)
        {
            //
            // dup key found - display error message - bring back to same screen
            //
            DynamicURI duri =
                redirect(
                    rundata,
                    SecurityConstants.PARAM_MODE_INSERT,
                    SecurityConstants.MID_ENTITY_ALREADY_EXISTS);
            rundata.setRedirectURI(duri.toString());
            resetForm(rundata);

            logger.error(
                this.getClass().getName()
                    + ": Trying to create duplicate entry");
        }
        catch (Exception e)
        {
            DynamicURI duri =
                redirect(
                    rundata,
                    SecurityConstants.PARAM_MODE_INSERT,
                    SecurityConstants.MID_UPDATE_FAILED);
            duri = duri.addQueryData(REASON, e.getMessage());
            rundata.setRedirectURI(duri.toString());
            resetForm(rundata);

            logger.error("Exception", e);
        }
    }

    /**
     * Update a registry entry
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     * @throws Exception
     */
    public void doUpdate(RunData rundata, Context context) throws Exception
    {
        try
        {
            String entryName =
                rundata.getParameters().getString(registryEntryName);
            RegistryEntry registryEntry =
                Registry.getEntry(registry, entryName);
            if (registryEntry != null)
            {
                updateRegistryEntry(rundata, registryEntry);

                Registry.addEntry(registry, registryEntry);
                clearUserData(rundata);
            }
            else
            {
                DynamicURI duri =
                    redirect(
                        rundata,
                        SecurityConstants.PARAM_MODE_UPDATE,
                        SecurityConstants.MID_INVALID_ENTITY_NAME);
                rundata.setRedirectURI(duri.toString());
                resetForm(rundata);

                logger.error(
                    this.getClass().getName()
                        + ": Failed to find registry entry for updating");
            }
        }
        catch (Exception e)
        {
            DynamicURI duri =
                redirect(
                    rundata,
                    SecurityConstants.PARAM_MODE_UPDATE,
                    SecurityConstants.MID_UPDATE_FAILED);
            duri = duri.addQueryData(REASON, e.getMessage());
            rundata.setRedirectURI(duri.toString());
            resetForm(rundata);

            logger.error("Exception", e);
        }
    }

    /**
     * Delete a registry entry
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     * @throws Exception
     */
    public void doDelete(RunData rundata, Context context) throws Exception
    {
        try
        {
            String entryName =
                rundata.getParameters().getString(registryEntryName);

            if (entryName == null || entryName.length() == 0)
            {
                DynamicURI duri =
                    redirect(
                        rundata,
                        SecurityConstants.PARAM_MODE_DELETE,
                        SecurityConstants.MID_INVALID_ENTITY_NAME);
                rundata.setRedirectURI(duri.toString());
                resetForm(rundata);

                logger.error(
                    this.getClass().getName()
                        + ": Failed to find registry entry for deleting");
            }
            else
            {
                Registry.removeEntry(registry, entryName);
                clearUserData(rundata);
                
                rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry + ":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
            }
        }
        catch (Exception e)
        {
            DynamicURI duri =
                redirect(
                    rundata,
                    SecurityConstants.PARAM_MODE_DELETE,
                    SecurityConstants.MID_DELETE_FAILED);
            duri = duri.addQueryData(REASON, e.getMessage());
            rundata.setRedirectURI(duri.toString());
            resetForm(rundata);

            logger.error("Exception", e);
        }
    }

    /**
     * Cleanup method
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     * @throws Exception
     */
    public void doCancel(RunData rundata, Context context) throws Exception
    {
        clearUserData(rundata);
    }

    /**
     * Basic implementation of a method to update a registry entry.  The fields that
     * are common to all registry entries can simply be added below.
     * @param rundata The turbine rundata context for this request.
     * @param registryEntry The registry entry to update
     */
    protected void updateRegistryEntry(
        RunData rundata,
        RegistryEntry registryEntry)
        throws Exception
    {
        String description = rundata.getParameters().getString("description");
        String title = rundata.getParameters().getString("title");
        Boolean hidden = rundata.getParameters().getBool("hidden");
        String securityRef = rundata.getParameters().getString("security_ref");

        SecurityReference security = registryEntry.getSecurityRef();
        String securityParent = null;
        if (security != null)
        {
            securityParent = security.getParent();
        }

        if (hasChanged(securityParent, securityRef))
        {
            if (security == null)
            {
                security = new BaseSecurityReference();
            }
            security.setParent(securityRef);
            registryEntry.setSecurityRef(security);
        }

        if (hasChanged(registryEntry.getDescription(), description))
        {
            registryEntry.setDescription(description);
        }
        if (hasChanged(String.valueOf(registryEntry.isHidden()),
            String.valueOf(hidden)))
        {
            registryEntry.setHidden(hidden.booleanValue());
        }
        if (hasChanged(registryEntry.getTitle(), title))
        {
            registryEntry.setTitle(title);
        }
    }

    /**
     * Determines whether a field has changed value.  Used in update methods.
     * 
     * @param oldValue The original value
     * @param newValue The new value
     */
    protected boolean hasChanged(String oldValue, String newValue)
    {
        boolean result = false;

        if (oldValue == null && newValue == null)
        {
            result = false;
        }
        else if (
            oldValue == null && (newValue != null && newValue.length() == 0))
        {
            result = false;
        }
        else if (oldValue == null && (newValue != null))
        {
            result = true;
        }
        else if (oldValue != null && newValue == null)
        {
            result = true;
        }
        else if (!oldValue.equals(newValue))
        {
            result = true;
        }

        return result;
    }

    /**
     * Add a parameter to a registry entry
     * @param rundata The turbine rundata context for this request.
     * @param context The velocity context for this request.
     * @throws Exception
     */
    public void doAddparameter(RunData rundata, Context context)
        throws Exception
    {
        try
        {
            String entryName =
                rundata.getParameters().getString(registryEntryName);
            PortletInfoEntry regEntry =
                (PortletInfoEntry) Registry.getEntry(registry, entryName);
            if (regEntry != null)
            {
                String parameterName =
                    rundata.getParameters().getString("parameter_name");
                if (parameterName != null && parameterName.length() > 0)
                {
                    Parameter parameter = null;
                    if (regEntry instanceof PortletEntry)
                    {
                        parameter = new BaseCachedParameter();

                        boolean isCachedOnName =
                            rundata.getParameters().getBoolean(
                                "cached_on_name",
                                false);
                        boolean isCachedOnValue =
                            rundata.getParameters().getBoolean(
                                "cached_on_value",

⌨️ 快捷键说明

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