a_cmslistresourcecollector.java

来自「找了很久才找到到源代码」· Java 代码 · 共 695 行 · 第 1/2 页

JAVA
695
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/list/A_CmsListResourceCollector.java,v $
 * Date   : $Date: 2007-08-13 16:29:48 $
 * Version: $Revision: 1.5 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.workplace.list;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.main.CmsException;
import org.opencms.main.CmsIllegalStateException;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import org.opencms.workplace.commons.CmsProgressThread;
import org.opencms.workplace.explorer.CmsResourceUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;

/**
 * Collector to provide {@link CmsResource} objects for a explorer List.<p>
 * 
 * @author Michael Moossen
 * 
 * @version $Revision: 1.5 $ 
 * 
 * @since 6.1.0 
 */
public abstract class A_CmsListResourceCollector implements I_CmsListResourceCollector {

    /** VFS path to use for a dummy resource object. */
    public static final String VFS_PATH_NONE = "none";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(A_CmsListResourceCollector.class);

    /** The collector parameter. */
    protected String m_collectorParameter;

    /** List item cache. */
    protected Map m_liCache = new HashMap();

    /** Resource cache. */
    protected Map m_resCache = new HashMap();

    /** Cache for resource list result. */
    protected List m_resources = null;

    /** The workplace object where the collector is used from. */
    private A_CmsListExplorerDialog m_wp;

    /**
     * Constructor, creates a new list collector.<p>
     * 
     * @param wp the workplace object where the collector is used from
     */
    protected A_CmsListResourceCollector(A_CmsListExplorerDialog wp) {

        m_wp = wp;
        CmsListState state = (wp != null ? wp.getListStateForCollector() : new CmsListState());
        if (state.getPage() < 1) {
            state.setPage(1);
        }
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(state.getColumn())) {
            state.setColumn(A_CmsListExplorerDialog.LIST_COLUMN_NAME);
        }
        if (state.getOrder() == null) {
            state.setOrder(CmsListOrderEnum.ORDER_ASCENDING);
        }
        if (state.getFilter() == null) {
            state.setFilter("");
        }
        m_collectorParameter = I_CmsListResourceCollector.PARAM_PAGE
            + I_CmsListResourceCollector.SEP_KEYVAL
            + state.getPage();
        m_collectorParameter += I_CmsListResourceCollector.SEP_PARAM
            + I_CmsListResourceCollector.PARAM_SORTBY
            + I_CmsListResourceCollector.SEP_KEYVAL
            + state.getColumn();
        m_collectorParameter += I_CmsListResourceCollector.SEP_PARAM
            + I_CmsListResourceCollector.PARAM_ORDER
            + I_CmsListResourceCollector.SEP_KEYVAL
            + state.getOrder();
        m_collectorParameter += I_CmsListResourceCollector.SEP_PARAM
            + I_CmsListResourceCollector.PARAM_FILTER
            + I_CmsListResourceCollector.SEP_KEYVAL
            + state.getFilter();
    }

    /**
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Object arg0) {

        return 0;
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject)
     */
    public String getCreateLink(CmsObject cms) {

        return null;
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
     */
    public String getCreateLink(CmsObject cms, String collectorName, String param) {

        return null;
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject)
     */
    public String getCreateParam(CmsObject cms) {

        return null;
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
     */
    public String getCreateParam(CmsObject cms, String collectorName, String param) {

        return null;
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getDefaultCollectorName()
     */
    public String getDefaultCollectorName() {

        return (String)getCollectorNames().get(0);
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getDefaultCollectorParam()
     */
    public String getDefaultCollectorParam() {

        return m_collectorParameter;
    }

    /**
     * Returns a list of list items from a list of resources.<p>
     * 
     * @param parameter the collector parameter or <code>null</code> for default.<p>
     * 
     * @return a list of {@link CmsListItem} objects
     * 
     * @throws CmsException if something goes wrong
     */
    public List getListItems(String parameter) throws CmsException {

        synchronized (this) {
            if (parameter == null) {
                parameter = m_collectorParameter;
            }
            Map params = CmsStringUtil.splitAsMap(
                parameter,
                I_CmsListResourceCollector.SEP_PARAM,
                I_CmsListResourceCollector.SEP_KEYVAL);
            CmsListState state = getState(params);
            List resources = getInternalResources(getWp().getCms(), params);
            List ret = new ArrayList();
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_COLLECTOR_PROCESS_ITEMS_START_1,
                    new Integer(resources.size())));
            }
            getWp().applyColumnVisibilities();
            CmsHtmlList list = getWp().getList();

            // check if progress should be set in the thread
            CmsProgressThread thread = null;
            int progressOffset = 0;
            if (Thread.currentThread() instanceof CmsProgressThread) {
                thread = (CmsProgressThread)Thread.currentThread();
                progressOffset = thread.getProgress();
            }

            CmsListColumnDefinition colPermissions = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_PERMISSIONS);
            boolean showPermissions = (colPermissions.isVisible() || colPermissions.isPrintable());
            CmsListColumnDefinition colDateLastMod = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_DATELASTMOD);
            boolean showDateLastMod = (colDateLastMod.isVisible() || colDateLastMod.isPrintable());
            CmsListColumnDefinition colUserLastMod = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_USERLASTMOD);
            boolean showUserLastMod = (colUserLastMod.isVisible() || colUserLastMod.isPrintable());
            CmsListColumnDefinition colDateCreate = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_DATECREATE);
            boolean showDateCreate = (colDateCreate.isVisible() || colDateCreate.isPrintable());
            CmsListColumnDefinition colUserCreate = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_USERCREATE);
            boolean showUserCreate = (colUserCreate.isVisible() || colUserCreate.isPrintable());
            CmsListColumnDefinition colDateRel = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_DATEREL);
            boolean showDateRel = (colDateRel.isVisible() || colDateRel.isPrintable());
            CmsListColumnDefinition colDateExp = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_DATEEXP);
            boolean showDateExp = (colDateExp.isVisible() || colDateExp.isPrintable());
            CmsListColumnDefinition colState = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_STATE);
            boolean showState = (colState.isVisible() || colState.isPrintable());
            CmsListColumnDefinition colLockedBy = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_LOCKEDBY);
            boolean showLockedBy = (colLockedBy.isVisible() || colLockedBy.isPrintable());
            CmsListColumnDefinition colSite = list.getMetadata().getColumnDefinition(
                A_CmsListExplorerDialog.LIST_COLUMN_SITE);
            boolean showSite = (colSite.isVisible() || colSite.isPrintable());

            // get content
            Iterator itRes = resources.iterator();
            int count = 0;
            while (itRes.hasNext()) {
                // set progress in thread
                if (thread != null) {
                    count++;
                    if (thread.isInterrupted()) {
                        throw new CmsIllegalStateException(org.opencms.workplace.commons.Messages.get().container(
                            org.opencms.workplace.commons.Messages.ERR_PROGRESS_INTERRUPTED_0));
                    }
                    thread.setProgress((count * 40 / resources.size()) + progressOffset);
                    thread.setDescription(org.opencms.workplace.commons.Messages.get().getBundle(thread.getLocale()).key(
                        org.opencms.workplace.commons.Messages.GUI_PROGRESS_PUBLISH_STEP2_2,
                        new Integer(count),
                        new Integer(resources.size())));
                }

                Object obj = itRes.next();
                if (!(obj instanceof CmsResource)) {
                    ret.add(getDummyListItem(list));
                    continue;
                }
                CmsResource resource = (CmsResource)obj;
                CmsListItem item = (CmsListItem)m_liCache.get(resource.getStructureId().toString());
                if (item == null) {
                    item = createResourceListItem(
                        resource,
                        list,
                        showPermissions,
                        showDateLastMod,
                        showUserLastMod,
                        showDateCreate,
                        showUserCreate,
                        showDateRel,
                        showDateExp,
                        showState,
                        showLockedBy,
                        showSite);
                    m_liCache.put(resource.getStructureId().toString(), item);
                }
                ret.add(item);
            }
            CmsListMetadata metadata = list.getMetadata();
            if (metadata != null) {
                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(state.getFilter())) {
                    // filter
                    ret = metadata.getSearchAction().filter(ret, state.getFilter());
                }
                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(state.getColumn())) {
                    if ((metadata.getColumnDefinition(state.getColumn()) != null)
                        && metadata.getColumnDefinition(state.getColumn()).isSorteable()) {
                        // sort
                        I_CmsListItemComparator c = metadata.getColumnDefinition(state.getColumn()).getListItemComparator();
                        Collections.sort(ret, c.getComparator(state.getColumn(), getWp().getLocale()));
                        if (state.getOrder().equals(CmsListOrderEnum.ORDER_DESCENDING)) {
                            Collections.reverse(ret);
                        }
                    }
                }
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(
                    Messages.LOG_COLLECTOR_PROCESS_ITEMS_END_1,
                    new Integer(ret.size())));
            }
            return ret;
        }
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getOrder()
     */
    public int getOrder() {

        return 0;
    }

    /**
     * Returns the resource for the given item.<p>
     * 
     * @param cms the cms object
     * @param item the item
     * 
     * @return the resource
     */
    public CmsResource getResource(CmsObject cms, CmsListItem item) {

        CmsResource res = (CmsResource)m_resCache.get(item.getId());
        if (res == null) {
            try {
                res = cms.readResource(
                    (String)item.get(A_CmsListExplorerDialog.LIST_COLUMN_NAME),
                    CmsResourceFilter.ALL);
                m_resCache.put(item.getId(), res);
            } catch (CmsException e) {
                // should never happen
                if (LOG.isInfoEnabled()) {
                    LOG.info(e);

⌨️ 快捷键说明

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