ReportedData.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.search;

  18. import org.jivesoftware.smack.packet.Stanza;
  19. import org.jivesoftware.smackx.xdata.FormField;
  20. import org.jivesoftware.smackx.xdata.packet.DataForm;
  21. import org.jivesoftware.smackx.xdata.packet.DataForm.Item;

  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;

  25. /**
  26.  * Represents a set of data results returned as part of a search. The report is structured
  27.  * in columns and rows.
  28.  *
  29.  * @author Gaston Dombiak
  30.  */
  31. public class ReportedData {
  32.    
  33.     private List<Column> columns = new ArrayList<Column>();
  34.     private List<Row> rows = new ArrayList<Row>();
  35.     private String title = "";
  36.    
  37.     /**
  38.      * Returns a new ReportedData if the packet is used for reporting data and includes an
  39.      * extension that matches the elementName and namespace "x","jabber:x:data".
  40.      *
  41.      * @param packet the packet used for reporting data.
  42.      */
  43.     public static ReportedData getReportedDataFrom(Stanza packet) {
  44.         // Check if the packet includes the DataForm extension
  45.         DataForm dataForm = DataForm.from(packet);
  46.         if (dataForm != null) {
  47.             if (dataForm.getReportedData() != null)
  48.                 return new ReportedData(dataForm);
  49.         }
  50.         // Otherwise return null
  51.         return null;
  52.     }


  53.     /**
  54.      * Creates a new ReportedData based on the returned dataForm from a search
  55.      *(namespace "jabber:iq:search").
  56.      *
  57.      * @param dataForm the dataForm returned from a search (namespace "jabber:iq:search").
  58.      */
  59.     private ReportedData(DataForm dataForm) {
  60.         // Add the columns to the report based on the reported data fields
  61.         for (FormField field : dataForm.getReportedData().getFields()) {
  62.             columns.add(new Column(field.getLabel(), field.getVariable(), field.getType()));
  63.         }

  64.         // Add the rows to the report based on the form's items
  65.         for (Item item : dataForm.getItems()) {
  66.             List<Field> fieldList = new ArrayList<Field>(columns.size());
  67.             for (FormField field : item.getFields()) {
  68.                 // The field is created with all the values of the data form's field
  69.                 List<String> values = new ArrayList<String>();
  70.                 for (String value : field.getValues()) {
  71.                     values.add(value);
  72.                 }
  73.                 fieldList.add(new Field(field.getVariable(), values));
  74.             }
  75.             rows.add(new Row(fieldList));
  76.         }

  77.         // Set the report's title
  78.         this.title = dataForm.getTitle();
  79.     }


  80.     public ReportedData(){
  81.         // Allow for model creation of ReportedData.
  82.     }

  83.     /**
  84.      * Adds a new <code>Row</code>.
  85.      * @param row the new row to add.
  86.      */
  87.     public void addRow(Row row){
  88.         rows.add(row);
  89.     }

  90.     /**
  91.      * Adds a new <code>Column</code>
  92.      * @param column the column to add.
  93.      */
  94.     public void addColumn(Column column){
  95.         columns.add(column);
  96.     }


  97.     /**
  98.      * Returns a List of the rows returned from a search.
  99.      *
  100.      * @return a List of the rows returned from a search.
  101.      */
  102.     public List<Row> getRows() {
  103.         return Collections.unmodifiableList(new ArrayList<Row>(rows));
  104.     }

  105.     /**
  106.      * Returns a List of the columns returned from a search.
  107.      *
  108.      * @return a List of the columns returned from a search.
  109.      */
  110.     public List<Column> getColumns() {
  111.         return Collections.unmodifiableList(new ArrayList<Column>(columns));
  112.     }


  113.     /**
  114.      * Returns the report's title. It is similar to the title on a web page or an X
  115.      * window.
  116.      *
  117.      * @return title of the report.
  118.      */
  119.     public String getTitle() {
  120.         return title;
  121.     }

  122.     /**
  123.      *
  124.      * Represents the columns definition of the reported data.
  125.      *
  126.      * @author Gaston Dombiak
  127.      */
  128.     public static class Column {
  129.         private final String label;
  130.         private final String variable;
  131.         private final FormField.Type type;

  132.         /**
  133.          * Creates a new column with the specified definition.
  134.          *
  135.          * @param label the columns's label.
  136.          * @param variable the variable name of the column.
  137.          * @param type the format for the returned data.
  138.          */
  139.         public Column(String label, String variable, FormField.Type type) {
  140.             this.label = label;
  141.             this.variable = variable;
  142.             this.type = type;
  143.         }

  144.         /**
  145.          * Returns the column's label.
  146.          *
  147.          * @return label of the column.
  148.          */
  149.         public String getLabel() {
  150.             return label;
  151.         }


  152.         /**
  153.          * Returns the column's data format.
  154.          *
  155.          * @return format for the returned data.
  156.          */
  157.         public FormField.Type getType() {
  158.             return type;
  159.         }


  160.         /**
  161.          * Returns the variable name that the column is showing.
  162.          *
  163.          * @return the variable name of the column.
  164.          */
  165.         public String getVariable() {
  166.             return variable;
  167.         }


  168.     }

  169.     public static class Row {
  170.         private List<Field> fields = new ArrayList<Field>();

  171.         public Row(List<Field> fields) {
  172.             this.fields = fields;
  173.         }

  174.         /**
  175.          * Returns the values of the field whose variable matches the requested variable.
  176.          *
  177.          * @param variable the variable to match.
  178.          * @return the values of the field whose variable matches the requested variable.
  179.          */
  180.         public List<String> getValues(String variable) {
  181.             for(Field field : getFields()) {
  182.                 if (variable.equalsIgnoreCase(field.getVariable())) {
  183.                     return field.getValues();
  184.                 }
  185.             }
  186.             return null;
  187.         }

  188.         /**
  189.          * Returns the fields that define the data that goes with the item.
  190.          *
  191.          * @return the fields that define the data that goes with the item.
  192.          */
  193.         private List<Field> getFields() {
  194.             return Collections.unmodifiableList(new ArrayList<Field>(fields));
  195.         }
  196.     }

  197.     public static class Field {
  198.         private String variable;
  199.         private List<String> values;

  200.         public Field(String variable, List<String> values) {
  201.             this.variable = variable;
  202.             this.values = values;
  203.         }

  204.         /**
  205.          * Returns the variable name that the field represents.
  206.          *
  207.          * @return the variable name of the field.
  208.          */
  209.         public String getVariable() {
  210.             return variable;
  211.         }

  212.         /**
  213.          * Returns a List of the values reported as part of the search.
  214.          *
  215.          * @return the returned values of the search.
  216.          */
  217.         public List<String> getValues() {
  218.             return Collections.unmodifiableList(values);
  219.         }
  220.     }
  221. }