root/sandbox/nuxeo-api-sample/trunk/src/main/java/org/nuxeo/ecm/sample/search/SearchSample.java

Revision 25187, 3.2 kB (checked in by sfermigier, 1 year ago)

Cleanup and format sample code.

Line 
1 /*
2  * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the GNU Lesser General Public License
6  * (LGPL) version 2.1 which accompanies this distribution, and is available at
7  * http://www.gnu.org/licenses/lgpl.html
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * Contributors:
15  *     Nuxeo - initial API and implementation
16  *
17  * $Id$
18  */
19
20 package org.nuxeo.ecm.sample.search;
21
22 import java.util.GregorianCalendar;
23
24 import javax.security.auth.login.LoginContext;
25
26 import org.nuxeo.ecm.core.query.sql.SQLQueryParser;
27 import org.nuxeo.ecm.core.search.api.client.SearchService;
28 import org.nuxeo.ecm.core.search.api.client.common.SearchServiceDelegate;
29 import org.nuxeo.ecm.core.search.api.client.query.impl.ComposedNXQueryImpl;
30 import org.nuxeo.ecm.core.search.api.client.search.results.ResultItem;
31 import org.nuxeo.ecm.core.search.api.client.search.results.ResultSet;
32 import org.nuxeo.ecm.sample.SampleApplication;
33 import org.nuxeo.runtime.api.Framework;
34
35 /**
36  * Search example.
37  *
38  * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
39  *
40  */
41 public class SearchSample extends SampleApplication {
42
43     private static final String USERNAME = "Administrator";
44
45     private static final String PWD = "Administrator";
46
47     /**
48      * This should be overwritten by samples.
49      */
50     @Override
51     protected void runSample() throws Exception {
52         try {
53             LoginContext loginCtx = Framework.login(USERNAME, PWD);
54             loginCtx.login();
55             SearchService searchService = SearchServiceDelegate.getRemoteSearchService();
56             String query = "SELECT * FROM Document WHERE ecm:fulltext LIKE 'balance'";
57             ResultSet set = searchService.searchQuery(new ComposedNXQueryImpl(
58                     SQLQueryParser.parse(query)), 0, 2);
59
60             System.out.println("Number of results found : "
61                     + String.valueOf(set.getTotalHits()));
62
63             while (true) {
64                 System.out.println("--------------------------------");
65                 System.out.println("Number of results for this page : "
66                         + set.size() + " with page size=" + set.getRange());
67                 for (int i = 0; i < set.size(); i++) {
68
69                     ResultItem item = set.get(i);
70                     System.out.println("Title : " + item.get("dc:title"));
71                     System.out.println("Created : "
72                             + ((GregorianCalendar) item.get("dc:created")).toString());
73                 }
74                 if (set.hasNextPage()) {
75                     set = set.nextPage();
76                 } else {
77                     break;
78                 }
79                 System.out.println("--------------------------------");
80             }
81
82             // loginCtx.logout();
83         } catch (Throwable t) {
84             t.printStackTrace();
85         }
86     }
87
88     public static void main(String[] args) {
89         // run my application!
90         new SearchSample().start(args);
91     }
92
93 }
Note: See TracBrowser for help on using the browser.