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 java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;

  21. import org.jivesoftware.smack.packet.Stanza;

  22. import org.jivesoftware.smackx.xdata.FormField;
  23. import org.jivesoftware.smackx.xdata.packet.DataForm;
  24. import org.jivesoftware.smackx.xdata.packet.DataForm.Item;

  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.     private final List<Column> columns = new ArrayList<>();
  33.     private final List<Row> rows = new ArrayList<>();
  34.     private String title = "";

  35.     /**
  36.      * Returns a new ReportedData if the stanza is used for reporting data and includes an
  37.      * extension that matches the elementName and namespace "x","jabber:x:data".
  38.      *
  39.      * @param packet the stanza used for reporting data.
  40.      * @return ReportedData from the packet if present, otherwise null.
  41.      */
  42.     public static ReportedData getReportedDataFrom(Stanza packet) {
  43.         // Check if the packet includes the DataForm extension
  44.         DataForm dataForm = DataForm.from(packet);
  45.         if (dataForm != null) {
  46.             if (dataForm.getReportedData() != null)
  47.                 return new ReportedData(dataForm);
  48.         }
  49.         // Otherwise return null
  50.         return null;
  51.     }


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

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

  74.         // Set the report's title
  75.         this.title = dataForm.getTitle();
  76.     }


  77.     public ReportedData() {
  78.         // Allow for model creation of ReportedData.
  79.     }

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

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


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

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


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

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

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

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


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


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


  165.     }

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

  168.         public Row(List<Field> fields) {
  169.             this.fields = fields;
  170.         }

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

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

  194.     public static class Field {
  195.         private final String variable;
  196.         private final List<? extends CharSequence> values;

  197.         public Field(String variable, List<? extends CharSequence> values) {
  198.             this.variable = variable;
  199.             this.values = values;
  200.         }

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

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