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