01: /**********************************************************************
02: Copyright (c) 2004 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: 2005 Andy Jefferson - cater for JDOHelper.getObjectId being used in result
18: ...
19: **********************************************************************/package org.jpox.store.expression;
20:
21: import org.jpox.api.ApiAdapter;
22: import org.jpox.store.DatastoreAdapter;
23: import org.jpox.store.mapping.JavaTypeMapping;
24: import org.jpox.store.mapping.PersistenceCapableMapping;
25:
26: /**
27: * Representation of JDOHelper in JDOQL
28: * @version $Revision: 1.10 $
29: */
30: public class JDOHelperExpression extends ScalarExpression {
31: /**
32: * Constructor.
33: * @param qs The query expression
34: */
35: public JDOHelperExpression(QueryExpression qs) {
36: super (qs);
37: }
38:
39: /**
40: * Returns an object expression for the argument.
41: * @param expr the expression
42: * @return the returned value for an object expression argument is the
43: * argument itself. The returned for a literal argument is a
44: * ObjectLiteral or NullLiteral
45: */
46: public ScalarExpression getObjectIdMethod(ScalarExpression expr) {
47: if (expr == null) {
48: return new NullLiteral(qs);
49: }
50: if (expr instanceof Literal) {
51: ApiAdapter api = qs.getStoreManager().getApiAdapter();
52: Object id = api.getIdForObject(((Literal) expr).getValue());
53: if (id == null) {
54: return new NullLiteral(qs);
55: } else {
56: DatastoreAdapter dba = qs.getStoreManager()
57: .getDatastoreAdapter();
58: JavaTypeMapping m = dba
59: .getMapping(id.getClass(),
60: qs.getStoreManager(), qs
61: .getClassLoaderResolver());
62: return new ObjectLiteral(qs, m, id);
63: }
64: } else if (ObjectExpression.class.isAssignableFrom(expr
65: .getClass())) {
66: if (((ObjectExpression) expr).getMapping() instanceof PersistenceCapableMapping) {
67: // JDOHelper.getObjectId only requires the identity, whereas a PCMapping by default will
68: // represent the object so we change it to be an identity representation.
69: ((ObjectExpression) expr).useIdentityFormOfPCMapping();
70: }
71: return expr;
72: }
73:
74: throw new IllegalOperationException(this , "getObjectId", expr);
75: }
76: }
|