KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > sample > WebServiceSample2


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.sample;
18
19 import java.rmi.RemoteException JavaDoc;
20
21 import javax.xml.rpc.ServiceException JavaDoc;
22
23 import org.alfresco.webservice.repository.QueryResult;
24 import org.alfresco.webservice.repository.RepositoryFault;
25 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
26 import org.alfresco.webservice.types.NamedValue;
27 import org.alfresco.webservice.types.Query;
28 import org.alfresco.webservice.types.QueryLanguageEnum;
29 import org.alfresco.webservice.types.Reference;
30 import org.alfresco.webservice.types.ResultSet;
31 import org.alfresco.webservice.types.ResultSetRow;
32 import org.alfresco.webservice.util.AuthenticationUtils;
33 import org.alfresco.webservice.util.WebServiceFactory;
34
35 /**
36  * Web service sample 2
37  * <p>
38  * This sample shows how to execute a search using the repository web service and how to
39  * query for a nodes parents.
40  *
41  * @author Roy Wetherall
42  */

43 public class WebServiceSample2 extends WebServiceSampleBase
44 {
45     /**
46      * Main function
47      */

48     public static void main(String JavaDoc[] args)
49         throws Exception JavaDoc
50     {
51         // Start the session
52
AuthenticationUtils.startSession(USERNAME, PASSWORD);
53         
54         try
55         {
56             // Make sure smaple data has been created
57
createSampleData();
58             
59             // Execute the search sample
60
executeSearch();
61         }
62         finally
63         {
64             // End the session
65
AuthenticationUtils.endSession();
66         }
67     }
68
69     /**
70      * Executes a sample query and provides an example of using a parent query.
71      *
72      * @return returns a reference to a folder that is the parent of the search results
73      * ( used in further samples)
74      * @throws ServiceException Service exception
75      * @throws RemoteException Remove exception
76      * @throws RepositoryFault Repository fault
77      */

78     public static Reference executeSearch() throws ServiceException JavaDoc, RemoteException JavaDoc, RepositoryFault
79     {
80         Reference parentReference = null;
81         
82         // Get a reference to the respository web service
83
RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
84         
85         // Create a query object, looking for all items with alfresco in the name of text
86
Query query = new Query(QueryLanguageEnum.lucene, "TEXT:'alfresco development team'");
87         
88         // Execute the query
89
QueryResult queryResult = repositoryService.query(STORE, query, false);
90         
91         // Display the results
92
ResultSet resultSet = queryResult.getResultSet();
93         ResultSetRow[] rows = resultSet.getRows();
94         if (rows == null)
95         {
96             System.out.println("No query results found.");
97         }
98         else
99         {
100             System.out.println("Results from query:");
101             outputResultSet(rows);
102             
103             // Get the id of the first result
104
String JavaDoc firstResultId = rows[0].getNode().getId();
105             Reference reference = new Reference(STORE, firstResultId, null);
106             
107             // Get the parent(s) of the first result
108
QueryResult parentQueryResult = repositoryService.queryParents(reference);
109             
110             // Get the parent of the first result
111
ResultSet parentResultSet = parentQueryResult.getResultSet();
112             ResultSetRow[] parentRows = parentResultSet.getRows();
113             if (parentRows == null)
114             {
115                 System.out.println("No query results found.");
116             }
117             else
118             {
119                 System.out.println("Results from parent query:");
120                 outputResultSet(parentRows);
121                 
122                 // Return the first parent (we can use in other samples)
123
String JavaDoc firstParentId = parentRows[0].getNode().getId();
124                 parentReference = new Reference(STORE, firstParentId, null);
125             }
126         }
127         
128         return parentReference;
129     }
130
131     /**
132      * Helper method to output the rows contained within a result set
133      *
134      * @param rows an array of rows
135      */

136     public static void outputResultSet(ResultSetRow[] rows)
137     {
138         if (rows != null)
139         {
140             for (int x = 0; x < rows.length; x++)
141             {
142                 ResultSetRow row = rows[x];
143                 
144                 NamedValue[] columns = row.getColumns();
145                 for (int y = 0; y < columns.length; y++)
146                 {
147                     System.out.println("row " + x + ": "
148                             + row.getColumns(y).getName() + " = "
149                             + row.getColumns(y).getValue());
150                 }
151             }
152         }
153     }
154
155 }
156
Popular Tags