001 package org.apache.ojb.broker.query;
002
003 /* Copyright 2002-2005 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 import org.apache.ojb.broker.Identity;
019
020 /**
021 * Represents a search by identity.
022 * <i>"find the article with id 7"</i>
023 * could be represented as:<br/>
024 * <br/>
025 *
026 * <code>
027 * Article example = new Article();<br/>
028 * example.setId(7);<br/>
029 * Query qry = new QueryByIdentity(example);<br/>
030 * </code>
031 * <br/>
032 * The PersistenceBroker can retrieve Objects by examples as follows:<br/>
033 * <br/>
034 * <code>
035 * PersistenceBroker broker = PersistenceBrokerFactory.createPersistenceBroker();<br/>
036 * Collection col = broker.getObjectByQuery(qry);<br/>
037 * </code>
038 * <br/>
039 * This Class can also handle working with OJB Identity objects:
040 * <i>"find the article with Identity xyz"</i> could be represnted as<br/>
041 * <br/>
042 * <code>
043 * Article example = new Article();<br/>
044 * example.setId(7);<br/>
045 * Identity xyz = broker.serviceIdentity().buildIdentity(example);<br/>
046 * Query qry = new QueryByIdentity(xyz);<br/>
047 * Collection col = broker.getObjectByQuery(qry);<br/>
048 * </code>
049 * <br/>
050 * But in this case a smarter solution will be<br/>
051 * <br/>
052 * <code>
053 * Identity xyz = broker.serviceIdentity().buildIdentity(Article.class, new Integer(7));<br/>
054 * Collection col = broker.getObjectByIdentity(xyz);<br/>
055 * </code>
056 *
057 * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
058 * @version $Id: QueryByIdentity.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $
059 */
060 public class QueryByIdentity extends AbstractQueryImpl
061 {
062 private Object m_exampleObjectOrIdentity;
063
064 /**
065 * QueryByIdentity can be generated from example Objects or by Identity Objects
066 */
067 public QueryByIdentity(Object example_or_identity)
068 {
069 m_exampleObjectOrIdentity = example_or_identity;
070 }
071
072 /**
073 * Answer the example Object
074 * @return the example Object or an Identity
075 */
076 public Object getExampleObject()
077 {
078 return m_exampleObjectOrIdentity;
079 }
080
081 /**
082 * Answer the search class.
083 * This is the class of the example object or
084 * the class represented by Identity.
085 * @return Class
086 */
087 public Class getSearchClass()
088 {
089 Object obj = getExampleObject();
090
091 if (obj instanceof Identity)
092 {
093 return ((Identity) obj).getObjectsTopLevelClass();
094 }
095 else
096 {
097 return obj.getClass();
098 }
099 }
100
101 }