01: package org.apache.ojb.broker.query;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import org.apache.ojb.broker.Identity;
19:
20: /**
21: * Represents a search by identity.
22: * <i>"find the article with id 7"</i>
23: * could be represented as:<br/>
24: * <br/>
25: *
26: * <code>
27: * Article example = new Article();<br/>
28: * example.setId(7);<br/>
29: * Query qry = new QueryByIdentity(example);<br/>
30: * </code>
31: * <br/>
32: * The PersistenceBroker can retrieve Objects by examples as follows:<br/>
33: * <br/>
34: * <code>
35: * PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker();<br/>
36: * Collection col = broker.getObjectByQuery(qry);<br/>
37: * </code>
38: * <br/>
39: * This Class can also handle working with OJB Identity objects:
40: * <i>"find the article with Identity xyz"</i> could be represnted as<br/>
41: * <br/>
42: * <code>
43: * Article example = new Article();<br/>
44: * example.setId(7);<br/>
45: * Identity xyz = broker.serviceIdentity().buildIdentity(example);<br/>
46: * Query qry = new QueryByIdentity(xyz);<br/>
47: * Collection col = broker.getObjectByQuery(qry);<br/>
48: * </code>
49: * <br/>
50: * But in this case a smarter solution will be<br/>
51: * <br/>
52: * <code>
53: * Identity xyz = broker.serviceIdentity().buildIdentity(Article.class, new Integer(7));<br/>
54: * Collection col = broker.getObjectByIdentity(xyz);<br/>
55: * </code>
56: *
57: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
58: * @version $Id: QueryByIdentity.java,v 1.9.2.3 2005/12/21 22:27:09 tomdz Exp $
59: */
60: public class QueryByIdentity extends AbstractQueryImpl {
61: private Object m_exampleObjectOrIdentity;
62:
63: /**
64: * QueryByIdentity can be generated from example Objects or by Identity Objects
65: */
66: public QueryByIdentity(Object example_or_identity) {
67: m_exampleObjectOrIdentity = example_or_identity;
68: }
69:
70: /**
71: * Answer the example Object
72: * @return the example Object or an Identity
73: */
74: public Object getExampleObject() {
75: return m_exampleObjectOrIdentity;
76: }
77:
78: /**
79: * Answer the search class.
80: * This is the class of the example object or
81: * the class represented by Identity.
82: * @return Class
83: */
84: public Class getSearchClass() {
85: Object obj = getExampleObject();
86:
87: if (obj instanceof Identity) {
88: return ((Identity) obj).getObjectsTopLevelClass();
89: } else {
90: return obj.getClass();
91: }
92: }
93:
94: }
|