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