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