Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 10:11:34 GMT
  2. Package jalview.fts.core

File FTSRestClient.java

 

Coverage histogram

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

Code metrics

50
168
41
1
565
465
105
0.62
4.1
41
2.56

Classes

Class Line # Actions
FTSRestClient 46 168 105
0.783783878.4%
 

Contributing tests

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