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