cmscategoryresourcecollector.java

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

JAVA
413
字号
                } else if (PARAM_KEY_COUNT.equals(key)) {
                    int count = 0;
                    try {
                        count = Integer.parseInt(value);
                    } catch (NumberFormatException e) {
                        // ignore
                    }
                    setCount(count);
                } else {
                    LOG.error("Unknow key found in collector parameters.");
                }
            }
        }
    }

    /** Compares the release date of resources in descending order. */
    public static final Comparator COMPARE_DATE_RELEASED_DESC = new Comparator() {

        /**
         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
         */
        public int compare(Object o1, Object o2) {

            if ((o1 == o2) || !(o1 instanceof CmsResource) || !(o2 instanceof CmsResource)) {
                return 0;
            }

            CmsResource r1 = (CmsResource)o1;
            CmsResource r2 = (CmsResource)o2;

            long date1 = r1.getDateReleased();
            if (date1 == CmsResource.DATE_RELEASED_DEFAULT) {
                // use creation date if release date is not set
                date1 = r1.getDateLastModified();
            }

            long date2 = r2.getDateReleased();
            if (date2 == CmsResource.DATE_RELEASED_DEFAULT) {
                // use creation date if release date is not set
                date2 = r2.getDateLastModified();
            }

            return (date1 > date2) ? 1 : (date1 < date2) ? -1 : 0;
        }
    };

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

    /** Static array of the collectors implemented by this class. */
    private static final String[] COLLECTORS = {"allKeyValuePairFiltered"};

    /** Array list for fast collector name lookup. */
    private static final List COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS));

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
     */
    public List getCollectorNames() {

        return COLLECTORS_LIST;
    }

    /**
     * @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)
    throws CmsException, CmsDataAccessException {

        // if action is not set, use default action
        if (collectorName == null) {
            collectorName = COLLECTORS[0];
        }

        switch (COLLECTORS_LIST.indexOf(collectorName)) {
            case 0:
                // "allKeyValuePairFiltered"
                return null;
            default:
                throw new CmsDataAccessException(Messages.get().container(
                    Messages.ERR_COLLECTOR_NAME_INVALID_1,
                    collectorName));
        }
    }

    /**
     * @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) throws CmsDataAccessException {

        // if action is not set, use default action
        if (collectorName == null) {
            collectorName = COLLECTORS[0];
        }

        switch (COLLECTORS_LIST.indexOf(collectorName)) {
            case 0:
                // "allKeyValuePairFiltered"
                return null;
            default:
                throw new CmsDataAccessException(Messages.get().container(
                    Messages.ERR_COLLECTOR_NAME_INVALID_1,
                    collectorName));
        }
    }

    /**
     * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
     */
    public List getResults(CmsObject cms, String collectorName, String param)
    throws CmsDataAccessException, CmsException {

        // if action is not set use default
        if (collectorName == null) {
            collectorName = COLLECTORS[0];
        }

        switch (COLLECTORS_LIST.indexOf(collectorName)) {

            case 0:
                // "allKeyValuePairFiltered"
                return allKeyValuePairFiltered(cms, param);
            default:
                throw new CmsDataAccessException(Messages.get().container(
                    Messages.ERR_COLLECTOR_NAME_INVALID_1,
                    collectorName));
        }
    }

    /**
     * Collects all resources for the given categories filtered and sorted by the given collector parameter.<p>
     * 
     * @param cms the current OpenCms user context
     * @param param value parameter to filter the resources
     * 
     * @return a list of resources filtered and sorted by the given collector parameter
     * 
     * @throws CmsException if something goes wrong
     */
    protected List allKeyValuePairFiltered(CmsObject cms, String param) throws CmsException {

        CmsCategoryCollectorData data = new CmsCategoryCollectorData(param);
        if ((data.getCategoryTypes() != null) && (data.getCategoryTypes().size() > 0)) {
            List result = new ArrayList();
            Map sortCategories = new HashMap();
            String foldername = null;
            boolean includeSubTree = false;
            if (data.getFileName() != null) {
                foldername = CmsResource.getFolderPath(data.getFileName());
                includeSubTree = data.isSubTree();
            } else {
                foldername = "/";
                includeSubTree = true;
            }

            CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addExcludeFlags(CmsResource.FLAG_TEMPFILE);
            if (data.getType() != -1) {
                filter = filter.addRequireType(data.getType());
            }

            List resources = cms.readResources(foldername, filter, includeSubTree);
            List categoryTypes = data.getCategoryTypes();
            Iterator itResources = resources.iterator();
            CmsResource resource;
            CmsCategoryService service = CmsCategoryService.getInstance();
            while (itResources.hasNext()) {
                resource = (CmsResource)itResources.next();
                Iterator itCategories = service.readResourceCategories(cms, cms.getSitePath(resource)).iterator();
                while (itCategories.hasNext()) {
                    CmsCategory category = (CmsCategory)itCategories.next();
                    if (categoryTypes.contains(category.getPath())) {
                        if ((data.getSortBy() != null) && data.getSortBy().equals("category")) {
                            if (sortCategories.containsKey(category.getPath())) {
                                ((List)sortCategories.get(category.getPath())).add(resource);
                            } else {
                                List sortResources = new ArrayList();
                                sortResources.add(resource);
                                sortCategories.put(category.getPath(), sortResources);
                            }
                        } else {
                            if (!result.contains(resource)) {
                                result.add(resource);
                            }
                        }
                    }
                }
            }

            if ((data.getSortBy() != null) && data.getSortBy().equals("date")) {
                if (!data.isSortAsc()) {
                    Collections.sort(result, COMPARE_DATE_RELEASED_DESC);
                } else {
                    Collections.sort(result, CmsResource.COMPARE_DATE_RELEASED);
                }
            } else if ((data.getSortBy() != null) && data.getSortBy().equals("category")) {
                // categories are sort by their paths
                Iterator itCategoryTypes = categoryTypes.iterator();
                while (itCategoryTypes.hasNext()) {
                    result.addAll((List)sortCategories.get(itCategoryTypes.next()));
                }
            }
            return shrinkToFit(result, data.getCount());
        }
        return null;
    }
}

⌨️ 快捷键说明

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