1 package org.astrogrid.portal.registry;
2
3 import org.apache.avalon.framework.parameters.Parameters;
4 import org.apache.cocoon.acting.AbstractAction;
5 import org.apache.cocoon.environment.Request;
6 import org.apache.cocoon.environment.Session;
7 import org.apache.cocoon.environment.Redirector;
8 import org.apache.cocoon.environment.SourceResolver;
9 import org.apache.cocoon.environment.ObjectModelHelper;
10 import java.util.Map;
11 import java.util.HashMap;
12 import java.util.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.TreeMap;
15 import java.util.Set;
16 import java.util.Iterator;
17 import java.io.File;
18 import java.io.IOException;
19 import org.astrogrid.registry.client.admin.RegistryAdminDocumentHelper;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.DocumentFragment;
22 import org.w3c.dom.NodeList;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.Element;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.ParserConfigurationException;
28 import org.xml.sax.SAXException;
29 import org.apache.commons.logging.Log;
30 import org.apache.axis.components.logger.LogFactory;
31 import org.apache.axis.utils.XMLUtils;
32 import org.astrogrid.registry.client.RegistryDelegateFactory;
33 import org.astrogrid.registry.client.query.RegistryService;
34 import org.astrogrid.util.DomHelper;
35
36 import org.astrogrid.registry.NoResourcesFoundException;
37 import org.astrogrid.registry.RegistryException;
38
39 import org.astrogrid.config.Config;
40
41 import org.astrogrid.util.DomHelper;
42 import org.astrogrid.registry.common.WSDLBasicInformation;
43 import org.astrogrid.store.Ivorn;
44
45 import java.io.InputStream;
46 import java.net.URL;
47
48
49 /***
50 *
51 *
52 */
53 public class RegistryBrowserAction extends AbstractAction
54 {
55 private static Log log = LogFactory.getLog("RegistryBrowserAction");
56 /***
57 * Switch for our debug statements.
58 *
59 */
60 public static boolean DEBUG_FLAG = true;
61 public static boolean STACK_TRACE = false;
62
63 public static final String PARAM_MAIN_ELEMENT = "mainelement";
64
65 /***
66 * Cocoon param for the user param in the session.
67 *
68 */
69
70 public static final String PARAM_ACTION = "action";
71
72 public static final String QUERY_ACTION = "queryregistry";
73
74 public static final String SELECT_ACTION = "selectentry";
75
76 public static final String CONFIRM_ACTION = "Verify";
77
78 public static final String TABLE_ACTION = "getTable";
79
80
81
82
83
84 public static final String SEPARATOR = "!";
85
86 public static final String SEPARATORHACK = "!";
87
88
89 public static final String PARAM_SERVER = "Server";
90
91 public static final String PARAM_ID = "identifier";
92
93 public static final String UNIQUE_ID = "uniqueID";
94
95 public static final String PARAM_TITLE = "title";
96
97 public static final String PARAM_AUTHORITY_ID = "authId";
98
99 public static final String PARAM_PARENT_AUTHORITY_ID = "parent_authId";
100
101 public static final String PARAM_RESOURCE_KEY = "resourceKey";
102
103 public static final String PARAM_COLUMN = "column";
104
105 public static final String PARAM_COLUMN_NAME = "colname";
106
107 public static final String PARAM_TABLE_NAME = "tabname";
108
109 public static final String PARAM_UCD = "colucd";
110
111 public static final String PARAM_UNITS = "colunits";
112
113 public static final String PARAM_Description = "coldescr";
114
115 public static final String PARAM_RESULT_LIST = "resultlist";
116
117 public static final String QUERY_RESULT = "queryresult";
118
119 public static final String RESULT_IDENTIFIER = "resultidentifier";
120
121 public static final String CATALOG_SEARCH = "Catalog";
122
123 public static final String TOOL_SEARCH = "Tool";
124
125 public static final String TABLE_SEARCH = "Table";
126
127 public static final String ERROR_MESSAGE = "errormessage";
128
129 public static final String SESSION_TABLEID = "tableID";
130 public static final String SESSION_SINGLE_CATALOG = "resultSingleCatalog";
131 public static final String SESSION_UNIQUEID = "uniqueID";
132
133 public static Config conf = null;
134
135 static {
136 if(conf == null) {
137 conf = org.astrogrid.config.SimpleConfig.getSingleton();
138 }
139 }
140
141
142 /***
143 * Action page to do a query.
144 *
145 */
146 public Map act(
147 Redirector redirector,
148 SourceResolver resolver,
149 Map objectModel,
150 String source,
151 Parameters params) {
152
153
154
155 Request request = ObjectModelHelper.getRequest(objectModel);
156 Session session = request.getSession();
157 String errorMessage = null;
158 int crit_number = 0;
159 Document registryDocument = null;
160 ArrayList resultlist = null;
161 NodeList resultNodes = null;
162 String result = null;
163 String resultid = null;
164 String method = "Action()";
165
166
167 String action = request.getParameter(PARAM_ACTION);
168 String mainElem = request.getParameter(PARAM_MAIN_ELEMENT);
169 String identifier = request.getParameter(PARAM_ID);
170 if ( identifier != null && (identifier.length() == 0
171 || identifier.equals("null") ) ) identifier = null;
172 String authid = request.getParameter(PARAM_AUTHORITY_ID);
173 if ( authid != null && ( authid.length() == 0
174 || authid.equals("null") ) ) authid = null;
175 String parent_authid = request.getParameter(PARAM_PARENT_AUTHORITY_ID);
176 if ( parent_authid != null && ( parent_authid.length() == 0
177 || parent_authid.equals("null") ) ) parent_authid = null;
178 String resourcekey = request.getParameter(PARAM_RESOURCE_KEY);
179 if ( resourcekey != null && ( resourcekey.length() == 0
180 || resourcekey.equals("null") ) ) resourcekey = null;
181 String title = request.getParameter(PARAM_TITLE);
182 if ( title != null && ( title.length() == 0
183 || title.equals("null") ) ) title = null;
184 String colname = request.getParameter(PARAM_COLUMN_NAME);
185 if ( colname != null && ( colname.length() == 0
186 || colname.equals("null") ) ) colname = null;
187 String tabname = request.getParameter(PARAM_TABLE_NAME);
188 if ( tabname != null && ( tabname.length() == 0
189 || tabname.equals("null") ) ) tabname = null;
190 String ucd = request.getParameter(PARAM_UCD);
191 if ( ucd != null && ( ucd.length() == 0
192 || ucd.equals("null") ) ) ucd = null;
193 String units = request.getParameter(PARAM_UNITS);
194 if ( units != null && ( units.length() == 0
195 || units.equals("null") ) ) units = null;
196 String coldesc = request.getParameter(PARAM_Description);
197 if ( coldesc != null && ( coldesc.length() == 0
198 || coldesc.equals("null") ) ) coldesc = null;
199
200
201 if(DEBUG_FLAG) {
202 printDebug( method, "the action is = " + action );
203 printDebug( method, "the mainElem = " + mainElem );
204 printDebug( method, "the title = " + title );
205 printDebug( method, "the authid = " + authid );
206 printDebug( method, "the parent_authid = " + parent_authid );
207 printDebug( method, "the resourcekey = " + resourcekey );
208 printDebug( method, "the identifier = " + identifier );
209 printDebug( method, "column name = " + colname );
210 printDebug( method, "table name = " + tabname );
211 printDebug( method, "UCD = " + ucd );
212 printDebug( method, "units = " + units );
213 printDebug( method, "descr = " + coldesc );
214 }
215
216
217 if( QUERY_ACTION.equals(action) ) {
218
219
220 String query = buildQuery( mainElem, authid, resourcekey,
221 title, tabname, colname,
222 ucd, units, coldesc );
223 printDebug( method, "Query = \n" + query);
224
225 try {
226
227 RegistryService rs = RegistryDelegateFactory.createQuery( );
228 printDebug( method, "Service = " + rs);
229 Document doc = rs.submitQuery( query );
230 request.setAttribute("resultDoc", (Node) doc);
231
232
233 resultlist = createList( doc );
234 resultNodes = createNodes( doc );
235 request.setAttribute("resultlist", resultlist);
236 request.setAttribute("resultNodes", resultNodes);
237
238 printDebug( method, "Number of Result = " + resultlist.size());
239 if( resultlist.size() == 0 )
240 throw new NoResourcesFoundException("No Results found");
241
242 } catch( NoResourcesFoundException nrfe ) {
243 errorMessage = "Your query produced no results." ;
244 printDebug( method, errorMessage + " : " + nrfe );
245 if ( STACK_TRACE) nrfe.printStackTrace();
246 } catch( RegistryException re ) {
247 errorMessage =
248 "A error occurred in processing your query with the Registry.";
249 printDebug( method, errorMessage + " : " + re );
250 if ( STACK_TRACE) re.printStackTrace();
251 } catch( Exception e ) {
252 errorMessage = "An exception occurred. " + e;
253 printDebug( method, errorMessage );
254 if ( STACK_TRACE) e.printStackTrace();
255 }
256 action = SELECT_ACTION;
257 } else if ( SELECT_ACTION.equals(action) ) {
258 resultid = request.getParameter( PARAM_ID );
259 printDebug( method, "Result id = " + resultid );
260
261 } else if ( TABLE_ACTION.equals(action) ) {
262 try
263 {
264 String table = "";
265 printDebug( method, "In table action!!" );
266 String uniqueID = request.getParameter(UNIQUE_ID);
267 String tableQuery = null;
268 if (uniqueID != null && uniqueID.length() > 0)
269 {
270 printDebug( method, "uniqueID = " + uniqueID);
271 String authorityID = uniqueID.substring( 0,
272 uniqueID.indexOf(SEPARATOR));
273 printDebug( method, "uniqueID - auth = " + authorityID );
274 String resourceKey = uniqueID.substring(
275 uniqueID.indexOf(SEPARATOR)+1,
276 uniqueID.lastIndexOf(SEPARATOR)
277 );
278 printDebug( method, "uniqueID - res = " + resourceKey);
279
280
281
282
283
284
285
286
287 table = uniqueID.substring( uniqueID.lastIndexOf(SEPARATOR)+1
288 ).trim();
289 printDebug( method, "uniqueID -table = " + table);
290
291 tableQuery = buildQuery( TABLE_SEARCH, authorityID,
292 resourceKey, null, null, null,
293 null, null, null);
294
295 printDebug( method, "tableQuery = " + tableQuery);
296
297 }
298
299 RegistryService rs = RegistryDelegateFactory.createQuery();
300 printDebug( method, "Service = " + rs);
301 Document doc = rs.submitQuery( tableQuery );
302
303
304
305 session.setAttribute(SESSION_TABLEID, table);
306 session.setAttribute(SESSION_SINGLE_CATALOG, doc);
307 session.setAttribute(SESSION_UNIQUEID, uniqueID);
308
309 }
310 catch( NoResourcesFoundException nrfe )
311 {
312 errorMessage = "Your query produced no results." ;
313 printDebug( method, errorMessage + " : " + nrfe );
314 if ( STACK_TRACE) nrfe.printStackTrace();
315 }
316 catch( RegistryException re )
317 {
318 errorMessage =
319 "A error occurred in processing your query with the Registry.";
320 printDebug( method, errorMessage + " : " + re );
321 if ( STACK_TRACE) re.printStackTrace();
322 }
323 catch( Exception e )
324 {
325 errorMessage = "An exception occurred. " + e;
326 printDebug( method, errorMessage );
327 if ( STACK_TRACE) e.printStackTrace();
328 }
329
330 } else if ( action == null ) {
331 printDebug( method, "First Time set action" );
332 action = QUERY_ACTION;
333 }
334
335
336 request.setAttribute( PARAM_ACTION, action );
337 request.setAttribute( PARAM_MAIN_ELEMENT, mainElem );
338 request.setAttribute( PARAM_AUTHORITY_ID, authid );
339 request.setAttribute( PARAM_PARENT_AUTHORITY_ID, parent_authid );
340 request.setAttribute( PARAM_RESOURCE_KEY, resourcekey );
341 request.setAttribute( ERROR_MESSAGE, errorMessage );
342
343
344
345 Map results = new HashMap() ;
346 results.put( PARAM_MAIN_ELEMENT, mainElem );
347 results.put( PARAM_RESULT_LIST, resultlist );
348 results.put( PARAM_AUTHORITY_ID, authid );
349 results.put( PARAM_PARENT_AUTHORITY_ID, parent_authid );
350 results.put( PARAM_RESOURCE_KEY, resourcekey );
351 results.put( ERROR_MESSAGE, errorMessage );
352 if( result != null && result.length() > 0 ) {
353 results.put( QUERY_RESULT, result );
354 }
355 return results;
356
357 }
358
359
360 /***
361 * This method gets a list of key elements from the result document.
362 * @param doc Query results in a DOM tree format.
363 * @return ArrayList of relevant the String XML results.
364 */
365 private ArrayList createList( Document doc ) {
366 NodeList nodes = doc.getDocumentElement().getChildNodes();
367 return listNodes( nodes );
368 }
369 private NodeList createNodes( Document doc ) {
370 NodeList nodes = doc.getDocumentElement().getChildNodes();
371 return nodes;
372 }
373 private ArrayList createList( Document doc, String Elem ) {
374 NodeList nodes = doc.getElementsByTagName(Elem);
375 return listNodes( nodes );
376 }
377
378 private ArrayList listNodes( NodeList nl ) {
379 ArrayList al = new ArrayList();
380 for(int i = 0; i < nl.getLength(); i++) {
381 String element = null;
382 Node node = nl.item(i);
383 if( node instanceof org.w3c.dom.Element ) {
384 element = XMLUtils.ElementToString( (Element) node );
385 al.add( element );
386 }
387 if (DEBUG_FLAG && i < 2 )
388 printDebug( "CreateList", "Result " + i + " ("
389 + node.getNodeType() + ") = " + element);
390 }
391 return al;
392 }
393
394 /***
395 * This method build the query string for a Authentification key.
396 * @param main the type of query (either Catalog or Resource).
397 * @param id identifier keywords.
398 * @param key keywords in the description.
399 * @return the Query as a String.
400 */
401 private String buildQuery( String main, String id, String key ) {
402 return buildQuery( main, id, key, null, null, null, null, null, null);
403 }
404
405 /***
406 * This method build the query string with the specified criteria.
407 * @param main the type of query (either Catalog or Resource).
408 * @param id keywords in the identifier.
409 * @param key keywords in the description.
410 * @param title keywords in the description.
411 * @param tabname keywords in the description.
412 * @param colname keywords in the description.
413 * @param ucd keywords in the description.
414 * @param units keywords in the description.
415 * @return the Query as a String.
416 */
417 private String buildQuery( String main, String id, String key,
418 String title, String tabname, String colname,
419 String ucd, String units, String desc ) {
420 printDebug( "BuildQuery", main + " : " + id + "/" + key + " : " + title
421 + " : " + tabname + " : " + colname );
422
423 String query = "<query>\n<selectionSequence>";
424 query += "\n<selection item='searchElements' itemOp='EQ' value='Resource'/>";
425 query += "\n<selectionOp op='$and$'/>";
426 if ( CATALOG_SEARCH.equals( main ) ) {
427 query += "\n<selectionSequence>\n";
428
429
430 query += "<selection item='vr:Type' itemOp='EQ' value='Catalog'/>";
431 query += "\n</selectionSequence>";
432 }
433 else if ( TOOL_SEARCH.equals( main ) ) {
434 query += "\n<selection item='@xsi:type' itemOp='EQ'";
435 query += " value='CeaApplicationType'/>";
436
437
438
439
440
441 }
442 else if ( TABLE_SEARCH.equals( main ) ) {
443 query += "<selection item='vr:Type' itemOp='EQ' value='Catalog'/>";
444 }
445
446
447 if ( id != null ) {
448 query += "\n<selectionOp op='AND'/>";
449 query += "<selection item='vr:Identifier/vr:AuthorityID' itemOp='CONTAINS'";
450 query += " value='" + id + "'/>";
451 }
452 if ( key != null ) {
453 query += "\n<selectionOp op='AND'/>";
454 query += "<selection item='vr:Identifier/vr:ResourceKey' itemOp='CONTAINS'";
455 query += " value='" + key + "'/>";
456 }
457
458 if ( title != null ) {
459 query += "\n<selectionOp op='AND'/>";
460 query += "<selection item='vr:Title' itemOp='CONTAINS' value='"
461 + title + "'/>";
462 }
463 if ( tabname != null ) {
464 query += "\n<selectionOp op='AND'/>";
465 query += "<selection item='*:Table/vr:Name' "
466 + "itemOp='EQ' value='" + tabname + "'/>";
467 }
468 if ( colname != null ) {
469 query += "\n<selectionOp op='AND'/>";
470 query += "<selection item='*:Table/*:Column/vr:Name' "
471 + "itemOp='EQ' value='" + colname + "'/>";
472 }
473 if ( ucd != null ) {
474 query += "\n<selectionOp op='AND'/>";
475 query += "<selection item='*:Table/*:Column/*:UCD' "
476 + "itemOp='EQ' value='" + ucd + "'/>";
477 }
478 if ( units != null ) {
479 query += "\n<selectionOp op='AND'/>";
480 query += "<selection item='*:Table/*:Column/*:Unit' "
481 + "itemOp='EQ' value='" + units + "'/>";
482 }
483 if ( desc != null ) {
484 query += "\n<selectionOp op='AND'/>";
485 query += "<selection item='*:Table/*:Column/vr:Description' "
486 + "itemOp='CN' value='" + desc + "'/>";
487 }
488
489
490 query += "\n</selectionSequence></query>";
491
492 return query;
493 }
494
495 private String getResultMessage(Document doc) {
496 String message = null;
497 NodeList nl = doc.getElementsByTagName("error");
498 if(nl != null && nl.getLength() > 0) {
499 message = nl.item(0).getFirstChild().getNodeValue();
500 }
501 return message;
502 }
503
504 private String getId( Document doc ) {
505 String id = null;
506 NodeList nl = doc.getElementsByTagName("Identifier");
507 if( nl != null && nl.getLength() > 0 ) {
508 id = nl.item(0).getFirstChild().getNodeValue();
509 }
510 nl = doc.getElementsByTagName("AuthorityId");
511 if( nl != null && nl.getLength() > 0 ) {
512 id = nl.item(0).getFirstChild().getNodeValue();
513 }
514 nl = doc.getElementsByTagName("ResourceKey");
515 if( nl != null && nl.getLength() > 0 ) {
516 id += "/" + nl.item(0).getFirstChild().getNodeValue();
517 }
518 return id;
519 }
520
521 /***
522 * Small convenience method to print DEBUG Messages.
523 * Now converted to use commons.logging
524 * @param method The java function
525 * @param message The Debug message
526 */
527
528 private void printDebug (String method, String message) {
529 if( DEBUG_FLAG ) {
530 log.debug( method + " : \n" + message );
531 System.out.println( method + " : " + message );
532 }
533 }
534 private void printMessage (String message) {
535 log.info( message );
536 }
537 }