JAL-1668 added filtering and sorting capabilites
[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.ws.dbsources.PDBRestClient.PDBDocField;
25
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Objects;
29
30 import javax.swing.DefaultListModel;
31 import javax.swing.table.DefaultTableModel;
32
33 import org.json.simple.JSONObject;
34
35 /**
36  * Represents the response model produced by the PDBRestClient upon successful
37  * execution of a given request
38  * 
39  * @author tcnofoegbu
40  *
41  */
42 public class PDBRestResponse
43 {
44   private int numberOfItemsFound;
45
46   private String responseTime;
47
48   private Collection<PDBResponseSummary> searchSummary;
49
50   public int getNumberOfItemsFound()
51   {
52     return numberOfItemsFound;
53   }
54
55   public void setNumberOfItemsFound(int itemFound)
56   {
57     this.numberOfItemsFound = itemFound;
58   }
59
60   public String getResponseTime()
61   {
62     return responseTime;
63   }
64
65   public void setResponseTime(String responseTime)
66   {
67     this.responseTime = responseTime;
68   }
69
70   public Collection<PDBResponseSummary> getSearchSummary()
71   {
72     return searchSummary;
73   }
74
75   public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
76   {
77     this.searchSummary = searchSummary;
78   }
79
80   /**
81    * Convenience method to obtain a List model for a given summary list
82    * 
83    * @param summariesList
84    *          the summary list which contains the data for generating the lists
85    *          data
86    * @return the list model generated for the search summary
87    */
88   public static DefaultListModel<PDBResponseSummary> getListModel(
89           Collection<PDBResponseSummary> summariesList)
90   {
91     DefaultListModel<PDBResponseSummary> defaultListModel = new DefaultListModel<PDBResponseSummary>();
92     for (PDBResponseSummary summaryList : summariesList)
93     {
94       defaultListModel.addElement(summaryList);
95     }
96     return defaultListModel;
97   }
98
99   /**
100    * Convenience method to obtain a Table model for a given summary List and
101    * request
102    * 
103    * @param request
104    *          the PDBRestRequest object which holds useful information for
105    *          creating a table model
106    * @param summariesList
107    *          the summary list which contains the data for populating the
108    *          table's rows
109    * @return the table model which was dynamically generated
110    */
111   public static DefaultTableModel getTableModel(PDBRestRequest request,
112           Collection<PDBResponseSummary> summariesList)
113   {
114     DefaultTableModel model = new DefaultTableModel();
115
116     if (request.getAssociatedSequence() != null)
117     {
118       model.addColumn("Sequence");
119     }
120     for (PDBDocField field : request.getWantedFields())
121     {
122       model.addColumn(field.getName());
123     }
124
125     for (PDBResponseSummary res : summariesList)
126     {
127       model.addRow(res.getSummaryData());
128     }
129     return model;
130   }
131
132   /**
133    * Model for a unique response summary
134    * 
135    * @author tcnofoegbu
136    *
137    */
138   public class PDBResponseSummary
139   {
140     private String pdbId;
141
142     private String concatenatedSummaryData;
143
144     private String[] summaryData;
145
146     private String associatedSequence;
147
148     private int width = 480;
149
150     public PDBResponseSummary(JSONObject doc, PDBRestRequest request)
151     {
152       StringBuilder summaryBuilder = new StringBuilder();
153       List<PDBDocField> diplayFields = request.getWantedFields();
154       String associatedSeq = request.getAssociatedSequence();
155       int colCounter = 0;
156       summaryData = new String[(associatedSeq != null) ? diplayFields
157               .size() + 1 : diplayFields.size()];
158       if (associatedSeq != null)
159       {
160         this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
161                 .substring(0, 18) : associatedSeq;
162         summaryData[0] = associatedSequence;
163         colCounter = 1;
164       }
165
166       for (PDBDocField field : diplayFields)
167       {
168         if (field.equals(PDBDocField.MOLECULE_TYPE)
169                 && doc.get(PDBDocField.MOLECULE_TYPE.getCode()) != null)
170         {
171           String moleculeType = doc
172                   .get(PDBDocField.MOLECULE_TYPE.getCode()).toString();
173           if (moleculeType.equalsIgnoreCase("protein"))
174           {
175             summaryBuilder.append("<img src=\""
176                     + getClass().getResource("/images/protein.png")
177                             .toString() + "\">");
178           }
179           if (moleculeType.equalsIgnoreCase("dna"))
180           {
181             summaryBuilder.append("<img src=\""
182                     + getClass().getResource("/images/dna.png").toString()
183                     + "\">");
184           }
185           if (moleculeType.equalsIgnoreCase("rna"))
186           {
187             summaryBuilder.append("<img src=\""
188                     + getClass().getResource("/images/dna.png").toString()
189                     + "\">");
190           }
191           if (moleculeType.equalsIgnoreCase("sugar"))
192           {
193             summaryBuilder.append("<img src=\""
194                     + getClass().getResource("/images/sugar.png")
195                             .toString() + "\">");
196           }
197           summaryData[colCounter++] = moleculeType;
198         }
199         else if (field.equals(PDBDocField.PDB_ID)
200                 && doc.get(PDBDocField.PDB_ID.getCode()) != null)
201         {
202           this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString();
203           summaryBuilder.append(this.pdbId).append(" | ");
204           summaryData[colCounter++] = this.pdbId;
205         }
206         else if (doc.get(field.getCode()) != null)
207         {
208           summaryBuilder.append(field.getName()).append(": ")
209                   .append(doc.get(field.getCode())).append(" | ");
210           summaryData[colCounter++] = doc.get(field.getCode()).toString();
211         }
212       }
213       int endIndex = summaryBuilder.lastIndexOf(" | ");
214       String fSummary = summaryBuilder.toString().substring(0, endIndex);
215       this.concatenatedSummaryData = fSummary.trim();
216       summaryBuilder = null;
217     }
218
219     public String getPdbId()
220     {
221       return pdbId;
222     }
223
224     public void setPdbId(String pdbId)
225     {
226       this.pdbId = pdbId;
227     }
228
229     public String getConcatenatedSummaryData()
230     {
231       return concatenatedSummaryData;
232     }
233
234     public void setConcatenatedSummaryData(String concatenatedSummaryData)
235     {
236       this.concatenatedSummaryData = concatenatedSummaryData;
237     }
238
239     public String[] getSummaryData()
240     {
241       return summaryData;
242     }
243
244     public void setSummaryData(String[] summaryData)
245     {
246       this.summaryData = summaryData;
247     }
248
249     public String toString()
250     {
251       StringBuilder html = new StringBuilder();
252       html.append("<html><div style=\"width:" + width
253               + "; word-wrap: break-word; border-bottom-style: dotted;\"> ");
254       html.append(concatenatedSummaryData);
255       html.append("</div></html>");
256       return html.toString();
257     }
258
259     @Override
260     public int hashCode()
261     {
262       return Objects.hash(this.pdbId, this.concatenatedSummaryData);
263     }
264
265     @Override
266     public boolean equals(Object other)
267     {
268       if (!(other instanceof PDBResponseSummary))
269       {
270         return false;
271       }
272
273       PDBResponseSummary that = (PDBResponseSummary) other;
274
275       // Custom equality check here.
276       return this.pdbId.equals(that.pdbId)
277               && this.concatenatedSummaryData
278                       .equals(that.concatenatedSummaryData);
279     }
280
281   }
282
283 }
284