2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.fts.core;
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;
31 import jalview.fts.api.FTSDataColumnI;
32 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
33 import jalview.fts.api.FTSRestClientI;
36 * Base class providing implementation for common methods defined in
41 * @note implementations MUST be accessed as a singleton.
43 public abstract class FTSRestClient implements FTSRestClientI
45 protected Collection<FTSDataColumnI> dataColumns = new ArrayList<>();
47 protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<>();
49 protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<>();
51 protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<>();
53 protected FTSDataColumnI primaryKeyColumn;
55 private String primaryKeyColumnCode = null;
57 private int defaultResponsePageSize = 100;
59 protected FTSRestClient()
64 public void parseDataColumnsConfigFile()
66 String fileName = getColumnDataConfigFileName();
68 InputStream in = getClass().getResourceAsStream(fileName);
70 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
73 while ((line = br.readLine()) != null)
75 final String[] lineData = line.split(";");
78 if (lineData.length == 2)
80 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
82 primaryKeyColumnCode = lineData[1];
84 if (lineData[0].equalsIgnoreCase(
85 "_data_column.default_response_page_size"))
87 defaultResponsePageSize = Integer.valueOf(lineData[1]);
90 else if (lineData.length == 3)
92 dataColumnGroups.add(new FTSDataColumnGroupI()
101 public String getName()
107 public int getSortOrder()
109 return Integer.valueOf(lineData[2]);
113 public String toString()
119 public int hashCode()
121 return Objects.hash(this.getID(), this.getName(),
122 this.getSortOrder());
126 public boolean equals(Object otherObject)
128 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
129 return this.getID().equals(that.getID())
130 && this.getName().equals(that.getName())
131 && this.getSortOrder() == that.getSortOrder();
135 else if (lineData.length > 6)
137 FTSDataColumnI dataCol = new FTSDataColumnI()
140 public String toString()
146 public String getName()
152 public String getCode()
154 return lineData[1].split("\\|")[0];
158 public String getAltCode()
160 return lineData[1].split("\\|").length > 1
161 ? lineData[1].split("\\|")[1]
166 public DataTypeI getDataType()
168 final String[] dataTypeString = lineData[2].split("\\|");
169 final String classString = dataTypeString[0].toUpperCase();
171 return new DataTypeI()
175 public boolean isFormtted()
177 if (dataTypeString.length > 1
178 && dataTypeString[1] != null)
180 switch (dataTypeString[1].toUpperCase())
195 public int getSignificantFigures()
197 if (dataTypeString.length > 2
198 && dataTypeString[2] != null)
200 return Integer.valueOf(dataTypeString[2]);
206 public Class getDataTypeClass()
212 return Integer.class;
225 public FTSDataColumnGroupI getGroup()
227 FTSDataColumnGroupI group = null;
230 group = getDataColumnGroupById(lineData[3]);
231 } catch (Exception e)
239 public int getMinWidth()
241 return Integer.valueOf(lineData[4]);
245 public int getMaxWidth()
247 return Integer.valueOf(lineData[5]);
251 public int getPreferredWidth()
253 return Integer.valueOf(lineData[6]);
257 public boolean isPrimaryKeyColumn()
259 return getName().equalsIgnoreCase(primaryKeyColumnCode)
260 || getCode().equalsIgnoreCase(primaryKeyColumnCode);
264 public boolean isVisibleByDefault()
266 return Boolean.valueOf(lineData[7]);
270 public boolean isSearchable()
272 return Boolean.valueOf(lineData[8]);
276 public int hashCode()
278 return Objects.hash(this.getName(), this.getCode(),
283 public boolean equals(Object otherObject)
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());
293 dataColumns.add(dataCol);
295 if (dataCol.isSearchable())
297 searchableDataColumns.add(dataCol);
300 if (dataCol.isVisibleByDefault())
302 defaulDisplayedDataColumns.add(dataCol);
310 } catch (Exception e)
317 this.primaryKeyColumn = getDataColumnByNameOrCode(
318 primaryKeyColumnCode);
319 } catch (Exception e)
323 } catch (IOException e)
330 public int getPrimaryKeyColumIndex(
331 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
335 // If a reference sequence is attached then start counting from 1 else
337 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
339 for (FTSDataColumnI field : wantedFields)
341 if (field.isPrimaryKeyColumn())
343 break; // Once PDB Id index is determined exit iteration
345 ++pdbFieldIndexCounter;
347 return pdbFieldIndexCounter;
351 public String getDataColumnsFieldsAsCommaDelimitedString(
352 Collection<FTSDataColumnI> dataColumnFields)
355 if (dataColumnFields != null && !dataColumnFields.isEmpty())
357 StringBuilder returnedFields = new StringBuilder();
358 for (FTSDataColumnI field : dataColumnFields)
360 returnedFields.append(",").append(field.getCode());
362 returnedFields.deleteCharAt(0);
363 result = returnedFields.toString();
369 public Collection<FTSDataColumnI> getAllFTSDataColumns()
371 if (dataColumns == null || dataColumns.isEmpty())
373 parseDataColumnsConfigFile();
379 public Collection<FTSDataColumnI> getSearchableDataColumns()
381 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
383 parseDataColumnsConfigFile();
385 return searchableDataColumns;
389 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
391 if (defaulDisplayedDataColumns == null
392 || defaulDisplayedDataColumns.isEmpty())
394 parseDataColumnsConfigFile();
396 return defaulDisplayedDataColumns;
400 public FTSDataColumnI getPrimaryKeyColumn()
402 if (defaulDisplayedDataColumns == null
403 || defaulDisplayedDataColumns.isEmpty())
405 parseDataColumnsConfigFile();
407 return primaryKeyColumn;
411 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
414 if (dataColumns == null || dataColumns.isEmpty())
416 parseDataColumnsConfigFile();
418 for (FTSDataColumnI column : dataColumns)
420 if (column.getName().equalsIgnoreCase(nameOrCode)
421 || column.getCode().equalsIgnoreCase(nameOrCode))
427 "Couldn't find data column with name : " + nameOrCode);
431 public FTSDataColumnGroupI getDataColumnGroupById(String id)
434 if (dataColumns == null || dataColumns.isEmpty())
436 parseDataColumnsConfigFile();
438 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
440 if (columnGroup.getID().equalsIgnoreCase(id))
445 throw new Exception("Couldn't find data column group with id : " + id);
448 public static String getMessageByHTTPStatusCode(int code, String service)
454 message = "Bad request. There is a problem with your input.";
458 message = service + " rest services no longer available!";
462 message = "The requested resource could not be found";
471 message = "There seems to be an error from the " + service
475 message = "Service not available. The server is being updated, try again later.";
480 return String.valueOf(code) + " " + message;
483 protected String getResourceFile(String fileName)
488 result = getClass().getResource(fileName).getFile();
489 } catch (Exception e)
498 public int getDefaultResponsePageSize()
500 if (dataColumns == null || dataColumns.isEmpty())
502 parseDataColumnsConfigFile();
504 return defaultResponsePageSize;