KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > access > util > QueryUtils


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.access.util;
57
58 import java.util.Iterator JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.ListIterator JavaDoc;
61
62 import org.objectstyle.cayenne.CayenneRuntimeException;
63 import org.objectstyle.cayenne.DataObject;
64 import org.objectstyle.cayenne.ObjectId;
65 import org.objectstyle.cayenne.access.QueryEngine;
66 import org.objectstyle.cayenne.exp.Expression;
67 import org.objectstyle.cayenne.exp.ExpressionFactory;
68 import org.objectstyle.cayenne.map.DbRelationship;
69 import org.objectstyle.cayenne.map.ObjEntity;
70 import org.objectstyle.cayenne.map.ObjRelationship;
71 import org.objectstyle.cayenne.query.SelectQuery;
72
73 /**
74  * Implements helper methods that perform different query-related operations. <i>May be
75  * deprecated in the future, after its functionality is moved to the places where it is
76  * used now. </i>
77  *
78  * @author Andrei Adamchik
79  */

80 public class QueryUtils {
81
82     /**
83      * Creates and returns a select query that can be used to fetch an object given an
84      * ObjectId.
85      */

86     public static SelectQuery selectObjectForId(ObjectId oid) {
87         return new SelectQuery(oid.getObjectClass(), ExpressionFactory.matchAllDbExp(oid
88                 .getIdSnapshot(), Expression.EQUAL_TO));
89     }
90
91     /**
92      * Creates and returns a select query that can be used to fetch a list of objects
93      * given a list of ObjectIds. All ObjectIds must belong to the same entity.
94      */

95     public static SelectQuery selectQueryForIds(List JavaDoc oids) {
96         if (oids == null || oids.size() == 0) {
97             throw new IllegalArgumentException JavaDoc("List must contain at least one ObjectId");
98         }
99
100         SelectQuery sel = new SelectQuery();
101         sel.setRoot(((ObjectId) oids.get(0)).getObjectClass());
102
103         Iterator JavaDoc it = oids.iterator();
104
105         ObjectId firstId = (ObjectId) it.next();
106         Expression exp = ExpressionFactory.matchAllDbExp(firstId.getIdSnapshot(),
107                 Expression.EQUAL_TO);
108
109         while (it.hasNext()) {
110             ObjectId anId = (ObjectId) it.next();
111             exp = exp.orExp(ExpressionFactory.matchAllDbExp(anId.getIdSnapshot(),
112                     Expression.EQUAL_TO));
113         }
114
115         sel.setQualifier(exp);
116         return sel;
117     }
118
119     /**
120      * Generates a SelectQuery that can be used to fetch relationship destination objects
121      * given a source object of a to-many relationship.
122      *
123      * @deprecated since 1.2 Use RelationshipQuery.
124      */

125     public static SelectQuery selectRelationshipObjects(
126             QueryEngine e,
127             DataObject source,
128             String JavaDoc relName) {
129
130         ObjEntity ent = e.getEntityResolver().lookupObjEntity(source);
131         ObjRelationship rel = (ObjRelationship) ent.getRelationship(relName);
132         ObjEntity destEnt = (ObjEntity) rel.getTargetEntity();
133
134         List JavaDoc dbRels = rel.getDbRelationships();
135
136         // sanity check
137
if (dbRels == null || dbRels.size() == 0) {
138             throw new CayenneRuntimeException("ObjRelationship '"
139                     + rel.getName()
140                     + "' is unmapped.");
141         }
142
143         // build a reverse DB path
144
// ...while reverse ObjRelationship may be absent,
145
// reverse DB must always be there...
146
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147         ListIterator JavaDoc it = dbRels.listIterator(dbRels.size());
148         while (it.hasPrevious()) {
149             if (buf.length() > 0) {
150                 buf.append(".");
151             }
152             DbRelationship dbRel = (DbRelationship) it.previous();
153             DbRelationship reverse = dbRel.getReverseRelationship();
154
155             // another sanity check
156
if (reverse == null) {
157                 throw new CayenneRuntimeException("DbRelationship '"
158                         + dbRel.getName()
159                         + "' has no reverse relationship");
160             }
161
162             buf.append(reverse.getName());
163         }
164
165         SelectQuery select = new SelectQuery(destEnt);
166         select.setQualifier(ExpressionFactory.matchDbExp(buf.toString(), source));
167         // resolve inheritance
168
select.setResolvingInherited(true);
169         return select;
170     }
171 }
Popular Tags