2004-03-29 23:29:55 +02:00
|
|
|
/**
|
2004-11-03 00:37:00 +01:00
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-11-03 00:37:00 +01:00
|
|
|
*
|
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2004-03-29 23:29:55 +02:00
|
|
|
|
|
|
|
package org.jivesoftware.smackx;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
2004-03-29 23:29:55 +02:00
|
|
|
import org.jivesoftware.smackx.packet.DataForm;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Represents a set of data results returned as part of a search. The report is structured
|
|
|
|
* in columns and rows.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class ReportedData {
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
private List<Column> columns = new ArrayList<Column>();
|
|
|
|
private List<Row> rows = new ArrayList<Row>();
|
2004-03-29 23:29:55 +02:00
|
|
|
private String title = "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new ReportedData if the packet is used for reporting data and includes an
|
|
|
|
* extension that matches the elementName and namespace "x","jabber:x:data".
|
|
|
|
*
|
|
|
|
* @param packet the packet used for reporting data.
|
|
|
|
*/
|
|
|
|
public static ReportedData getReportedDataFrom(Packet packet) {
|
|
|
|
// Check if the packet includes the DataForm extension
|
|
|
|
PacketExtension packetExtension = packet.getExtension("x","jabber:x:data");
|
|
|
|
if (packetExtension != null) {
|
|
|
|
// Check if the existing DataForm is a result of a search
|
|
|
|
DataForm dataForm = (DataForm) packetExtension;
|
|
|
|
if (dataForm.getReportedData() != null)
|
|
|
|
return new ReportedData(dataForm);
|
|
|
|
}
|
|
|
|
// Otherwise return null
|
|
|
|
return null;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Creates a new ReportedData based on the returned dataForm from a search
|
2005-09-19 06:06:59 +02:00
|
|
|
*(namespace "jabber:iq:search").
|
|
|
|
*
|
|
|
|
* @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
|
|
|
private ReportedData(DataForm dataForm) {
|
|
|
|
// Add the columns to the report based on the reported data fields
|
|
|
|
for (Iterator fields = dataForm.getReportedData().getFields(); fields.hasNext();) {
|
|
|
|
FormField field = (FormField)fields.next();
|
|
|
|
columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the rows to the report based on the form's items
|
|
|
|
for (Iterator items = dataForm.getItems(); items.hasNext();) {
|
|
|
|
DataForm.Item item = (DataForm.Item)items.next();
|
2006-07-18 07:14:33 +02:00
|
|
|
List<Field> fieldList = new ArrayList<Field>(columns.size());
|
2004-03-29 23:29:55 +02:00
|
|
|
FormField field;
|
|
|
|
for (Iterator fields = item.getFields(); fields.hasNext();) {
|
|
|
|
field = (FormField) fields.next();
|
2005-05-27 04:17:15 +02:00
|
|
|
// The field is created with all the values of the data form's field
|
2006-07-18 07:14:33 +02:00
|
|
|
List<String> values = new ArrayList<String>();
|
|
|
|
for (Iterator<String> it=field.getValues(); it.hasNext();) {
|
2005-05-27 04:17:15 +02:00
|
|
|
values.add(it.next());
|
|
|
|
}
|
|
|
|
fieldList.add(new Field(field.getVariable(), values));
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
rows.add(new Row(fieldList));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the report's title
|
|
|
|
this.title = dataForm.getTitle();
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
public ReportedData(){
|
|
|
|
// Allow for model creation of ReportedData.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new <code>Row</code>.
|
|
|
|
* @param row the new row to add.
|
|
|
|
*/
|
|
|
|
public void addRow(Row row){
|
|
|
|
rows.add(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new <code>Column</code>
|
|
|
|
* @param column the column to add.
|
|
|
|
*/
|
|
|
|
public void addColumn(Column column){
|
|
|
|
columns.add(column);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns an Iterator for the rows returned from a search.
|
|
|
|
*
|
|
|
|
* @return an Iterator for the rows returned from a search.
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<Row> getRows() {
|
|
|
|
return Collections.unmodifiableList(new ArrayList<Row>(rows)).iterator();
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an Iterator for the columns returned from a search.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return an Iterator for the columns returned from a search.
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<Column> getColumns() {
|
|
|
|
return Collections.unmodifiableList(new ArrayList<Column>(columns)).iterator();
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-09-19 06:06:59 +02:00
|
|
|
* Returns the report's title. It is similar to the title on a web page or an X
|
2004-03-29 23:29:55 +02:00
|
|
|
* window.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return title of the report.
|
|
|
|
*/
|
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* Represents the columns definition of the reported data.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public static class Column {
|
|
|
|
private String label;
|
|
|
|
private String variable;
|
|
|
|
private String type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new column with the specified definition.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @param label the columns's label.
|
|
|
|
* @param variable the variable name of the column.
|
|
|
|
* @param type the format for the returned data.
|
|
|
|
*/
|
2005-09-19 06:06:59 +02:00
|
|
|
public Column(String label, String variable, String type) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.label = label;
|
|
|
|
this.variable = variable;
|
|
|
|
this.type = type;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the column's label.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return label of the column.
|
|
|
|
*/
|
|
|
|
public String getLabel() {
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the column's data format. Valid formats are:
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* <ul>
|
|
|
|
* <li>text-single -> single line or word of text
|
2005-09-19 06:06:59 +02:00
|
|
|
* <li>text-private -> instead of showing the user what they typed, you show ***** to
|
2004-03-29 23:29:55 +02:00
|
|
|
* protect it
|
|
|
|
* <li>text-multi -> multiple lines of text entry
|
|
|
|
* <li>list-single -> given a list of choices, pick one
|
|
|
|
* <li>list-multi -> given a list of choices, pick one or more
|
|
|
|
* <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
|
2005-09-19 06:06:59 +02:00
|
|
|
* <li>fixed -> fixed for putting in text to show sections, or just advertise your web
|
2004-03-29 23:29:55 +02:00
|
|
|
* site in the middle of the form
|
|
|
|
* <li>hidden -> is not given to the user at all, but returned with the questionnaire
|
2005-09-19 06:06:59 +02:00
|
|
|
* <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
|
2004-03-29 23:29:55 +02:00
|
|
|
* on the rules for a JID.
|
|
|
|
* <li>jid-multi -> multiple entries for JIDs
|
|
|
|
* </ul>
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return format for the returned data.
|
|
|
|
*/
|
|
|
|
public String getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the variable name that the column is showing.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return the variable name of the column.
|
|
|
|
*/
|
|
|
|
public String getVariable() {
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
public static class Row {
|
2006-07-18 07:14:33 +02:00
|
|
|
private List<Field> fields = new ArrayList<Field>();
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
public Row(List<Field> fields) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.fields = fields;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2004-03-30 04:33:28 +02:00
|
|
|
/**
|
2005-05-27 04:17:15 +02:00
|
|
|
* Returns the values of the field whose variable matches the requested variable.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-30 04:33:28 +02:00
|
|
|
* @param variable the variable to match.
|
2005-05-27 04:17:15 +02:00
|
|
|
* @return the values of the field whose variable matches the requested variable.
|
2004-03-30 04:33:28 +02:00
|
|
|
*/
|
2005-05-27 04:17:15 +02:00
|
|
|
public Iterator getValues(String variable) {
|
2006-07-18 07:14:33 +02:00
|
|
|
for(Iterator<Field> it=getFields();it.hasNext();) {
|
|
|
|
Field field = it.next();
|
2005-09-19 16:44:02 +02:00
|
|
|
if (variable.equalsIgnoreCase(field.getVariable())) {
|
2005-05-27 04:17:15 +02:00
|
|
|
return field.getValues();
|
2004-03-30 04:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the fields that define the data that goes with the item.
|
2005-09-19 06:06:59 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return the fields that define the data that goes with the item.
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
private Iterator<Field> getFields() {
|
|
|
|
return Collections.unmodifiableList(new ArrayList<Field>(fields)).iterator();
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
2005-09-19 06:06:59 +02:00
|
|
|
|
|
|
|
public static class Field {
|
2004-03-29 23:29:55 +02:00
|
|
|
private String variable;
|
2006-07-18 07:14:33 +02:00
|
|
|
private List<String> values;
|
2004-03-29 23:29:55 +02:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
public Field(String variable, List<String> values) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.variable = variable;
|
2005-05-27 04:17:15 +02:00
|
|
|
this.values = values;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the variable name that the field represents.
|
|
|
|
*
|
|
|
|
* @return the variable name of the field.
|
|
|
|
*/
|
|
|
|
public String getVariable() {
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-05-27 04:17:15 +02:00
|
|
|
* Returns an iterator on the values reported as part of the search.
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2005-05-27 04:17:15 +02:00
|
|
|
* @return the returned values of the search.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<String> getValues() {
|
2005-05-27 04:17:15 +02:00
|
|
|
return Collections.unmodifiableList(values).iterator();
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|