/* * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2) * Copyright (C) 2014 The Jalview Authors * * This file is part of Jalview. * * Jalview is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * Jalview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalview. If not, see . * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.ws.uimodel; import jalview.ws.dbsources.PDBRestClient.PDBDocField; import java.util.Collection; import java.util.List; import java.util.Objects; import javax.swing.DefaultListModel; import javax.swing.table.DefaultTableModel; import org.json.simple.JSONObject; /** * Represents the response model produced by the PDBRestClient upon successful * execution of a given request * * @author tcnofoegbu * */ public class PDBRestResponse { private int numberOfItemsFound; private String responseTime; private Collection searchSummary; public int getNumberOfItemsFound() { return numberOfItemsFound; } public void setNumberOfItemsFound(int itemFound) { this.numberOfItemsFound = itemFound; } public String getResponseTime() { return responseTime; } public void setResponseTime(String responseTime) { this.responseTime = responseTime; } public Collection getSearchSummary() { return searchSummary; } public void setSearchSummary(Collection searchSummary) { this.searchSummary = searchSummary; } /** * Convenience method to obtain a List model for a given summary list * * @param summariesList * the summary list which contains the data for generating the lists * data * @return the list model generated for the search summary */ public static DefaultListModel getListModel( Collection summariesList) { DefaultListModel defaultListModel = new DefaultListModel(); for (PDBResponseSummary summaryList : summariesList) { defaultListModel.addElement(summaryList); } return defaultListModel; } /** * Convenience method to obtain a Table model for a given summary List and * request * * @param request * the PDBRestRequest object which holds useful information for * creating a table model * @param summariesList * the summary list which contains the data for populating the * table's rows * @return the table model which was dynamically generated */ public static DefaultTableModel getTableModel(PDBRestRequest request, Collection summariesList) { DefaultTableModel model = new DefaultTableModel(); if (request.getAssociatedSequence() != null) { model.addColumn("Sequence"); } for (PDBDocField field : request.getWantedFields()) { model.addColumn(field.getName()); } for (PDBResponseSummary res : summariesList) { model.addRow(res.getSummaryData()); } return model; } /** * Model for a unique response summary * * @author tcnofoegbu * */ public class PDBResponseSummary { private String pdbId; private String concatenatedSummaryData; private String[] summaryData; private String associatedSequence; private int width = 480; public PDBResponseSummary(JSONObject doc, PDBRestRequest request) { StringBuilder summaryBuilder = new StringBuilder(); List diplayFields = request.getWantedFields(); String associatedSeq = request.getAssociatedSequence(); int colCounter = 0; summaryData = new String[(associatedSeq != null) ? diplayFields .size() + 1 : diplayFields.size()]; if (associatedSeq != null) { this.associatedSequence = (associatedSeq.length() > 18) ? associatedSeq .substring(0, 18) : associatedSeq; summaryData[0] = associatedSequence; colCounter = 1; } for (PDBDocField field : diplayFields) { if (field.equals(PDBDocField.MOLECULE_TYPE) && doc.get(PDBDocField.MOLECULE_TYPE.getCode()) != null) { String moleculeType = doc .get(PDBDocField.MOLECULE_TYPE.getCode()).toString(); if (moleculeType.equalsIgnoreCase("protein")) { summaryBuilder.append(""); } if (moleculeType.equalsIgnoreCase("dna")) { summaryBuilder.append(""); } if (moleculeType.equalsIgnoreCase("rna")) { summaryBuilder.append(""); } if (moleculeType.equalsIgnoreCase("sugar")) { summaryBuilder.append(""); } summaryData[colCounter++] = moleculeType; } else if (field.equals(PDBDocField.PDB_ID) && doc.get(PDBDocField.PDB_ID.getCode()) != null) { this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString(); summaryBuilder.append(this.pdbId).append(" | "); summaryData[colCounter++] = this.pdbId; } else if (doc.get(field.getCode()) != null) { summaryBuilder.append(field.getName()).append(": ") .append(doc.get(field.getCode())).append(" | "); summaryData[colCounter++] = doc.get(field.getCode()).toString(); } } int endIndex = summaryBuilder.lastIndexOf(" | "); String fSummary = summaryBuilder.toString().substring(0, endIndex); this.concatenatedSummaryData = fSummary.trim(); summaryBuilder = null; } public String getPdbId() { return pdbId; } public void setPdbId(String pdbId) { this.pdbId = pdbId; } public String getConcatenatedSummaryData() { return concatenatedSummaryData; } public void setConcatenatedSummaryData(String concatenatedSummaryData) { this.concatenatedSummaryData = concatenatedSummaryData; } public String[] getSummaryData() { return summaryData; } public void setSummaryData(String[] summaryData) { this.summaryData = summaryData; } public String toString() { StringBuilder html = new StringBuilder(); html.append("
"); html.append(concatenatedSummaryData); html.append("
"); return html.toString(); } @Override public int hashCode() { return Objects.hash(this.pdbId, this.concatenatedSummaryData); } @Override public boolean equals(Object other) { if (!(other instanceof PDBResponseSummary)) { return false; } PDBResponseSummary that = (PDBResponseSummary) other; // Custom equality check here. return this.pdbId.equals(that.pdbId) && this.concatenatedSummaryData .equals(that.concatenatedSummaryData); } } }