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.util.Locale;
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.HashMap;
32 import java.util.Objects;
34 import jalview.fts.api.FTSDataColumnI;
35 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
36 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
37 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
38 import jalview.fts.api.FTSRestClientI;
41 * Base class providing implementation for common methods defined in
46 * @note implementations MUST be accessed as a singleton.
48 public abstract class FTSRestClient implements FTSRestClientI
50 protected Collection<FTSDataColumnI> dataColumns = new ArrayList<>();
52 protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<>();
54 protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<>();
56 protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<>();
58 protected FTSDataColumnI primaryKeyColumn;
60 private String primaryKeyColumnCode = null;
62 private int defaultResponsePageSize = 100;
64 protected HashMap<String, String> mockQueries = null;
66 protected FTSRestClient()
71 public void parseDataColumnsConfigFile()
73 String fileName = getColumnDataConfigFileName();
75 InputStream in = getClass().getResourceAsStream(fileName);
77 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
80 while ((line = br.readLine()) != null)
82 final String[] lineData = line.split(";");
85 if (lineData.length == 2)
87 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
89 primaryKeyColumnCode = lineData[1];
91 if (lineData[0].equalsIgnoreCase(
92 "_data_column.default_response_page_size"))
94 defaultResponsePageSize = Integer.valueOf(lineData[1]);
97 else if (lineData.length == 3)
99 dataColumnGroups.add(new FTSDataColumnGroupI()
102 public String getID()
108 public String getName()
114 public int getSortOrder()
116 return Integer.valueOf(lineData[2]);
120 public String toString()
126 public int hashCode()
128 return Objects.hash(this.getID(), this.getName(),
129 this.getSortOrder());
133 public boolean equals(Object otherObject)
135 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
136 return this.getID().equals(that.getID())
137 && this.getName().equals(that.getName())
138 && this.getSortOrder() == that.getSortOrder();
142 else if (lineData.length > 6)
144 FTSDataColumnI dataCol = new FTSDataColumnI()
147 public String toString()
153 public String getName()
159 public String getCode()
161 return lineData[1].split("\\|")[0];
165 public String getAltCode()
167 return lineData[1].split("\\|").length > 1
168 ? lineData[1].split("\\|")[1]
173 public DataTypeI getDataType()
175 final String[] dataTypeString = lineData[2].split("\\|");
176 final String classString = dataTypeString[0]
177 .toUpperCase(Locale.ROOT);
179 return new DataTypeI()
183 public boolean isFormtted()
185 if (dataTypeString.length > 1
186 && dataTypeString[1] != null)
188 switch (dataTypeString[1].toUpperCase(Locale.ROOT))
203 public int getSignificantFigures()
205 if (dataTypeString.length > 2
206 && dataTypeString[2] != null)
208 return Integer.valueOf(dataTypeString[2]);
214 public Class getDataTypeClass()
220 return Integer.class;
233 public FTSDataColumnGroupI getGroup()
235 FTSDataColumnGroupI group = null;
238 group = getDataColumnGroupById(lineData[3]);
239 } catch (Exception e)
247 public int getMinWidth()
249 return Integer.valueOf(lineData[4]);
253 public int getMaxWidth()
255 return Integer.valueOf(lineData[5]);
259 public int getPreferredWidth()
261 return Integer.valueOf(lineData[6]);
265 public boolean isPrimaryKeyColumn()
267 return getName().equalsIgnoreCase(primaryKeyColumnCode)
268 || getCode().equalsIgnoreCase(primaryKeyColumnCode);
272 public boolean isVisibleByDefault()
274 return Boolean.valueOf(lineData[7]);
278 public boolean isSearchable()
280 return Boolean.valueOf(lineData[8]);
284 public int hashCode()
286 return Objects.hash(this.getName(), this.getCode(),
291 public boolean equals(Object otherObject)
293 FTSDataColumnI that = (FTSDataColumnI) otherObject;
294 return otherObject == null ? false
295 : this.getCode().equals(that.getCode())
296 && this.getName().equals(that.getName())
297 && this.getGroup().equals(that.getGroup());
301 dataColumns.add(dataCol);
303 if (dataCol.isSearchable())
305 searchableDataColumns.add(dataCol);
308 if (dataCol.isVisibleByDefault())
310 defaulDisplayedDataColumns.add(dataCol);
318 } catch (Exception e)
325 this.primaryKeyColumn = getDataColumnByNameOrCode(
326 primaryKeyColumnCode);
327 } catch (Exception e)
331 } catch (IOException e)
338 public int getPrimaryKeyColumIndex(
339 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
343 // If a reference sequence is attached then start counting from 1 else
345 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
347 for (FTSDataColumnI field : wantedFields)
349 if (field.isPrimaryKeyColumn())
351 break; // Once PDB Id index is determined exit iteration
353 ++pdbFieldIndexCounter;
355 return pdbFieldIndexCounter;
359 public String getDataColumnsFieldsAsCommaDelimitedString(
360 Collection<FTSDataColumnI> dataColumnFields)
363 if (dataColumnFields != null && !dataColumnFields.isEmpty())
365 StringBuilder returnedFields = new StringBuilder();
366 for (FTSDataColumnI field : dataColumnFields)
368 returnedFields.append(",").append(field.getCode());
370 returnedFields.deleteCharAt(0);
371 result = returnedFields.toString();
377 public Collection<FTSDataColumnI> getAllFTSDataColumns()
379 if (dataColumns == null || dataColumns.isEmpty())
381 parseDataColumnsConfigFile();
387 public Collection<FTSDataColumnI> getSearchableDataColumns()
389 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
391 parseDataColumnsConfigFile();
393 return searchableDataColumns;
397 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
399 if (defaulDisplayedDataColumns == null
400 || defaulDisplayedDataColumns.isEmpty())
402 parseDataColumnsConfigFile();
404 return defaulDisplayedDataColumns;
408 public FTSDataColumnI getPrimaryKeyColumn()
410 if (defaulDisplayedDataColumns == null
411 || defaulDisplayedDataColumns.isEmpty())
413 parseDataColumnsConfigFile();
415 return primaryKeyColumn;
419 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
422 if (dataColumns == null || dataColumns.isEmpty())
424 parseDataColumnsConfigFile();
426 for (FTSDataColumnI column : dataColumns)
428 if (column.getName().equalsIgnoreCase(nameOrCode)
429 || column.getCode().equalsIgnoreCase(nameOrCode))
435 "Couldn't find data column with name : " + nameOrCode);
442 * {{working query, working response}, ...}
444 public static void createMockFTSRestClient(FTSRestClient instance,
447 instance.setMock(mocks);
451 public FTSDataColumnGroupI getDataColumnGroupById(String id)
454 if (dataColumns == null || dataColumns.isEmpty())
456 parseDataColumnsConfigFile();
458 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
460 if (columnGroup.getID().equalsIgnoreCase(id))
465 throw new Exception("Couldn't find data column group with id : " + id);
468 public static String getMessageByHTTPStatusCode(int code, String service)
474 message = "Bad request. There is a problem with your input.";
478 message = service + " rest services no longer available!";
482 message = "The requested resource could not be found";
491 message = "There seems to be an error from the " + service
495 message = "Service not available. The server is being updated, try again later.";
500 return String.valueOf(code) + " " + message;
503 public static void unMock(FTSRestClient instance)
505 instance.mockQueries = null;
508 protected String getResourceFile(String fileName)
513 result = getClass().getResource(fileName).getFile();
514 } catch (Exception e)
523 public int getDefaultResponsePageSize()
525 if (dataColumns == null || dataColumns.isEmpty())
527 parseDataColumnsConfigFile();
529 return defaultResponsePageSize;
532 protected void setMock(String[][] mocks)
539 mockQueries = new HashMap<String, String>();
540 for (String[] mock : mocks)
542 mockQueries.put(mock[0], mock[1]);
546 protected boolean isMocked()
548 return mockQueries != null;
552 public String[] getPreferencesColumnsFor(PreferenceSource source)
554 String[] columnNames = null;
558 columnNames = new String[] { "", "Display", "Group" };
561 // non structure sources don't return any other kind of preferences