Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.fts.core

File FTSRestClient.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

48
151
36
1
507
420
98
0.65
4.19
36
2.72

Classes

Class Line # Actions
FTSRestClient 43 151 98
0.727659672.8%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ 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    package jalview.fts.core;
22   
23    import java.io.BufferedReader;
24    import java.io.IOException;
25    import java.io.InputStream;
26    import java.io.InputStreamReader;
27    import java.util.ArrayList;
28    import java.util.Collection;
29    import java.util.Objects;
30   
31    import jalview.fts.api.FTSDataColumnI;
32    import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
33    import jalview.fts.api.FTSRestClientI;
34   
35    /**
36    * Base class providing implementation for common methods defined in
37    * FTSRestClientI
38    *
39    * @author tcnofoegbu
40    *
41    * @note implementations MUST be accessed as a singleton.
42    */
 
43    public abstract class FTSRestClient implements FTSRestClientI
44    {
45    protected Collection<FTSDataColumnI> dataColumns = new ArrayList<>();
46   
47    protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<>();
48   
49    protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<>();
50   
51    protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<>();
52   
53    protected FTSDataColumnI primaryKeyColumn;
54   
55    private String primaryKeyColumnCode = null;
56   
57    private int defaultResponsePageSize = 100;
58   
 
59  17 toggle protected FTSRestClient()
60    {
61   
62    }
63   
 
64  17 toggle public void parseDataColumnsConfigFile()
65    {
66  17 String fileName = getColumnDataConfigFileName();
67   
68  17 InputStream in = getClass().getResourceAsStream(fileName);
69   
70  17 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
71    {
72  17 String line;
73  ? while ((line = br.readLine()) != null)
74    {
75  2930 final String[] lineData = line.split(";");
76  2930 try
77    {
78  2930 if (lineData.length == 2)
79    {
80  51 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
81    {
82  17 primaryKeyColumnCode = lineData[1];
83    }
84  51 if (lineData[0].equalsIgnoreCase(
85    "_data_column.default_response_page_size"))
86    {
87  17 defaultResponsePageSize = Integer.valueOf(lineData[1]);
88    }
89    }
90  2879 else if (lineData.length == 3)
91    {
92  294 dataColumnGroups.add(new FTSDataColumnGroupI()
93    {
 
94  2165 toggle @Override
95    public String getID()
96    {
97  2165 return lineData[0];
98    }
99   
 
100  235 toggle @Override
101    public String getName()
102    {
103  235 return lineData[1];
104    }
105   
 
106  1730 toggle @Override
107    public int getSortOrder()
108    {
109  1730 return Integer.valueOf(lineData[2]);
110    }
111   
 
112  1 toggle @Override
113    public String toString()
114    {
115  1 return lineData[1];
116    }
117   
 
118  212 toggle @Override
119    public int hashCode()
120    {
121  212 return Objects.hash(this.getID(), this.getName(),
122    this.getSortOrder());
123    }
124   
 
125  11 toggle @Override
126    public boolean equals(Object otherObject)
127    {
128  11 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
129  11 return this.getID().equals(that.getID())
130    && this.getName().equals(that.getName())
131    && this.getSortOrder() == that.getSortOrder();
132    }
133    });
134    }
135  2585 else if (lineData.length > 6)
136    {
137  1956 FTSDataColumnI dataCol = new FTSDataColumnI()
138    {
 
139  51 toggle @Override
140    public String toString()
141    {
142  51 return lineData[0];
143    }
144   
 
145  1225 toggle @Override
146    public String getName()
147    {
148  1225 return lineData[0];
149    }
150   
 
151  1938 toggle @Override
152    public String getCode()
153    {
154  1938 return lineData[1].split("\\|")[0];
155    }
156   
 
157  0 toggle @Override
158    public String getAltCode()
159    {
160  0 return lineData[1].split("\\|").length > 1
161    ? lineData[1].split("\\|")[1]
162    : getCode();
163    }
164   
 
165  2 toggle @Override
166    public DataTypeI getDataType()
167    {
168  2 final String[] dataTypeString = lineData[2].split("\\|");
169  2 final String classString = dataTypeString[0].toUpperCase();
170   
171  2 return new DataTypeI()
172    {
173   
 
174  0 toggle @Override
175    public boolean isFormtted()
176    {
177  0 if (dataTypeString.length > 1
178    && dataTypeString[1] != null)
179    {
180  0 switch (dataTypeString[1].toUpperCase())
181    {
182  0 case "T":
183  0 case "TRUE":
184  0 return true;
185  0 case "F":
186  0 case "False":
187  0 default:
188  0 return false;
189    }
190    }
191  0 return false;
192    }
193   
 
194  0 toggle @Override
195    public int getSignificantFigures()
196    {
197  0 if (dataTypeString.length > 2
198    && dataTypeString[2] != null)
199    {
200  0 return Integer.valueOf(dataTypeString[2]);
201    }
202  0 return 0;
203    }
204   
 
205  2 toggle @Override
206    public Class getDataTypeClass()
207    {
208  2 switch (classString)
209    {
210  1 case "INT":
211  0 case "INTEGER":
212  1 return Integer.class;
213  0 case "DOUBLE":
214  0 return Double.class;
215  1 case "STRING":
216  0 default:
217  1 return String.class;
218    }
219    }
220    };
221   
222    }
223   
 
224  400 toggle @Override
225    public FTSDataColumnGroupI getGroup()
226    {
227  400 FTSDataColumnGroupI group = null;
228  400 try
229    {
230  400 group = getDataColumnGroupById(lineData[3]);
231    } catch (Exception e)
232    {
233  0 e.printStackTrace();
234    }
235  400 return group;
236    }
237   
 
238  1 toggle @Override
239    public int getMinWidth()
240    {
241  1 return Integer.valueOf(lineData[4]);
242    }
243   
 
244  1 toggle @Override
245    public int getMaxWidth()
246    {
247  1 return Integer.valueOf(lineData[5]);
248    }
249   
 
250  1 toggle @Override
251    public int getPreferredWidth()
252    {
253  1 return Integer.valueOf(lineData[6]);
254    }
255   
 
256  12 toggle @Override
257    public boolean isPrimaryKeyColumn()
258    {
259  12 return getName().equalsIgnoreCase(primaryKeyColumnCode)
260    || getCode().equalsIgnoreCase(primaryKeyColumnCode);
261    }
262   
 
263  1956 toggle @Override
264    public boolean isVisibleByDefault()
265    {
266  1956 return Boolean.valueOf(lineData[7]);
267    }
268   
 
269  1956 toggle @Override
270    public boolean isSearchable()
271    {
272  1956 return Boolean.valueOf(lineData[8]);
273    }
274   
 
275  212 toggle @Override
276    public int hashCode()
277    {
278  212 return Objects.hash(this.getName(), this.getCode(),
279    this.getGroup());
280    }
281   
 
282  655 toggle @Override
283    public boolean equals(Object otherObject)
284    {
285  655 FTSDataColumnI that = (FTSDataColumnI) otherObject;
286  655 return otherObject == null ? false
287    : this.getCode().equals(that.getCode())
288    && this.getName().equals(that.getName())
289    && this.getGroup().equals(that.getGroup());
290    }
291   
292    };
293  1956 dataColumns.add(dataCol);
294   
295  1956 if (dataCol.isSearchable())
296    {
297  361 searchableDataColumns.add(dataCol);
298    }
299   
300  1956 if (dataCol.isVisibleByDefault())
301    {
302  116 defaulDisplayedDataColumns.add(dataCol);
303    }
304   
305    }
306    else
307    {
308  629 continue;
309    }
310    } catch (Exception e)
311    {
312  0 e.printStackTrace();
313    }
314    }
315  17 try
316    {
317  17 this.primaryKeyColumn = getDataColumnByNameOrCode(
318    primaryKeyColumnCode);
319    } catch (Exception e)
320    {
321  0 e.printStackTrace();
322    }
323    } catch (IOException e)
324    {
325  0 e.printStackTrace();
326    }
327    }
328   
 
329  4 toggle @Override
330    public int getPrimaryKeyColumIndex(
331    Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
332    throws Exception
333    {
334   
335    // If a reference sequence is attached then start counting from 1 else
336    // start from zero
337  4 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
338   
339  4 for (FTSDataColumnI field : wantedFields)
340    {
341  12 if (field.isPrimaryKeyColumn())
342    {
343  4 break; // Once PDB Id index is determined exit iteration
344    }
345  8 ++pdbFieldIndexCounter;
346    }
347  4 return pdbFieldIndexCounter;
348    }
349   
 
350  3 toggle @Override
351    public String getDataColumnsFieldsAsCommaDelimitedString(
352    Collection<FTSDataColumnI> dataColumnFields)
353    {
354  3 String result = "";
355  3 if (dataColumnFields != null && !dataColumnFields.isEmpty())
356    {
357  3 StringBuilder returnedFields = new StringBuilder();
358  3 for (FTSDataColumnI field : dataColumnFields)
359    {
360  16 returnedFields.append(",").append(field.getCode());
361    }
362  3 returnedFields.deleteCharAt(0);
363  3 result = returnedFields.toString();
364    }
365  3 return result;
366    }
367   
 
368  3 toggle @Override
369    public Collection<FTSDataColumnI> getAllFTSDataColumns()
370    {
371  3 if (dataColumns == null || dataColumns.isEmpty())
372    {
373  1 parseDataColumnsConfigFile();
374    }
375  3 return dataColumns;
376    }
377   
 
378  4 toggle @Override
379    public Collection<FTSDataColumnI> getSearchableDataColumns()
380    {
381  4 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
382    {
383  2 parseDataColumnsConfigFile();
384    }
385  4 return searchableDataColumns;
386    }
387   
 
388  89 toggle @Override
389    public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
390    {
391  89 if (defaulDisplayedDataColumns == null
392    || defaulDisplayedDataColumns.isEmpty())
393    {
394  4 parseDataColumnsConfigFile();
395    }
396  89 return defaulDisplayedDataColumns;
397    }
398   
 
399  3 toggle @Override
400    public FTSDataColumnI getPrimaryKeyColumn()
401    {
402  3 if (defaulDisplayedDataColumns == null
403    || defaulDisplayedDataColumns.isEmpty())
404    {
405  1 parseDataColumnsConfigFile();
406    }
407  3 return primaryKeyColumn;
408    }
409   
 
410  36 toggle @Override
411    public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
412    throws Exception
413    {
414  36 if (dataColumns == null || dataColumns.isEmpty())
415    {
416  7 parseDataColumnsConfigFile();
417    }
418  36 for (FTSDataColumnI column : dataColumns)
419    {
420  398 if (column.getName().equalsIgnoreCase(nameOrCode)
421    || column.getCode().equalsIgnoreCase(nameOrCode))
422    {
423  34 return column;
424    }
425    }
426  2 throw new Exception(
427    "Couldn't find data column with name : " + nameOrCode);
428    }
429   
 
430  404 toggle @Override
431    public FTSDataColumnGroupI getDataColumnGroupById(String id)
432    throws Exception
433    {
434  404 if (dataColumns == null || dataColumns.isEmpty())
435    {
436  1 parseDataColumnsConfigFile();
437    }
438  404 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
439    {
440  1931 if (columnGroup.getID().equalsIgnoreCase(id))
441    {
442  402 return columnGroup;
443    }
444    }
445  2 throw new Exception("Couldn't find data column group with id : " + id);
446    }
447   
 
448  0 toggle public static String getMessageByHTTPStatusCode(int code, String service)
449    {
450  0 String message = "";
451  0 switch (code)
452    {
453  0 case 400:
454  0 message = "Bad request. There is a problem with your input.";
455  0 break;
456   
457  0 case 410:
458  0 message = service + " rest services no longer available!";
459  0 break;
460  0 case 403:
461  0 case 404:
462  0 message = "The requested resource could not be found";
463  0 break;
464  0 case 408:
465  0 case 409:
466  0 case 500:
467  0 case 501:
468  0 case 502:
469  0 case 504:
470  0 case 505:
471  0 message = "There seems to be an error from the " + service
472    + " server";
473  0 break;
474  0 case 503:
475  0 message = "Service not available. The server is being updated, try again later.";
476  0 break;
477  0 default:
478  0 break;
479    }
480  0 return String.valueOf(code) + " " + message;
481    }
482   
 
483  1 toggle protected String getResourceFile(String fileName)
484    {
485  1 String result = "";
486  1 try
487    {
488  1 result = getClass().getResource(fileName).getFile();
489    } catch (Exception e)
490    {
491  0 e.printStackTrace();
492    }
493  1 return result;
494   
495    }
496   
 
497  2 toggle @Override
498    public int getDefaultResponsePageSize()
499    {
500  2 if (dataColumns == null || dataColumns.isEmpty())
501    {
502  1 parseDataColumnsConfigFile();
503    }
504  2 return defaultResponsePageSize;
505    }
506   
507    }