JAL-1563 JAL-2091 Added generic pagination for FTS Service, pagination for PDB FTS...
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21
22 package jalview.fts.service.uniprot;
23
24 import jalview.fts.api.FTSDataColumnI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.gui.SequenceFetcher;
30 import jalview.util.MessageManager;
31
32 import java.util.HashSet;
33
34 @SuppressWarnings("serial")
35 public class UniprotFTSPanel extends GFTSPanel
36 {
37
38   private static String defaultFTSFrameTitle = MessageManager
39           .getString("label.uniprot_sequence_fetcher");
40
41   private String ftsFrameTitle = defaultFTSFrameTitle;
42
43
44
45   public UniprotFTSPanel(SequenceFetcher seqFetcher)
46   {
47     pageLimit = UniProtFTSRestClient.getInstance()
48             .getDefaultResponsePageSize();
49     this.seqFetcher = seqFetcher;
50     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
51             .getProgressIndicator();
52   }
53
54   @Override
55   public void searchAction(boolean isFreshSearch)
56   {
57     if (isFreshSearch)
58     {
59       offSet = 0;
60     }
61     new Thread()
62   {
63       @Override
64       public void run()
65       {
66         ftsFrameTitle = defaultFTSFrameTitle;
67         reset();
68         if (getTypedText().length() > 0)
69         {
70           setSearchInProgress(true);
71           long startTime = System.currentTimeMillis();
72
73           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
74                   .getSelectedItem()).getAltCode();
75
76           wantedFields = UniProtFTSRestClient.getInstance()
77                   .getAllDefaulDisplayedDataColumns();
78           String searchTerm = decodeSearchTerm(txt_search.getText(),
79                   searchTarget);
80
81           FTSRestRequest request = new FTSRestRequest();
82           request.setFieldToSearchBy(searchTarget);
83           request.setSearchTerm(searchTerm);
84           request.setOffSet(offSet);
85           request.setWantedFields(wantedFields);
86           FTSRestClientI uniProtRestCleint = UniProtFTSRestClient
87                   .getInstance();
88           FTSRestResponse resultList;
89           try
90           {
91             resultList = uniProtRestCleint.executeRequest(request);
92           } catch (Exception e)
93           {
94             e.printStackTrace();
95             setErrorMessage(e.getMessage());
96             checkForErrors();
97             return;
98           }
99
100           if (resultList.getSearchSummary() != null
101                   && resultList.getSearchSummary().size() > 0)
102           {
103             getResultTable().setModel(
104                     FTSRestResponse.getTableModel(request,
105                             resultList.getSearchSummary()));
106             FTSRestResponse.configureTableColumn(getResultTable(),
107                     wantedFields);
108             getResultTable().setVisible(true);
109           }
110
111           long endTime = System.currentTimeMillis();
112           resultSetCount = resultList.getNumberOfItemsFound();
113           String result = (resultSetCount > 0) ? MessageManager
114                   .getString("label.results") : MessageManager
115                   .getString("label.result");
116           if (isPaginationEnabled() && resultSetCount > 0)
117           {
118             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
119                     + " " + (offSet + 1) + " to "
120                     + (offSet + resultSetCount)
121                     + " " + " (" + (endTime - startTime) + " milli secs)");
122           }
123           else
124           {
125             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
126                     + resultSetCount + " " + result + " ("
127                     + (endTime - startTime) + " milli secs)");
128           }
129           setSearchInProgress(false);
130           refreshPaginatorState();
131         }
132       }
133     }.start();
134
135   }
136
137   public String decodeSearchTerm(String enteredText, String targetField)
138   {
139     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
140             : targetField.length() + 1;
141     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
142             : targetField + ":";
143     String foundSearchTerms = enteredText;
144     StringBuilder foundSearchTermsBuilder = new StringBuilder();
145     if (enteredText.contains(";"))
146     {
147       String[] searchTerms = enteredText.split(";");
148       for (String searchTerm : searchTerms)
149       {
150         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
151                 .append(" OR ");
152       }
153       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
154       foundSearchTerms = foundSearchTermsBuilder.toString();
155       if (foundSearchTerms.contains(" OR "))
156       {
157         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
158                 endIndex);
159       }
160     }
161     return foundSearchTerms;
162   }
163
164
165   @Override
166   public boolean isPaginationEnabled()
167   {
168     return true;
169   }
170
171   @Override
172   public void okAction()
173   {
174     disableActionButtons();
175     StringBuilder selectedIds = new StringBuilder();
176     HashSet<String> selectedIdsSet = new HashSet<String>();
177     int primaryKeyColIndex = 0;
178     try
179     {
180       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
181               wantedFields, false);
182     } catch (Exception e)
183     {
184       e.printStackTrace();
185     }
186     int[] selectedRows = getResultTable().getSelectedRows();
187     for (int summaryRow : selectedRows)
188     {
189       String idStr = getResultTable().getValueAt(summaryRow,
190               primaryKeyColIndex).toString();
191       selectedIdsSet.add(idStr);
192     }
193
194     for (String selectedId : selectedIdsSet)
195     {
196       selectedIds.append(selectedId).append(";");
197     }
198
199     String ids = selectedIds.toString();
200     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
201     seqFetcher.getTextArea().setText(ids);
202     Thread worker = new Thread(seqFetcher);
203     worker.start();
204     delayAndEnableActionButtons();
205   }
206
207   @Override
208   public FTSRestClientI getFTSRestClient()
209   {
210     return UniProtFTSRestClient.getInstance();
211   }
212
213   @Override
214   public String getFTSFrameTitle()
215   {
216     return ftsFrameTitle;
217   }
218
219 }