View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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     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      * Gets the items from the query (possibility to post filter)
126      */
127     protected Collection getResult(QueryResult result) {
128         return result.getContent(this.getResultNodeType());
129     }
130 
131     /**
132      * Returns the uuid of the node
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      * get repository Id
143      * @return repository id
144      */
145     public String getRepositoryId() {
146         return repositoryId;
147     }
148 
149     /**
150      * set repository id
151      * @param repositoryId
152      */
153     public void setRepositoryId(String repositoryId) {
154         this.repositoryId = repositoryId;
155     }
156 
157     /**
158      * get workspace Id
159      * @return workspace id
160      */
161     public String getWorkspaceId() {
162         return workspaceId;
163     }
164 
165     /**
166      * set workspace Id
167      * @param workspaceId
168      */
169     public void setWorkspaceId(String workspaceId) {
170         this.workspaceId = workspaceId;
171     }
172 
173     /**
174      * get select node type, query will be executed only on these if set
175      * @return nodeType name
176      */
177     public String getNodeType() {
178         return nodeType;
179     }
180 
181     /**
182      * set select node type value, query will be executed only on these if set
183      * @param nodeType
184      */
185     public void setNodeType(String selectNodeType) {
186         this.nodeType = selectNodeType;
187     }
188 
189     /**
190      * get jcr path, under which search will be executed
191      * @return path
192      */
193     public String getSearchPath() {
194         return searchPath;
195     }
196 
197     /**
198      * set jcr path, under which search will be executed
199      * @param searchPath
200      */
201     public void setSearchPath(String searchPath) {
202         this.searchPath = searchPath;
203     }
204 
205     /**
206      * set Query
207      * @param query
208      */
209     public void setQuery(SearchQuery query) {
210         this.query = query;
211     }
212 
213     /**
214      * get query
215      * @return query
216      */
217     public SearchQuery getQuery() {
218         // this is needed in case the list page is not a searchable list
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 }