KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > treeservice > ss > ContentNodeSupplier


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.treeservice.ss;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
34 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
35 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
36 import org.infoglue.cms.entities.content.ContentVO;
37 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
38 import org.infoglue.cms.exception.ConstraintException;
39 import org.infoglue.cms.exception.SystemException;
40 import org.infoglue.cms.security.InfoGluePrincipal;
41 import org.infoglue.cms.util.CmsPropertyHandler;
42 import org.infoglue.cms.util.sorters.ReflectionComparator;
43
44 import com.frovi.ss.Tree.BaseNode;
45 import com.frovi.ss.Tree.BaseNodeSupplier;
46
47 /**
48  * ContentNodeSupplier.java
49  * Created on 2002-sep-30
50  * @author Stefan Sik, ss@frovi.com
51  * @author Frank Febbraro (frank@phase2technology.com) Refactoring and Sorting
52  */

53
54 public class ContentNodeSupplier extends BaseNodeSupplier
55 {
56     private final static Logger logger = Logger.getLogger(ContentNodeSupplier.class.getName());
57
58     private ArrayList JavaDoc cacheLeafs;
59     private boolean showLeafs = true;
60     private String JavaDoc[] allowedContentTypeIds = null;
61     private InfoGluePrincipal infogluePrincipal = null;
62     
63     public ContentNodeSupplier(Integer JavaDoc repositoryId, InfoGluePrincipal infogluePrincipal) throws SystemException
64     {
65         ContentVO vo =null;
66         try
67         {
68             this.infogluePrincipal = infogluePrincipal;
69             
70             vo = ContentControllerProxy.getController().getRootContentVO(repositoryId, infogluePrincipal.getName());
71             BaseNode rootNode = new ContentNodeImpl();
72             rootNode.setChildren(true);
73             rootNode.setId(vo.getId());
74             rootNode.setTitle(vo.getName());
75             rootNode.setContainer(vo.getIsBranch().booleanValue());
76             
77             setRootNode(rootNode);
78         }
79         catch (ConstraintException e)
80         {
81             e.printStackTrace();
82         }
83             
84     }
85     /**
86      * @see com.frovi.ss.Tree.BaseNodeSupplier#hasChildren()
87      */

88     public boolean hasChildren()
89     {
90         if (showLeafs)
91             return false;
92         else
93             return true;
94     }
95
96     /**
97      * @see com.frovi.ss.Tree.INodeSupplier#getChildContainerNodes(Integer)
98      */

99     public Collection JavaDoc getChildContainerNodes(Integer JavaDoc parentNode)
100     {
101         ArrayList JavaDoc ret = new ArrayList JavaDoc();
102         cacheLeafs = new ArrayList JavaDoc();
103         
104         List JavaDoc children = null;
105         try
106         {
107             children = ContentController.getContentController().getContentChildrenVOList(parentNode);
108         }
109         catch (ConstraintException e)
110         {
111             logger.warn("Error getting Content Children", e);
112         }
113         catch (SystemException e)
114         {
115             logger.warn("Error getting Content Children", e);
116         }
117         
118         //Filter list on content type names if set such is stated
119
try
120         {
121             if(allowedContentTypeIds != null)
122             {
123                 List JavaDoc filteredList = new ArrayList JavaDoc();
124                 Iterator JavaDoc iterator = children.iterator();
125                 while(iterator.hasNext())
126                 {
127                     ContentVO contentVO = (ContentVO) iterator.next();
128                     if(contentVO.getContentTypeDefinitionId() != null && !contentVO.getIsBranch().booleanValue())
129                     {
130                         ContentTypeDefinitionVO contentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithId(contentVO.getContentTypeDefinitionId());
131                         boolean exists = false;
132                         for(int i=0; i<allowedContentTypeIds.length; i++)
133                         {
134                             String JavaDoc allowedId = allowedContentTypeIds[i];
135                             
136                             if(allowedId.equalsIgnoreCase(contentTypeDefinitionVO.getId().toString()))
137                             {
138                                 exists = true;
139                                 break;
140                             }
141                         }
142
143                         if(exists)
144                         {
145                             filteredList.add(contentVO);
146                         }
147                     }
148                     else
149                     {
150                         filteredList.add(contentVO);
151                     }
152                 }
153                 
154                 children = filteredList;
155             }
156         }
157         catch(Exception JavaDoc e)
158         {
159             logger.warn("Error filtering Content Children", e);
160         }
161         
162         //Sort the tree nodes if setup to do so
163
String JavaDoc sortProperty = CmsPropertyHandler.getContentTreeSort();
164         if(sortProperty != null)
165             Collections.sort(children, new ReflectionComparator(sortProperty));
166         
167         Iterator JavaDoc i = children.iterator();
168         while(i.hasNext())
169         {
170             ContentVO vo = (ContentVO) i.next();
171             
172             if(!vo.getName().equals("Meta info folder") || this.infogluePrincipal.getIsAdministrator())
173             {
174                 BaseNode node = new ContentNodeImpl();
175                 node.setId(vo.getId());
176                 node.setTitle(vo.getName());
177                 
178                 if (vo.getIsBranch().booleanValue())
179                 {
180                     node.setContainer(true);
181                     node.setChildren((vo.getChildCount().intValue() > 0));
182                     
183                     ret.add(node);
184                 }
185                 else if(showLeafs)
186                 {
187                     node.setContainer(false);
188                     
189                     cacheLeafs.add(node);
190                 }
191             }
192         }
193         
194         return ret;
195     }
196
197     /**
198      * @see com.frovi.ss.Tree.INodeSupplier#getChildLeafNodes(Integer)
199      */

200     public Collection JavaDoc getChildLeafNodes(Integer JavaDoc parentNode)
201     {
202         return (cacheLeafs == null ) ? new ArrayList JavaDoc() : cacheLeafs;
203     }
204
205     /**
206      * Sets the showLeafs.
207      * @param showLeafs The showLeafs to set
208      */

209     public void setShowLeafs(boolean showLeafs)
210     {
211         this.showLeafs = showLeafs;
212     }
213
214     public String JavaDoc[] getAllowedContentTypeIds()
215     {
216         return allowedContentTypeIds;
217     }
218     
219     public void setAllowedContentTypeIds(String JavaDoc[] allowedContentTypeIds)
220     {
221         this.allowedContentTypeIds = allowedContentTypeIds;
222     }
223 }
224
Popular Tags