View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Sameer Charles $Id:RepositorySearchListModel.java 2544 2006-04-04 12:47:32Z philipp $
53   */
54  public class RepositorySearchListModel extends AbstractSearchableListModel {
55      private static final Logger log = LoggerFactory.getLogger(RepositorySearchListModel.class);
56  
57      /**
58       * repository id
59       */
60      private String repositoryId;
61  
62      /**
63       * workspace Id, if no workspace is defined - default is used which has the same name as repository name
64       */
65      private String workspaceId;
66  
67      /**
68       * select from node type (optional)
69       */
70      private String nodeType = "nt:base";
71  
72      private String resultNodeType = "mgnl:content";
73  
74      /**
75       * search path (optional)
76       */
77      private String searchPath;
78  
79      /**
80       * search query to be used by sub implementation
81       */
82      protected SearchQuery query;
83  
84      /**
85       * default constructor
86       */
87      public RepositorySearchListModel(String repositoryId) {
88          this.repositoryId = repositoryId;
89      }
90  
91      /**
92       * Returns the jcr query statement used by the model.
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      * Executes the query statement and returns the QueryResult.
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      * Creates the jcr query and executes it.
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      * Gets the items from the query (possibility to post filter)
127      */
128     protected Collection getResult(QueryResult result) {
129         return result.getContent(this.getResultNodeType());
130     }
131 
132     /**
133      * Returns the uuid of the node
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      * get repository Id
145      * @return repository id
146      */
147     public String getRepositoryId() {
148         return repositoryId;
149     }
150 
151     /**
152      * set repository id
153      * @param repositoryId
154      */
155     public void setRepositoryId(String repositoryId) {
156         this.repositoryId = repositoryId;
157     }
158 
159     /**
160      * get workspace Id
161      * @return workspace id
162      */
163     public String getWorkspaceId() {
164         return workspaceId;
165     }
166 
167     /**
168      * set workspace Id
169      * @param workspaceId
170      */
171     public void setWorkspaceId(String workspaceId) {
172         this.workspaceId = workspaceId;
173     }
174 
175     /**
176      * get select node type, query will be executed only on these if set
177      * @return nodeType name
178      */
179     public String getNodeType() {
180         return nodeType;
181     }
182 
183     /**
184      * set select node type value, query will be executed only on these if set
185      * @param nodeType
186      */
187     public void setNodeType(String selectNodeType) {
188         this.nodeType = selectNodeType;
189     }
190 
191     /**
192      * get jcr path, under which search will be executed
193      * @return path
194      */
195     public String getSearchPath() {
196         return searchPath;
197     }
198 
199     /**
200      * set jcr path, under which search will be executed
201      * @param searchPath
202      */
203     public void setSearchPath(String searchPath) {
204         this.searchPath = searchPath;
205     }
206 
207     /**
208      * set Query
209      * @param query
210      */
211     @Override
212     public void setQuery(SearchQuery query) {
213         this.query = query;
214     }
215 
216     /**
217      * get query
218      * @return query
219      */
220     @Override
221     public SearchQuery getQuery() {
222         // this is needed in case the list page is not a searchable list
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 }