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 java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.Stanza;
024
025import org.jivesoftware.smackx.xdata.FormField;
026import org.jivesoftware.smackx.xdata.packet.DataForm;
027import org.jivesoftware.smackx.xdata.packet.DataForm.Item;
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 stanza(/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 stanza(/packet) used for reporting data.
046     */
047    public static ReportedData getReportedDataFrom(Stanza packet) {
048        // Check if the packet includes the DataForm extension
049        DataForm dataForm = DataForm.from(packet);
050        if (dataForm != null) {
051            if (dataForm.getReportedData() != null)
052                return new ReportedData(dataForm);
053        }
054        // Otherwise return null
055        return null;
056    }
057
058
059    /**
060     * Creates a new ReportedData based on the returned dataForm from a search
061     *(namespace "jabber:iq:search").
062     *
063     * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
064     */
065    private ReportedData(DataForm dataForm) {
066        // Add the columns to the report based on the reported data fields
067        for (FormField field : dataForm.getReportedData().getFields()) {
068            columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
069        }
070
071        // Add the rows to the report based on the form's items
072        for (Item item : dataForm.getItems()) {
073            List<Field> fieldList = new ArrayList<Field>(columns.size());
074            for (FormField field : item.getFields()) {
075                // The field is created with all the values of the data form's field
076                List<String> values = new ArrayList<String>();
077                for (String value : field.getValues()) {
078                    values.add(value);
079                }
080                fieldList.add(new Field(field.getVariable(), values));
081            }
082            rows.add(new Row(fieldList));
083        }
084
085        // Set the report's title
086        this.title = dataForm.getTitle();
087    }
088
089
090    public ReportedData() {
091        // Allow for model creation of ReportedData.
092    }
093
094    /**
095     * Adds a new <code>Row</code>.
096     * @param row the new row to add.
097     */
098    public void addRow(Row row) {
099        rows.add(row);
100    }
101
102    /**
103     * Adds a new <code>Column</code>.
104     * @param column the column to add.
105     */
106    public void addColumn(Column column) {
107        columns.add(column);
108    }
109
110
111    /**
112     * Returns a List of the rows returned from a search.
113     *
114     * @return a List of the rows returned from a search.
115     */
116    public List<Row> getRows() {
117        return Collections.unmodifiableList(new ArrayList<Row>(rows));
118    }
119
120    /**
121     * Returns a List of the columns returned from a search.
122     *
123     * @return a List of the columns returned from a search.
124     */
125    public List<Column> getColumns() {
126        return Collections.unmodifiableList(new ArrayList<Column>(columns));
127    }
128
129
130    /**
131     * Returns the report's title. It is similar to the title on a web page or an X
132     * window.
133     *
134     * @return title of the report.
135     */
136    public String getTitle() {
137        return title;
138    }
139
140    /**
141     *
142     * Represents the columns definition of the reported data.
143     *
144     * @author Gaston Dombiak
145     */
146    public static class Column {
147        private final String label;
148        private final String variable;
149        private final FormField.Type type;
150
151        /**
152         * Creates a new column with the specified definition.
153         *
154         * @param label the columns's label.
155         * @param variable the variable name of the column.
156         * @param type the format for the returned data.
157         */
158        public Column(String label, String variable, FormField.Type type) {
159            this.label = label;
160            this.variable = variable;
161            this.type = type;
162        }
163
164        /**
165         * Returns the column's label.
166         *
167         * @return label of the column.
168         */
169        public String getLabel() {
170            return label;
171        }
172
173
174        /**
175         * Returns the column's data format.
176         *
177         * @return format for the returned data.
178         */
179        public FormField.Type getType() {
180            return type;
181        }
182
183
184        /**
185         * Returns the variable name that the column is showing.
186         *
187         * @return the variable name of the column.
188         */
189        public String getVariable() {
190            return variable;
191        }
192
193
194    }
195
196    public static class Row {
197        private List<Field> fields = new ArrayList<Field>();
198
199        public Row(List<Field> fields) {
200            this.fields = fields;
201        }
202
203        /**
204         * Returns the values of the field whose variable matches the requested variable.
205         *
206         * @param variable the variable to match.
207         * @return the values of the field whose variable matches the requested variable.
208         */
209        public List<String> getValues(String variable) {
210            for (Field field : getFields()) {
211                if (variable.equalsIgnoreCase(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 List<Field> getFields() {
224            return Collections.unmodifiableList(new ArrayList<Field>(fields));
225        }
226    }
227
228    public static class Field {
229        private String variable;
230        private List<String> values;
231
232        public Field(String variable, List<String> 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 getVariable() {
243            return variable;
244        }
245
246        /**
247         * Returns a List of the values reported as part of the search.
248         * 
249         * @return the returned values of the search.
250         */
251        public List<String> getValues() {
252            return Collections.unmodifiableList(values);
253        }
254    }
255}