Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QueryByIdentity |
|
| 1.6666666666666667;1.667 |
1 | package org.apache.ojb.broker.query; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * 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.1 2007-08-24 22:17:36 ewestfal Exp $ | |
59 | */ | |
60 | public class QueryByIdentity extends AbstractQueryImpl | |
61 | { | |
62 | private Object m_exampleObjectOrIdentity; | |
63 | ||
64 | /** | |
65 | * QueryByIdentity can be generated from example Objects or by Identity Objects | |
66 | */ | |
67 | public QueryByIdentity(Object example_or_identity) | |
68 | { | |
69 | m_exampleObjectOrIdentity = example_or_identity; | |
70 | } | |
71 | ||
72 | /** | |
73 | * Answer the example Object | |
74 | * @return the example Object or an Identity | |
75 | */ | |
76 | public Object getExampleObject() | |
77 | { | |
78 | return m_exampleObjectOrIdentity; | |
79 | } | |
80 | ||
81 | /** | |
82 | * Answer the search class. | |
83 | * This is the class of the example object or | |
84 | * the class represented by Identity. | |
85 | * @return Class | |
86 | */ | |
87 | public Class getSearchClass() | |
88 | { | |
89 | Object obj = getExampleObject(); | |
90 | ||
91 | if (obj instanceof Identity) | |
92 | { | |
93 | return ((Identity) obj).getObjectsTopLevelClass(); | |
94 | } | |
95 | else | |
96 | { | |
97 | return obj.getClass(); | |
98 | } | |
99 | } | |
100 | ||
101 | } |