889a324fa861273221b7f5d14987272d24ac4c52
[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.Objects;
28
29 import javax.swing.DefaultListModel;
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    * Convenience method to obtain a List model for a given summary list
81    * 
82    * @param summariesList
83    *          the summary list which contains the data for generating the lists
84    *          data
85    * @return the list model generated for the search summary
86    */
87   public static DefaultListModel<PDBResponseSummary> getListModel(
88           Collection<PDBResponseSummary> summariesList)
89   {
90     DefaultListModel<PDBResponseSummary> defaultListModel = new DefaultListModel<PDBResponseSummary>();
91     for (PDBResponseSummary summaryList : summariesList)
92     {
93       defaultListModel.addElement(summaryList);
94     }
95     return defaultListModel;
96   }
97
98   /**
99    * Convenience method to obtain a Table model for a given summary List and
100    * request
101    * 
102    * @param request
103    *          the PDBRestRequest object which holds useful information for
104    *          creating a table model
105    * @param summariesList
106    *          the summary list which contains the data for populating the
107    *          table's rows
108    * @return the table model which was dynamically generated
109    */
110   public static DefaultTableModel getTableModel(PDBRestRequest request,
111           Collection<PDBResponseSummary> summariesList)
112   {
113     DefaultTableModel model = new DefaultTableModel();
114
115     if (request.getAssociatedSequence() != null)
116     {
117       model.addColumn("Sequence");
118     }
119     for (PDBDocField field : request.getWantedFields())
120     {
121       model.addColumn(field.getName());
122     }
123
124     for (PDBResponseSummary res : summariesList)
125     {
126       model.addRow(res.getSummaryData());
127     }
128     return model;
129   }
130
131   /**
132    * Model for a unique response summary
133    * 
134    * @author tcnofoegbu
135    *
136    */
137   public class PDBResponseSummary
138   {
139     private String pdbId;
140
141     private String concatenatedSummaryData;
142
143     private String[] summaryData;
144
145     private String associatedSequence;
146
147     private int width = 480;
148
149     public PDBResponseSummary(JSONObject doc, PDBRestRequest request)
150     {
151        StringBuilder summaryBuilder = new StringBuilder();
152       Collection<PDBDocField> diplayFields = request.getWantedFields();
153       String associatedSeq = request.getAssociatedSequence();
154       int colCounter = 0;
155       summaryData = new String[(associatedSeq != null) ? diplayFields
156               .size() + 1 : diplayFields.size()];
157       if (associatedSeq != null)
158       {
159         this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
160                 .substring(0, 18) : associatedSeq;
161         summaryData[0] = associatedSequence;
162         colCounter = 1;
163       }
164
165       for (PDBDocField field : diplayFields)
166       {
167         if (field.equals(PDBDocField.PDB_ID)
168                 && doc.get(PDBDocField.PDB_ID.getCode()) != null)
169         {
170           this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString();
171           summaryData[colCounter++] = this.pdbId;
172         }
173         else
174         {
175           String value = (doc.get(field.getCode()) == null) ? "" : doc.get(
176                   field.getCode()).toString();
177            summaryBuilder.append(field.getName()).append(": ").append(value)
178                   .append(" | ");
179           summaryData[colCounter++] = value;
180         }
181       }
182       this.concatenatedSummaryData = summaryBuilder.toString();
183       summaryBuilder = null;
184     }
185
186     public String getPdbId()
187     {
188       return pdbId;
189     }
190
191     public void setPdbId(String pdbId)
192     {
193       this.pdbId = pdbId;
194     }
195
196     public String getConcatenatedSummaryData()
197     {
198       return concatenatedSummaryData;
199     }
200
201     public void setConcatenatedSummaryData(String concatenatedSummaryData)
202     {
203       this.concatenatedSummaryData = concatenatedSummaryData;
204     }
205
206     public String[] getSummaryData()
207     {
208       return summaryData;
209     }
210
211     public void setSummaryData(String[] summaryData)
212     {
213       this.summaryData = summaryData;
214     }
215
216     public String toString()
217     {
218       StringBuilder html = new StringBuilder();
219       html.append("<html><div style=\"width:" + width
220               + "; word-wrap: break-word; border-bottom-style: dotted;\"> ");
221       html.append(concatenatedSummaryData);
222       html.append("</div></html>");
223       return html.toString();
224     }
225
226     @Override
227     public int hashCode()
228     {
229       return Objects.hash(this.pdbId, this.concatenatedSummaryData);
230     }
231
232     @Override
233     public boolean equals(Object other)
234     {
235       if (!(other instanceof PDBResponseSummary))
236       {
237         return false;
238       }
239
240       PDBResponseSummary that = (PDBResponseSummary) other;
241
242       // Custom equality check here.
243       return this.pdbId.equals(that.pdbId)
244               && this.concatenatedSummaryData
245                       .equals(that.concatenatedSummaryData);
246     }
247
248   }
249
250 }
251