01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.gps.device.jpa.queryprovider;
18:
19: import javax.persistence.EntityManager;
20: import javax.persistence.Query;
21:
22: import org.compass.gps.device.jpa.entities.EntityInformation;
23:
24: /**
25: * A simple Jpa query provider based on a select statement. The select
26: * statement can be automatically generated based on the entity name
27: * as well.
28: *
29: * @author kimchy
30: */
31: public class DefaultJpaQueryProvider implements JpaQueryProvider {
32:
33: private String selectQuery;
34:
35: private boolean isUsingDefaultSelectQuery;
36:
37: /**
38: * Creates a new query provider based on the entity name. The select
39: * statement is <code>select x from entityName x</code>.
40: *
41: * @param entityClass The entity class
42: * @param entityName The entity name
43: */
44: public DefaultJpaQueryProvider(Class<?> entityClass,
45: String entityName) {
46: this .selectQuery = "select x from " + entityName + " x";
47: this .isUsingDefaultSelectQuery = true;
48: }
49:
50: /**
51: * Creates a new query provider based on the provided select statement.
52: *
53: * @param selectQuery The select query
54: */
55: public DefaultJpaQueryProvider(String selectQuery) {
56: this .selectQuery = selectQuery;
57: }
58:
59: /**
60: * Creates a query based on the select statement initlaized in the query provider
61: * construction.
62: */
63: public Query createQuery(EntityManager entityManager,
64: EntityInformation entityInformation) {
65: if (selectQuery != null) {
66: return entityManager.createQuery(selectQuery);
67: }
68: return entityManager.createQuery("select x from "
69: + entityInformation.getName() + " x");
70: }
71:
72: protected boolean isUsingDefaultSelectQuery() {
73: return this .isUsingDefaultSelectQuery;
74: }
75:
76: public String toString() {
77: return "QueryProvider[" + selectQuery + "]";
78: }
79: }
|