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 otherObject == null ? false
288 : this.getCode().equals(that.getCode())
289 && this.getName().equals(that.getName())
290 && this.getGroup().equals(that.getGroup());
294 dataColumns.add(dataCol);
296 if (dataCol.isSearchable())
298 searchableDataColumns.add(dataCol);
301 if (dataCol.isVisibleByDefault())
303 defaulDisplayedDataColumns.add(dataCol);
311 } catch (Exception e)
318 this.primaryKeyColumn = getDataColumnByNameOrCode(
319 primaryKeyColumnCode);
320 } catch (Exception e)
324 } catch (IOException e)
331 public int getPrimaryKeyColumIndex(
332 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
336 // If a reference sequence is attached then start counting from 1 else
338 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
340 for (FTSDataColumnI field : wantedFields)
342 if (field.isPrimaryKeyColumn())
344 break; // Once PDB Id index is determined exit iteration
346 ++pdbFieldIndexCounter;
348 return pdbFieldIndexCounter;
352 public String getDataColumnsFieldsAsCommaDelimitedString(
353 Collection<FTSDataColumnI> dataColumnFields)
356 if (dataColumnFields != null && !dataColumnFields.isEmpty())
358 StringBuilder returnedFields = new StringBuilder();
359 for (FTSDataColumnI field : dataColumnFields)
361 returnedFields.append(",").append(field.getCode());
363 returnedFields.deleteCharAt(0);
364 result = returnedFields.toString();
370 public Collection<FTSDataColumnI> getAllFTSDataColumns()
372 if (dataColumns == null || dataColumns.isEmpty())
374 parseDataColumnsConfigFile();
380 public Collection<FTSDataColumnI> getSearchableDataColumns()
382 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
384 parseDataColumnsConfigFile();
386 return searchableDataColumns;
390 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
392 if (defaulDisplayedDataColumns == null
393 || defaulDisplayedDataColumns.isEmpty())
395 parseDataColumnsConfigFile();
397 return defaulDisplayedDataColumns;
401 public FTSDataColumnI getPrimaryKeyColumn()
403 if (defaulDisplayedDataColumns == null
404 || defaulDisplayedDataColumns.isEmpty())
406 parseDataColumnsConfigFile();
408 return primaryKeyColumn;
412 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
415 if (dataColumns == null || dataColumns.isEmpty())
417 parseDataColumnsConfigFile();
419 for (FTSDataColumnI column : dataColumns)
421 if (column.getName().equalsIgnoreCase(nameOrCode)
422 || column.getCode().equalsIgnoreCase(nameOrCode))
428 "Couldn't find data column with name : " + nameOrCode);
432 public FTSDataColumnGroupI getDataColumnGroupById(String id)
435 if (dataColumns == null || dataColumns.isEmpty())
437 parseDataColumnsConfigFile();
439 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
441 if (columnGroup.getID().equalsIgnoreCase(id))
446 throw new Exception("Couldn't find data column group with id : " + id);
449 public String getMessageByHTTPStatusCode(int code, String service)
455 message = MessageManager.getString("exception.bad_request");
459 message = MessageManager.formatMessage(
460 "exception.fts_rest_service_no_longer_available", service);
464 message = MessageManager.getString("exception.resource_not_be_found");
473 message = MessageManager.formatMessage("exception.fts_server_error",
477 message = MessageManager.getString("exception.service_not_available");
485 protected String getResourceFile(String fileName)
490 result = getClass().getResource(fileName).getFile();
491 } catch (Exception e)
500 public int getDefaultResponsePageSize()
502 if (dataColumns == null || dataColumns.isEmpty())
504 parseDataColumnsConfigFile();
506 return defaultResponsePageSize;