JAL-2835 spike updated with latest
[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.HashMap;
33 import java.util.HashSet;
34 import java.util.Map;
35
36 @SuppressWarnings("serial")
37 public class UniprotFTSPanel extends GFTSPanel
38 {
39
40   private static String defaultFTSFrameTitle = MessageManager
41           .getString("label.uniprot_sequence_fetcher");
42
43   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
44
45   private static final String UNIPROT_FTS_CACHE_KEY = "CACHE.UNIPROT_FTS";
46
47   private static final String UNIPROT_AUTOSEARCH = "FTS.UNIPROT.AUTOSEARCH";
48
49   public UniprotFTSPanel(SequenceFetcher fetcher)
50   {
51     super(fetcher);
52     pageLimit = UniProtFTSRestClient.getInstance()
53             .getDefaultResponsePageSize();
54     this.seqFetcher = fetcher;
55     this.progressIndicator = (fetcher == null) ? null
56             : fetcher.getProgressIndicator();
57   }
58
59   @Override
60   public void searchAction(boolean isFreshSearch)
61   {
62     mainFrame.requestFocusInWindow();
63     if (isFreshSearch)
64     {
65       offSet = 0;
66     }
67     new Thread()
68     {
69       @Override
70       public void run()
71       {
72         reset();
73         String searchInput = getTypedText();
74         if (searchInput.length() > 0)
75         {
76           setSearchInProgress(true);
77           long startTime = System.currentTimeMillis();
78           searchInput = getTypedText();
79           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
80                   .getSelectedItem()).getAltCode();
81           wantedFields = UniProtFTSRestClient.getInstance()
82                   .getAllDefaultDisplayedFTSDataColumns();
83           String searchTerm = decodeSearchTerm(searchInput, searchTarget);
84
85           FTSRestRequest request = new FTSRestRequest();
86           request.setFieldToSearchBy(searchTarget);
87           request.setSearchTerm(searchTerm);
88           request.setOffSet(offSet);
89           request.setWantedFields(wantedFields);
90           FTSRestClientI uniProtRestClient = UniProtFTSRestClient
91                   .getInstance();
92           FTSRestResponse resultList;
93           try
94           {
95             resultList = uniProtRestClient.executeRequest(request);
96           } catch (Exception e)
97           {
98             setErrorMessage(e.getMessage());
99             checkForErrors();
100             setSearchInProgress(false);
101             return;
102           }
103
104           if (resultList.getSearchSummary() != null
105                   && resultList.getSearchSummary().size() > 0)
106           {
107             getResultTable().setModel(FTSRestResponse.getTableModel(request,
108                     resultList.getSearchSummary()));
109             FTSRestResponse.configureTableColumn(getResultTable(),
110                     wantedFields, tempUserPrefs);
111             getResultTable().setVisible(true);
112           }
113
114           long endTime = System.currentTimeMillis();
115           totalResultSetCount = resultList.getNumberOfItemsFound();
116           resultSetCount = resultList.getSearchSummary() == null ? 0
117                   : resultList.getSearchSummary().size();
118           String result = (resultSetCount > 0)
119                   ? MessageManager.getString("label.results")
120                   : MessageManager.getString("label.result");
121           if (isPaginationEnabled() && resultSetCount > 0)
122           {
123             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
124                     + " "
125                     + totalNumberformatter.format((Number) (offSet + 1))
126                     + " to "
127                     + totalNumberformatter
128                             .format((Number) (offSet + resultSetCount))
129                     + " of "
130                     + totalNumberformatter
131                             .format((Number) totalResultSetCount)
132                     + " " + " (" + (endTime - startTime) + " milli secs)");
133           }
134           else
135           {
136             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
137                     + resultSetCount + " " + result + " ("
138                     + (endTime - startTime) + " milli secs)");
139           }
140           setSearchInProgress(false);
141           refreshPaginatorState();
142           updateSummaryTableSelections();
143         }
144         txt_search.updateCache();
145       }
146     }.start();
147
148   }
149
150   public String decodeSearchTerm(String enteredText, String targetField)
151   {
152     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
153             : targetField.length() + 1;
154     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
155             : targetField + ":";
156     String foundSearchTerms = enteredText;
157     StringBuilder foundSearchTermsBuilder = new StringBuilder();
158     if (enteredText.contains(";"))
159     {
160       String[] searchTerms = enteredText.split(";");
161       for (String searchTerm : searchTerms)
162       {
163         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
164                 .append(" OR ");
165       }
166       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
167       foundSearchTerms = foundSearchTermsBuilder.toString();
168       if (foundSearchTerms.contains(" OR "))
169       {
170         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
171                 endIndex);
172       }
173     }
174     return foundSearchTerms;
175   }
176
177   @Override
178   public boolean isPaginationEnabled()
179   {
180     return true;
181   }
182
183   @Override
184   public void okAction()
185   {
186     disableActionButtons();
187     StringBuilder selectedIds = new StringBuilder();
188     HashSet<String> selectedIdsSet = new HashSet<>();
189     int primaryKeyColIndex = 0;
190     try
191     {
192       primaryKeyColIndex = getFTSRestClient()
193               .getPrimaryKeyColumIndex(wantedFields, false);
194     } catch (Exception e)
195     {
196       e.printStackTrace();
197     }
198     int[] selectedRows = getResultTable().getSelectedRows();
199     for (int summaryRow : selectedRows)
200     {
201       String idStr = getResultTable()
202               .getValueAt(summaryRow, primaryKeyColIndex).toString();
203       selectedIdsSet.add(idStr);
204     }
205     selectedIdsSet.addAll(paginatorCart);
206     for (String selectedId : selectedIdsSet)
207     {
208       selectedIds.append(selectedId).append(";");
209     }
210
211     String ids = selectedIds.toString();
212     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
213     seqFetcher.getTextArea().setText(ids);
214     Thread worker = new Thread(seqFetcher);
215     worker.start();
216     delayAndEnableActionButtons();
217   }
218
219   @Override
220   public FTSRestClientI getFTSRestClient()
221   {
222     return UniProtFTSRestClient.getInstance();
223   }
224
225   @Override
226   public String getFTSFrameTitle()
227   {
228     return defaultFTSFrameTitle;
229   }
230
231   @Override
232   public Map<String, Integer> getTempUserPrefs()
233   {
234     return tempUserPrefs;
235   }
236
237   @Override
238   public String getCacheKey()
239   {
240     return UNIPROT_FTS_CACHE_KEY;
241   }
242
243   @Override
244   public String getAutosearchPreference()
245   {
246     return UNIPROT_AUTOSEARCH;
247   }
248 }