1 | package eu.clarin.sru.fcs.aggregator.app; |
---|
2 | |
---|
3 | import com.google.gson.Gson; |
---|
4 | import com.google.gson.reflect.TypeToken; |
---|
5 | import com.sun.jersey.api.client.Client; |
---|
6 | import eu.clarin.sru.client.SRUVersion; |
---|
7 | import eu.clarin.sru.fcs.aggregator.data.CenterRegistry; |
---|
8 | import eu.clarin.sru.fcs.aggregator.sparam.CorpusTreeModel; |
---|
9 | import eu.clarin.sru.fcs.aggregator.sparam.CorpusTreeNodeRenderer; |
---|
10 | import eu.clarin.sru.fcs.aggregator.sresult.SearchResultsController; |
---|
11 | import java.lang.reflect.Type; |
---|
12 | import java.util.ArrayList; |
---|
13 | import java.util.LinkedHashMap; |
---|
14 | import java.util.List; |
---|
15 | import java.util.Map; |
---|
16 | import java.util.Set; |
---|
17 | import java.util.logging.*; |
---|
18 | import org.zkoss.util.media.AMedia; |
---|
19 | import org.zkoss.util.media.Media; |
---|
20 | import org.zkoss.zk.ui.Component; |
---|
21 | import org.zkoss.zk.ui.Executions; |
---|
22 | import org.zkoss.zk.ui.event.Event; |
---|
23 | import org.zkoss.zk.ui.select.SelectorComposer; |
---|
24 | import org.zkoss.zk.ui.select.annotation.Listen; |
---|
25 | import org.zkoss.zk.ui.select.annotation.Wire; |
---|
26 | import org.zkoss.zul.Button; |
---|
27 | import org.zkoss.zul.Combobox; |
---|
28 | import org.zkoss.zul.Comboitem; |
---|
29 | import org.zkoss.zul.Groupbox; |
---|
30 | import org.zkoss.zul.Iframe; |
---|
31 | import org.zkoss.zul.Label; |
---|
32 | import org.zkoss.zul.Menuitem; |
---|
33 | import org.zkoss.zul.Messagebox; |
---|
34 | import org.zkoss.zul.Textbox; |
---|
35 | import org.zkoss.zul.Tree; |
---|
36 | import org.zkoss.zul.Treechildren; |
---|
37 | import org.zkoss.zul.Treeitem; |
---|
38 | import org.zkoss.zul.Window; |
---|
39 | import org.zkoss.zul.event.ZulEvents; |
---|
40 | import com.sun.jersey.api.client.WebResource; |
---|
41 | import com.sun.jersey.api.client.config.ClientConfig; |
---|
42 | import com.sun.jersey.api.client.config.DefaultClientConfig; |
---|
43 | import eu.clarin.weblicht.wlfxb.tc.api.GeoLongLatFormat; |
---|
44 | import eu.clarin.weblicht.wlfxb.tc.api.Token; |
---|
45 | import eu.clarin.weblicht.wlfxb.tc.xb.TextCorpusStored; |
---|
46 | import eu.clarin.weblicht.wlfxb.xb.WLData; |
---|
47 | import javax.ws.rs.core.MediaType; |
---|
48 | import org.zkoss.zul.Popup; |
---|
49 | |
---|
50 | /** |
---|
51 | * Main window of the Aggregator application. |
---|
52 | * |
---|
53 | * @author Yana Panchenko |
---|
54 | */ |
---|
55 | public class Aggregator extends SelectorComposer<Component> { |
---|
56 | |
---|
57 | private static final Logger logger = Logger.getLogger(Aggregator.class.getName()); |
---|
58 | // @Wire |
---|
59 | // private Grid anzeigeGrid; |
---|
60 | @Wire |
---|
61 | private Textbox searchString; |
---|
62 | @Wire |
---|
63 | private Combobox languageSelect; |
---|
64 | // @Wire |
---|
65 | // private Button searchButton; |
---|
66 | // @Wire |
---|
67 | // private Groupbox allCorpora; |
---|
68 | // @Wire |
---|
69 | // private Comboitem german; |
---|
70 | @Wire |
---|
71 | private Comboitem anyLanguage; |
---|
72 | @Wire |
---|
73 | private Window resultsBox; |
---|
74 | // @Wire |
---|
75 | // private Button selectAll; |
---|
76 | // @Wire |
---|
77 | // private Button deselectAll; |
---|
78 | // @Wire |
---|
79 | // private Window mainWindow; |
---|
80 | @Wire |
---|
81 | private Combobox maximumRecordsSelect; |
---|
82 | // @Wire |
---|
83 | // private Button addForeignEndpoint; |
---|
84 | @Wire |
---|
85 | Combobox foreignEndpointSelect; |
---|
86 | @Wire |
---|
87 | private Tree tree; |
---|
88 | @Wire |
---|
89 | private Label searchResultsProgress; |
---|
90 | @Wire |
---|
91 | private Popup wspaceSigninpop; |
---|
92 | @Wire |
---|
93 | private Textbox wspaceUserName; |
---|
94 | @Wire |
---|
95 | private Textbox wspaceUserPwd; |
---|
96 | private WebResource mapGenerator; |
---|
97 | public static final String MAPS_SERVICE_URL = "http://weblicht.sfs.uni-tuebingen.de/rws/service-geolocationconsumer/resources/geoloc/"; |
---|
98 | private Map<String, List<String>> xAggregationContext; |
---|
99 | private SRUVersion version = SRUVersion.VERSION_1_2; |
---|
100 | private SearchResultsController searchResultsController; |
---|
101 | private CenterRegistry registry; |
---|
102 | private boolean testingMode = false; |
---|
103 | |
---|
104 | @Override |
---|
105 | public void doAfterCompose(Component comp) throws Exception { |
---|
106 | |
---|
107 | super.doAfterCompose(comp); |
---|
108 | |
---|
109 | processParameters(); |
---|
110 | |
---|
111 | languageSelect.setSelectedItem(anyLanguage); |
---|
112 | |
---|
113 | searchResultsController = new SearchResultsController(resultsBox, searchResultsProgress); |
---|
114 | // assign the search controller to desktop, so that it can be accessed to be shutdown when the desktop is destroyed |
---|
115 | Executions.getCurrent().getDesktop().setAttribute(searchResultsController.getClass().getSimpleName(), searchResultsController); |
---|
116 | // also add it to the list of actice controllers of the web application, so that they can be shutdown when the application stops |
---|
117 | Set<SearchResultsController> activeControllers = (Set<SearchResultsController>) Executions.getCurrent().getDesktop().getWebApp().getAttribute(WebAppListener.ACTIVE_SEARCH_CONTROLLERS); |
---|
118 | activeControllers.add(searchResultsController); |
---|
119 | |
---|
120 | registry = new CenterRegistry(); |
---|
121 | registry.loadChildren(testingMode); |
---|
122 | CorpusTreeModel corporaModel = new CorpusTreeModel(registry); |
---|
123 | tree.setModel(corporaModel); |
---|
124 | tree.setItemRenderer(new CorpusTreeNodeRenderer()); |
---|
125 | tree.setMultiple(true); |
---|
126 | |
---|
127 | |
---|
128 | //tempMap(); |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | @Listen("onSelect = #languageSelect") |
---|
133 | public void onSelectLanguage(Event ev) { |
---|
134 | //TODO |
---|
135 | } |
---|
136 | |
---|
137 | @Listen(ZulEvents.ON_AFTER_RENDER + "=#tree") |
---|
138 | public void onAfterRenderCorporaTree(Event ev) { |
---|
139 | CorpusTreeNodeRenderer.selectEndpoints(this.tree, this.xAggregationContext); |
---|
140 | } |
---|
141 | |
---|
142 | @Listen("onClick = #selectAll") |
---|
143 | public void onSelectAll(Event ev) { |
---|
144 | Treechildren openTreeItems = tree.getTreechildren(); |
---|
145 | for (Treeitem openItem : openTreeItems.getItems()) { |
---|
146 | CorpusTreeNodeRenderer.selectItem(openItem); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | @Listen("onClick = #deselectAll") |
---|
151 | public void onDeselectAll(Event ev) { |
---|
152 | Treechildren openTreeItems = tree.getTreechildren(); |
---|
153 | for (Treeitem openItem : openTreeItems.getItems()) { |
---|
154 | CorpusTreeNodeRenderer.unselectItem(openItem); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | @Listen("onClick = #searchButton") |
---|
159 | public void onExecuteSearch(Event ev) { |
---|
160 | int maxRecords = Integer.parseInt(maximumRecordsSelect.getValue()); |
---|
161 | searchResultsController.executeSearch(tree.getSelectedItems(), maxRecords, searchString.getText(), version); |
---|
162 | } |
---|
163 | |
---|
164 | @Listen("onOK = #searchString") |
---|
165 | public void onEnterSearchString(Event ev) { |
---|
166 | onExecuteSearch(ev); |
---|
167 | } |
---|
168 | |
---|
169 | @Listen("onClick=#clearResults") |
---|
170 | public void onClearResults(Event ev) { |
---|
171 | resultsBox.getChildren().clear(); |
---|
172 | } |
---|
173 | |
---|
174 | @Listen("onClick=#showHelp") |
---|
175 | public void onShowHelp(Event ev) { |
---|
176 | resultsBox.getChildren().clear(); |
---|
177 | Iframe help = new Iframe(); |
---|
178 | help.setWidth("100%"); |
---|
179 | help.setHeight("100%"); |
---|
180 | help.setSrc("help.html"); |
---|
181 | resultsBox.appendChild(help); |
---|
182 | } |
---|
183 | |
---|
184 | @Listen("onClick=#showAbout") |
---|
185 | public void onShowAbout(Event ev) { |
---|
186 | Messagebox.show("CLARIN-D Federated Content Search Aggregator\n\nVersion 0.0.1", "FCS", 0, Messagebox.INFORMATION); |
---|
187 | |
---|
188 | } |
---|
189 | |
---|
190 | @Listen("onClick=#exportResultsCSV") |
---|
191 | public void onExportResultsCSV(Event ev) { |
---|
192 | searchResultsController.exportCSV(); |
---|
193 | } |
---|
194 | |
---|
195 | @Listen("onClick=#exportResultsTCF") |
---|
196 | public void onExportResultsTCF(Event ev) { |
---|
197 | searchResultsController.exportTCF(); |
---|
198 | } |
---|
199 | |
---|
200 | @Listen("onClick=#exportResultsPWTCF") |
---|
201 | public void onExportResultsPWTCF(Event ev) { |
---|
202 | wspaceSigninpop.open(resultsBox, "top_center"); |
---|
203 | } |
---|
204 | |
---|
205 | @Listen("onClick=#wspaceSigninBtn") |
---|
206 | public void onSignInExportResultsPWTCF(Event ev) { |
---|
207 | String user = wspaceUserName.getValue(); |
---|
208 | String pswd = wspaceUserPwd.getValue(); |
---|
209 | if (user.isEmpty() || pswd.isEmpty()) { |
---|
210 | Messagebox.show("Need user name and password!"); |
---|
211 | } else { |
---|
212 | wspaceUserPwd.setValue(""); |
---|
213 | wspaceSigninpop.close(); |
---|
214 | searchResultsController.exportPWTCF(user, pswd); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | @Listen("onOK=#wspaceUserPwd") |
---|
219 | public void onSignInExportResultsPWTCFPwdOK(Event ev) { |
---|
220 | onSignInExportResultsPWTCF(ev); |
---|
221 | } |
---|
222 | |
---|
223 | @Listen("onClick=#wspaceCancelBtn") |
---|
224 | public void onSignInPWCancel(Event ev) { |
---|
225 | wspaceUserPwd.setValue(""); |
---|
226 | wspaceSigninpop.close(); |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | @Listen("onClick=#addForeignEndpoint") |
---|
231 | public void onAddForeignEndpoint(Event ev) { |
---|
232 | registry.addForeignPoint(foreignEndpointSelect.getValue().split(";")[1], foreignEndpointSelect.getValue().split(";")[0]); |
---|
233 | } |
---|
234 | |
---|
235 | private void processParameters() { |
---|
236 | |
---|
237 | String[] paramValue; |
---|
238 | String contextJson = null; |
---|
239 | |
---|
240 | String[] paramsReceived = new String[4]; |
---|
241 | |
---|
242 | paramValue = Executions.getCurrent().getParameterMap().get("query"); |
---|
243 | if (paramValue != null) { |
---|
244 | searchString.setValue(paramValue[0].trim()); |
---|
245 | paramsReceived[0] = searchString.getValue(); |
---|
246 | } |
---|
247 | paramValue = Executions.getCurrent().getParameterMap().get("operation"); |
---|
248 | if (paramValue != null) { |
---|
249 | String operationString = paramValue[0].trim(); |
---|
250 | paramsReceived[1] = operationString; |
---|
251 | if (!operationString.equals("searchRetrieve")) { |
---|
252 | Messagebox.show("Not supported operation " + operationString, "FCS", 0, Messagebox.INFORMATION); |
---|
253 | } |
---|
254 | } |
---|
255 | paramValue = Executions.getCurrent().getParameterMap().get("version"); |
---|
256 | if (paramValue != null) { |
---|
257 | String versionString = paramValue[0].trim(); |
---|
258 | paramsReceived[2] = versionString; |
---|
259 | if (versionString.equals("1.2")) { |
---|
260 | version = SRUVersion.VERSION_1_2; |
---|
261 | } else if (versionString.equals("1.1")) { |
---|
262 | version = SRUVersion.VERSION_1_1; |
---|
263 | } else { |
---|
264 | Messagebox.show("SRU Version " + version + " not supported", "FCS", 0, Messagebox.INFORMATION); |
---|
265 | } |
---|
266 | } |
---|
267 | paramValue = Executions.getCurrent().getParameterMap().get("x-aggregation-context"); |
---|
268 | if (paramValue != null) { |
---|
269 | contextJson = paramValue[0].trim(); |
---|
270 | paramsReceived[3] = contextJson; |
---|
271 | } |
---|
272 | logger.log(Level.INFO, "Received parameters: query[{0}], operation[{1}], version[{2}], x-aggregation-context[{3}], ", paramsReceived); |
---|
273 | |
---|
274 | paramValue = Executions.getCurrent().getParameterMap().get("mode"); |
---|
275 | if (paramValue != null) { |
---|
276 | String mode = paramValue[0].trim(); |
---|
277 | if (mode.equals("testing")) { |
---|
278 | testingMode = true; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | if (contextJson != null) { |
---|
283 | Gson gson = new Gson(); |
---|
284 | Type mapType = new TypeToken<LinkedHashMap<String, ArrayList<String>>>() { |
---|
285 | }.getType(); |
---|
286 | try { |
---|
287 | this.xAggregationContext = gson.fromJson(contextJson, mapType); |
---|
288 | } catch (Exception ex) { |
---|
289 | logger.log(Level.SEVERE, "Error parsing JSON from x-aggregation-context: {0} {1}", new String[]{ex.getMessage(), contextJson}); |
---|
290 | Messagebox.show("Error in x-aggregation-context parameter", "FCS", 0, Messagebox.INFORMATION); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | } |
---|
295 | |
---|
296 | private void tempMap() { |
---|
297 | ClientConfig config = new DefaultClientConfig(); |
---|
298 | Client client = Client.create(config); |
---|
299 | mapGenerator = client.resource(MAPS_SERVICE_URL); |
---|
300 | TextCorpusStored tc = new TextCorpusStored("en"); |
---|
301 | Token t1 = tc.createTokensLayer().addToken("Virginia"); |
---|
302 | List<Token> s1 = new ArrayList<Token>(); |
---|
303 | s1.add(t1); |
---|
304 | tc.createSentencesLayer().addSentence(s1); |
---|
305 | tc.createGeoLayer("unknown", GeoLongLatFormat.DegDec); |
---|
306 | //tc.getGeoLayer().addPoint("138.56027", "-34.6663", 15.0, null, null, null, t1); |
---|
307 | WLData data = new WLData(tc); |
---|
308 | |
---|
309 | Iframe resultsPic = (Iframe) resultsBox.getFellow("resultsPic"); |
---|
310 | |
---|
311 | try { |
---|
312 | String output = mapGenerator.path("3").accept(MediaType.TEXT_HTML).type("text/tcf+xml").post(String.class, data); |
---|
313 | Media media = new AMedia("map-" + 4 + ".html", null, "text/html", output); |
---|
314 | resultsPic.setContent(media); |
---|
315 | } catch (Exception e) { |
---|
316 | Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, "ERROR accessing the maps generator service", e); |
---|
317 | Messagebox.show("ERROR accessing the maps generator service \n" + e.getMessage(), "FCS", 0, Messagebox.INFORMATION); |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|