JAL-1563 JAL-2091 Added total result count to Uniprot pagination, implemented caching...
[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           totalResultSetCount = resultList.getNumberOfItemsFound();
113           resultSetCount = resultList.getSearchSummary() == null ? 0
114                   : resultList.getSearchSummary().size();
115           String result = (resultSetCount > 0) ? MessageManager
116                   .getString("label.results") : MessageManager
117                   .getString("label.result");
118           if (isPaginationEnabled() && resultSetCount > 0)
119           {
120             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
121                     + " " + (offSet + 1) + " to "
122                     + (offSet + resultSetCount) + " of "
123                     + totalResultSetCount
124                     + " " + " (" + (endTime - startTime) + " milli secs)");
125           }
126           else
127           {
128             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
129                     + resultSetCount + " " + result + " ("
130                     + (endTime - startTime) + " milli secs)");
131           }
132           setSearchInProgress(false);
133           refreshPaginatorState();
134           updateSummaryTableSelections();
135         }
136       }
137     }.start();
138
139   }
140
141   public String decodeSearchTerm(String enteredText, String targetField)
142   {
143     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
144             : targetField.length() + 1;
145     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
146             : targetField + ":";
147     String foundSearchTerms = enteredText;
148     StringBuilder foundSearchTermsBuilder = new StringBuilder();
149     if (enteredText.contains(";"))
150     {
151       String[] searchTerms = enteredText.split(";");
152       for (String searchTerm : searchTerms)
153       {
154         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
155                 .append(" OR ");
156       }
157       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
158       foundSearchTerms = foundSearchTermsBuilder.toString();
159       if (foundSearchTerms.contains(" OR "))
160       {
161         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
162                 endIndex);
163       }
164     }
165     return foundSearchTerms;
166   }
167
168
169   @Override
170   public boolean isPaginationEnabled()
171   {
172     return true;
173   }
174
175   @Override
176   public void okAction()
177   {
178     disableActionButtons();
179     StringBuilder selectedIds = new StringBuilder();
180     HashSet<String> selectedIdsSet = new HashSet<String>();
181     int primaryKeyColIndex = 0;
182     try
183     {
184       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
185               wantedFields, false);
186     } catch (Exception e)
187     {
188       e.printStackTrace();
189     }
190     int[] selectedRows = getResultTable().getSelectedRows();
191     for (int summaryRow : selectedRows)
192     {
193       String idStr = getResultTable().getValueAt(summaryRow,
194               primaryKeyColIndex).toString();
195       selectedIdsSet.add(idStr);
196     }
197     selectedIdsSet.addAll(paginatorCart);
198     for (String selectedId : selectedIdsSet)
199     {
200       selectedIds.append(selectedId).append(";");
201     }
202
203     String ids = selectedIds.toString();
204     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
205     seqFetcher.getTextArea().setText(ids);
206     Thread worker = new Thread(seqFetcher);
207     worker.start();
208     delayAndEnableActionButtons();
209   }
210
211   @Override
212   public FTSRestClientI getFTSRestClient()
213   {
214     return UniProtFTSRestClient.getInstance();
215   }
216
217   @Override
218   public String getFTSFrameTitle()
219   {
220     return ftsFrameTitle;
221   }
222
223 }