3471fab0ae4e8e233df672a38c21630b82b6ac4d
[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 == null || fieldData
202                     .isEmpty()) ? null : fieldData;
203           }
204         }
205       }
206     }
207
208     public Object getPdbId()
209     {
210       return pdbId;
211     }
212
213     public void setPdbId(String pdbId)
214     {
215       this.pdbId = pdbId;
216     }
217
218     public Object[] getSummaryData()
219     {
220       return summaryRowData;
221     }
222
223     public void setSummaryData(Object[] summaryData)
224     {
225       this.summaryRowData = summaryData;
226     }
227
228     /**
229      * Returns a string representation of this object;
230      */
231     @Override
232     public String toString()
233     {
234       StringBuilder summaryFieldValues = new StringBuilder();
235       for (Object summaryField : summaryRowData)
236       {
237         summaryFieldValues.append(
238                 summaryField == null ? " " : summaryField.toString())
239                 .append("\t");
240       }
241       return summaryFieldValues.toString();
242     }
243
244     /**
245      * Returns hash code value for this object
246      */
247     @Override
248     public int hashCode()
249     {
250       return Objects.hash(this.pdbId, this.toString());
251     }
252
253     /**
254      * Indicates whether some object is equal to this one
255      */
256     @Override
257     public boolean equals(Object that)
258     {
259       if (!(that instanceof PDBResponseSummary))
260       {
261         return false;
262       }
263       PDBResponseSummary another = (PDBResponseSummary) that;
264       return this.toString().equals(another.toString());
265     }
266
267   }
268
269   public static void configureTableColumn(JTable tbl_summary,
270           Collection<PDBDocField> wantedFields)
271   {
272     try
273     {
274       // wait for table model initialisation to complete
275       Thread.sleep(1200);
276     } catch (InterruptedException e1)
277     {
278       e1.printStackTrace();
279     }
280     for (PDBDocField wantedField : wantedFields)
281     {
282       try
283       {
284       if (wantedField.equals(PDBDocField.PDB_ID))
285       {
286         tbl_summary.getColumn(wantedField.getName()).setMinWidth(40);
287         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(60);
288         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(45);
289       }
290       else if (wantedField.equals(PDBDocField.TITLE))
291       {
292         tbl_summary.getColumn(wantedField.getName()).setMinWidth(300);
293         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(1000);
294         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(400);
295       }
296       else if (wantedField.getGroup() == Group.QUALITY_MEASURES)
297       {
298         tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
299         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(150);
300         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(85);
301       }
302       else
303       {
304         tbl_summary.getColumn(wantedField.getName()).setMinWidth(50);
305         tbl_summary.getColumn(wantedField.getName()).setMaxWidth(400);
306         tbl_summary.getColumn(wantedField.getName()).setPreferredWidth(95);
307       }
308       } catch (Exception e)
309       {
310         e.printStackTrace();
311       }
312     }
313   }
314 }