001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.search;
018
019import org.jivesoftware.smack.packet.Packet;
020import org.jivesoftware.smack.packet.PacketExtension;
021import org.jivesoftware.smackx.xdata.FormField;
022import org.jivesoftware.smackx.xdata.packet.DataForm;
023import org.jivesoftware.smackx.xdata.packet.DataForm.Item;
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028
029/**
030 * Represents a set of data results returned as part of a search. The report is structured 
031 * in columns and rows.
032 * 
033 * @author Gaston Dombiak
034 */
035public class ReportedData {
036    
037    private List<Column> columns = new ArrayList<Column>();
038    private List<Row> rows = new ArrayList<Row>();
039    private String title = "";
040    
041    /**
042     * Returns a new ReportedData if the packet is used for reporting data and includes an 
043     * extension that matches the elementName and namespace "x","jabber:x:data".
044     * 
045     * @param packet the packet used for reporting data.
046     */
047    public static ReportedData getReportedDataFrom(Packet packet) {
048        // Check if the packet includes the DataForm extension
049        PacketExtension packetExtension = packet.getExtension("x","jabber:x:data");
050        if (packetExtension != null) {
051            // Check if the existing DataForm is a result of a search
052            DataForm dataForm = (DataForm) packetExtension;
053            if (dataForm.getReportedData() != null)
054                return new ReportedData(dataForm);
055        }
056        // Otherwise return null
057        return null;
058    }
059
060
061    /**
062     * Creates a new ReportedData based on the returned dataForm from a search
063     *(namespace "jabber:iq:search").
064     *
065     * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
066     */
067    private ReportedData(DataForm dataForm) {
068        // Add the columns to the report based on the reported data fields
069        for (FormField field : dataForm.getReportedData().getFields()) {
070            columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
071        }
072
073        // Add the rows to the report based on the form's items
074        for (Item item : dataForm.getItems()) {
075            List<Field> fieldList = new ArrayList<Field>(columns.size());
076            for (FormField field : item.getFields()) {
077                // The field is created with all the values of the data form's field
078                List<String> values = new ArrayList<String>();
079                for (String value : field.getValues()) {
080                    values.add(value);
081                }
082                fieldList.add(new Field(field.getVariable(), values));
083            }
084            rows.add(new Row(fieldList));
085        }
086
087        // Set the report's title
088        this.title = dataForm.getTitle();
089    }
090
091
092    public ReportedData(){
093        // Allow for model creation of ReportedData.
094    }
095
096    /**
097     * Adds a new <code>Row</code>.
098     * @param row the new row to add.
099     */
100    public void addRow(Row row){
101        rows.add(row);
102    }
103
104    /**
105     * Adds a new <code>Column</code>
106     * @param column the column to add.
107     */
108    public void addColumn(Column column){
109        columns.add(column);
110    }
111
112
113    /**
114     * Returns a List of the rows returned from a search.
115     *
116     * @return a List of the rows returned from a search.
117     */
118    public List<Row> getRows() {
119        return Collections.unmodifiableList(new ArrayList<Row>(rows));
120    }
121
122    /**
123     * Returns a List of the columns returned from a search.
124     *
125     * @return a List of the columns returned from a search.
126     */
127    public List<Column> getColumns() {
128        return Collections.unmodifiableList(new ArrayList<Column>(columns));
129    }
130
131
132    /**
133     * Returns the report's title. It is similar to the title on a web page or an X
134     * window.
135     *
136     * @return title of the report.
137     */
138    public String getTitle() {
139        return title;
140    }
141
142    /**
143     *
144     * Represents the columns definition of the reported data.
145     *
146     * @author Gaston Dombiak
147     */
148    public static class Column {
149        private String label;
150        private String variable;
151        private String type;
152
153        /**
154         * Creates a new column with the specified definition.
155         *
156         * @param label the columns's label.
157         * @param variable the variable name of the column.
158         * @param type the format for the returned data.
159         */
160        public Column(String label, String variable, String type) {
161            this.label = label;
162            this.variable = variable;
163            this.type = type;
164        }
165
166        /**
167         * Returns the column's label.
168         *
169         * @return label of the column.
170         */
171        public String getLabel() {
172            return label;
173        }
174
175
176        /**
177         * Returns the column's data format. Valid formats are:
178         *
179         * <ul>
180         *  <li>text-single -> single line or word of text
181         *  <li>text-private -> instead of showing the user what they typed, you show ***** to
182         * protect it
183         *  <li>text-multi -> multiple lines of text entry
184         *  <li>list-single -> given a list of choices, pick one
185         *  <li>list-multi -> given a list of choices, pick one or more
186         *  <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
187         *  <li>fixed -> fixed for putting in text to show sections, or just advertise your web
188         * site in the middle of the form
189         *  <li>hidden -> is not given to the user at all, but returned with the questionnaire
190         *  <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
191         * on the rules for a JID.
192         *  <li>jid-multi -> multiple entries for JIDs
193         * </ul>
194         *
195         * @return format for the returned data.
196         */
197        public String getType() {
198            return type;
199        }
200
201
202        /**
203         * Returns the variable name that the column is showing.
204         *
205         * @return the variable name of the column.
206         */
207        public String getVariable() {
208            return variable;
209        }
210
211
212    }
213
214    public static class Row {
215        private List<Field> fields = new ArrayList<Field>();
216
217        public Row(List<Field> fields) {
218            this.fields = fields;
219        }
220
221        /**
222         * Returns the values of the field whose variable matches the requested variable.
223         *
224         * @param variable the variable to match.
225         * @return the values of the field whose variable matches the requested variable.
226         */
227        public List<String> getValues(String variable) {
228            for(Field field : getFields()) {
229                if (variable.equalsIgnoreCase(field.getVariable())) {
230                    return field.getValues();
231                }
232            }
233            return null;
234        }
235
236        /**
237         * Returns the fields that define the data that goes with the item.
238         *
239         * @return the fields that define the data that goes with the item.
240         */
241        private List<Field> getFields() {
242            return Collections.unmodifiableList(new ArrayList<Field>(fields));
243        }
244    }
245
246    public static class Field {
247        private String variable;
248        private List<String> values;
249
250        public Field(String variable, List<String> values) {
251            this.variable = variable;
252            this.values = values;
253        }
254
255        /**
256         * Returns the variable name that the field represents.
257         * 
258         * @return the variable name of the field.
259         */
260        public String getVariable() {
261            return variable;
262        }
263
264        /**
265         * Returns a List of the values reported as part of the search.
266         * 
267         * @return the returned values of the search.
268         */
269        public List<String> getValues() {
270            return Collections.unmodifiableList(values);
271        }
272    }
273}