summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/core/ProcessCoveragesRequest.java
blob: ed3b1ae0f771a09e4a1e77eec0e56c7536973ed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003 - 2010 Peter Baumann / rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcps.server.core;

import petascope.core.IDynamicMetadataSource;
import petascope.exceptions.PetascopeException;
import petascope.exceptions.WCPSException;
import petascope.wcps.grammar.WCPSRequest;
import petascope.wcps.grammar.wcpsLexer;
import petascope.wcps.grammar.wcpsParser;
import petascope.wcps.grammar.wcpsParser.wcpsRequest_return;
import java.io.IOException;
import java.io.StringReader;
import org.antlr.runtime.RecognitionException;
import org.odmg.DBag;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
import org.odmg.OQLQuery;
import org.odmg.QueryException;
import org.odmg.Transaction;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import rasj.RasGMArray;
import rasj.RasImplementation;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.xml.sax.InputSource;
import petascope.exceptions.ExceptionCode;

/** A WCPS ProcessCoveragesRequest request provides a (just one) rasdaman query, that it executes.
 *
 * Internally, it relies on XmlRequest, which computes the RasQL query.
 *
 */
public class ProcessCoveragesRequest {

    private String database;
    private IDynamicMetadataSource source;
    private String url;
    private WCPS wcps;
    private String rasqlQuery;
    private String mime;
    private XmlQuery xmlQuery;

    public ProcessCoveragesRequest(String url, String database, Node node, IDynamicMetadataSource source, WCPS wcps)
            throws WCPSException, SAXException, IOException, PetascopeException {
        super();
        this.source = source;
        this.url = url;
        this.database = database;
        this.wcps = wcps;
        Node child = node.getFirstChild();
        this.rasqlQuery = null;

        System.err.println("Parsing ProcessCoveragesRequest node: " + child.getNodeName());

        if (child.getNodeName().equals("ProcessCoveragesRequest") == false) {
            throw new WCPSException("The document contains an unrecognized node : "
                    + child.getNodeName());
        }

        child = child.getFirstChild();
        while (child.getNodeName().equals("#text")) {
            child = child.getNextSibling();
        }

        if (child.getNodeName().equals("query") == false) {
            throw new WCPSException("Could not find node <query>: " + child.getNodeName());
        }

        // "child" is now the node <query>.
        Node queryNode = child.getFirstChild();
        while (queryNode.getNodeName().equals("#text")) {
            queryNode = queryNode.getNextSibling();
        }

        /**
         * The following code is essential. It handles the two cases:
         * 1) the xml contains an <xmlSyntax> request
         * 2) the xml contains an <abstractSyntax> request
         */
        if (queryNode.getNodeName().equals("xmlSyntax")) {
            System.err.println("Found XML Syntax query");
            this.xmlQuery = new XmlQuery(this.source);
            xmlQuery.startParsing(queryNode);
        } else if (queryNode.getNodeName().equals("abstractSyntax")) {
            try {
                String abstractQuery = queryNode.getFirstChild().getNodeValue();
                System.err.println("Found Abstract Syntax query: " + abstractQuery);
                String xmlString = abstractQueryToXmlQuery(abstractQuery);
                InputSource xmlStringSource = new InputSource(new StringReader(xmlString));
                System.err.println("Coverted the Abstract syntax query to an XML query:");
                System.err.println("***********************************************");
                System.err.println(xmlString);
                System.err.println("***********************************************");
                ProcessCoveragesRequest newRequest = wcps.pcPrepare(url, database, xmlStringSource);
                this.xmlQuery = newRequest.getXmlRequestStructure();
            } catch (RecognitionException e) {
                throw new WCPSException("Abstract Syntax query is invalid: "
                        + e.getMessage());
            }
        } else {
            throw new WCPSException("Error ! Unexpected node: " + queryNode.getNodeName());
        }

        // If everything went well, we now have a proper value for "xmlQuery"
        this.rasqlQuery = xmlQuery.toRasQL();
        System.err.println("Final RasQL query: " + rasqlQuery);
        this.mime = xmlQuery.getMimeType();
    }

    public static String abstractQueryToXmlQuery(String abstractQuery) throws RecognitionException {
        CharStream cs = new ANTLRStringStream(abstractQuery);
        wcpsLexer lexer = new wcpsLexer(cs);
        CommonTokenStream tokens = new CommonTokenStream();
        tokens.setTokenSource(lexer);
        wcpsParser parser = new wcpsParser(tokens);
        wcpsRequest_return rrequest = parser.wcpsRequest();
        WCPSRequest request = rrequest.value;
        String xmlRequest = request.toXML();

        return xmlRequest;
    }

    public String getMime() {
        return mime;
    }

    private XmlQuery getXmlRequestStructure() {
        return xmlQuery;
    }

    public String getRasqlQuery() {
        return this.rasqlQuery;
    }

    public List<byte[]> execute() throws WCPSException {
        ArrayList<byte[]> results = new ArrayList<byte[]>();

        if (this.rasqlQuery != null) {
            Implementation impl = new RasImplementation(url);
            Database db = impl.newDatabase();

            try {
                db.open(database, Database.OPEN_READ_ONLY);
            } catch (ODMGException odmge) {
                try {
                    db.close();
                } catch (ODMGException e) {
                }

                throw new WCPSException(ExceptionCode.ResourceError, "Could not connect to rasdaman at "
                        + url + ", database "
                        + database, odmge);
            }

            Transaction tr = impl.newTransaction();

            tr.begin();
            OQLQuery q = impl.newOQLQuery();
            DBag resultSet;

            try {
                q.create(this.getRasqlQuery());
                resultSet = (DBag) q.execute();

                if (resultSet != null) {
                    Iterator resultIterator = resultSet.iterator();

                    while (resultIterator.hasNext()) {
                        Object current = resultIterator.next();

                        try {
                            RasGMArray resultArray =
                                    (RasGMArray) current;

                            results.add(resultArray.getArray());
                        } catch (ClassCastException e) {    // not a RasGMarray
                            if (!mime.equals("text/plain")) {
                                throw new WCPSException(ExceptionCode.ResourceError, 
                                        "Incompatible mime and data type!");
                            }

                            System.err.println("result="
                                    + current.toString());
                            results.add(current.toString().getBytes());

                        }


                        /*
                         *                   if (mime.equals("text/plain")) {
                         * System.err.println("dataType is :" + resultArray.getBaseTypeSchema().toString());
                         * }
                         */

                    }
                }
            } catch (QueryException qe) {
                tr.commit();

                try {
                    db.close();
                } catch (ODMGException odmge) {
                }

                throw new WCPSException(ExceptionCode.ResourceError, "Could not evaluate rasdaman query: '"
                        + getRasqlQuery() + "'. Cause: " + qe.getMessage(), qe);
            }

            tr.commit();

            try {
                db.close();
            } catch (ODMGException odmge) {
            }
        }

        if (mime.equals("text/plain")) {
        }

        return results;

    }
}