KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > viewtools > CategoriesWebAPI


1 package com.dotmarketing.viewtools;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import com.dotmarketing.beans.Inode;
8 import com.dotmarketing.factories.InodeFactory;
9 import com.dotmarketing.portlets.categories.factories.CategoryFactory;
10 import com.dotmarketing.portlets.categories.model.Category;
11 import com.dotmarketing.portlets.entities.factories.EntityFactory;
12 import com.dotmarketing.portlets.entities.model.Entity;
13 import com.dotmarketing.util.UtilHTML;
14
15 public class CategoriesWebAPI {
16
17     public void init(Object JavaDoc obj) {
18
19     }
20
21     public String JavaDoc getCategoriesByEntityName(String JavaDoc parameterName, String JavaDoc entityName, boolean displayTitle,
22             boolean multipleValues, int size) {
23         long childInode = 0;
24         boolean readonly = false;
25         return getCategoriesByEntityName(parameterName, entityName, childInode, displayTitle, multipleValues, size,
26                 readonly);
27     }
28
29     public List JavaDoc<Category> getCategoriesListByEntityName(String JavaDoc entityName) {
30         Entity e = EntityFactory.getEntity(entityName);
31         return EntityFactory.getEntityCategories(e);
32     }
33
34
35     public List JavaDoc getChildrenCategoriesByKey(String JavaDoc key) {
36         if (key == null) {
37             return new ArrayList JavaDoc();
38         }
39         Category cat = CategoryFactory.getCategoryByKey(key);
40         if (cat.getInode() == 0) {
41             return new ArrayList JavaDoc();
42         }
43         return CategoryFactory.getChildrenCategories(cat);
44
45     }
46
47     public Category getCategoryByKey(String JavaDoc key) {
48
49         return CategoryFactory.getCategoryByKey(key);
50         
51
52     }
53     public List JavaDoc getChildrenCategories(Category cat) {
54         return CategoryFactory.getChildrenCategories(cat);
55     }
56
57     public List JavaDoc<Category> getChildrenCategories(Inode inode) {
58         return InodeFactory.getChildrenClass(inode, Category.class);
59     }
60
61     public List JavaDoc<Category> getChildrenCategories(String JavaDoc inode) {
62         Inode inodeObj =new Inode();
63         inodeObj.setInode(inode);
64         return InodeFactory.getChildrenClass(inodeObj, Category.class);
65     }
66
67     public List JavaDoc<Category> getInodeCategories(String JavaDoc inode) {
68         Inode inodeObj =new Inode();
69         inodeObj.setInode(inode);
70         return InodeFactory.getParentsOfClass(inodeObj, Category.class);
71     }
72
73     public List JavaDoc<Category> getInodeCategories(Inode inodeObj) {
74         return InodeFactory.getParentsOfClass(inodeObj, Category.class);
75     }
76
77     /**
78      *
79      * @param parameterName
80      * value of the "name" attribute for the <select> html tag
81      * @param entityName
82      * name of the entity that have the categories
83      * @param childInode
84      * Inode value to obtain the selected categories
85      * @param displayTitle
86      * if do you want that the title display or not (Entity Name)
87      * @param multipleValues
88      * if the user could select multiple values
89      * @param size
90      * the height in entries that could be displayed
91      * @param readonly
92      * if the select is disable
93      * @return
94      */

95     @SuppressWarnings JavaDoc("unchecked")
96     public String JavaDoc getCategoriesByEntityName(String JavaDoc parameterName, String JavaDoc entityName, long childInode,
97             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
98         String JavaDoc[] selectCatsString = new String JavaDoc[0];
99         // Find the selected values
100
if (childInode != 0) {
101             Inode inode = InodeFactory.getInode(childInode, Inode.class);
102             List JavaDoc<Category> categories = InodeFactory.getParentsOfClass(inode, Category.class);
103             selectCatsString = new String JavaDoc[categories.size()];
104             for (int i = 0; i < categories.size(); i++) {
105                 selectCatsString[i] = Long.toString(categories.get(i).getInode());
106             }
107         }
108
109         return getCategoriesByEntityName(parameterName, entityName, selectCatsString, displayTitle, multipleValues,
110                 size, readonly);
111     }
112
113     /**
114      *
115      * @param parameterName
116      * value of the "name" attribute for the <select> html tag
117      * @param entityName
118      * name of the entity that have the categories
119      * @param selectedCategories
120      * Selected categories inodes
121      * @param displayTitle
122      * if do you want that the title display or not (Entity Name)
123      * @param multipleValues
124      * if the user could select multiple values
125      * @param size
126      * the height in entries that could be displayed
127      * @param readonly
128      * if the select is disable
129      * @return
130      */

131     public String JavaDoc getCategoriesByEntityName(String JavaDoc parameterName, String JavaDoc entityName, long[] selectedCategories,
132             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
133         String JavaDoc[] selectCatsString = new String JavaDoc[selectedCategories.length];
134         // Find the selected values
135
for (int i = 0; i < selectedCategories.length; i++) {
136             selectCatsString[i] = Long.toString(selectedCategories[i]);
137         }
138         return getCategoriesByEntityName(parameterName, entityName, selectCatsString, displayTitle, multipleValues,
139                 size, readonly);
140     }
141
142     /***************************************************************************
143      * This methods return the selects required for the categories of an entity
144      *
145      * @param parameterName
146      * value of the "name" attribute for the <select> html tag
147      * @param entityName
148      * name of the entity that have the categories
149      * @param selectCatsString
150      * Selected categories inodes
151      * @param displayTitle
152      * if do you want that the title display or not (Entity Name)
153      * @param multipleValues
154      * if the user could select multiple values
155      * @param size
156      * the height in entries that could be displayed
157      * @param readonly
158      * if the select is disable
159      * @return the the HTML that render the selects
160      */

161     public String JavaDoc getCategoriesByEntityName(String JavaDoc parameterName, String JavaDoc entityName, String JavaDoc[] selectCatsString,
162             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
163         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
164         // GETS THE ENTITY
165
Entity entity = EntityFactory.getEntity(entityName);
166         // GET ALL THE MAIN CATEGORIES FOR THIS ENTITY
167
List JavaDoc categories = EntityFactory.getEntityCategories(entity);
168         if (categories.size() > 0) {
169             buffer.append("<table>");
170             buffer.append("<tr>");
171             buffer.append("<td align='left' colspan='2'>");
172             buffer.append("<table border='0'>");
173             Iterator JavaDoc categoriesIterator = categories.iterator();
174             // /FOR EACH CATEGORY WE GET THE CHILDREN
175
while (categoriesIterator.hasNext()) {
176                 Category category = (Category) categoriesIterator.next();
177                 int count = InodeFactory.countChildrenOfClass(category, Category.class);
178                 if (count > 1) {
179                     buffer.append("<tr>");
180                     if (displayTitle) {
181                         buffer.append("<td valign='top'>");
182                         buffer.append("<B>" + category.getCategoryName() + ":</B>");
183                         buffer.append("</td>");
184                     }
185                     buffer.append("<td align='left'>");
186                     String JavaDoc multiple = (multipleValues ? "multiple='multiple'" : "");
187                     int sizeAux = (size == -1 ? count - 1 : size);
188                     String JavaDoc disabled = (readonly ? "disabled" : "");
189                     buffer.append("<select name='" + parameterName + "' size='" + sizeAux + "' " + multiple + " "
190                             + disabled + ">");
191                     buffer.append(UtilHTML.getSelectCategories(category, -1, selectCatsString));
192                     buffer.append("</select>");
193                     buffer.append("</td>");
194                     buffer.append("</tr>");
195                 }
196             }
197             buffer.append("</table>");
198             buffer.append("</td>");
199             buffer.append("</tr>");
200             buffer.append("</table>");
201         }
202         return buffer.toString();
203     }
204
205     public String JavaDoc getCategoriesByEntityNameTextMode(String JavaDoc parameterName, String JavaDoc entityName, long[] selectedCats,
206             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
207         String JavaDoc[] selectCatsString = new String JavaDoc[selectedCats.length];
208         // Find the selected values
209
for (int i = 0; i < selectedCats.length; i++) {
210             selectCatsString[i] = Long.toString(selectedCats[i]);
211         }
212         return getCategoriesByEntityNameTextMode(parameterName, entityName, selectCatsString, displayTitle,
213                 multipleValues, size, readonly);
214     }
215
216     @SuppressWarnings JavaDoc("unchecked")
217     public String JavaDoc getCategoriesByEntityNameTextMode(String JavaDoc parameterName, String JavaDoc entityName, long childInode,
218             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
219         String JavaDoc[] selectCatsString = new String JavaDoc[0];
220         // Find the selected values
221
if (childInode != 0) {
222             Inode inode = InodeFactory.getInode(childInode, Inode.class);
223             List JavaDoc<Category> categories = InodeFactory.getParentsOfClass(inode, Category.class);
224             selectCatsString = new String JavaDoc[categories.size()];
225             for (int i = 0; i < categories.size(); i++) {
226                 selectCatsString[i] = Long.toString(categories.get(i).getInode());
227             }
228         }
229
230         return getCategoriesByEntityNameTextMode(parameterName, entityName, selectCatsString, displayTitle,
231                 multipleValues, size, readonly);
232     }
233
234     public String JavaDoc getCategoriesByEntityNameTextMode(String JavaDoc parameterName, String JavaDoc entityName, String JavaDoc[] selectCatsString,
235             boolean displayTitle, boolean multipleValues, int size, boolean readonly) {
236         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
237         // GETS THE ENTITY
238
Entity entity = EntityFactory.getEntity(entityName);
239         // GET ALL THE MAIN CATEGORIES FOR THIS ENTITY
240
List JavaDoc categories = EntityFactory.getEntityCategories(entity);
241         if (categories.size() > 0) {
242             buffer.append("<table cellpadding=\"0\" cellspacing=\"0\">");
243             buffer.append("<tr>");
244             buffer.append("<td align='left' colspan='2'>");
245             buffer.append("<table cellpadding=\"0\" cellspacing=\"0\">");
246             Iterator JavaDoc categoriesIterator = categories.iterator();
247             // /FOR EACH CATEGORY WE GET THE CHILDREN
248
while (categoriesIterator.hasNext()) {
249                 Category category = (Category) categoriesIterator.next();
250                 int count = InodeFactory.countChildrenOfClass(category, Category.class);
251                 if (count > 1) {
252                     buffer.append("<tr>");
253                     if (displayTitle) {
254                         buffer.append("<td valign='top' nowrap>");
255                         buffer.append("<B>" + category.getCategoryName() + ":</B>&nbsp;&nbsp;");
256                         buffer.append("</td>");
257                     }
258                     buffer.append("<td align='left'>");
259                     // String multiple = (multipleValues ? "multiple='multiple'"
260
// : "");
261
// int sizeAux = (size == -1 ? count -1 : size);
262
// String disabled = (readonly ? "disabled" : "");
263
// String mode = "";
264
buffer.append("<table cellpadding=\"0\" cellspacing=\"0\">");
265                     buffer.append(UtilHTML.getSelectCategoriesTextMode(category, 1, selectCatsString));
266                     buffer.append("</table>");
267                     buffer.append("</td>");
268                     buffer.append("</tr>");
269                 }
270             }
271             buffer.append("</table>");
272             buffer.append("</td>");
273             buffer.append("</tr>");
274             buffer.append("</table>");
275         }
276         return buffer.toString();
277     }
278
279     /**
280      *
281      * @param parameterName
282      * value of the "name" attribute for the <select> html tag
283      * @param entityName
284      * name of the entity that have the categories
285      * @param selectedCategories
286      * Selected categories inodes
287      * @param displayTitle
288      * if do you want that the title display or not (Entity Name)
289      * @param multipleValues
290      * if the user could select multiple values
291      * @param size
292      * the height in entries that could be displayed
293      * @param readonly
294      * if the select is disable
295      * @return
296      */

297     public String JavaDoc getSimpleComboByEntityName(String JavaDoc parameterName, String JavaDoc entityName, long selectedCategory) {
298         String JavaDoc[] selectCatsString = new String JavaDoc[1];
299         selectCatsString[0] = Long.toString(selectedCategory);
300         return getCategoriesByEntityName(parameterName, entityName, selectCatsString, false, false, 1, false);
301     }
302
303     public String JavaDoc getSimpleCategoriesByEntityName(String JavaDoc parameterName, String JavaDoc entityName, long selectedCategory) {
304         String JavaDoc[] selectCatsString = new String JavaDoc[1];
305         selectCatsString[0] = Long.toString(selectedCategory);
306         return getCategoriesByEntityNameTextMode(parameterName, entityName, selectCatsString, false, false, 1, false);
307     }
308
309     public Category getCategoryByInode(String JavaDoc inode) {
310         return (Category) CategoryFactory.getCategory(inode);
311     }
312
313     public Category getCategoryByInode(long inode) {
314         return (Category) CategoryFactory.getCategory(inode);
315     }
316
317 }
318
Popular Tags