KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.alfresco.webservice.classification.ClassificationServiceSoapBindingStub;
20 import org.alfresco.webservice.repository.QueryResult;
21 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
22 import org.alfresco.webservice.types.Category;
23 import org.alfresco.webservice.types.Classification;
24 import org.alfresco.webservice.types.Query;
25 import org.alfresco.webservice.types.QueryLanguageEnum;
26 import org.alfresco.webservice.util.AuthenticationUtils;
27 import org.alfresco.webservice.util.ISO9075;
28 import org.alfresco.webservice.util.WebServiceFactory;
29
30 /**
31  * Web service sample 6
32  * <p>
33  * Example showing how content can be queried for using categories
34  *
35  * @author Roy Wetherall
36  */

37 public class WebServiceSample6 extends WebServiceSampleBase
38 {
39     /**
40      * Main function
41      */

42     public static void main(String JavaDoc[] args)
43         throws Exception JavaDoc
44     {
45         AuthenticationUtils.startSession(USERNAME, PASSWORD);
46         try
47         {
48             // Make sure smaple data has been created
49
createSampleData();
50             
51             // Get the services
52
RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
53             ClassificationServiceSoapBindingStub classificationService = WebServiceFactory.getClassificationService();
54            
55             // Get all the classifications
56
Classification[] classifications = classificationService.getClassifications(STORE);
57             
58             // Output some details
59
System.out.println("All classifications:");
60             for (Classification classification : classifications)
61             {
62                 System.out.println(classification.getClassification());
63                 System.out.println("Classification = " + classification.getTitle() + "; Root category = " + classification.getRootCategory().getTitle());
64             }
65             
66             // Get the class definition for the classification we are interested in
67
Classification classification = classifications[0];
68             
69             // Get the child categories
70
Category[] categories = null;
71             if (classifications.length > 0)
72             {
73                 categories = classificationService.getChildCategories(classifications[0].getRootCategory().getId());
74                 if (categories != null)
75                 {
76                     // Output some details
77
System.out.println("The child categories of classification '" + classifications[0].getTitle() + "':");
78                     for (Category category : categories)
79                     {
80                         System.out.println("Title = " + category.getTitle());
81                     }
82                 }
83                 else
84                 {
85                     System.out.println("No child categories found.");
86                 }
87             }
88           
89             // Now build a path query
90
StringBuilder JavaDoc pathQuery = new StringBuilder JavaDoc(128);
91             
92             //pathQuery.append("PATH:\"cm:generalclassifiable/cm:MyTestCategory/cm:One/member\"");
93

94             // Encode the root category name
95
String JavaDoc encodedRoot = ISO9075.encode(classification.getRootCategory().getTitle());
96         
97             // Build up the search path
98
if (categories != null && categories.length != 0)
99             {
100                 for (int i=0; i<categories.length; i++)
101                 {
102                     if (pathQuery.length() != 0)
103                     {
104                         pathQuery.append("OR");
105                     }
106                     
107                     String JavaDoc encoded = ISO9075.encode(categories[i].getTitle());
108                     pathQuery.append(" PATH:\"cm:generalclassifiable/cm:" + encodedRoot + "/cm:" + encoded + "/member\" ");
109                 }
110             }
111             
112             System.out.println("Query path: " + pathQuery.toString());
113             
114             // Call the repository service to do search based on category
115
Query query = new Query(QueryLanguageEnum.lucene, pathQuery.toString());
116     
117             // Execute the query
118
QueryResult queryResult = repositoryService.query(STORE, query, true);
119     
120             System.out.println("Category query results:");
121             WebServiceSample2.outputResultSet(queryResult.getResultSet().getRows());
122         }
123         finally
124         {
125             AuthenticationUtils.endSession();
126         }
127     }
128 }
129
Popular Tags