Package org.apache.cocoon.portal.impl

Source Code of org.apache.cocoon.portal.impl.PageLabelLinkService

/*
* Copyright 1999-2002,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.cocoon.portal.impl;

import java.util.Iterator;
import java.util.List;

import org.apache.cocoon.portal.event.Event;
import org.apache.cocoon.portal.event.impl.ChangeAspectDataEvent;
import org.apache.cocoon.portal.layout.CompositeLayout;
import org.apache.cocoon.portal.layout.Item;
import org.apache.cocoon.portal.layout.NamedItem;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.excalibur.source.SourceUtil;

/**
* The PageLabelLinkService generates links for named items defined in the layout portal.xml.
* Links for other entities are passed to the DefaultLinkService to be resolved.
*
* @author Ralph Goers
*
* @version CVS $Id: $
*/
public class PageLabelLinkService extends DefaultLinkService {

    protected String apectName;

    /** The label manager */
    protected PageLabelManager labelManager;

    /* (non-Javadoc)
    * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
    */
    public void service(ServiceManager manager) throws ServiceException {
        super.service(manager);
        this.labelManager = (PageLabelManager)this.manager.lookup(PageLabelManager.ROLE);
    }

    /**
     * Get the uri for the coplet containing event
     *
     * @param event The event to find
     * @return A URI
     */
    public String getLinkURI(Event event) {
        if (event == null) {
            return this.getRefreshLinkURI();
        }
        if (this.labelManager == null) {
            super.getLinkURI(event);
        }

        String requestParameterName = this.labelManager.getRequestParameterName();

        if (event instanceof ChangeAspectDataEvent &&
            ((ChangeAspectDataEvent) event).getTarget() instanceof CompositeLayout) {

            ChangeAspectDataEvent e = (ChangeAspectDataEvent)event;
            CompositeLayout layout = (CompositeLayout)e.getTarget();
            int i = ((Integer)e.getData()).intValue();

            Item item = layout.getItem(i);
            if (item instanceof NamedItem) {
                StringBuffer key = new StringBuffer("");
                getKey(item, key);

                if (this.labelManager.getPageLabelEvents(key.toString()) != null) {
                    final Info info = this.getInfo();
                    boolean hasParams = info.hasParameters;
                    final StringBuffer buffer = new StringBuffer(info.linkBase.toString());
                    if (hasParams) {
                        buffer.append('&');
                    }
                    else {
                        buffer.append('?');
                    }
                    buffer.append(requestParameterName).append('=')
                        .append(SourceUtil.encode(key.toString()));
                    return buffer.toString();
                }
            }
        }

        String label = this.labelManager.getCurrentLabel();

        return getLink(super.getLinkURI(event), requestParameterName, label);
    }

    /**
     * Get the uri for this coplet containing the additional events.
     *
     * @param events The events that will be processed by the generated uri.
     * @return A URI
     */
    public String getLinkURI(List events)
    {
        if (events == null || events.size() == 0) {
            return this.getRefreshLinkURI();
        }
        if (this.labelManager == null) {
            super.getLinkURI(events);
        }

        String requestParameterName = this.labelManager.getRequestParameterName();
        final Info info = this.getInfo();
        final StringBuffer buffer = new StringBuffer(info.linkBase.toString());
        boolean hasParams = info.hasParameters;
        Iterator iter = events.iterator();
        StringBuffer value = new StringBuffer("");

        while (iter.hasNext())
        {
            Event event = (Event)iter.next();

            if (event instanceof ChangeAspectDataEvent &&
                ((ChangeAspectDataEvent) event).getTarget() instanceof CompositeLayout) {

                ChangeAspectDataEvent e = (ChangeAspectDataEvent) event;
                CompositeLayout layout = (CompositeLayout) e.getTarget();
                int i = ((Integer) e.getData()).intValue();

                Item item = layout.getItem(i);
                if (value.length() > 0) {
                    value.append('.');
                }
                if (item instanceof NamedItem) {
                    value.append(((NamedItem)item).getName());
                }
                else {
                    value.append(Integer.toString(i));
                }
            }
            else {
                String label = this.labelManager.getCurrentLabel();

                return getLink(super.getLinkURI(events), requestParameterName, label);
            }
        }

        if (value.length() > 0 && this.labelManager.getPageLabelEvents(value.toString()) != null) {
            if (hasParams) {
                buffer.append('&');
            }
            else {
                buffer.append('?');
            }
            buffer.append(requestParameterName).append('=')
                .append(SourceUtil.encode(value.toString()));

            return buffer.toString();
        }

        String label = this.labelManager.getCurrentLabel();

        return getLink(super.getLinkURI(events), requestParameterName, label);
    }

    /* (non-Javadoc)
    * @see org.apache.avalon.framework.activity.Disposable#dispose()
    */
    public void dispose() {
        if (this.manager != null) {
            if (this.labelManager != null) {
                this.manager.release(this.labelManager);
                this.labelManager = null;
            }
        }
        super.dispose();
    }

    /*
     * Generates the page label.
     * @param item An Item.
     * @param key The StringBuffer in which to create the page label.
     */
    private void getKey(Item item, StringBuffer key) {
        CompositeLayout parentLayout = item.getParent();
        Item parentItem = parentLayout.getParent();

        if (parentItem != null) {
            getKey(parentItem, key);
        }

        if (key.length() > 0) {
            key.append('.');
        }
        if (item instanceof NamedItem) {
            key.append(((NamedItem) item).getName());
        }
        else {
            key.append(parentLayout.getItems().indexOf(item));
        }
    }

    /*
     * Append the page label to the link.
     * @param link The link to add the label to.
     * @param parmName The request parameter name.
     * @param label The page label.
     * @return The modified link.
     */
    private String getLink(String link, String parmName, String label)
    {
        if (label == null) {
            return link;
        }
        StringBuffer uri = new StringBuffer(link);
        if (link.indexOf('?') >= 0) {
            uri.append('&');
        } else {
            uri.append('?');
        }
        uri.append(parmName).append('=').append(SourceUtil.encode(label));
        return uri.toString();
    }
}
TOP

Related Classes of org.apache.cocoon.portal.impl.PageLabelLinkService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.