JAL-2757 fix PDB also; suppress stack trace and compiler warnings
[jalview.git] / src / jalview / fts / service / pdb / PDBFTSPanel.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.pdb;
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 PDBFTSPanel extends GFTSPanel
38 {
39   private static String defaultFTSFrameTitle = MessageManager
40           .getString("label.pdb_sequence_fetcher");
41
42   private static Map<String, Integer> tempUserPrefs = new HashMap<String, Integer>();
43
44   private static final String PDB_FTS_CACHE_KEY = "CACHE.PDB_FTS";
45
46   public PDBFTSPanel(SequenceFetcher fetcher)
47   {
48     super();
49     pageLimit = PDBFTSRestClient.getInstance().getDefaultResponsePageSize();
50     this.seqFetcher = fetcher;
51     this.progressIndicator = (fetcher == null) ? null
52             : fetcher.getProgressIndicator();
53   }
54
55   @Override
56   public void searchAction(boolean isFreshSearch)
57   {
58     mainFrame.requestFocusInWindow();
59     if (isFreshSearch)
60     {
61       offSet = 0;
62     }
63     new Thread()
64     {
65       @Override
66       public void run()
67       {
68         reset();
69         boolean allowEmptySequence = false;
70         if (getTypedText().length() > 0)
71         {
72           setSearchInProgress(true);
73           long startTime = System.currentTimeMillis();
74
75           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
76                   .getSelectedItem()).getCode();
77           wantedFields = PDBFTSRestClient.getInstance()
78                   .getAllDefaultDisplayedFTSDataColumns();
79           String searchTerm = decodeSearchTerm(getTypedText(),
80                   searchTarget);
81
82           FTSRestRequest request = new FTSRestRequest();
83           request.setAllowEmptySeq(allowEmptySequence);
84           request.setResponseSize(100);
85           request.setFieldToSearchBy("(" + searchTarget + ":");
86           request.setSearchTerm(searchTerm + ")");
87           request.setOffSet(offSet);
88           request.setWantedFields(wantedFields);
89           FTSRestClientI pdbRestClient = PDBFTSRestClient.getInstance();
90           FTSRestResponse resultList;
91           try
92           {
93             resultList = pdbRestClient.executeRequest(request);
94           } catch (Exception e)
95           {
96             setErrorMessage(e.getMessage());
97             checkForErrors();
98             setSearchInProgress(false);
99             return;
100           }
101
102           if (resultList.getSearchSummary() != null
103                   && resultList.getSearchSummary().size() > 0)
104           {
105             getResultTable().setModel(FTSRestResponse.getTableModel(request,
106                     resultList.getSearchSummary()));
107             FTSRestResponse.configureTableColumn(getResultTable(),
108                     wantedFields, tempUserPrefs);
109             getResultTable().setVisible(true);
110           }
111
112           long endTime = System.currentTimeMillis();
113           totalResultSetCount = resultList.getNumberOfItemsFound();
114           resultSetCount = resultList.getSearchSummary() == null ? 0
115                   : resultList.getSearchSummary().size();
116           String result = (resultSetCount > 0)
117                   ? MessageManager.getString("label.results")
118                   : MessageManager.getString("label.result");
119
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
140           setSearchInProgress(false);
141           refreshPaginatorState();
142           updateSummaryTableSelections();
143         }
144         txt_search.updateCache();
145       }
146     }.start();
147   }
148
149   public static String decodeSearchTerm(String enteredText,
150           String targetField)
151   {
152     String foundSearchTerms = enteredText;
153     StringBuilder foundSearchTermsBuilder = new StringBuilder();
154     if (enteredText.contains(";"))
155     {
156       String[] searchTerms = enteredText.split(";");
157       for (String searchTerm : searchTerms)
158       {
159         if (searchTerm.contains(":"))
160         {
161           foundSearchTermsBuilder.append(targetField).append(":")
162                   .append(searchTerm.split(":")[0]).append(" OR ");
163         }
164         else
165         {
166           foundSearchTermsBuilder.append(targetField).append(":")
167                   .append(searchTerm).append(" OR ");
168         }
169       }
170       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
171       foundSearchTerms = foundSearchTermsBuilder.toString();
172       if (foundSearchTerms.contains(" OR "))
173       {
174         foundSearchTerms = foundSearchTerms
175                 .substring(targetField.length() + 1, endIndex);
176       }
177     }
178     else if (enteredText.contains(":"))
179     {
180       foundSearchTerms = foundSearchTerms.split(":")[0];
181     }
182     return foundSearchTerms;
183   }
184
185   @Override
186   public void okAction()
187   {
188     // mainFrame.dispose();
189     disableActionButtons();
190     StringBuilder selectedIds = new StringBuilder();
191     HashSet<String> selectedIdsSet = new HashSet<String>();
192     int primaryKeyColIndex = 0;
193     try
194     {
195       primaryKeyColIndex = getFTSRestClient()
196               .getPrimaryKeyColumIndex(wantedFields, false);
197     } catch (Exception e)
198     {
199       e.printStackTrace();
200     }
201     int[] selectedRows = getResultTable().getSelectedRows();
202     String searchTerm = getTypedText();
203     for (int summaryRow : selectedRows)
204     {
205       String idStr = getResultTable()
206               .getValueAt(summaryRow, primaryKeyColIndex).toString();
207       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
208     }
209
210     for (String idStr : paginatorCart)
211     {
212       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
213     }
214
215     for (String selectedId : selectedIdsSet)
216     {
217       selectedIds.append(selectedId).append(";");
218     }
219
220     String ids = selectedIds.toString();
221     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
222     seqFetcher.getTextArea().setText(ids);
223     Thread worker = new Thread(seqFetcher);
224     worker.start();
225     delayAndEnableActionButtons();
226   }
227
228   public static String getPDBIdwithSpecifiedChain(String pdbId,
229           String searchTerm)
230   {
231     String pdbIdWithChainCode = "";
232     if (searchTerm.contains(";"))
233     {
234       String[] foundTerms = searchTerm.split(";");
235       for (String foundTerm : foundTerms)
236       {
237         if (foundTerm.contains(pdbId))
238         {
239           pdbIdWithChainCode = foundTerm;
240         }
241       }
242     }
243     else if (searchTerm.contains(pdbId))
244     {
245       pdbIdWithChainCode = searchTerm;
246     }
247     else
248     {
249       pdbIdWithChainCode = pdbId;
250     }
251     return pdbIdWithChainCode;
252   }
253
254   @Override
255   public FTSRestClientI getFTSRestClient()
256   {
257     return PDBFTSRestClient.getInstance();
258   }
259
260   @Override
261   public String getFTSFrameTitle()
262   {
263     return defaultFTSFrameTitle;
264   }
265
266   @Override
267   public boolean isPaginationEnabled()
268   {
269     return true;
270   }
271
272   @Override
273   public Map<String, Integer> getTempUserPrefs()
274   {
275     return tempUserPrefs;
276   }
277
278   @Override
279   public String getCacheKey()
280   {
281     return PDB_FTS_CACHE_KEY;
282   }
283
284 }