KickJava   Java API By Example, From Geeks To Geeks.

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


1 // NOTE: if you change the package location of this class you will need to update the WS_SEURITY_INFO XML as for this example this class
2
// doubles as the passwordCAllbackClass
3
package org.alfresco.webservice.sample;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.alfresco.webservice.repository.QueryResult;
9 import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
10 import org.alfresco.webservice.types.NamedValue;
11 import org.alfresco.webservice.types.Node;
12 import org.alfresco.webservice.types.Predicate;
13 import org.alfresco.webservice.types.Query;
14 import org.alfresco.webservice.types.QueryLanguageEnum;
15 import org.alfresco.webservice.types.Reference;
16 import org.alfresco.webservice.types.ResultSet;
17 import org.alfresco.webservice.types.ResultSetRow;
18 import org.alfresco.webservice.util.AuthenticationUtils;
19 import org.alfresco.webservice.util.Constants;
20 import org.alfresco.webservice.util.WebServiceFactory;
21
22 public class WebServiceSampleGetRankedContent extends WebServiceSampleBase
23 {
24     /**
25      * Main method
26      *
27      * @param args
28      */

29     public static void main(String JavaDoc[] args)
30     {
31         WebServiceSampleGetRankedContent sample = new WebServiceSampleGetRankedContent();
32         List JavaDoc<ContentResult> results = sample.getRankedContent("Web Service Sample Folder", "Alfresco Development Team");
33         
34         // Output the results for visual inspection
35
int iCount = 1;
36         for (ContentResult result : results)
37         {
38             System.out.println("Result " + iCount + ": " + result.toString());
39             iCount ++;
40         }
41     }
42     
43     /**
44      * Get a list of ordered results of documents in the space specified matching the search
45      * text provided.
46      *
47      * @param spaceName the name of the space (immediatly beneth the company home space) to search
48      * @param searchValue the FTS search value
49      * @return list of results
50      */

51     public List JavaDoc<ContentResult> getRankedContent(String JavaDoc spaceName, String JavaDoc searchValue)
52     {
53         List JavaDoc<ContentResult> results = new ArrayList JavaDoc<ContentResult>();
54         
55         try
56         {
57             AuthenticationUtils.startSession(USERNAME, PASSWORD);
58             try
59             {
60                 RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
61                 
62                 // Get a reference to the space we have named
63
Reference reference = new Reference(STORE, null, "/app:company_home/*[@cm:name=\"" + spaceName + "\"]");
64                 Predicate predicate = new Predicate(new Reference[]{reference}, null, null);
65                 Node[] nodes = repositoryService.get(predicate);
66                 
67                 // Create a query object, looking for all items with alfresco in the name of text
68
Query query = new Query(
69                         QueryLanguageEnum.lucene,
70                         "+PARENT:\"workspace://SpacesStore/"+ nodes[0].getReference().getUuid() + "\" +TEXT:\"" + searchValue + "\"");
71                 
72                 // Execute the query
73
QueryResult queryResult = repositoryService.query(STORE, query, false);
74                 
75                 // Display the results
76
ResultSet resultSet = queryResult.getResultSet();
77                 ResultSetRow[] rows = resultSet.getRows();
78                 
79                 if (rows != null)
80                 {
81                     // Get the infomation from the result set
82
for(ResultSetRow row : rows)
83                     {
84                         String JavaDoc nodeId = row.getNode().getId();
85                         ContentResult contentResult = new ContentResult(nodeId);
86                         
87                         for (NamedValue namedValue : row.getColumns())
88                         {
89                             if (namedValue.getName().endsWith(Constants.PROP_CREATED) == true)
90                             {
91                                 contentResult.setCreateDate(namedValue.getValue());
92                             }
93                             else if (namedValue.getName().endsWith(Constants.PROP_NAME) == true)
94                             {
95                                 contentResult.setName(namedValue.getValue());
96                             }
97                             else if (namedValue.getName().endsWith(Constants.PROP_DESCRIPTION) == true)
98                             {
99                                 contentResult.setDescription(namedValue.getValue());
100                             }
101                             else if (namedValue.getName().endsWith(Constants.PROP_CONTENT) == true)
102                             {
103                                 // We could go to the content service and ask for the content to get the URL but to save time we
104
// might as well dig the content URL out of the results.
105
String JavaDoc contentString = namedValue.getValue();
106                                 String JavaDoc[] values = contentString.split("[|=]");
107                                 contentResult.setUrl(values[1]);
108                             }
109                         }
110                         
111                         results.add(contentResult);
112                     }
113                 }
114             }
115             finally
116             {
117                 // End the session
118
AuthenticationUtils.endSession();
119             }
120         }
121         catch (Exception JavaDoc serviceException)
122         {
123             throw new RuntimeException JavaDoc("Unable to perform search.", serviceException);
124         }
125         
126         return results;
127     }
128     
129     /**
130      * Class to contain the information about the result from the query
131      */

132     public class ContentResult
133     {
134         private String JavaDoc id;
135         private String JavaDoc name;
136         private String JavaDoc description;
137         private String JavaDoc url;
138         private String JavaDoc createDate;
139         
140         public ContentResult(String JavaDoc id)
141         {
142             this.id = id;
143         }
144         
145         public String JavaDoc getCreateDate()
146         {
147             return createDate;
148         }
149         
150         public void setCreateDate(String JavaDoc createDate)
151         {
152             this.createDate = createDate;
153         }
154         
155         public String JavaDoc getDescription()
156         {
157             return description;
158         }
159         
160         public void setDescription(String JavaDoc description)
161         {
162             this.description = description;
163         }
164         
165         public String JavaDoc getId()
166         {
167             return id;
168         }
169
170         public String JavaDoc getName()
171         {
172             return name;
173         }
174         
175         public void setName(String JavaDoc name)
176         {
177             this.name = name;
178         }
179         
180         public String JavaDoc getUrl()
181         {
182             return url;
183         }
184         
185         public void setUrl(String JavaDoc url)
186         {
187             this.url = url;
188         }
189
190         @Override JavaDoc
191         public String JavaDoc toString()
192         {
193             return "id=" + this.id +
194                    "; name=" + this.name +
195                    "; description=" + this.description +
196                    "; created=" + this.createDate +
197                    "; url=" + this.url;
198         }
199     }
200 }
201
Popular Tags