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.core.FTSDataColumnPreferences.PreferenceSource;
34 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
35 import jalview.fts.api.FTSRestClientI;
38 * Base class providing implementation for common methods defined in
43 * @note implementations MUST be accessed as a singleton.
45 public abstract class FTSRestClient implements FTSRestClientI
47 protected Collection<FTSDataColumnI> dataColumns = new ArrayList<>();
49 protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<>();
51 protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<>();
53 protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<>();
55 protected FTSDataColumnI primaryKeyColumn;
57 private String primaryKeyColumnCode = null;
59 private int defaultResponsePageSize = 100;
61 protected String mockQuery = null;
63 protected String mockResponse = null;
65 protected FTSRestClient()
70 public void parseDataColumnsConfigFile()
72 String fileName = getColumnDataConfigFileName();
74 InputStream in = getClass().getResourceAsStream(fileName);
76 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
79 while ((line = br.readLine()) != null)
81 final String[] lineData = line.split(";");
84 if (lineData.length == 2)
86 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
88 primaryKeyColumnCode = lineData[1];
90 if (lineData[0].equalsIgnoreCase(
91 "_data_column.default_response_page_size"))
93 defaultResponsePageSize = Integer.valueOf(lineData[1]);
96 else if (lineData.length == 3)
98 dataColumnGroups.add(new FTSDataColumnGroupI()
101 public String getID()
107 public String getName()
113 public int getSortOrder()
115 return Integer.valueOf(lineData[2]);
119 public String toString()
125 public int hashCode()
127 return Objects.hash(this.getID(), this.getName(),
128 this.getSortOrder());
132 public boolean equals(Object otherObject)
134 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
135 return this.getID().equals(that.getID())
136 && this.getName().equals(that.getName())
137 && this.getSortOrder() == that.getSortOrder();
141 else if (lineData.length > 6)
143 FTSDataColumnI dataCol = new FTSDataColumnI()
146 public String toString()
152 public String getName()
158 public String getCode()
160 return lineData[1].split("\\|")[0];
164 public String getAltCode()
166 return lineData[1].split("\\|").length > 1
167 ? lineData[1].split("\\|")[1]
172 public DataTypeI getDataType()
174 final String[] dataTypeString = lineData[2].split("\\|");
175 final String classString = dataTypeString[0].toUpperCase();
177 return new DataTypeI()
181 public boolean isFormtted()
183 if (dataTypeString.length > 1
184 && dataTypeString[1] != null)
186 switch (dataTypeString[1].toUpperCase())
201 public int getSignificantFigures()
203 if (dataTypeString.length > 2
204 && dataTypeString[2] != null)
206 return Integer.valueOf(dataTypeString[2]);
212 public Class getDataTypeClass()
218 return Integer.class;
231 public FTSDataColumnGroupI getGroup()
233 FTSDataColumnGroupI group = null;
236 group = getDataColumnGroupById(lineData[3]);
237 } catch (Exception e)
245 public int getMinWidth()
247 return Integer.valueOf(lineData[4]);
251 public int getMaxWidth()
253 return Integer.valueOf(lineData[5]);
257 public int getPreferredWidth()
259 return Integer.valueOf(lineData[6]);
263 public boolean isPrimaryKeyColumn()
265 return getName().equalsIgnoreCase(primaryKeyColumnCode)
266 || getCode().equalsIgnoreCase(primaryKeyColumnCode);
270 public boolean isVisibleByDefault()
272 return Boolean.valueOf(lineData[7]);
276 public boolean isSearchable()
278 return Boolean.valueOf(lineData[8]);
282 public int hashCode()
284 return Objects.hash(this.getName(), this.getCode(),
289 public boolean equals(Object otherObject)
291 FTSDataColumnI that = (FTSDataColumnI) otherObject;
292 return otherObject == null ? false
293 : this.getCode().equals(that.getCode())
294 && this.getName().equals(that.getName())
295 && this.getGroup().equals(that.getGroup());
299 dataColumns.add(dataCol);
301 if (dataCol.isSearchable())
303 searchableDataColumns.add(dataCol);
306 if (dataCol.isVisibleByDefault())
308 defaulDisplayedDataColumns.add(dataCol);
316 } catch (Exception e)
323 this.primaryKeyColumn = getDataColumnByNameOrCode(
324 primaryKeyColumnCode);
325 } catch (Exception e)
329 } catch (IOException e)
336 public int getPrimaryKeyColumIndex(
337 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
341 // If a reference sequence is attached then start counting from 1 else
343 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
345 for (FTSDataColumnI field : wantedFields)
347 if (field.isPrimaryKeyColumn())
349 break; // Once PDB Id index is determined exit iteration
351 ++pdbFieldIndexCounter;
353 return pdbFieldIndexCounter;
357 public String getDataColumnsFieldsAsCommaDelimitedString(
358 Collection<FTSDataColumnI> dataColumnFields)
361 if (dataColumnFields != null && !dataColumnFields.isEmpty())
363 StringBuilder returnedFields = new StringBuilder();
364 for (FTSDataColumnI field : dataColumnFields)
366 returnedFields.append(",").append(field.getCode());
368 returnedFields.deleteCharAt(0);
369 result = returnedFields.toString();
375 public Collection<FTSDataColumnI> getAllFTSDataColumns()
377 if (dataColumns == null || dataColumns.isEmpty())
379 parseDataColumnsConfigFile();
385 public Collection<FTSDataColumnI> getSearchableDataColumns()
387 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
389 parseDataColumnsConfigFile();
391 return searchableDataColumns;
395 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
397 if (defaulDisplayedDataColumns == null
398 || defaulDisplayedDataColumns.isEmpty())
400 parseDataColumnsConfigFile();
402 return defaulDisplayedDataColumns;
406 public FTSDataColumnI getPrimaryKeyColumn()
408 if (defaulDisplayedDataColumns == null
409 || defaulDisplayedDataColumns.isEmpty())
411 parseDataColumnsConfigFile();
413 return primaryKeyColumn;
417 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
420 if (dataColumns == null || dataColumns.isEmpty())
422 parseDataColumnsConfigFile();
424 for (FTSDataColumnI column : dataColumns)
426 if (column.getName().equalsIgnoreCase(nameOrCode)
427 || column.getCode().equalsIgnoreCase(nameOrCode))
433 "Couldn't find data column with name : " + nameOrCode);
436 public static void createMockFTSRestClient(FTSRestClient instance,String workingQuery,
439 instance.setMock(workingQuery,jsonResponse);
443 public FTSDataColumnGroupI getDataColumnGroupById(String id)
446 if (dataColumns == null || dataColumns.isEmpty())
448 parseDataColumnsConfigFile();
450 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
452 if (columnGroup.getID().equalsIgnoreCase(id))
457 throw new Exception("Couldn't find data column group with id : " + id);
460 public static String getMessageByHTTPStatusCode(int code, String service)
466 message = "Bad request. There is a problem with your input.";
470 message = service + " rest services no longer available!";
474 message = "The requested resource could not be found";
483 message = "There seems to be an error from the " + service
487 message = "Service not available. The server is being updated, try again later.";
492 return String.valueOf(code) + " " + message;
495 public static void unMock(FTSRestClient instance)
497 instance.mockQuery =null;
498 instance.mockResponse =null;
501 protected String getResourceFile(String fileName)
506 result = getClass().getResource(fileName).getFile();
507 } catch (Exception e)
516 public int getDefaultResponsePageSize()
518 if (dataColumns == null || dataColumns.isEmpty())
520 parseDataColumnsConfigFile();
522 return defaultResponsePageSize;
525 protected void setMock(String workingQuery, String jsonResponse)
527 mockQuery=workingQuery;
528 mockResponse = jsonResponse;
531 protected boolean isMocked()
533 return mockQuery != null && mockResponse!=null;
537 public String[] getPreferencesColumnsFor(PreferenceSource source)
539 String[] columnNames = null;
543 columnNames = new String[] { "", "Display", "Group" };
546 // non structure sources don't return any other kind of preferences columns