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 jalview.fts.api.FTSDataColumnI;
24 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.util.MessageManager;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Objects;
37 * Base class providing implementation for common methods defined in
42 * @note implementations MUST be accessed as a singleton.
44 public abstract class FTSRestClient implements FTSRestClientI
46 protected Collection<FTSDataColumnI> dataColumns = new ArrayList<FTSDataColumnI>();
48 protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<FTSDataColumnGroupI>();
50 protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<FTSDataColumnI>();
52 protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<FTSDataColumnI>();
54 protected FTSDataColumnI primaryKeyColumn;
56 private String primaryKeyColumnCode = null;
58 private int defaultResponsePageSize = 100;
60 protected FTSRestClient()
65 public void parseDataColumnsConfigFile()
67 String fileName = getColumnDataConfigFileName();
69 InputStream in = getClass().getResourceAsStream(fileName);
71 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
74 while ((line = br.readLine()) != null)
76 final String[] lineData = line.split(";");
79 if (lineData.length == 2)
81 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
83 primaryKeyColumnCode = lineData[1];
85 if (lineData[0].equalsIgnoreCase(
86 "_data_column.default_response_page_size"))
88 defaultResponsePageSize = Integer.valueOf(lineData[1]);
91 else if (lineData.length == 3)
93 dataColumnGroups.add(new FTSDataColumnGroupI()
102 public String getName()
108 public int getSortOrder()
110 return Integer.valueOf(lineData[2]);
114 public String toString()
120 public int hashCode()
122 return Objects.hash(this.getID(), this.getName(),
123 this.getSortOrder());
127 public boolean equals(Object otherObject)
129 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
130 return this.getID().equals(that.getID())
131 && this.getName().equals(that.getName())
132 && this.getSortOrder() == that.getSortOrder();
136 else if (lineData.length > 6)
138 FTSDataColumnI dataCol = new FTSDataColumnI()
141 public String toString()
147 public String getName()
153 public String getCode()
155 return lineData[1].split("\\|")[0];
159 public String getAltCode()
161 return lineData[1].split("\\|").length > 1
162 ? lineData[1].split("\\|")[1]
167 public DataTypeI getDataType()
169 final String[] dataTypeString = lineData[2].split("\\|");
170 final String classString = dataTypeString[0].toUpperCase();
172 return new DataTypeI()
176 public boolean isFormtted()
178 if (dataTypeString.length > 1
179 && dataTypeString[1] != null)
181 switch (dataTypeString[1].toUpperCase())
196 public int getSignificantFigures()
198 if (dataTypeString.length > 2
199 && dataTypeString[2] != null)
201 return Integer.valueOf(dataTypeString[2]);
207 public Class getDataTypeClass()
213 return Integer.class;
226 public FTSDataColumnGroupI getGroup()
228 FTSDataColumnGroupI group = null;
231 group = getDataColumnGroupById(lineData[3]);
232 } catch (Exception e)
240 public int getMinWidth()
242 return Integer.valueOf(lineData[4]);
246 public int getMaxWidth()
248 return Integer.valueOf(lineData[5]);
252 public int getPreferredWidth()
254 return Integer.valueOf(lineData[6]);
258 public boolean isPrimaryKeyColumn()
260 return getName().equalsIgnoreCase(primaryKeyColumnCode)
261 || getCode().equalsIgnoreCase(primaryKeyColumnCode);
265 public boolean isVisibleByDefault()
267 return Boolean.valueOf(lineData[7]);
271 public boolean isSearchable()
273 return Boolean.valueOf(lineData[8]);
277 public int hashCode()
279 return Objects.hash(this.getName(), this.getCode(),
284 public boolean equals(Object otherObject)
286 FTSDataColumnI that = (FTSDataColumnI) otherObject;
287 return 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 String getMessageByHTTPStatusCode(int code, String service)
454 message = MessageManager.getString("exception.bad_request");
458 message = MessageManager.formatMessage(
459 "exception.fts_rest_service_no_longer_available", service);
463 message = MessageManager.getString("exception.resource_not_be_found");
472 message = MessageManager.formatMessage("exception.fts_server_error",
476 message = MessageManager.getString("exception.service_not_available");
484 protected String getResourceFile(String fileName)
489 result = getClass().getResource(fileName).getFile();
490 } catch (Exception e)
499 public int getDefaultResponsePageSize()
501 if (dataColumns == null || dataColumns.isEmpty())
503 parseDataColumnsConfigFile();
505 return defaultResponsePageSize;