KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > ReportedData


1 /**
2  * $RCSfile$
3  * $Revision: 2492 $
4  * $Date: 2005-05-26 23:17:15 -0300 (Thu, 26 May 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smackx;
22
23 import java.util.*;
24
25 import org.jivesoftware.smack.packet.*;
26 import org.jivesoftware.smackx.packet.DataForm;
27
28 /**
29  * Represents a set of data results returned as part of a search. The report is structured
30  * in columns and rows.
31  *
32  * @author Gaston Dombiak
33  */

34 public class ReportedData {
35     
36     private List columns = new ArrayList();
37     private List rows = new ArrayList();
38     private String JavaDoc title = "";
39     
40     /**
41      * Returns a new ReportedData if the packet is used for reporting data and includes an
42      * extension that matches the elementName and namespace "x","jabber:x:data".
43      *
44      * @param packet the packet used for reporting data.
45      */

46     public static ReportedData getReportedDataFrom(Packet packet) {
47         // Check if the packet includes the DataForm extension
48
PacketExtension packetExtension = packet.getExtension("x","jabber:x:data");
49         if (packetExtension != null) {
50             // Check if the existing DataForm is a result of a search
51
DataForm dataForm = (DataForm) packetExtension;
52             if (dataForm.getReportedData() != null)
53                 return new ReportedData(dataForm);
54         }
55         // Otherwise return null
56
return null;
57     }
58     
59     
60     /**
61      * Creates a new ReportedData based on the returned dataForm from a search
62      *(namespace "jabber:iq:search").
63      *
64      * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
65      */

66     private ReportedData(DataForm dataForm) {
67         // Add the columns to the report based on the reported data fields
68
for (Iterator fields = dataForm.getReportedData().getFields(); fields.hasNext();) {
69             FormField field = (FormField)fields.next();
70             columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
71         }
72
73         // Add the rows to the report based on the form's items
74
for (Iterator items = dataForm.getItems(); items.hasNext();) {
75             DataForm.Item item = (DataForm.Item)items.next();
76             List fieldList = new ArrayList(columns.size());
77             FormField field;
78             for (Iterator fields = item.getFields(); fields.hasNext();) {
79                 field = (FormField) fields.next();
80                 // The field is created with all the values of the data form's field
81
List values = new ArrayList();
82                 for (Iterator it=field.getValues(); it.hasNext();) {
83                     values.add(it.next());
84                 }
85                 fieldList.add(new Field(field.getVariable(), values));
86             }
87             rows.add(new Row(fieldList));
88         }
89
90         // Set the report's title
91
this.title = dataForm.getTitle();
92     }
93     
94     /**
95      * Returns an Iterator for the rows returned from a search.
96      *
97      * @return an Iterator for the rows returned from a search.
98      */

99     public Iterator getRows() {
100         return Collections.unmodifiableList(new ArrayList(rows)).iterator();
101     }
102
103     /**
104      * Returns an Iterator for the columns returned from a search.
105      *
106      * @return an Iterator for the columns returned from a search.
107      */

108     public Iterator getColumns() {
109         return Collections.unmodifiableList(new ArrayList(columns)).iterator();
110     }
111
112
113     /**
114      * Returns the report's title. It is similar to the title on a web page or an X
115      * window.
116      *
117      * @return title of the report.
118      */

119     public String JavaDoc getTitle() {
120         return title;
121     }
122     
123     /**
124      *
125      * Represents the columns definition of the reported data.
126      *
127      * @author Gaston Dombiak
128      */

129     public static class Column {
130         private String JavaDoc label;
131         private String JavaDoc variable;
132         private String JavaDoc type;
133
134         /**
135          * Creates a new column with the specified definition.
136          *
137          * @param label the columns's label.
138          * @param variable the variable name of the column.
139          * @param type the format for the returned data.
140          */

141         private Column(String JavaDoc label, String JavaDoc variable, String JavaDoc type) {
142             this.label = label;
143             this.variable = variable;
144             this.type = type;
145         }
146         
147         /**
148          * Returns the column's label.
149          *
150          * @return label of the column.
151          */

152         public String JavaDoc getLabel() {
153             return label;
154         }
155
156
157         /**
158          * Returns the column's data format. Valid formats are:
159          *
160          * <ul>
161          * <li>text-single -> single line or word of text
162          * <li>text-private -> instead of showing the user what they typed, you show ***** to
163          * protect it
164          * <li>text-multi -> multiple lines of text entry
165          * <li>list-single -> given a list of choices, pick one
166          * <li>list-multi -> given a list of choices, pick one or more
167          * <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
168          * <li>fixed -> fixed for putting in text to show sections, or just advertise your web
169          * site in the middle of the form
170          * <li>hidden -> is not given to the user at all, but returned with the questionnaire
171          * <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
172          * on the rules for a JID.
173          * <li>jid-multi -> multiple entries for JIDs
174          * </ul>
175          *
176          * @return format for the returned data.
177          */

178         public String JavaDoc getType() {
179             return type;
180         }
181
182
183         /**
184          * Returns the variable name that the column is showing.
185          *
186          * @return the variable name of the column.
187          */

188         public String JavaDoc getVariable() {
189             return variable;
190         }
191
192
193     }
194     
195     public static class Row {
196         private List fields = new ArrayList();
197         
198         private Row(List fields) {
199             this.fields = fields;
200         }
201         
202         /**
203          * Returns the values of the field whose variable matches the requested variable.
204          *
205          * @param variable the variable to match.
206          * @return the values of the field whose variable matches the requested variable.
207          */

208         public Iterator getValues(String JavaDoc variable) {
209             for(Iterator it=getFields();it.hasNext();) {
210                 Field field = (Field) it.next();
211                 if (variable.equals(field.getVariable())) {
212                     return field.getValues();
213                 }
214             }
215             return null;
216         }
217         
218         /**
219          * Returns the fields that define the data that goes with the item.
220          *
221          * @return the fields that define the data that goes with the item.
222          */

223         private Iterator getFields() {
224             return Collections.unmodifiableList(new ArrayList(fields)).iterator();
225         }
226     }
227     
228     private static class Field {
229         private String JavaDoc variable;
230         private List values;
231
232         private Field(String JavaDoc variable, List values) {
233             this.variable = variable;
234             this.values = values;
235         }
236
237         /**
238          * Returns the variable name that the field represents.
239          *
240          * @return the variable name of the field.
241          */

242         public String JavaDoc getVariable() {
243             return variable;
244         }
245
246         /**
247          * Returns an iterator on the values reported as part of the search.
248          *
249          * @return the returned values of the search.
250          */

251         public Iterator getValues() {
252             return Collections.unmodifiableList(values).iterator();
253         }
254     }
255 }
256
Popular Tags