JAL-1667 refactoring to support JAL-1668
[jalview.git] / src / jalview / ws / uimodel / PDBSearchResponse.java
1 package jalview.ws.uimodel;
2
3 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
4
5 import java.util.Collection;
6 import java.util.List;
7 import java.util.Objects;
8
9 import javax.swing.DefaultListModel;
10
11 import org.json.simple.JSONObject;
12
13 public class PDBSearchResponse
14 {
15   private int itemsFound;
16
17   private String responseTime;
18
19   private Collection<PDBResponseSummary> searchSummary;
20
21   public int getItemsFound()
22   {
23     return itemsFound;
24   }
25
26   public void setItemsFound(int itemFound)
27   {
28     this.itemsFound = itemFound;
29   }
30
31   public String getResponseTime()
32   {
33     return responseTime;
34   }
35
36   public void setResponseTime(String responseTime)
37   {
38     this.responseTime = responseTime;
39   }
40
41   public Collection<PDBResponseSummary> getSearchSummary()
42   {
43     return searchSummary;
44   }
45
46   public void setSearchSummary(Collection<PDBResponseSummary> searchSummary)
47   {
48     this.searchSummary = searchSummary;
49   }
50
51   public static DefaultListModel<PDBResponseSummary> getListModel(
52           Collection<PDBResponseSummary> summariesList)
53   {
54     DefaultListModel<PDBResponseSummary> defaultListModel = new DefaultListModel<PDBResponseSummary>();
55     for (PDBResponseSummary summaryList : summariesList)
56     {
57       defaultListModel.addElement(summaryList);
58     }
59     return defaultListModel;
60   }
61
62   public class PDBResponseSummary
63   {
64     private String pdbId;
65
66     private String summary;
67
68     private int width = 480;
69
70     public PDBResponseSummary(JSONObject doc, List<PDBDocField> diplayFields)
71     {
72       StringBuilder summaryBuilder = new StringBuilder();
73
74       for (PDBDocField field : diplayFields)
75       {
76         if (field.equals(PDBDocField.MOLECULE_TYPE)
77                 && doc.get(PDBDocField.MOLECULE_TYPE.getCode()) != null)
78         {
79           String moleculeType = doc
80                   .get(PDBDocField.MOLECULE_TYPE.getCode()).toString();
81           if (moleculeType.equalsIgnoreCase("protein"))
82           {
83             summaryBuilder.append("<img src=\""
84                     + getClass().getResource("/images/protein.png")
85                             .toString() + "\">");
86           }
87           if (moleculeType.equalsIgnoreCase("dna"))
88           {
89             summaryBuilder.append("<img src=\""
90                     + getClass().getResource("/images/dna.png").toString()
91                     + "\">");
92           }
93           if (moleculeType.equalsIgnoreCase("rna"))
94           {
95             summaryBuilder.append("<img src=\""
96                     + getClass().getResource("/images/dna.png").toString()
97                     + "\">");
98           }
99           if (moleculeType.equalsIgnoreCase("sugar"))
100           {
101             summaryBuilder.append("<img src=\""
102                     + getClass().getResource("/images/sugar.png")
103                             .toString() + "\">");
104           }
105         }
106         else if (field.equals(PDBDocField.PDB_ID)
107                 && doc.get(PDBDocField.PDB_ID.getCode()) != null)
108         {
109           this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString();
110         }
111         else if (doc.get(field.getCode()) != null)
112         {
113           summaryBuilder.append(field.getName()).append(": ")
114                   .append(doc.get(field.getCode())).append(" | ");
115         }
116       }
117       int endIndex = summaryBuilder.lastIndexOf(" | ");
118       String fSummary = summaryBuilder.toString().substring(0, endIndex);
119       this.summary = fSummary.trim();
120       summaryBuilder = null;
121     }
122
123     public String getPdbId()
124     {
125       return pdbId;
126     }
127
128     public void setPdbId(String pdbId)
129     {
130       this.pdbId = pdbId;
131     }
132
133     public String getSummary()
134     {
135       return summary;
136     }
137
138     public void setSummary(String summary)
139     {
140       this.summary = summary;
141     }
142
143     public String toString()
144     {
145       StringBuilder html = new StringBuilder();
146       html.append("<html><div style=\"width:" + width
147               + "; word-wrap: break-word; border-bottom-style: dotted;\"> ");
148       html.append(summary);
149       html.append("</div></html>");
150       return html.toString();
151     }
152
153     @Override
154     public int hashCode()
155     {
156       return Objects.hash(this.pdbId, this.summary);
157     }
158   }
159
160 }
161