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.HashMap;
30 import java.util.Objects;
32 import jalview.fts.api.FTSDataColumnI;
33 import jalview.fts.api.FTSDataColumnI.FTSDataColumnGroupI;
34 import jalview.fts.core.FTSDataColumnPreferences.PreferenceSource;
35 import jalview.fts.service.threedbeacons.TDBeaconsFTSRestClient;
36 import jalview.fts.api.FTSRestClientI;
39 * Base class providing implementation for common methods defined in
44 * @note implementations MUST be accessed as a singleton.
46 public abstract class FTSRestClient implements FTSRestClientI
48 protected Collection<FTSDataColumnI> dataColumns = new ArrayList<>();
50 protected Collection<FTSDataColumnGroupI> dataColumnGroups = new ArrayList<>();
52 protected Collection<FTSDataColumnI> searchableDataColumns = new ArrayList<>();
54 protected Collection<FTSDataColumnI> defaulDisplayedDataColumns = new ArrayList<>();
56 protected FTSDataColumnI primaryKeyColumn;
58 private String primaryKeyColumnCode = null;
60 private int defaultResponsePageSize = 100;
62 protected HashMap<String,String> mockQueries = null;
64 protected FTSRestClient()
69 public void parseDataColumnsConfigFile()
71 String fileName = getColumnDataConfigFileName();
73 InputStream in = getClass().getResourceAsStream(fileName);
75 try (BufferedReader br = new BufferedReader(new InputStreamReader(in)))
78 while ((line = br.readLine()) != null)
80 final String[] lineData = line.split(";");
83 if (lineData.length == 2)
85 if (lineData[0].equalsIgnoreCase("_data_column.primary_key"))
87 primaryKeyColumnCode = lineData[1];
89 if (lineData[0].equalsIgnoreCase(
90 "_data_column.default_response_page_size"))
92 defaultResponsePageSize = Integer.valueOf(lineData[1]);
95 else if (lineData.length == 3)
97 dataColumnGroups.add(new FTSDataColumnGroupI()
100 public String getID()
106 public String getName()
112 public int getSortOrder()
114 return Integer.valueOf(lineData[2]);
118 public String toString()
124 public int hashCode()
126 return Objects.hash(this.getID(), this.getName(),
127 this.getSortOrder());
131 public boolean equals(Object otherObject)
133 FTSDataColumnGroupI that = (FTSDataColumnGroupI) otherObject;
134 return this.getID().equals(that.getID())
135 && this.getName().equals(that.getName())
136 && this.getSortOrder() == that.getSortOrder();
140 else if (lineData.length > 6)
142 FTSDataColumnI dataCol = new FTSDataColumnI()
145 public String toString()
151 public String getName()
157 public String getCode()
159 return lineData[1].split("\\|")[0];
163 public String getAltCode()
165 return lineData[1].split("\\|").length > 1
166 ? lineData[1].split("\\|")[1]
171 public DataTypeI getDataType()
173 final String[] dataTypeString = lineData[2].split("\\|");
174 final String classString = dataTypeString[0].toUpperCase();
176 return new DataTypeI()
180 public boolean isFormtted()
182 if (dataTypeString.length > 1
183 && dataTypeString[1] != null)
185 switch (dataTypeString[1].toUpperCase())
200 public int getSignificantFigures()
202 if (dataTypeString.length > 2
203 && dataTypeString[2] != null)
205 return Integer.valueOf(dataTypeString[2]);
211 public Class getDataTypeClass()
217 return Integer.class;
230 public FTSDataColumnGroupI getGroup()
232 FTSDataColumnGroupI group = null;
235 group = getDataColumnGroupById(lineData[3]);
236 } catch (Exception e)
244 public int getMinWidth()
246 return Integer.valueOf(lineData[4]);
250 public int getMaxWidth()
252 return Integer.valueOf(lineData[5]);
256 public int getPreferredWidth()
258 return Integer.valueOf(lineData[6]);
262 public boolean isPrimaryKeyColumn()
264 return getName().equalsIgnoreCase(primaryKeyColumnCode)
265 || getCode().equalsIgnoreCase(primaryKeyColumnCode);
269 public boolean isVisibleByDefault()
271 return Boolean.valueOf(lineData[7]);
275 public boolean isSearchable()
277 return Boolean.valueOf(lineData[8]);
281 public int hashCode()
283 return Objects.hash(this.getName(), this.getCode(),
288 public boolean equals(Object otherObject)
290 FTSDataColumnI that = (FTSDataColumnI) otherObject;
291 return otherObject == null ? false
292 : this.getCode().equals(that.getCode())
293 && this.getName().equals(that.getName())
294 && this.getGroup().equals(that.getGroup());
298 dataColumns.add(dataCol);
300 if (dataCol.isSearchable())
302 searchableDataColumns.add(dataCol);
305 if (dataCol.isVisibleByDefault())
307 defaulDisplayedDataColumns.add(dataCol);
315 } catch (Exception e)
322 this.primaryKeyColumn = getDataColumnByNameOrCode(
323 primaryKeyColumnCode);
324 } catch (Exception e)
328 } catch (IOException e)
335 public int getPrimaryKeyColumIndex(
336 Collection<FTSDataColumnI> wantedFields, boolean hasRefSeq)
340 // If a reference sequence is attached then start counting from 1 else
342 int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
344 for (FTSDataColumnI field : wantedFields)
346 if (field.isPrimaryKeyColumn())
348 break; // Once PDB Id index is determined exit iteration
350 ++pdbFieldIndexCounter;
352 return pdbFieldIndexCounter;
356 public String getDataColumnsFieldsAsCommaDelimitedString(
357 Collection<FTSDataColumnI> dataColumnFields)
360 if (dataColumnFields != null && !dataColumnFields.isEmpty())
362 StringBuilder returnedFields = new StringBuilder();
363 for (FTSDataColumnI field : dataColumnFields)
365 returnedFields.append(",").append(field.getCode());
367 returnedFields.deleteCharAt(0);
368 result = returnedFields.toString();
374 public Collection<FTSDataColumnI> getAllFTSDataColumns()
376 if (dataColumns == null || dataColumns.isEmpty())
378 parseDataColumnsConfigFile();
384 public Collection<FTSDataColumnI> getSearchableDataColumns()
386 if (searchableDataColumns == null || searchableDataColumns.isEmpty())
388 parseDataColumnsConfigFile();
390 return searchableDataColumns;
394 public Collection<FTSDataColumnI> getAllDefaultDisplayedFTSDataColumns()
396 if (defaulDisplayedDataColumns == null
397 || defaulDisplayedDataColumns.isEmpty())
399 parseDataColumnsConfigFile();
401 return defaulDisplayedDataColumns;
405 public FTSDataColumnI getPrimaryKeyColumn()
407 if (defaulDisplayedDataColumns == null
408 || defaulDisplayedDataColumns.isEmpty())
410 parseDataColumnsConfigFile();
412 return primaryKeyColumn;
416 public FTSDataColumnI getDataColumnByNameOrCode(String nameOrCode)
419 if (dataColumns == null || dataColumns.isEmpty())
421 parseDataColumnsConfigFile();
423 for (FTSDataColumnI column : dataColumns)
425 if (column.getName().equalsIgnoreCase(nameOrCode)
426 || column.getCode().equalsIgnoreCase(nameOrCode))
432 "Couldn't find data column with name : " + nameOrCode);
438 * @param mocks {{working query, working response}, ...}
440 public static void createMockFTSRestClient(FTSRestClient instance,String[][] mocks)
442 instance.setMock(mocks);
446 public FTSDataColumnGroupI getDataColumnGroupById(String id)
449 if (dataColumns == null || dataColumns.isEmpty())
451 parseDataColumnsConfigFile();
453 for (FTSDataColumnGroupI columnGroup : dataColumnGroups)
455 if (columnGroup.getID().equalsIgnoreCase(id))
460 throw new Exception("Couldn't find data column group with id : " + id);
463 public static String getMessageByHTTPStatusCode(int code, String service)
469 message = "Bad request. There is a problem with your input.";
473 message = service + " rest services no longer available!";
477 message = "The requested resource could not be found";
486 message = "There seems to be an error from the " + service
490 message = "Service not available. The server is being updated, try again later.";
495 return String.valueOf(code) + " " + message;
498 public static void unMock(FTSRestClient instance)
500 instance.mockQueries=null;
503 protected String getResourceFile(String fileName)
508 result = getClass().getResource(fileName).getFile();
509 } catch (Exception e)
518 public int getDefaultResponsePageSize()
520 if (dataColumns == null || dataColumns.isEmpty())
522 parseDataColumnsConfigFile();
524 return defaultResponsePageSize;
527 protected void setMock(String[][] mocks)
533 mockQueries=new HashMap<String,String>();
534 for (String[] mock:mocks)
536 mockQueries.put(mock[0],mock[1]);
540 protected boolean isMocked()
542 return mockQueries!=null;
546 public String[] getPreferencesColumnsFor(PreferenceSource source)
548 String[] columnNames = null;
552 columnNames = new String[] { "", "Display", "Group" };
555 // non structure sources don't return any other kind of preferences columns