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.hibernate;
18:
19: import org.compass.gps.device.hibernate.entities.EntityInformation;
20: import org.hibernate.Criteria;
21: import org.hibernate.Query;
22: import org.hibernate.Session;
23:
24: /**
25: * A simple Hibernate 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 DefaultHibernateQueryProvider implements
32: HibernateQueryProvider {
33:
34: private String selectQuery;
35:
36: private boolean isUsingDefaultSelectQuery;
37:
38: /**
39: * Creates a new query provider based on the entity name. The select
40: * statement is <code>from entityName</code>.
41: *
42: * @param entityClass The entity class
43: * @param entityName The entity name
44: */
45: public DefaultHibernateQueryProvider(Class entityClass,
46: String entityName) {
47: this .selectQuery = "from " + entityName;
48: this .isUsingDefaultSelectQuery = true;
49: }
50:
51: /**
52: * Creates a new query provider based on the provided select statement.
53: *
54: * @param selectQuery The select query
55: */
56: public DefaultHibernateQueryProvider(String selectQuery) {
57: this .selectQuery = selectQuery;
58: }
59:
60: /**
61: * Creates a query based on the select statement initlaized in the query provider
62: * construction.
63: */
64: public Query createQuery(Session session,
65: EntityInformation entityInformation) {
66: if (selectQuery != null) {
67: return session.createQuery(selectQuery);
68: }
69: return session.createQuery("from "
70: + entityInformation.getName());
71: }
72:
73: /**
74: * Creates the Hibernate criteria for the given entity information. Returns
75: * <code>null</code> if the select query is set.
76: */
77: public Criteria createCriteria(Session session,
78: EntityInformation entityInformation) {
79: if (!isUsingDefaultSelectQuery) {
80: return null;
81: }
82: Class type = entityInformation.getEntityClass();
83: return session.createCriteria(type);
84: }
85:
86: public String toString() {
87: return "QueryProvider[" + selectQuery + "]";
88: }
89: }
|