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];
86 .equalsIgnoreCase("_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 ? lineData[1]
162 .split("\\|")[1] : getCode();
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 this.getCode().equals(that.getCode())
287 && this.getName().equals(that.getName())
288 && this.getGroup().equals(that.getGroup());
292 dataColumns.add(dataCol);
294 if (dataCol.isSearchable())
296 searchableDataColumns.add(dataCol);
299 if (dataCol.isVisibleByDefault())
301 defaulDisplayedDataColumns.add(dataCol);
309 } catch (Exception e)
316 this.primaryKeyColumn = getDataColumnByNameOrCode(primaryKeyColumnCode);
317 } catch (Exception e)
321 } catch (IOException e)
328 public int getPrimaryKeyColumIndex(
329 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
333 // If a reference sequence is attached then start counting from 1 else
335 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
337 for (FTSDataColumnI field : wantedFields)
339 if (field.isPrimaryKeyColumn())
341 break; // Once PDB Id index is determined exit iteration
343 ++pdbFieldIndexCounter;
345 return pdbFieldIndexCounter;
349 public String getDataColumnsFieldsAsCommaDelimitedString(
350 Collection<FTSDataColumnI> dataColumnFields)
353 if (dataColumnFields != null && !dataColumnFields.isEmpty())
355 StringBuilder returnedFields = new StringBuilder();
356 for (FTSDataColumnI field : dataColumnFields)
358 returnedFields.append(",").append(field.getCode());
360 returnedFields.deleteCharAt(0);
361 result = returnedFields.toString();
367 public Collection<FTSDataColumnI> getAllFTSDataColumns()
369 if (dataColumns == null || dataColumns.isEmpty())
371 parseDataColumnsConfigFile();
377 public Collection<FTSDataColumnI> getSearchableDataColumns()
379 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
381 parseDataColumnsConfigFile();
383 return searchableDataColumns;
387 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
389 if (defaulDisplayedDataColumns == null
390 || defaulDisplayedDataColumns.isEmpty())
392 parseDataColumnsConfigFile();
394 return defaulDisplayedDataColumns;
398 public FTSDataColumnI getPrimaryKeyColumn()
400 if (defaulDisplayedDataColumns == null
401 || defaulDisplayedDataColumns.isEmpty())
403 parseDataColumnsConfigFile();
405 return primaryKeyColumn;
409 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
412 if (dataColumns == null || dataColumns.isEmpty())
414 parseDataColumnsConfigFile();
416 for (FTSDataColumnI column : dataColumns)
418 if (column.getName().equalsIgnoreCase(nameOrCode)
419 || column.getCode().equalsIgnoreCase(nameOrCode))
424 throw new Exception("Couldn't find data column with name : "
429 public FTSDataColumnGroupI getDataColumnGroupById(String id)
432 if (dataColumns == null || dataColumns.isEmpty())
434 parseDataColumnsConfigFile();
436 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
438 if (columnGroup.getID().equalsIgnoreCase(id))
443 throw new Exception("Couldn't find data column group with id : " + id);
446 public String getMessageByHTTPStatusCode(int code, String service)
452 message = MessageManager.getString("exception.bad_request");
456 message = MessageManager.formatMessage(
457 "exception.fts_rest_service_no_longer_available", service);
461 message = MessageManager.getString("exception.resource_not_be_found");
470 message = MessageManager.formatMessage("exception.fts_server_error",
474 message = MessageManager.getString("exception.service_not_available");
482 protected String getResourceFile(String fileName)
487 result = getClass().getResource(fileName).getFile();
488 } catch (Exception e)
497 public int getDefaultResponsePageSize()
499 if (dataColumns == null || dataColumns.isEmpty())
501 parseDataColumnsConfigFile();
503 return defaultResponsePageSize;