001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.file.collectors; 029 030import org.opencms.file.CmsDataAccessException; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.main.CmsException; 035 036import java.util.Arrays; 037import java.util.Collections; 038import java.util.List; 039 040/** 041 * A default resource collector that supports flexible sorting based on resource dates.<p> 042 * 043 * @since 7.0.2 044 */ 045public class CmsDateResourceCollector extends A_CmsResourceCollector { 046 047 /** Static array of the collectors implemented by this class. */ 048 private static final String[] COLLECTORS = { 049 "allInFolderDateDesc", 050 "allInFolderDateAsc", 051 "allInSubTreeDateDesc", 052 "allInSubTreeDateAsc"}; 053 054 /** Array list for fast collector name lookup. */ 055 private static final List<String> COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS)); 056 057 /** 058 * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames() 059 */ 060 public List<String> getCollectorNames() { 061 062 return COLLECTORS_LIST; 063 } 064 065 /** 066 * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 067 */ 068 public String getCreateLink(CmsObject cms, String collectorName, String param) 069 throws CmsDataAccessException, CmsException { 070 071 // if action is not set, use default action 072 if (collectorName == null) { 073 collectorName = COLLECTORS[0]; 074 } 075 076 switch (COLLECTORS_LIST.indexOf(collectorName)) { 077 case 0: 078 // "allInFolderDateDesc" 079 case 1: 080 // "allInFolderDateAsc" 081 return getCreateInFolder(cms, new CmsExtendedCollectorData(param)); 082 case 2: 083 // "allInSubTreeDateDesc" 084 case 3: 085 // "allInSubTreeDateAsc" 086 return null; 087 default: 088 throw new CmsDataAccessException( 089 Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName)); 090 } 091 } 092 093 /** 094 * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 095 */ 096 public String getCreateParam(CmsObject cms, String collectorName, String param) throws CmsDataAccessException { 097 098 // if action is not set, use default action 099 if (collectorName == null) { 100 collectorName = COLLECTORS[0]; 101 } 102 103 switch (COLLECTORS_LIST.indexOf(collectorName)) { 104 case 0: 105 // "allInFolderDateDesc" 106 case 1: 107 // "allInFolderDateAsc" 108 return param; 109 case 2: 110 // "allInSubTreeDateDesc" 111 case 3: 112 // "allInSubTreeDateAsc" 113 return null; 114 default: 115 throw new CmsDataAccessException( 116 Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName)); 117 } 118 } 119 120 /** 121 * @see org.opencms.file.collectors.A_CmsResourceCollector#getCreateTypeId(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 122 */ 123 @Override 124 public int getCreateTypeId(CmsObject cms, String collectorName, String param) { 125 126 int result = -1; 127 if (param != null) { 128 result = new CmsExtendedCollectorData(param).getType(); 129 } 130 return result; 131 } 132 133 /** 134 * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 135 */ 136 public List<CmsResource> getResults(CmsObject cms, String collectorName, String param) 137 throws CmsDataAccessException, CmsException { 138 139 return getResults(cms, collectorName, param, -1); 140 } 141 142 /** 143 * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String) 144 */ 145 public List<CmsResource> getResults(CmsObject cms, String collectorName, String param, int numResults) 146 throws CmsDataAccessException, CmsException { 147 148 // if action is not set use default 149 if (collectorName == null) { 150 collectorName = COLLECTORS[0]; 151 } 152 153 switch (COLLECTORS_LIST.indexOf(collectorName)) { 154 case 0: 155 // "allInFolderDateDesc" 156 return allInFolderDate(cms, param, false, false, numResults); 157 case 1: 158 // "allInFolderDateAsc" 159 return allInFolderDate(cms, param, false, true, numResults); 160 case 2: 161 // "allInSubTreeDateDesc" 162 return allInFolderDate(cms, param, true, false, numResults); 163 case 3: 164 // "allInSubTreeDateAsc" 165 return allInFolderDate(cms, param, true, true, numResults); 166 default: 167 throw new CmsDataAccessException( 168 Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName)); 169 } 170 } 171 172 /** 173 * Returns a List of all resources in the folder pointed to by the parameter 174 * sorted by the selected dates.<p> 175 * 176 * @param cms the current CmsObject 177 * @param param must contain an extended collector parameter set as described by {@link CmsExtendedCollectorData} 178 * @param tree if true, look in folder and all child folders, if false, look only in given folder 179 * @param asc if <code>true</code>, the sort is ascending (old dates first), otherwise it is descending 180 * (new dates first) 181 * @param numResults number of results 182 * 183 * @return a List of all resources in the folder pointed to by the parameter sorted by the selected dates 184 * 185 * @throws CmsException if something goes wrong 186 */ 187 protected List<CmsResource> allInFolderDate(CmsObject cms, String param, boolean tree, boolean asc, int numResults) 188 throws CmsException { 189 190 CmsExtendedCollectorData data = new CmsExtendedCollectorData(param); 191 String foldername = CmsResource.getFolderPath(data.getFileName()); 192 List<String> dateIdentifiers = data.getAdditionalParams(); 193 194 CmsResourceFilter filter = CmsResourceFilter.DEFAULT.addRequireType(data.getType()).addExcludeFlags( 195 CmsResource.FLAG_TEMPFILE); 196 if (data.isExcludeTimerange() && !cms.getRequestContext().getCurrentProject().isOnlineProject()) { 197 // include all not yet released and expired resources in an offline project 198 filter = filter.addExcludeTimerange(); 199 } 200 List<CmsResource> result = cms.readResources(foldername, filter, tree); 201 202 // a special date comparator is used to sort the resources 203 CmsDateResourceComparator comparator = new CmsDateResourceComparator(cms, dateIdentifiers, asc); 204 Collections.sort(result, comparator); 205 206 return shrinkToFit(result, data.getCount(), numResults); 207 } 208}