b82b997835bdc535e5a6f19047cbdf4f90b7a92e
[jalview.git] / src / jalview / ws / uimodel / PDBRestResponse.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.ws.uimodel;
23
24 import jalview.datamodel.SequenceI;
25 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
26
27 import java.util.Collection;
28 import java.util.Objects;
29
30 import javax.swing.table.DefaultTableModel;
31
32 import org.json.simple.JSONObject;
33
34 /**
35  * Represents the response model produced by the PDBRestClient upon successful
36  * execution of a given request
37  * 
38  * @author tcnofoegbu
39  *
40  */
41 public class PDBRestResponse
42 {
43   private int numberOfItemsFound;
44
45   private String responseTime;
46
47   private Collection<PDBResponseSummary> searchSummary;
48
49   public int getNumberOfItemsFound()
50   {
51     return numberOfItemsFound;
52   }
53
54   public void setNumberOfItemsFound(int itemFound)
55   {
56     this.numberOfItemsFound = itemFound;
57   }
58
59   public String getResponseTime()
60   {
61     return responseTime;
62   }
63
64   public void setResponseTime(String responseTime)
65   {
66     this.responseTime = responseTime;
67   }
68
69   public Collection<PDBResponseSummary> getSearchSummary()
70   {
71     return searchSummary;
72   }
73
74   public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
75   {
76     this.searchSummary = searchSummary;
77   }
78
79
80   /**
81    * Convenience method to obtain a Table model for a given summary List based
82    * on the request parameters
83    * 
84    * @param request
85    *          the PDBRestRequest object which holds useful information for
86    *          creating a table model
87    * @param summariesList
88    *          the summary list which contains the data for populating the
89    *          table's rows
90    * @return the table model which was dynamically generated
91    */
92   public static DefaultTableModel getTableModel(PDBRestRequest request,
93           Collection<PDBResponseSummary> summariesList)
94   {
95     DefaultTableModel tableModel = new DefaultTableModel()
96     {
97       @Override
98       public boolean isCellEditable(int row, int column)
99       {
100         return false;
101       }
102     };
103     if (request.getAssociatedSequence() != null)
104     {
105       tableModel.addColumn("Ref Sequence"); // Create sequence column header if
106                                         // exists in the request
107     }
108     for (PDBDocField field : request.getWantedFields())
109     {
110       tableModel.addColumn(field.getName()); // Create sequence column header if
111                                              // exists in the request
112     }
113
114     for (PDBResponseSummary res : summariesList)
115     {
116       tableModel.addRow(res.getSummaryData()); // Populate table rows with
117                                                // summary list
118     }
119
120     return tableModel;
121   }
122
123   /**
124    * Model for a unique response summary
125    * 
126    */
127   public class PDBResponseSummary
128   {
129     private String pdbId;
130
131     private Object[] summaryRowData;
132
133     private SequenceI associatedSequence;
134
135     public PDBResponseSummary(JSONObject pdbJsonDoc, PDBRestRequest request)
136     {
137       Collection<PDBDocField> diplayFields = request.getWantedFields();
138       SequenceI associatedSeq = request.getAssociatedSequence();
139       int colCounter = 0;
140       summaryRowData = new Object[(associatedSeq != null) ? diplayFields
141               .size() + 1 : diplayFields.size()];
142       if (associatedSeq != null)
143       {
144         this.associatedSequence = associatedSeq;
145         summaryRowData[0] = associatedSequence;
146         colCounter = 1;
147       }
148
149       for (PDBDocField field : diplayFields)
150       {
151         String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
152                 : pdbJsonDoc
153                 .get(field.getCode()).toString();
154         if (field.equals(PDBDocField.PDB_ID))
155         {
156           this.pdbId = fieldData;
157           summaryRowData[colCounter++] = this.pdbId;
158         }
159         else
160         {
161           summaryRowData[colCounter++] = fieldData;
162         }
163       }
164     }
165
166     public String getPdbId()
167     {
168       return pdbId;
169     }
170
171     public void setPdbId(String pdbId)
172     {
173       this.pdbId = pdbId;
174     }
175
176     public Object[] getSummaryData()
177     {
178       return summaryRowData;
179     }
180
181     public void setSummaryData(String[] summaryData)
182     {
183       this.summaryRowData = summaryData;
184     }
185
186     /**
187      * Returns a string representation of this object;
188      */
189     @Override
190     public String toString()
191     {
192       StringBuilder summaryFieldValues = new StringBuilder();
193       for (Object summaryField : summaryRowData)
194       {
195         summaryFieldValues.append(summaryField.toString()).append("\t");
196       }
197       return summaryFieldValues.toString();
198     }
199
200     /**
201      * Returns hash code value for this object
202      */
203     @Override
204     public int hashCode()
205     {
206       return Objects.hash(this.pdbId, this.toString());
207     }
208
209     /**
210      * Indicates whether some object is equal to this one
211      */
212     @Override
213     public boolean equals(Object that)
214     {
215       if (!(that instanceof PDBResponseSummary))
216       {
217         return false;
218       }
219       PDBResponseSummary another = (PDBResponseSummary) that;
220       return this.toString().equals(another.toString());
221     }
222
223   }
224
225 }
226