1 /*
2 * ModeShape (http://www.modeshape.org)
3 * See the COPYRIGHT.txt file distributed with this work for information
4 * regarding copyright ownership. Some portions may be licensed
5 * to Red Hat, Inc. under one or more contributor license agreements.
6 * See the AUTHORS.txt file in the distribution for a full listing of
7 * individual contributors.
8 *
9 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10 * is licensed to you under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * ModeShape is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this software; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23 */
24 package org.modeshape.jcr.query.qom;
25
26 import javax.jcr.ItemNotFoundException;
27 import javax.jcr.Node;
28 import javax.jcr.PathNotFoundException;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.nodetype.ConstraintViolationException;
31 import javax.jcr.query.Query;
32 import org.modeshape.common.collection.Problem;
33 import org.modeshape.common.collection.Problems;
34 import org.modeshape.common.collection.Problem.Status;
35 import org.modeshape.graph.property.Path;
36 import org.modeshape.graph.query.model.QueryCommand;
37 import org.modeshape.graph.query.parse.QueryParser;
38 import org.modeshape.jcr.JcrI18n;
39 import org.modeshape.jcr.JcrNtLexicon;
40 import org.modeshape.jcr.query.JcrQueryContext;
41
42 /**
43 * Abstract implementation of JCR's {@link Query} interface.
44 */
45 public abstract class JcrAbstractQuery implements Query {
46
47 protected final JcrQueryContext context;
48 protected final String language;
49 protected final String statement;
50 private Path storedAtPath;
51
52 /**
53 * Creates a new JCR {@link Query} by specifying the query statement itself, the language in which the query is stated, the
54 * {@link QueryCommand} representation and, optionally, the node from which the query was loaded. The language must be a
55 * string from among those returned by {@code QueryManager#getSupportedQueryLanguages()}.
56 *
57 * @param context the context that was used to create this query and that will be used to execute this query; may not be null
58 * @param statement the original statement as supplied by the client; may not be null
59 * @param language the language obtained from the {@link QueryParser}; may not be null
60 * @param storedAtPath the path at which this query was stored, or null if this is not a stored query
61 */
62 protected JcrAbstractQuery( JcrQueryContext context,
63 String statement,
64 String language,
65 Path storedAtPath ) {
66 assert context != null;
67 assert statement != null;
68 assert language != null;
69 this.context = context;
70 this.language = language;
71 this.statement = statement;
72 this.storedAtPath = storedAtPath;
73 }
74
75 protected final JcrQueryContext context() {
76 return this.context;
77 }
78
79 protected final Path pathFor( String path ) {
80 return this.context.getExecutionContext().getValueFactories().getPathFactory().create(path);
81 }
82
83 /**
84 * {@inheritDoc}
85 *
86 * @see javax.jcr.query.Query#getLanguage()
87 */
88 public String getLanguage() {
89 return language;
90 }
91
92 /**
93 * {@inheritDoc}
94 *
95 * @see javax.jcr.query.Query#getStatement()
96 */
97 public String getStatement() {
98 return statement;
99 }
100
101 /**
102 * {@inheritDoc}
103 *
104 * @see javax.jcr.query.Query#getStoredQueryPath()
105 */
106 public String getStoredQueryPath() throws ItemNotFoundException {
107 if (storedAtPath == null) {
108 throw new ItemNotFoundException(JcrI18n.notStoredQuery.text());
109 }
110 return storedAtPath.getString(context.getExecutionContext().getNamespaceRegistry());
111 }
112
113 /**
114 * {@inheritDoc}
115 *
116 * @see javax.jcr.query.Query#storeAsNode(java.lang.String)
117 */
118 public Node storeAsNode( String absPath ) throws PathNotFoundException, ConstraintViolationException, RepositoryException {
119 context.isLive();
120 Node queryNode = context.store(absPath, JcrNtLexicon.QUERY, this.language, this.statement);
121 this.storedAtPath = pathFor(queryNode.getPath());
122 return queryNode;
123 }
124
125 protected void checkForProblems( Problems problems ) throws RepositoryException {
126 if (problems.hasErrors()) {
127 // Build a message with the problems ...
128 StringBuilder msg = new StringBuilder();
129 for (Problem problem : problems) {
130 if (problem.getStatus() != Status.ERROR) continue;
131 msg.append(problem.getMessageString()).append("\n");
132 }
133 throw new RepositoryException(msg.toString());
134 }
135 }
136 }