View Javadoc

1   package org.astrogrid.registry.server.query;
2   
3   import org.w3c.dom.Document;
4   import javax.xml.parsers.DocumentBuilderFactory;
5   import javax.xml.parsers.DocumentBuilder;
6   
7   import java.io.Reader;
8   import java.io.StringReader;
9   import org.xml.sax.InputSource;
10  import javax.xml.parsers.ParserConfigurationException;
11  import org.xml.sax.SAXException;
12  import java.io.IOException;
13  import org.astrogrid.util.DomHelper;
14  import org.astrogrid.config.Config;
15  import org.astrogrid.registry.server.XQueryExecution;
16  import org.astrogrid.registry.server.RegistryServerHelper;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  import org.astrogrid.xmldb.eXist.server.QueryDBService;
22  import org.apache.axis.AxisFault;
23  /***
24   * Registry Service class for querying the eXist database.
25   *  
26   * @deprecated Class is now deprecated see RegistryQueryService.
27   * @see org.astrogrid.registry.server.query.RegistryQueryService
28   * @link http://www.ivoa.net/twiki/bin/view/IVOA/IVOARegWp03
29   * @author Kevin Benson
30   */
31  public class RegistryService {
32  
33     /***
34      * Logging variable for writing information to the logs
35      */
36     private static final Log log = LogFactory.getLog(RegistryService.class);
37  
38     /***
39      * conf - Config variable to access the configuration for the server normally
40      * jndi to a config file.
41      * @see org.astrogrid.config.Config
42      */
43     public static Config conf = null;
44     
45     /***
46      * final variable for the default AuthorityID associated to this registry.
47      */
48     private static final String AUTHORITYID_PROPERTY = 
49                                            "org.astrogrid.registry.authorityid";
50  
51     /***
52      * variable set to the location of the xml files in the eXist XML database
53      * the eXist database calls them collections
54      */
55     private static String collectionName = null;                                          
56     
57     /***
58      * Static to be used on the initiatian of this class for the config and
59      * collectionName variables.
60      */
61     static {
62        if(conf == null) {
63           conf = org.astrogrid.config.SimpleConfig.getSingleton();
64           collectionName = "astrogridv" + conf.getString("org.astrogrid.registry.version");
65        }      
66     }
67  
68  
69     /***
70     * submitQuery queries the registry for Resources.  Currently uses
71     * an older xml query language that Astrogrid came up with, but will
72     * soon be rarely used for the ADQL version to be the standard for the
73     * IVOA.
74     * 
75     * @param query XML document object representing the query language used on the registry.
76     * @return XML docuemnt object representing the result of the query.
77     * @author Kevin Benson 
78     */
79     public Document submitQuery(Document query) throws AxisFault {
80        log.debug("start submitQuery");
81        long beginQ = System.currentTimeMillis();
82        Document registryDoc = null;
83        //log.info("received = " + DomHelper.DocumentToString(query));
84        //parse query right now actually does the query.
85        QueryDBService qdb = new QueryDBService();    
86        registryDoc = qdb.query(collectionName,XQueryExecution.createXQL(query));
87        if(registryDoc != null)
88           log.info("the registryDoc = " + DomHelper.
89                                           DocumentToString(registryDoc));
90        log.info("Time taken to complete submitQuery on server = " +
91                 (System.currentTimeMillis() - beginQ));
92        log.debug("end submitQuery");
93        return registryDoc;
94     }
95     
96     /***
97      * Queries for the Registry Resource element that is tied to this Registry.
98      * All Astrogrid Registries have one Registry Resource tied to the Registry.
99      * Which defines the AuthorityID's it manages and how to access the Registry.
100     * 
101     * @param query actually normally empty/null
102     * @return XML docuemnt object representing the result of the query.
103     */
104    public Document loadRegistry(Document query) throws AxisFault {
105       log.debug("start loadRegistry");
106       long beginQ = System.currentTimeMillis();
107       String authorityID = conf.getString(AUTHORITYID_PROPERTY);
108       authorityID = authorityID.trim();
109       Document doc = null;
110       Document responseDoc = null;
111       
112       String selectQuery = "<query><selectionSequence>" +
113             "<selection item='searchElements' itemOp='EQ' value='Resource'/>" +
114             "<selectionOp op='$and$'/>" +
115             "<selection item='vr:Identifier/vr:AuthorityID' itemOp='EQ' value='" +
116                 authorityID + "'/>" +
117             "<selectionOp op='AND'/>" +
118             "<selection item='@xsi:type' itemOp='EQ' value='RegistryType'/>"  +
119          "</selectionSequence></query>";
120       
121       try {         
122          doc = DomHelper.newDocument(selectQuery);
123       }catch(ParserConfigurationException pce) {
124          pce.printStackTrace();
125          log.error(pce);
126       }catch(IOException ioe) {
127          ioe.printStackTrace();
128          log.error(ioe);
129       }catch(SAXException sax) {
130          sax.printStackTrace();
131          log.error(sax);
132       }
133       
134       if(doc != null) {
135          QueryDBService qdb = new QueryDBService();
136          String xql = XQueryExecution.createXQL(doc);
137          log.info("The XQL String query = " + xql);
138          responseDoc = qdb.query(collectionName,xql);
139       }
140       log.info("Time taken to complete loadRegistry on server = " +
141                (System.currentTimeMillis() - beginQ));
142       log.debug("end loadRegistry");
143       return responseDoc;
144    }
145 }