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