3b5b05c7489b6701e70c039f40da56d2a95a56d4
[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.table.DefaultTableModel;
30
31 import org.json.simple.JSONObject;
32
33 /**
34  * Represents the response model produced by the PDBRestClient upon successful
35  * execution of a given request
36  * 
37  * @author tcnofoegbu
38  *
39  */
40 public class PDBRestResponse
41 {
42   private int numberOfItemsFound;
43
44   private String responseTime;
45
46   private Collection<PDBResponseSummary> searchSummary;
47
48   public int getNumberOfItemsFound()
49   {
50     return numberOfItemsFound;
51   }
52
53   public void setNumberOfItemsFound(int itemFound)
54   {
55     this.numberOfItemsFound = itemFound;
56   }
57
58   public String getResponseTime()
59   {
60     return responseTime;
61   }
62
63   public void setResponseTime(String responseTime)
64   {
65     this.responseTime = responseTime;
66   }
67
68   public Collection<PDBResponseSummary> getSearchSummary()
69   {
70     return searchSummary;
71   }
72
73   public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
74   {
75     this.searchSummary = searchSummary;
76   }
77
78
79   /**
80    * Convenience method to obtain a Table model for a given summary List based
81    * on the request parameters
82    * 
83    * @param request
84    *          the PDBRestRequest object which holds useful information for
85    *          creating a table model
86    * @param summariesList
87    *          the summary list which contains the data for populating the
88    *          table's rows
89    * @return the table model which was dynamically generated
90    */
91   public static DefaultTableModel getTableModel(PDBRestRequest request,
92           Collection<PDBResponseSummary> summariesList)
93   {
94     DefaultTableModel tableModel = new DefaultTableModel();
95
96     if (request.getAssociatedSequence() != null)
97     {
98       tableModel.addColumn("Sequence"); // Create sequence column header if
99                                         // exists in the request
100     }
101     for (PDBDocField field : request.getWantedFields())
102     {
103       tableModel.addColumn(field.getName()); // Create sequence column header if
104                                              // exists in the request
105     }
106
107     for (PDBResponseSummary res : summariesList)
108     {
109       tableModel.addRow(res.getSummaryData()); // Populate table rows with
110                                                // summary list
111     }
112
113     return tableModel;
114   }
115
116   /**
117    * Model for a unique response summary
118    * 
119    */
120   public class PDBResponseSummary
121   {
122     private String pdbId;
123
124     private String[] summaryRowData;
125
126     private String associatedSequence;
127
128     public PDBResponseSummary(JSONObject pdbJsonDoc, PDBRestRequest request)
129     {
130       Collection<PDBDocField> diplayFields = request.getWantedFields();
131       String associatedSeq = request.getAssociatedSequence();
132       int colCounter = 0;
133       summaryRowData = new String[(associatedSeq != null) ? diplayFields
134               .size() + 1 : diplayFields.size()];
135       if (associatedSeq != null)
136       {
137         this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq
138                 .substring(0, 18) : associatedSeq;
139         summaryRowData[0] = associatedSequence;
140         colCounter = 1;
141       }
142
143       for (PDBDocField field : diplayFields)
144       {
145         String fieldData = (pdbJsonDoc.get(field.getCode()) == null) ? ""
146                 : pdbJsonDoc
147                 .get(field.getCode()).toString();
148         if (field.equals(PDBDocField.PDB_ID))
149         {
150           this.pdbId = fieldData;
151           summaryRowData[colCounter++] = this.pdbId;
152         }
153         else
154         {
155           summaryRowData[colCounter++] = fieldData;
156         }
157       }
158     }
159
160     public String getPdbId()
161     {
162       return pdbId;
163     }
164
165     public void setPdbId(String pdbId)
166     {
167       this.pdbId = pdbId;
168     }
169
170     public String[] getSummaryData()
171     {
172       return summaryRowData;
173     }
174
175     public void setSummaryData(String[] summaryData)
176     {
177       this.summaryRowData = summaryData;
178     }
179
180     /**
181      * Returns a string representation of this object;
182      */
183     @Override
184     public String toString()
185     {
186       StringBuilder summaryFieldValues = new StringBuilder();
187       for (String summaryField : summaryRowData)
188       {
189         summaryFieldValues.append(summaryField).append("\t");
190       }
191       return summaryFieldValues.toString();
192     }
193
194     /**
195      * Returns hash code value for this object
196      */
197     @Override
198     public int hashCode()
199     {
200       return Objects.hash(this.pdbId, this.toString());
201     }
202
203     /**
204      * Indicates whether some object is equal to this one
205      */
206     @Override
207     public boolean equals(Object that)
208     {
209       if (!(that instanceof PDBResponseSummary))
210       {
211         return false;
212       }
213       PDBResponseSummary another = (PDBResponseSummary) that;
214       return this.toString().equals(another.toString());
215     }
216
217   }
218
219 }
220