KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.dotmarketing.viewtools;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.HashSet JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.apache.commons.beanutils.PropertyUtils;
12
13 //import org.apache.lucene.analysis.WhitespaceAnalyzer;
14
import com.dotmarketing.lucene.DotAnalyzer;
15
16 import org.apache.lucene.queryParser.QueryParser;
17 import org.apache.lucene.search.Query;
18 import org.apache.velocity.tools.view.tools.ViewTool;
19
20 import com.dotmarketing.beans.Identifier;
21 import com.dotmarketing.cache.FieldsCache;
22 import com.dotmarketing.cache.StructureCache;
23 import com.dotmarketing.factories.IdentifierFactory;
24 import com.dotmarketing.factories.InodeFactory;
25 import com.dotmarketing.portlets.categories.factories.CategoryFactory;
26 import com.dotmarketing.portlets.categories.model.Category;
27 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory;
28 import com.dotmarketing.portlets.contentlet.model.Contentlet;
29 import com.dotmarketing.portlets.structure.factories.RelationshipFactory;
30 import com.dotmarketing.portlets.structure.model.Field;
31 import com.dotmarketing.portlets.structure.model.Relationship;
32 import com.dotmarketing.portlets.structure.model.Structure;
33 import com.dotmarketing.util.Config;
34 import com.dotmarketing.util.Logger;
35 import com.dotmarketing.util.LuceneHits;
36 import com.dotmarketing.util.LuceneUtils;
37
38 public class ContentsWebAPI implements ViewTool {
39     
40     // private HttpServletRequest request;
41

42     public void init(Object JavaDoc obj) {
43         // ViewContext context = (ViewContext) obj;
44
// this.request = context.getRequest();
45
}
46     
47     /**
48      *
49      * @param cont
50      * @return
51      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
52      */

53     public Identifier getContentIdentifier(Contentlet cont) {
54         return (Identifier) IdentifierFactory.getIdentifierByInode(cont);
55     }
56     
57     /**
58      *
59      * @param inode
60      * @return
61      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
62      */

63     public Contentlet getContentByInode(long inode) {
64         return (Contentlet) InodeFactory.getInode(inode, Contentlet.class);
65     }
66     
67     /**
68      *
69      * @param inode
70      * @return
71      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
72      */

73     public Contentlet getContentByInode(String JavaDoc inode) {
74         return (Contentlet) InodeFactory.getInode(inode, Contentlet.class);
75     }
76     
77     /**
78      *
79      * @param structureType
80      * @return
81      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
82      */

83     public Structure getStructureByType(String JavaDoc structureType) {
84         return StructureCache.getStructureByType(structureType);
85     }
86     
87     /**
88      *
89      * @param structure
90      * @param category
91      * @param maxResults
92      * @return
93      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
94      */

95     public List JavaDoc<Contentlet> getLastestContents(Structure structure, Category category, int maxResults) {
96         return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc", maxResults);
97     }
98     
99     /**
100      *
101      * @param structureType
102      * @param categoryName
103      * @param maxResults
104      * @return
105      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
106      */

107     public List JavaDoc<Contentlet> getLastestContents(String JavaDoc structureType, String JavaDoc categoryName, int maxResults) {
108         Category category = CategoryFactory.getCategoryByName(categoryName);
109         Structure structure = StructureCache.getStructureByType(structureType);
110         return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc", maxResults);
111     }
112     
113     /**
114      *
115      * @param structure
116      * @param category
117      * @return
118      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
119      */

120     public List JavaDoc<Contentlet> getLastestContents(Structure structure, Category category) {
121         return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc");
122     }
123     
124     /**
125      *
126      * @param structureType
127      * @param categoryName
128      * @return
129      * @deprecated this methods was deprecated because it hits the database, try to use the lucene search methods instead.
130      */

131     public List JavaDoc<Contentlet> getLastestContents(String JavaDoc structureType, String JavaDoc categoryName) {
132         Category category = CategoryFactory.getCategoryByName(categoryName);
133         Structure structure = StructureCache.getStructureByType(structureType);
134         return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date desc");
135     }
136     
137     /**
138      * This methods retrieves the field of an structure based on his presentation name
139      * @param st Structure owner of the field
140      * @param fieldName The presentation name of the field
141      * @return The field found
142      * @deprecated This method was deprecated because it uses the presentation name of the field
143      * we encourage the use of the logical name of the field instead
144      */

145     public Field getFieldByName(Structure st, String JavaDoc fieldName) {
146         List JavaDoc<Field> fields = FieldsCache.getFieldsByStructureInode(st.getInode());
147         for (Field f : fields) {
148             if (f.getFieldName().equals(fieldName))
149                 return f;
150         }
151         return new Field();
152     }
153     
154     /**
155      * This methods retrieves the field of an structure based on his presentation name
156      * @param structureType The structure type name
157      * @param fieldName The presentation name of the field
158      * @return The field found
159      * @deprecated This method was deprecated because it uses the presentation name of the field
160      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
161      */

162     public Field getFieldByName(String JavaDoc structureType, String JavaDoc fieldName) {
163         Structure st = StructureCache.getStructureByType(structureType);
164         return getFieldByName(st, fieldName);
165     }
166     
167     /**
168      * This methods retrieves the field of an structure based on his presentation name
169      * @param structureInode The structure inode
170      * @param fieldName The presentation name of the field
171      * @return The field found
172      * @deprecated This method was deprecated because it uses the presentation name of the field
173      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
174      */

175     public Field getFieldByName(long structureInode, String JavaDoc fieldName) {
176         Structure st = StructureCache.getStructureByInode(structureInode);
177         return getFieldByName(st, fieldName);
178     }
179     
180     /**
181      * This methods retrieves the field of an structure based on his logical name. E.G. Last_Name
182      * @param st Structure owner of the field
183      * @param fieldName The presentation name of the field
184      * @return The field found, an empty field if it wasn't found
185      */

186     public Field getFieldByLogicalName(Structure st, String JavaDoc fieldName) {
187         List JavaDoc<Field> fields = FieldsCache.getFieldsByStructureInode(st.getInode());
188         for (Field f : fields) {
189             if (f.getFieldContentlet().equals(fieldName))
190                 return f;
191         }
192         return new Field();
193     }
194     
195     /**
196      * This methods retrieves the field of an structure based on his logical name. E.G. Last_Name
197      * @param structureType The structure type name
198      * @param fieldName The presentation name of the field
199      * @return The field found, an empty field if it wasn't found
200      */

201     public Field getFieldByLogicalName(String JavaDoc structureType, String JavaDoc fieldName) {
202         Structure st = StructureCache.getStructureByType(structureType);
203         return getFieldByLogicalName(st, fieldName);
204     }
205     
206     /**
207      * This methods retrieves the field of an structure based on his logical name. E.G. Last_Name
208      * @param structureInode The structure inode
209      * @param fieldName The presentation name of the field
210      * @return The field found, an empty field if it wasn't found
211      */

212     public Field getFieldByLogicalName(long structureInode, String JavaDoc fieldName) {
213         Structure st = StructureCache.getStructureByInode(structureInode);
214         return getFieldByLogicalName(st, fieldName);
215     }
216     
217     /**
218      *
219      * @param fieldName
220      * @param content
221      * @return
222      * @deprecated Try using the #getContentDetail or #getContentDetailByIdentifier macros to retrieve the fields of a content.
223      */

224     public Object JavaDoc getFieldValue(String JavaDoc fieldName, Contentlet content) {
225         Structure structure = StructureCache.getStructureByInode(content.getStructureInode());
226         Field theField = null;
227         List JavaDoc<Field> fields = FieldsCache.getFieldsByStructureInode(structure.getInode());
228         for (Field field : fields) {
229             if (field.getFieldName().equals(fieldName)) {
230                 theField = field;
231                 break;
232             }
233         }
234         if (theField == null)
235             return null;
236         try {
237             return PropertyUtils.getProperty(content, theField.getFieldContentlet());
238         } catch (Exception JavaDoc e) {
239             Logger.error(ContentletFactory.class, e.getMessage(), e);
240             return null;
241         }
242     }
243     
244     
245     /**
246      *
247      * @param structure
248      * @param fieldName
249      * @param fieldValue
250      * @return
251      * @deprecated This method was deprecated because it uses the presentation name of the field
252      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
253      */

254     public List JavaDoc<Contentlet> getContentsByStructureAndFieldValue(Structure structure, String JavaDoc fieldName, String JavaDoc fieldValue) {
255         return ContentletFactory.getContentletsByStuctureAndFieldValue(structure, fieldName, fieldValue, "mod_date", 0);
256     }
257     
258     /**
259      *
260      * @param structure
261      * @param orderFieldName
262      * @param direction
263      * @param rowNumber
264      * @return
265      * @deprecated This method was deprecated because it uses the presentation name of the field
266      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
267      */

268     public List JavaDoc<Contentlet> getContentletsByStructureAndOrder(Structure structure, String JavaDoc orderFieldName,
269             String JavaDoc direction, int rowNumber) {
270         return ContentletFactory.getContentletsByStructureAndOrder(structure, orderFieldName, direction, rowNumber);
271     }
272     
273     /**
274      *
275      * @param structureType
276      * @param categoryName
277      * @return
278      * @deprecated This method was deprecated because it uses the presentation name of the field
279      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
280      */

281     public List JavaDoc<Contentlet> getContents(String JavaDoc structureType, String JavaDoc categoryName) {
282         Category category = CategoryFactory.getCategoryByName(categoryName);
283         Structure structure = StructureCache.getStructureByType(structureType);
284         return ContentletFactory.getContentletsByStuctureAndCategory(structure, category, "mod_date");
285     }
286     
287     /**
288      * This method get a contentlet list from a category list and strutucture
289      *
290      * @param structureType
291      * name of the structure
292      * @param categoryInodelist
293      * categories inode list i.e (123,456,)
294      * @param orderBy
295      * display order
296      * @return
297      * @deprecated This method was deprecated because it uses the presentation name of the field
298      * we encourage the use of the logical name of the field instead @see getFieldByLogicalName
299      */

300     public List JavaDoc<Contentlet> getContentsByStructureAndCategoryList(String JavaDoc structureType, String JavaDoc categoryInodelist,
301             List JavaDoc conditionFields, String JavaDoc condition, String JavaDoc orderFieldName, String JavaDoc direction, int pageNumber,
302             int maxResults) {
303         Structure structure = StructureCache.getStructureByType(structureType);
304         return ContentletFactory.getContentsByStructureAndCategoryList(structure, categoryInodelist, conditionFields,
305                 condition, orderFieldName, direction, pageNumber, maxResults);
306         
307     }
308     
309     /**
310      * This method searches inside the lucene index for contents, this methods
311      * uses the contentlet database names of the fields in the condition and the
312      * sort by field so you should convert the display names of the fields to
313      * the contentlet fields names before call this method
314      *
315      * @param structureType
316      * The name or inode of the structure, E.G: "Web Page Content" or
317      * 12354
318      * @param luceneCondition
319      * The lucene query E.G. +text1:test text2:you
320      * @param sortBy
321      * The field used to sort you can also use the desc or asc
322      * suffix, E.G: text1 desc
323      * @param pageStr
324      * The page you want to display
325      * @param rowsPerPage
326      * The number of records you want to show per page.
327      * @return
328      */

329     public HashMap JavaDoc searchWithLuceneQuery(String JavaDoc structureType, String JavaDoc luceneCondition, String JavaDoc sortBy, String JavaDoc pageStr,
330             String JavaDoc rowsPerPage) {
331         
332         long structInode = 0;
333         Structure structure = null;
334         try {
335             structInode = Long.parseLong(structureType);
336             structure = new Structure();
337             structure.setInode(structInode);
338         } catch (Exception JavaDoc e) {
339             structure = StructureCache.getStructureByType(structureType);
340         }
341         
342         Logger.debug(ContentsWebAPI.class, "search: luceneCondition: " + luceneCondition + ", sortBy: " + sortBy
343                 + ", page: " + pageStr);
344         int perPage = Integer.parseInt(rowsPerPage);
345         int page = Integer.parseInt(pageStr);
346         int offset = (page - 1) * perPage;
347         LuceneHits assets = null;
348         HashMap JavaDoc retMap = new HashMap JavaDoc();
349         
350         assets = ContentletFactory.indexSearch(structure, luceneCondition, false, offset, perPage, sortBy);
351         
352         int totalRecords = assets.getTotal();
353         
354         retMap.put("assets", assets);
355         int totalPages = (int) Math.ceil((double) totalRecords / (double) perPage);
356         retMap.put("total_records", String.valueOf(totalRecords));
357         retMap.put("total_pages", String.valueOf(totalPages));
358         retMap.put("total_records_int", totalRecords);
359         retMap.put("total_pages_int", totalPages);
360         retMap.put("has_next_page", page < totalPages);
361         retMap.put("has_previous_page", page > 1);
362         
363         return retMap;
364         
365     }
366     
367     public HashMap JavaDoc searchWithLuceneQuery(String JavaDoc structureType, String JavaDoc luceneCondition, String JavaDoc sortBy, int maxResults) {
368         int page = 1;
369         int pageSize = -1;
370         return searchWithLuceneQuery(structureType, luceneCondition, sortBy, maxResults, page, pageSize);
371     }
372     
373     public HashMap JavaDoc searchWithLuceneQuery(String JavaDoc structureType, String JavaDoc luceneCondition, String JavaDoc sortBy, int maxResults,
374             int page, int pageSize) {
375         /*
376          * We avoid a db hit if we pass the structure inode
377          */

378         
379         long structInode = 0;
380         Structure structure = null;
381         try {
382             structInode = Long.parseLong(structureType);
383             structure = new Structure();
384             structure.setInode(structInode);
385         } catch (Exception JavaDoc e) {
386             structure = StructureCache.getStructureByType(structureType);
387         }
388         
389         int offSet = 0;
390         if (pageSize > 0) {
391             offSet = (page - 1) * pageSize;
392         }
393         Logger.debug(ContentsWebAPI.class, "search: luceneCondition: " + luceneCondition + ", sortBy: " + sortBy
394                 + ", max results: " + maxResults);
395         LuceneHits assets = null;
396         HashMap JavaDoc retMap = new HashMap JavaDoc();
397         
398         assets = ContentletFactory.indexSearch(structure, luceneCondition, false, offSet, maxResults, sortBy);
399         
400         int totalRecords = assets.getTotal();
401         
402         retMap.put("assets", assets);
403         retMap.put("total_records", String.valueOf(totalRecords));
404         
405         return retMap;
406         
407     }
408     
409     
410     /**
411      * This methods retrieves the disk paths for a list of contents hits retrieved from lucene
412      * this method is used in the dynamic containers code to get the list of contents paths
413      * @param assets The assets hits
414      * @param working True if you want the working path to the content file, false if you the live path to the file
415      * @return A list of paths to the contents
416      */

417     public List JavaDoc<String JavaDoc> getContentsPathsFromLuceneHits(LuceneHits assets, boolean working) {
418         ArrayList JavaDoc<String JavaDoc> paths = new ArrayList JavaDoc<String JavaDoc>();
419         for (int i = 0; i < assets.length(); i++) {
420             Logger.debug(this, "getContentsPathsFromLuceneHits: Adding asset path: "
421                     + (working ? "/working/" : "/live/") + assets.doc(i).get("identifier") + "_"
422                     + assets.doc(i).get("languageId") + "." + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION"));
423             paths.add((working ? "/working/" : "/live/") + assets.doc(i).get("identifier") + "_"
424                     + assets.doc(i).get("languageId") + "." + Config.getStringProperty("VELOCITY_CONTENT_EXTENSION"));
425             
426         }
427         return paths;
428     }
429     
430     public static List JavaDoc<Category> getContentletCategories(String JavaDoc inode) {
431         Contentlet content = (Contentlet) InodeFactory.getInode(inode, Contentlet.class);
432         return (List JavaDoc<Category>) InodeFactory.getParentsOfClass(content, Category.class);
433     }
434     
435     // Relationships methods
436
// We need to make the relationship not database dependant
437

438     /**
439      * This method retrieves the relationship by relation type value =
440      * relationship name
441      */

442     public Relationship getRelationshipByName(String JavaDoc relationshipName) {
443         return RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName);
444     }
445     
446     /**
447      * This methods gets the list of all the relationship objects associated to
448      * the structure of the contentlet as a parent or a child
449      *
450      * @param cont
451      * The contentlet
452      * @return A list of relationship objects
453      */

454     public List JavaDoc<Relationship> getRelationshipsOfContentlet(Contentlet cont) {
455         return getRelationshipsOfContentlet(cont.getInode());
456     }
457     
458     public List JavaDoc<Relationship> getRelationshipsOfContentlet(long contentletInode) {
459         return getRelationshipsOfContentlet(((Long JavaDoc) contentletInode).toString());
460     }
461     
462     public List JavaDoc<Relationship> getRelationshipsOfContentlet(String JavaDoc contentletInode) {
463         Contentlet cont = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
464         return RelationshipFactory.getAllRelationshipsByStructure(cont.getStructure());
465     }
466     
467     /**
468      * This gets the list of all the relationship objects associated to the
469      * structure of the contentlet
470      *
471      * @param cont
472      * The contentlet
473      * @param hasParent
474      * true If you find the relations where the contentlet is parent,
475      * false If you find the relations where the contentlet is child
476      * @return A list of relationship objects
477      */

478     public List JavaDoc<Relationship> getRelationshipsOfContentlet(Contentlet cont, boolean hasParent) {
479         return getRelationshipsOfContentlet(cont.getInode(), hasParent);
480     }
481     
482     public List JavaDoc<Relationship> getRelationshipsOfContentlet(long contentletInode, boolean hasParent) {
483         return getRelationshipsOfContentlet(((Long JavaDoc) contentletInode).toString(), hasParent);
484     }
485     
486     public List JavaDoc<Relationship> getRelationshipsOfContentlet(String JavaDoc contentletInode, boolean hasParent) {
487         Contentlet cont = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
488         return RelationshipFactory.getAllRelationshipsByStructure(cont.getStructure(), hasParent);
489     }
490     
491     /**
492      * This gets the list of all the relationship objects associated to the
493      * structure of the contentlet
494      *
495      * @param cont
496      * The contentlet
497      * @param hasParent
498      * true If you find the relations where the contentlet is parent,
499      * false If you find the relations where the contentlet is child
500      * @return A list of relationship objects
501      */

502     public List JavaDoc<Relationship> getRelationshipsOfStructure(Structure st, boolean hasParent) {
503         return getRelationshipsOfStructure(st.getInode(), hasParent);
504     }
505     
506     public List JavaDoc<Relationship> getRelationshipsOfStructure(long structureInode, boolean hasParent) {
507         return getRelationshipsOfStructure(((Long JavaDoc) structureInode).toString(), hasParent);
508     }
509     
510     public List JavaDoc<Relationship> getRelationshipsOfStructure(String JavaDoc structureInode, boolean hasParent) {
511         Structure st = (Structure) InodeFactory.getInode(structureInode, Structure.class);
512         return RelationshipFactory.getAllRelationshipsByStructure(st, hasParent);
513     }
514     
515     /**
516      * This methods retrieve the list of related contentlets in a relationship
517      * of a given contentlet
518      *
519      * @param relationship
520      * The relationship
521      * @param cont
522      * The contentlet
523      * @param hasParent
524      * true Find the related children contentlets where the given
525      * contentlet is the parent, false Find the related parents
526      * contentlets where the given contentlet is a child
527      * @return The list of related contentlets
528      */

529     public List JavaDoc<Contentlet> getRelatedContentlets(Relationship relationship, Contentlet contentlet, boolean hasParent) {
530         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,"");
531     }
532     
533     public List JavaDoc<Contentlet> getRelatedContentlets(long relationshipInode, long contentletInode, boolean hasParent) {
534         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
535         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
536         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,"");
537     }
538     
539     public List JavaDoc<Contentlet> getRelatedContentlets(String JavaDoc relationshipInode, String JavaDoc contentletInode, boolean hasParent) {
540         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
541         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
542         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true,"");
543     }
544     
545     public List JavaDoc<Contentlet> getRelatedChildContent(String JavaDoc relationshipName, String JavaDoc contentletInode) {
546         Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName);
547         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
548         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, true, true,"");
549     }
550     
551     public List JavaDoc<Contentlet> getRelatedParentContent(String JavaDoc relationshipName, String JavaDoc contentletInode) {
552         Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName);
553         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
554         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, false, true,"");
555     }
556     
557     public List JavaDoc<Contentlet> getRelatedSibblingContent(String JavaDoc relationshipName, String JavaDoc contentletInode) {
558         Relationship relationship = RelationshipFactory.getRelationshipByRelationTypeValue(relationshipName);
559         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
560         Set JavaDoc<Contentlet> contSet = new HashSet JavaDoc();
561         contSet.addAll(RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, false));
562         contSet.addAll(RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, true));
563         List JavaDoc<Contentlet> conts = new ArrayList JavaDoc();
564         conts.addAll(contSet);
565         return conts;
566     }
567     
568     public List JavaDoc<Contentlet> getRelatedContentletsByCondition(String JavaDoc relationshipInode, String JavaDoc contentletInode, boolean hasParent, String JavaDoc condition) {
569         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
570         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
571         return RelationshipFactory.getAllRelationshipRecords(relationship, contentlet, hasParent, true, condition);
572     }
573     
574     /**
575      * This methods checks if the given contentlet has the role of parent of the
576      * given relationship
577      *
578      * @param contentlet
579      * The contentlet
580      * @param relationship
581      * The relationship
582      * @return true If the contentlet has the role of parent, false otherwise
583      */

584     public boolean isParentOfTheRelationship(Contentlet contentlet, Relationship relationship) {
585         Structure contStructure = contentlet.getStructure();
586         return relationship.getParentStructure().getInode() == contStructure.getInode();
587     }
588     
589     public boolean isParentOfTheRelationship(long contentletInode, long relationshipInode) {
590         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
591         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
592         return isParentOfTheRelationship(contentlet, relationship);
593     }
594     
595     public boolean isParentOfTheRelationship(String JavaDoc contentletInode, String JavaDoc relationshipInode) {
596         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
597         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
598         return isParentOfTheRelationship(contentlet, relationship);
599     }
600     
601     /**
602      * This methods checks if the given contentlet has the role of child of the
603      * given relationship
604      *
605      * @param contentlet
606      * The contentlet
607      * @param relationship
608      * The relationship
609      * @return true If the contentlet has the role of child, false otherwise
610      */

611     public boolean isChildOfTheRelationship(Contentlet contentlet, Relationship relationship) {
612         Structure contStructure = contentlet.getStructure();
613         return relationship.getChildStructure().getInode() == contStructure.getInode();
614     }
615     
616     public boolean isChildOfTheRelationship(long contentletInode, long relationshipInode) {
617         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
618         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
619         return isChildOfTheRelationship(contentlet, relationship);
620     }
621     
622     public boolean isChildOfTheRelationship(String JavaDoc contentletInode, String JavaDoc relationshipInode) {
623         Relationship relationship = (Relationship) InodeFactory.getInode(relationshipInode, Relationship.class);
624         Contentlet contentlet = (Contentlet) InodeFactory.getInode(contentletInode, Contentlet.class);
625         return isChildOfTheRelationship(contentlet, relationship);
626     }
627     /*
628      * Used to pull dynamic lists of content in the form of maps
629      * for the front end of the web site. This is used by the
630      * #pullContent macro.
631      */

632     public List JavaDoc pullContent(String JavaDoc query, String JavaDoc lim, String JavaDoc sortBy) {
633         int limit = 0;
634         List JavaDoc<Map JavaDoc> l = new ArrayList JavaDoc<Map JavaDoc>();
635         LuceneHits hits = new LuceneHits();
636         
637         int offset = 0;
638         
639         try {
640             limit = Integer.parseInt(lim);
641             if(limit == 0){
642                 offset = -1;
643             }
644             
645         } catch (Exception JavaDoc e) {
646             return l;
647         }
648         
649         try {
650             
651             query = LuceneUtils.findAndReplaceQueryDates(query);
652             
653 // QueryParser parser = new QueryParser("", new WhitespaceAnalyzer());
654
QueryParser parser = new QueryParser("", new DotAnalyzer());
655             
656             Query luceneQuery = parser.parse(query);
657             hits.setLuceneQuery(luceneQuery.toString());
658             hits = LuceneUtils.searchInCurrentIndex(luceneQuery, offset, limit, sortBy);
659             
660             
661             
662         } catch (Exception JavaDoc ex) {
663             Logger.error(this.getClass(), "indexSearch: Error Searching Contentlets - lucene query: " + query, ex);
664         }
665         
666         /**
667          * when the limit is 0, set the limit to the size of the lucene result search
668          */

669         if(limit == 0){
670             limit = hits.getTotal();
671         }
672         
673         for (int i = 0; i < hits.getTotal(); i++) {
674             if(i==limit){
675                 break;
676             }
677             try{
678                 
679                 Map JavaDoc<String JavaDoc, Object JavaDoc> hm = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
680                 hm.put("inode", hits.doc(i).get("inode"));
681                 hm.put("identifier", hits.doc(i).get("identifier"));
682                 l.add(hm);
683                 
684             }
685             catch(Exception JavaDoc e){}
686         }
687         return l;
688         
689     }
690     
691     public List JavaDoc randomizeList(List JavaDoc list, String JavaDoc limit){
692         int i = 0;
693         try {
694             i = Integer.parseInt(limit);
695         }catch(Exception JavaDoc e){
696             i = list.size();
697         }
698         
699         if(list.size() > 0) {
700             Collections.shuffle(list);
701             return (list.size() > i ? list.subList(0,i):list);
702         }else
703            return list;
704     }
705     
706     /*
707      * Used to pull dynamic lists of content in the form of maps
708      * for the front end of the web site using pagination. This is used by the
709      * #pageContent macro.
710      */

711     public List JavaDoc pageContent(String JavaDoc query, String JavaDoc sortBy, String JavaDoc perPage, String JavaDoc currentPageNumber) {
712         
713         int pageNumber = 1;
714         try {
715             pageNumber = Integer.parseInt(currentPageNumber);
716         }catch(Exception JavaDoc e) {}
717         
718         int displayPerPage = Config.getIntProperty("PER_PAGE");
719         try {
720             displayPerPage = Integer.parseInt(perPage);
721         }catch(Exception JavaDoc e) {}
722         
723         int minIndex = (pageNumber - 1) * displayPerPage;
724         int maxIndex = displayPerPage * pageNumber;
725         
726         int limit = 0;
727                 
728         List JavaDoc<Map JavaDoc> l = new ArrayList JavaDoc<Map JavaDoc>();
729         LuceneHits hits = new LuceneHits();
730         
731         try {
732             
733             query = LuceneUtils.findAndReplaceQueryDates(query);
734             
735 // QueryParser parser = new QueryParser("", new WhitespaceAnalyzer());
736
QueryParser parser = new QueryParser("", new DotAnalyzer());
737             
738             Query luceneQuery = parser.parse(query);
739             hits.setLuceneQuery(luceneQuery.toString());
740             hits = LuceneUtils.searchInCurrentIndex(luceneQuery, -1, limit, sortBy);
741             
742             
743             
744         } catch (Exception JavaDoc ex) {
745             Logger.error(this.getClass(), "indexSearch: Error Searching Contentlets - lucene query: " + query, ex);
746         }
747         
748         for (int i = minIndex; i < hits.getTotal(); i++) {
749             if(i==maxIndex){
750                 break;
751             }
752             try{
753                 
754                 Map JavaDoc<String JavaDoc, Object JavaDoc> hm = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
755                 hm.put("inode", hits.doc(i).get("inode"));
756                 hm.put("identifier", hits.doc(i).get("identifier"));
757                 l.add(hm);
758                 
759             }
760             catch(Exception JavaDoc e){}
761         }
762         return l;
763         
764     }
765     
766     public Map JavaDoc getEmptyMap() {
767         return new HashMap JavaDoc();
768     }
769     
770     public List JavaDoc getEmptyList() {
771         return new ArrayList JavaDoc();
772     }
773     
774 }
Popular Tags