Merge branch 'optimisation/JAL-2001' into develop
[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 import jalview.ws.dbsources.PDBRestClient.PDBDocField.Group;
27
28 import java.util.Collection;
29 import java.util.Objects;
30
31 import javax.swing.JTable;
32 import javax.swing.table.DefaultTableModel;
33
34 import org.json.simple.JSONObject;
35
36 /**
37  * Represents the response model produced by the PDBRestClient upon successful
38  * execution of a given request
39  * 
40  * @author tcnofoegbu
41  *
42  */
43 public class PDBRestResponse
44 {
45   private int numberOfItemsFound;
46
47   private String responseTime;
48
49   private Collection<PDBResponseSummary> searchSummary;
50
51   public int getNumberOfItemsFound()
52   {
53     return numberOfItemsFound;
54   }
55
56   public void setNumberOfItemsFound(int itemFound)
57   {
58     this.numberOfItemsFound = itemFound;
59   }
60
61   public String getResponseTime()
62   {
63     return responseTime;
64   }
65
66   public void setResponseTime(String responseTime)
67   {
68     this.responseTime = responseTime;
69   }
70
71   public Collection<PDBResponseSummary> getSearchSummary()
72   {
73     return searchSummary;
74   }
75
76   public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
77   {
78     this.searchSummary = searchSummary;
79   }
80
81   /**
82    * Convenience method to obtain a Table model for a given summary List based
83    * on the request parameters
84    * 
85    * @param request
86    *          the PDBRestRequest object which holds useful information for
87    *          creating a table model
88    * @param summariesList
89    *          the summary list which contains the data for populating the
90    *          table's rows
91    * @return the table model which was dynamically generated
92    */
93   public static DefaultTableModel getTableModel(PDBRestRequest request,
94           Collection<PDBResponseSummary> summariesList)
95   {
96     final PDBDocField[] cols = request.getWantedFields().toArray(
97             new PDBDocField[0]);
98     final int colOffset = request.getAssociatedSequence() == null ? 0 : 1;
99     DefaultTableModel tableModel = new DefaultTableModel()
100     {
101       @Override
102       public boolean isCellEditable(int row, int column)
103       {
104         return false;
105       }
106
107       @Override
108       public Class<?> getColumnClass(int columnIndex)
109       {
110         if (colOffset == 1 && columnIndex == 0)
111         {
112           return String.class;
113         }
114         if (cols[columnIndex - colOffset].getGroup().getName()
115                 .equalsIgnoreCase(Group.QUALITY_MEASURES.getName()))
116         {
117           return Double.class;
118         }
119         return String.class;
120       }
121
122     };
123     if (request.getAssociatedSequence() != null)
124     {
125       tableModel.addColumn("Ref Sequence"); // Create sequence column header if
126       // exists in the request
127     }
128     for (PDBDocField field : request.getWantedFields())
129     {
130       tableModel.addColumn(field.getName()); // Create sequence column header if
131                                              // exists in the request
132     }
133
134     for (PDBResponseSummary res : summariesList)
135     {
136       tableModel.addRow(res.getSummaryData()); // Populate table rows with
137                                                // summary list
138     }
139
140     return tableModel;
141   }
142
143   /**
144    * Model for a unique response summary
145    * 
146    */
147   public class PDBResponseSummary
148   {
149     private String pdbId;
150
151     private Object[] summaryRowData;
152
153     private SequenceI associatedSequence;
154
155     public PDBResponseSummary(JSONObject pdbJsonDoc, PDBRestRequest request)
156     {
157       Collection<PDBDocField> diplayFields = request.getWantedFields();
158       SequenceI associatedSeq = request.getAssociatedSequence();
159       int colCounter = 0;
160       summaryRowData = new Object[(associatedSeq != null) ? diplayFields
161               .size() + 1 : diplayFields.size()];
162       if (associatedSeq != null)
163       {
164         this.associatedSequence = associatedSeq;
165         summaryRowData[0] = associatedSequence;
166         colCounter = 1;
167       }
168
169       for (PDBDocField field : diplayFields)
170       {
171         String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
172                 : pdbJsonDoc.get(field.getCode()).toString();
173         if (field.equals(PDBDocField.PDB_ID))
174         {
175           this.pdbId = fieldData;
176           summaryRowData[colCounter++] = this.pdbId;
177         }
178         else
179         {
180           if (field.getGroup().getName()
181                   .equals(Group.QUALITY_MEASURES.getName()))
182           {
183             try
184             {
185               if (fieldData == null || fieldData.isEmpty())
186               {
187                 summaryRowData[colCounter++] = null;
188               }
189               else
190               {
191               Double value = Double.valueOf(fieldData);
192               summaryRowData[colCounter++] = value;
193               }
194             } catch (Exception e)
195             {
196               e.printStackTrace();
197               System.out.println("offending value:" + fieldData);
198               summaryRowData[colCounter++] = 0.0;
199             }
200           }else{
201             summaryRowData[colCounter++] = fieldData;
202           }
203         }
204       }
205     }
206
207     public Object getPdbId()
208     {
209       return pdbId;
210     }
211
212     public void setPdbId(String pdbId)
213     {
214       this.pdbId = pdbId;
215     }
216
217     public Object[] getSummaryData()
218     {
219       return summaryRowData;
220     }
221
222     public void setSummaryData(Object[] summaryData)
223     {
224       this.summaryRowData = summaryData;
225     }
226
227     /**
228      * Returns a string representation of this object;
229      */
230     @Override
231     public String toString()
232     {
233       StringBuilder summaryFieldValues = new StringBuilder();
234       for (Object summaryField : summaryRowData)
235       {
236         summaryFieldValues.append(
237                 summaryField == null ? " " : summaryField.toString())
238                 .append("\t");
239       }
240       return summaryFieldValues.toString();
241     }
242
243     /**
244      * Returns hash code value for this object
245      */
246     @Override
247     public int hashCode()
248     {
249       return Objects.hash(this.pdbId, this.toString());
250     }
251
252     /**
253      * Indicates whether some object is equal to this one
254      */
255     @Override
256     public boolean equals(Object that)
257     {
258       if (!(that instanceof PDBResponseSummary))
259       {
260         return false;
261       }
262       PDBResponseSummary another = (PDBResponseSummary) that;
263       return this.toString().equals(another.toString());
264     }
265
266   }
267
268   public static void configureTableColumn(JTable tbl_summary,
269           Collection<PDBDocField> wantedFields)
270   {
271     for (PDBDocField wantedField : wantedFields)
272     {
273       if (wantedField.equals(PDBDocField.PDB_ID))
274       {
275         tbl_summary.getColumn(wantedField.getName()).setMinWidth(40);
276         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(60);
277         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(45);
278       }
279       else if (wantedField.equals(PDBDocField.TITLE))
280       {
281         tbl_summary.getColumn(wantedField.getName()).setMinWidth(300);
282         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(1000);
283         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(400);
284       }
285       else if (wantedField.getGroup() == Group.QUALITY_MEASURES)
286       {
287         tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
288         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(150);
289         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(85);
290       }
291       else
292       {
293         tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
294         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(400);
295         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(95);
296       }
297     }
298   }
299 }