1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.portal.cms.repository;
17
18 import java.util.ArrayList;
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import nl.hippo.client.api.content.Document;
24 import nl.hippo.client.api.content.DocumentCollection;
25 import nl.hippo.client.api.content.DocumentPath;
26 import nl.hippo.portal.cms.CMSService;
27
28 public class DocumentTreeBuilder {
29
30 private static final String dasl =
31 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
32 "<d:searchrequest xmlns:h=\"http://hippo.nl/cms/1.0\" xmlns:d=\"DAV:\" xmlns:s=\"http://jakarta.apache.org/slide/\">" +
33 " <d:basicsearch>" +
34 " <d:select>" +
35 " <d:prop>" +
36 " <h:caption/>" +
37 " <h:UID/>" +
38 " <h:index/>" +
39 " <h:isIndexDocument/>" +
40 " <h:type/>" +
41 " <d:displayname/>" +
42 " <d:resourcetype/>" +
43 " <s:nrHits/> " +
44 " </d:prop>" +
45 " </d:select>" +
46 " <d:from>" +
47 " <d:scope>" +
48 " <d:href/>" +
49 " <d:depth>1</d:depth>" +
50 " <s:minimum-depth>1</s:minimum-depth>"+
51 " </d:scope>" +
52 " </d:from>" +
53 " </d:basicsearch>" +
54 "</d:searchrequest>";
55
56 public static int DEFAULT_MAX_DEPTH = 1;
57
58 private Comparator documentComparator = new DocumentComparator();
59 private int maxNavDepth = DEFAULT_MAX_DEPTH;
60 private CMSService repositoryService;
61
62 public DocumentTreeBuilder(CMSService repositoryService){
63 this.repositoryService=repositoryService;
64 }
65
66 private List getChildrenFor(DocumentPath parent){
67 DocumentCollection dc=repositoryService.search(parent,dasl);
68 if (dc!=null){
69 return dc.getDocuments();
70 }
71 return null;
72 }
73
74 private WebdavResource generate(Document currDoc, int level){
75 if (currDoc.getMetadata().isCollection()){
76 WebdavCollection newCollection = new WebdavCollection(currDoc);
77 if (level < maxNavDepth + 1){
78 List childDocs = getChildrenFor(currDoc.getPath());
79 if (childDocs!=null && childDocs.size() > 0){
80 List childResources = generateChildren(childDocs,level);
81 newCollection.addChildren(childResources);
82 }
83 }
84 return newCollection;
85 } else {
86 return new WebdavResource(currDoc);
87 }
88 }
89
90 private List generateChildren(List documents, int level){
91 List result = new ArrayList();
92 for (Iterator iter = documents.iterator(); iter.hasNext();) {
93 WebdavResource child = generate((Document) iter.next(),level+1);
94 if (child != null){
95 result.add(child);
96 }
97 }
98 return result;
99 }
100
101 public WebdavResource build(String source){
102 DocumentPath path = repositoryService.createRelativePath(source);
103 WebdavResource result = null;
104 Document rootDoc=repositoryService.get(path);
105 if (rootDoc != null && rootDoc.getMetadata().isCollection()){
106 result = generate(rootDoc,0);
107 }
108 return result;
109 }
110
111 private class DocumentComparator implements Comparator{
112
113 public int compare(Object o1, Object o2) {
114 if (o1 instanceof Document && o2 instanceof Document){
115 Document doc1=(Document)o1;
116 Document doc2=(Document)o2;
117 return doc1.getPath().getFullPath().compareTo(doc2.getPath().getFullPath());
118 }
119 return 0;
120 }
121
122 }
123
124 public int getMaxNavDepth() {
125 return maxNavDepth;
126 }
127
128 public void setMaxNavDepth(int maxNavDepth) {
129 this.maxNavDepth = maxNavDepth;
130 }
131 }