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 @Override
116 protected Collection getResult() throws Exception {
117 String query = buildQuery();
118 if(log.isDebugEnabled()){
119 log.debug("query: " + query);
120 }
121 QueryResult result = this.getResult(query);
122 return getResult(result);
123 }
124
125
126
127
128 protected Collection getResult(QueryResult result) {
129 return result.getContent(this.getResultNodeType());
130 }
131
132
133
134
135 @Override
136 protected String resolveId(int index, Object value) {
137 if(value instanceof Content){
138 return ((Content)value).getUUID();
139 }
140 return super.resolveId(index, value);
141 }
142
143
144
145
146
147 public String getRepositoryId() {
148 return repositoryId;
149 }
150
151
152
153
154
155 public void setRepositoryId(String repositoryId) {
156 this.repositoryId = repositoryId;
157 }
158
159
160
161
162
163 public String getWorkspaceId() {
164 return workspaceId;
165 }
166
167
168
169
170
171 public void setWorkspaceId(String workspaceId) {
172 this.workspaceId = workspaceId;
173 }
174
175
176
177
178
179 public String getNodeType() {
180 return nodeType;
181 }
182
183
184
185
186
187 public void setNodeType(String selectNodeType) {
188 this.nodeType = selectNodeType;
189 }
190
191
192
193
194
195 public String getSearchPath() {
196 return searchPath;
197 }
198
199
200
201
202
203 public void setSearchPath(String searchPath) {
204 this.searchPath = searchPath;
205 }
206
207
208
209
210
211 @Override
212 public void setQuery(SearchQuery query) {
213 this.query = query;
214 }
215
216
217
218
219
220 @Override
221 public SearchQuery getQuery() {
222
223 if(this.query == null){
224 this.query = new SearchQuery();
225 }
226 return this.query;
227 }
228
229 public void setResultNodeType(String resultNodeType) {
230 this.resultNodeType = resultNodeType;
231 }
232
233 public String getResultNodeType() {
234 return resultNodeType;
235 }
236
237 }