JAL-2071 architectural improvement for Plugable Free Text Search Services
[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.HashSet;
33
34 import javax.swing.table.DefaultTableModel;
35
36 @SuppressWarnings("serial")
37 public class PDBFTSPanel extends GFTSPanel
38 {
39
40   public PDBFTSPanel(SequenceFetcher seqFetcher)
41   {
42     this.seqFetcher = seqFetcher;
43     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
44             .getProgressIndicator();
45   }
46
47
48   /**
49    * Action performed when an input is detected on txt_search field.
50    */
51   @Override
52   public void txt_search_ActionPerformed()
53   {
54     new Thread()
55     {
56       @Override
57       public void run()
58       {
59         lbl_loading.setVisible(false);
60         errorWarning.setLength(0);
61         lbl_warning.setVisible(false);
62         btn_ok.setEnabled(false);
63         boolean allowEmptySequence = false;
64         mainFrame.setTitle(getFTSFrameTitle());
65         tbl_summary.setModel(new DefaultTableModel());
66         tbl_summary.setVisible(false);
67         if (txt_search.getText().trim().length() > 0)
68         {
69           lbl_loading.setVisible(true);
70           long startTime = System.currentTimeMillis();
71
72           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
73                   .getSelectedItem()).getCode();
74
75           // wantedFields = FTSDataColumnPreferences.getSearchSummaryFields();
76           wantedFields = PDBFTSRestClient.getInstance()
77                   .getAllDefaulDisplayedDataColumns();
78           String searchTerm = decodeSearchTerm(txt_search.getText(),
79                   searchTarget);
80
81           FTSRestRequest request = new FTSRestRequest();
82           request.setAllowEmptySeq(allowEmptySequence);
83           request.setResponseSize(100);
84           request.setFieldToSearchBy("(" + searchTarget + ":");
85           request.setSearchTerm(searchTerm + ")");
86           request.setWantedFields(wantedFields);
87           // System.out.println(">>>>>>>>>>>>>> " + request.getQuery());
88           FTSRestClientI pdbRestCleint = PDBFTSRestClient.getInstance();
89           FTSRestResponse resultList;
90           try
91           {
92             resultList = pdbRestCleint.executeRequest(request);
93           } catch (Exception e)
94           {
95             // e.printStackTrace();
96             errorWarning.append(e.getMessage());
97             checkForErrors();
98             return;
99           }
100
101           if (resultList.getSearchSummary() != null)
102           {
103             tbl_summary.setModel(FTSRestResponse.getTableModel(request,
104                     resultList.getSearchSummary()));
105             FTSRestResponse.configureTableColumn(tbl_summary, wantedFields);
106             tbl_summary.setVisible(true);
107           }
108
109
110           long endTime = System.currentTimeMillis();
111           int resultSetCount = resultList.getNumberOfItemsFound();
112           String result = (resultSetCount > 1) ? MessageManager
113                   .getString("label.results") : MessageManager
114                   .getString("label.result");
115           mainFrame.setTitle(getFTSFrameTitle() + " - " + resultSetCount
116                   + " "
117                   + result + " (" + (endTime - startTime) + " milli secs)");
118           lbl_loading.setVisible(false);
119         }
120       }
121     }.start();
122   }
123
124   public static String decodeSearchTerm(String enteredText,
125           String targetField)
126   {
127     String foundSearchTerms = enteredText;
128     StringBuilder foundSearchTermsBuilder = new StringBuilder();
129     if (enteredText.contains(";"))
130     {
131       String[] searchTerms = enteredText.split(";");
132       for (String searchTerm : searchTerms)
133       {
134         if (searchTerm.contains(":"))
135         {
136           foundSearchTermsBuilder.append(targetField).append(":")
137                   .append(searchTerm.split(":")[0]).append(" OR ");
138         }
139         else
140         {
141           foundSearchTermsBuilder.append(targetField).append(":")
142                   .append(searchTerm).append(" OR ");
143         }
144       }
145       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
146       foundSearchTerms = foundSearchTermsBuilder.toString();
147       if (foundSearchTerms.contains(" OR "))
148       {
149         foundSearchTerms = foundSearchTerms.substring(
150                 targetField.length() + 1, endIndex);
151       }
152     }
153     else if (enteredText.contains(":"))
154     {
155       foundSearchTerms = foundSearchTerms.split(":")[0];
156     }
157     return foundSearchTerms;
158   }
159
160   @Override
161   public void btn_ok_ActionPerformed()
162   {
163     // mainFrame.dispose();
164     disableActionButtons();
165     StringBuilder selectedIds = new StringBuilder();
166     HashSet<String> selectedIdsSet = new HashSet<String>();
167     int pdbIdCol = 0;
168     try
169     {
170       pdbIdCol = getFTSRestClient().getPrimaryKeyColumIndex(wantedFields,
171               false);
172     } catch (Exception e)
173     {
174       // TODO Auto-generated catch block
175       e.printStackTrace();
176     }
177     int[] selectedRows = tbl_summary.getSelectedRows();
178     for (int summaryRow : selectedRows)
179     {
180       String idStr = tbl_summary.getValueAt(summaryRow, pdbIdCol)
181               .toString();
182       String searchTerm = txt_search.getText();
183       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
184     }
185
186     for (String selectedId : selectedIdsSet)
187     {
188       selectedIds.append(selectedId).append(";");
189     }
190
191     String ids = selectedIds.toString();
192     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
193     seqFetcher.getTextArea().setText(ids);
194     Thread worker = new Thread(seqFetcher);
195     worker.start();
196     delayAndEnableActionButtons();
197   }
198
199
200   public static String getPDBIdwithSpecifiedChain(String pdbId,
201           String searchTerm)
202   {
203     String pdbIdWithChainCode = "";
204     if (searchTerm.contains(";"))
205     {
206       String[] foundTerms = searchTerm.split(";");
207       for (String foundTerm : foundTerms)
208       {
209         if (foundTerm.contains(pdbId))
210         {
211           pdbIdWithChainCode = foundTerm;
212         }
213       }
214     }
215     else if (searchTerm.contains(pdbId))
216     {
217       pdbIdWithChainCode = searchTerm;
218     }
219     else
220     {
221       pdbIdWithChainCode = pdbId;
222     }
223     return pdbIdWithChainCode;
224   }
225
226
227
228   @Override
229   public FTSRestClientI getFTSRestClient()
230   {
231     return PDBFTSRestClient.getInstance();
232   }
233
234   @Override
235   public String getFTSFrameTitle()
236   {
237     return MessageManager.getString("label.pdb_sequence_fetcher");
238   }
239
240 }