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 final List<Column> columns = new ArrayList<>(); 038 private final List<Row> rows = new ArrayList<>(); 039 private String title = ""; 040 041 /** 042 * Returns a new ReportedData if the stanza 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 used for reporting data. 046 * @return ReportedData from the packet if present, otherwise null. 047 */ 048 public static ReportedData getReportedDataFrom(Stanza packet) { 049 // Check if the packet includes the DataForm extension 050 DataForm dataForm = DataForm.from(packet); 051 if (dataForm != null) { 052 if (dataForm.getReportedData() != null) 053 return new ReportedData(dataForm); 054 } 055 // Otherwise return null 056 return null; 057 } 058 059 060 /** 061 * Creates a new ReportedData based on the returned dataForm from a search 062 *(namespace "jabber:iq:search"). 063 * 064 * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search"). 065 */ 066 private ReportedData(DataForm dataForm) { 067 // Add the columns to the report based on the reported data fields 068 for (FormField field : dataForm.getReportedData().getFields()) { 069 columns.add(new Column(field.getLabel(), field.getFieldName(), field.getType())); 070 } 071 072 // Add the rows to the report based on the form's items 073 for (Item item : dataForm.getItems()) { 074 List<Field> fieldList = new ArrayList<>(columns.size()); 075 for (FormField field : item.getFields()) { 076 // The field is created with all the values of the data form's field 077 List<CharSequence> values = new ArrayList<>(); 078 values.addAll(field.getValues()); 079 fieldList.add(new Field(field.getFieldName(), 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<>(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<>(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<>(); 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<CharSequence> 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<>(fields)); 224 } 225 } 226 227 public static class Field { 228 private final String variable; 229 private final List<? extends CharSequence> values; 230 231 public Field(String variable, List<? extends CharSequence> 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<CharSequence> getValues() { 251 return Collections.unmodifiableList(values); 252 } 253 } 254}