KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ComponentController


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.exolab.castor.jdo.Database;
34 import org.infoglue.cms.entities.content.ContentVO;
35 import org.infoglue.cms.entities.content.ContentVersion;
36 import org.infoglue.cms.entities.kernel.BaseEntityVO;
37 import org.infoglue.cms.entities.management.Language;
38 import org.infoglue.cms.exception.Bug;
39 import org.infoglue.cms.exception.SystemException;
40 import org.infoglue.cms.util.sorters.ContentComparator;
41 import org.infoglue.deliver.util.CacheController;
42
43 /**
44  * This class handles all access to components.
45  *
46  * @author Mattias Bogeblad
47  */

48
49 public class ComponentController extends BaseController
50 {
51     private final static Logger logger = Logger.getLogger(ComponentController.class.getName());
52
53     /**
54      * Factory method
55      */

56
57     public static ComponentController getController()
58     {
59         return new ComponentController();
60     }
61
62     /**
63      * This method returns a sorted list of components.
64      * @param sortAttribute
65      * @return
66      * @throws SystemException
67      * @throws Bug
68      */

69     
70     public List JavaDoc getComponentVOList(String JavaDoc sortAttribute, String JavaDoc direction, String JavaDoc[] allowedComponentNames) throws SystemException, Bug, Exception JavaDoc
71     {
72         List JavaDoc componentVOList = null;
73         
74         Database db = CastorDatabaseService.getDatabase();
75         try
76         {
77             beginTransaction(db);
78             
79             componentVOList = getComponentVOList(sortAttribute, direction, allowedComponentNames, db);
80                 
81             commitTransaction(db);
82         }
83         catch ( Exception JavaDoc e )
84         {
85             e.printStackTrace();
86             rollbackTransaction(db);
87             throw new SystemException("An error occurred when we tried to fetch a list of users in this group. Reason:" + e.getMessage(), e);
88         }
89         
90         return componentVOList;
91     }
92     
93     /**
94      * This method returns a sorted list of components within a transaction.
95      * @param sortAttribute
96      * @return
97      * @throws SystemException
98      * @throws Bug
99      */

100     
101     private static List JavaDoc cachedComponents = null;
102     
103     public List JavaDoc getComponentVOList(String JavaDoc sortAttribute, String JavaDoc direction, String JavaDoc[] allowedComponentNames, Database db) throws SystemException, Bug, Exception JavaDoc
104     {
105         String JavaDoc allowedComponentNamesString = "";
106         if(allowedComponentNames != null)
107         {
108             for(int i=0; i<allowedComponentNames.length; i++)
109                 allowedComponentNamesString = allowedComponentNames[i] + ":";
110         }
111         
112         String JavaDoc componentsKey = "components_" + sortAttribute + "_" + direction + "_" + allowedComponentNamesString;
113         List JavaDoc components = (List JavaDoc)CacheController.getCachedObject("componentContentsCache", componentsKey);
114         if(components != null)
115         {
116             logger.info("There was cached components:" + components.size());
117         }
118         else
119         {
120             components = getComponents(allowedComponentNames);
121             Iterator JavaDoc componentsIterator = components.iterator();
122             while(componentsIterator.hasNext())
123             {
124                 ContentVO contentVO = (ContentVO)componentsIterator.next();
125     
126                 Language masterLanguage = LanguageController.getController().getMasterLanguage(db, contentVO.getRepositoryId());
127                 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentVO.getId(), masterLanguage.getId(), db);
128                 
129                 String JavaDoc groupName = "Unknown";
130                 
131                 if(contentVersion != null)
132                 {
133                     groupName = ContentVersionController.getContentVersionController().getAttributeValue(contentVersion.getValueObject(), "GroupName", false);
134                 }
135     
136                 contentVO.getExtraProperties().put("GroupName", groupName);
137             }
138             
139             CacheController.cacheObject("componentContentsCache", componentsKey, components);
140         }
141         
142         ContentComparator comparator = new ContentComparator(sortAttribute, direction, null);
143         Collections.sort(components, comparator);
144         
145         return components;
146     }
147     
148     
149     /**
150      * This method returns the contents that are of contentTypeDefinition "HTMLTemplate"
151      */

152     
153     public List JavaDoc getComponents(String JavaDoc[] allowedComponentNames) throws Exception JavaDoc
154     {
155         HashMap JavaDoc arguments = new HashMap JavaDoc();
156         arguments.put("method", "selectListOnContentTypeName");
157         
158         List JavaDoc argumentList = new ArrayList JavaDoc();
159         HashMap JavaDoc argument = new HashMap JavaDoc();
160         argument.put("contentTypeDefinitionName", "HTMLTemplate");
161         argumentList.add(argument);
162         arguments.put("arguments", argumentList);
163         
164         List JavaDoc results = ContentController.getContentController().getContentVOList(arguments);
165         
166         if(allowedComponentNames != null && allowedComponentNames.length > 0)
167         {
168             Iterator JavaDoc resultsIterator = results.iterator();
169             while(resultsIterator.hasNext())
170             {
171                 ContentVO contentVO = (ContentVO)resultsIterator.next();
172                 boolean isAllowed = false;
173                 for(int i=0; i<allowedComponentNames.length; i++)
174                 {
175                     if(contentVO.getName().equals(allowedComponentNames[i]))
176                         isAllowed = true;
177                 }
178                 
179                 if(!isAllowed)
180                     resultsIterator.remove();
181             }
182         }
183         
184         return results;
185     }
186     
187     /* (non-Javadoc)
188      * @see org.infoglue.cms.controllers.kernel.impl.simple.BaseController#getNewVO()
189      */

190     public BaseEntityVO getNewVO()
191     {
192         return null;
193     }
194     
195     
196 }
197
Popular Tags