All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.efaps.ui.wicket.request.EFapsResourceAggregator Maven / Gradle / Ivy

Go to download

eFaps WebApp provides a web interface as the User Interface for eFaps which can be easily expanded and altered.

There is a newer version: 3.2.0
Show newest version
/*
 * Copyright 2003 - 2012 The eFaps Team
 *
 * 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.
 *
 * Revision:        $Rev: 7675 $
 * Last Changed:    $Date: 2012-06-14 15:52:26 -0500 (Thu, 14 Jun 2012) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.ui.wicket.request;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.HeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.DecoratingHeaderResponse;
import org.efaps.admin.program.bundle.BundleMaker;
import org.efaps.admin.program.bundle.TempFileBundle;
import org.efaps.ui.wicket.behaviors.dojo.OnDojoReadyHeaderItem;
import org.efaps.ui.wicket.resources.AbstractEFapsHeaderItem;
import org.efaps.ui.wicket.resources.EFapsContentReference;
import org.efaps.ui.wicket.resources.EFapsJavaScriptHeaderItem;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 * @version $Id: EFapsResourceAggregator.java 7675 2012-06-14 20:52:26Z [email protected] $
 */
public class EFapsResourceAggregator
    extends DecoratingHeaderResponse
{

    /**
     * Logger for this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(EFapsResourceAggregator.class);

    /**
     * List of HeaderItems that will be rendered in on dojo ready script.
     */
    private final List dojoReadyItems = new ArrayList();

    /**
     * List of HeaderItems that will be rendered file using the eFaps kernel.
     */
    private final List eFapsHeaderItems = new ArrayList();

    /**
     * @param _real orginial Response
     */
    public EFapsResourceAggregator(final IHeaderResponse _real)
    {
        super(_real);
    }

    /**
     * Renders the given {@link HeaderItem} to the response if none of the
     * {@linkplain HeaderItem#getRenderTokens() tokens} of the item has been rendered before.
     *
     * @param _item  The item to render.
     */
    @Override
    public void render(final HeaderItem _item)
    {
        if (_item instanceof OnDojoReadyHeaderItem) {
            this.dojoReadyItems.add((OnDojoReadyHeaderItem) _item);
        } else if (_item instanceof AbstractEFapsHeaderItem) {
            this.eFapsHeaderItems.add((AbstractEFapsHeaderItem) _item);
        } else {
            getRealResponse().render(_item);
        }
    }

    /**
     * Before closing the combined Script and EFapsHeaderItems are added.
     */
    @Override
    public void close()
    {
        renderCombinedEventScripts();
        renderEFapsHeaderItems();
        super.close();
    }

    /**
     * render the eFaps Resource items.
     */
    private void renderEFapsHeaderItems()
    {
        Collections.sort(this.eFapsHeaderItems, new Comparator()
        {

            @Override
            public int compare(final AbstractEFapsHeaderItem _item0,
                               final AbstractEFapsHeaderItem _item1)
            {
                return _item0.getSortWeight().compareTo(_item1.getSortWeight());
            }
        });

        final List css = new ArrayList();
        final List js = new ArrayList();
        for (final AbstractEFapsHeaderItem item : this.eFapsHeaderItems) {
            if (item instanceof EFapsJavaScriptHeaderItem) {
                js.add(item.getReference().getName());
            } else {
                css.add(item.getReference().getName());
            }
        }
        try {
            if (!css.isEmpty()) {
                final String key = BundleMaker.getBundleKey(css, TempFileBundle.class);
                final TempFileBundle bundle = (TempFileBundle) BundleMaker.getBundle(key);
                bundle.setContentType("text/css");
                getRealResponse().render(CssHeaderItem.forUrl(new EFapsContentReference(key).getStaticContentUrl()));
            }
            if (!js.isEmpty()) {
                final String key = BundleMaker.getBundleKey(js, TempFileBundle.class);
                final TempFileBundle bundle = (TempFileBundle) BundleMaker.getBundle(key);
                bundle.setContentType("text/javascript");
                getRealResponse().render(
                                JavaScriptHeaderItem.forUrl(new EFapsContentReference(key).getStaticContentUrl()));
            }
        } catch (final EFapsException e) {
            EFapsResourceAggregator.LOG.error("Error on rendering eFaps Header items: ", e);
        }
    }

    /**
     * Combines all DOM ready and onLoad scripts and renders them as 2 script
     * tags.
     */
    private void renderCombinedEventScripts()
    {
        final StringBuilder combinedScript = new StringBuilder();

        for (final OnDojoReadyHeaderItem curItem : this.dojoReadyItems) {
            combinedScript.append("\n");
            combinedScript.append(curItem.getJavaScript());
            combinedScript.append(";");
        }

        if (combinedScript.length() > 0) {
            getRealResponse().render(OnDojoReadyHeaderItem.forScript(combinedScript.append("\n").toString()));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy