1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.controlx.search;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.search.Query;
38 import info.magnolia.cms.core.search.QueryResult;
39 import info.magnolia.cms.gui.query.SearchQuery;
40 import info.magnolia.context.MgnlContext;
41
42 import java.util.Collection;
43
44 import javax.jcr.RepositoryException;
45 import javax.jcr.query.InvalidQueryException;
46
47 import org.slf4j.LoggerFactory;
48 import org.slf4j.Logger;
49
50
51
52
53
54 public class RepositorySearchListModel extends AbstractSearchableListModel {
55 private static final Logger log = LoggerFactory.getLogger(RepositorySearchListModel.class);
56
57
58
59
60 private String repositoryId;
61
62
63
64
65 private String workspaceId;
66
67
68
69
70 private String nodeType = "nt:base";
71
72 private String resultNodeType = "mgnl:content";
73
74
75
76
77 private String searchPath;
78
79
80
81
82 protected SearchQuery query;
83
84
85
86
87 public RepositorySearchListModel(String repositoryId) {
88 this.repositoryId = repositoryId;
89 }
90
91
92
93
94 protected String buildQuery() {
95 QueryBuilder builder = getQueryBuilder();
96 return builder.getSQLStatement();
97 }
98
99 protected QueryBuilder getQueryBuilder() {
100 return new QueryBuilder(this);
101 }
102
103
104
105
106 protected QueryResult getResult(String statement) throws InvalidQueryException, RepositoryException {
107 Query q = MgnlContext.getQueryManager(this.repositoryId).createQuery(statement, Query.SQL);
108 QueryResult result = q.execute();
109 return result;
110 }
111
112
113
114
115 protected Collection getResult() throws Exception {
116 String query = buildQuery();
117 if(log.isDebugEnabled()){
118 log.debug("query: " + query);
119 }
120 QueryResult result = this.getResult(query);
121 return getResult(result);
122 }
123
124
125
126
127 protected Collection getResult(QueryResult result) {
128 return result.getContent(this.getResultNodeType());
129 }
130
131
132
133
134 protected String resolveId(int index, Object value) {
135 if(value instanceof Content){
136 return ((Content)value).getUUID();
137 }
138 return super.resolveId(index, value);
139 }
140
141
142
143
144
145 public String getRepositoryId() {
146 return repositoryId;
147 }
148
149
150
151
152
153 public void setRepositoryId(String repositoryId) {
154 this.repositoryId = repositoryId;
155 }
156
157
158
159
160
161 public String getWorkspaceId() {
162 return workspaceId;
163 }
164
165
166
167
168
169 public void setWorkspaceId(String workspaceId) {
170 this.workspaceId = workspaceId;
171 }
172
173
174
175
176
177 public String getNodeType() {
178 return nodeType;
179 }
180
181
182
183
184
185 public void setNodeType(String selectNodeType) {
186 this.nodeType = selectNodeType;
187 }
188
189
190
191
192
193 public String getSearchPath() {
194 return searchPath;
195 }
196
197
198
199
200
201 public void setSearchPath(String searchPath) {
202 this.searchPath = searchPath;
203 }
204
205
206
207
208
209 public void setQuery(SearchQuery query) {
210 this.query = query;
211 }
212
213
214
215
216
217 public SearchQuery getQuery() {
218
219 if(this.query == null){
220 this.query = new SearchQuery();
221 }
222 return this.query;
223 }
224
225 public void setResultNodeType(String resultNodeType) {
226 this.resultNodeType = resultNodeType;
227 }
228
229 public String getResultNodeType() {
230 return resultNodeType;
231 }
232
233 }