1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/***************************************************************************
* Copyright (c) 2004 - 2008 Eike Stepper, Germany, and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
* Martin Taal - changed handling of propertygetters/setters
**************************************************************************/
package org.eclipse.emf.cdo.server.internal.hibernate.tuplizer;
import org.eclipse.emf.cdo.internal.protocol.revision.InternalCDORevision;
import org.eclipse.emf.cdo.protocol.model.CDOClass;
import org.eclipse.emf.cdo.protocol.model.CDOFeature;
import org.eclipse.emf.cdo.protocol.model.CDOPackage;
import org.eclipse.emf.cdo.protocol.model.resource.CDOResourcePackage;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateStore;
import org.eclipse.emf.cdo.server.internal.hibernate.bundle.OM;
import org.eclipse.net4j.internal.util.om.trace.ContextTracer;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.property.Getter;
import org.hibernate.property.Setter;
import org.hibernate.proxy.ProxyFactory;
import org.hibernate.tuple.Instantiator;
import org.hibernate.tuple.entity.AbstractEntityTuplizer;
import org.hibernate.tuple.entity.EntityMetamodel;
/**
* @author Eike Stepper
*/
public class CDORevisionTuplizer extends AbstractEntityTuplizer
{
private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, CDORevisionTuplizer.class);
private CDOClass cdoClass;
public CDORevisionTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappingInfo)
{
super(entityMetamodel, mappingInfo);
TRACER.trace("Created CDORevisionTuplizer for entity " + mappingInfo.getEntityName());
initCDOClass(mappingInfo);
}
private void initCDOClass(PersistentClass mappingInfo)
{
if (cdoClass != null)
{
return;
}
final HibernateStore hbStore = HibernateStore.getCurrentHibernateStore();
// find the CDOClass/Package
// TODO: error handling if meta attribute not present
// TODO: error handling if entityname not set
final String entityName = mappingInfo.getEntityName();
final String ePackageURI = mappingInfo.getMetaAttribute("epackage").getValue();
TRACER.trace("EntityName/packageURI " + entityName + " " + ePackageURI);
for (CDOPackage cdoPackage : hbStore.getPackageHandler().getCDOPackages())
{
if (cdoPackage.getPackageURI().compareTo(ePackageURI) != 0)
{
continue;
}
for (CDOClass localCdoClass : cdoPackage.getClasses())
{
if (localCdoClass.getName().compareTo(entityName) == 0)
{
cdoClass = localCdoClass;
break;
}
}
}
if (cdoClass == null && ePackageURI.compareTo(CDOResourcePackage.PACKAGE_URI) == 0)
{
for (CDOClass localCdoClass : hbStore.getRepository().getPackageManager().getCDOResourcePackage().getClasses())
{
if (localCdoClass.getName().compareTo(entityName) == 0)
{
cdoClass = localCdoClass;
TRACER.trace("Class is CDOResource class");
break;
}
}
}
if (cdoClass == null)
{
throw new IllegalArgumentException("The mapped class " + mappingInfo.getEntityName());
}
}
// MT: probably not required as the property getter/setter do all the work
// /*
// * (non-Javadoc)
// *
// * @see org.hibernate.tuple.entity.AbstractEntityTuplizer#getVersion(java.lang.Object)
// */
// @Override
// public Object getVersion(Object entity) throws HibernateException
// {
// if (entity instanceof CDORevision)
// {
// final CDORevision cdoRevision = (CDORevision)entity;
// return cdoRevision.getVersion();
// }
// throw new IllegalArgumentException("Entity of type: " + entity.getClass().getName()
// + " not supported by this tuplizer");
// }
//
// @Override
// public Serializable getIdentifier(Object entity) throws HibernateException
// {
// if (entity instanceof CDORevision)
// {
// final CDOID cdoID = ((CDORevision)entity).getID();
// if (cdoID instanceof CDOIDHibernate)
// {
// return ((CDOIDHibernate)cdoID).getId();
// }
// }
// throw new IllegalArgumentException("Entity of type: " + entity.getClass().getName()
// + " not supported by this tuplizer");
// }
//
// @Override
// public void setIdentifier(Object entity, Serializable id) throws HibernateException
// {
// if (entity instanceof InternalCDORevision)
// {
// final InternalCDORevision cdoRevision = (InternalCDORevision)entity;
// final CDOID cdoID = cdoRevision.getID();
// if (cdoID == null)
// {
// CDOIDHibernate hid = (CDOIDHibernate)HibernateStore.CDOID_OBJECT_FACTORY.createCDOIDObject(null);
// hid.setId(id);
// cdoRevision.setID(hid);
// return;
// }
// else if (cdoID instanceof CDOIDHibernate)
// {
// ((CDOIDHibernate)cdoID).setId(id);
// return;
// }
// }
// throw new IllegalArgumentException("Entity of type: " + entity.getClass().getName()
// + " not supported by this tuplizer");
// }
@Override
public EntityMode getEntityMode()
{
return EntityMode.MAP;
}
public CDOClass getCDOClass()
{
return cdoClass;
}
@Override
protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity)
{
initCDOClass(mappedEntity);
TRACER.trace("Building property getter for " + cdoClass.getName() + "." + mappedProperty.getName());
if (mappedProperty.isBackRef())
{
return mappedProperty.getGetter(mappedEntity.getMappedClass());
}
else if (mappedProperty == mappedEntity.getIdentifierProperty())
{
return new CDOIDPropertyGetter(this, mappedProperty.getName());
}
else if (mappedProperty == mappedEntity.getVersion())
{
return new CDOVersionPropertyGetter(this, mappedProperty.getName());
}
else
{
final CDOFeature cdoFeature = getCDOClass().lookupFeature(mappedProperty.getName());
if (cdoFeature.isReference() && cdoFeature.isMany())
{
return new CDOManyReferenceGetter(this, mappedProperty.getName());
}
else if (cdoFeature.isReference())
{
return new CDOReferenceGetter(this, mappedProperty.getName());
}
else
{
return new CDOPropertyGetter(this, mappedProperty.getName());
}
}
}
@Override
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity)
{
initCDOClass(mappedEntity);
TRACER.trace("Building property setter for " + cdoClass.getName() + "." + mappedProperty.getName());
if (mappedProperty.isBackRef())
{
return mappedProperty.getSetter(mappedEntity.getMappedClass());
}
else if (mappedProperty == mappedEntity.getIdentifierProperty())
{
return new CDOIDPropertySetter(this, mappedProperty.getName());
}
else if (mappedProperty == mappedEntity.getVersion())
{
return new CDOVersionPropertySetter(this, mappedProperty.getName());
}
else
{
final CDOFeature cdoFeature = getCDOClass().lookupFeature(mappedProperty.getName());
if (cdoFeature.isReference() && cdoFeature.isMany())
{
return new CDOManyReferenceSetter(this, mappedProperty.getName());
}
else if (cdoFeature.isReference())
{
return new CDOReferenceSetter(this, mappedProperty.getName());
}
else
{
return new CDOPropertySetter(this, mappedProperty.getName());
}
}
}
@Override
protected Instantiator buildInstantiator(PersistentClass mappingInfo)
{
return new CDORevisionInstantiator(this, mappingInfo);
}
@Override
protected ProxyFactory buildProxyFactory(PersistentClass mappingInfo, Getter idGetter, Setter idSetter)
{
ProxyFactory pf = new CDORevisionProxyFactory();
try
{
pf.postInstantiate(getEntityName(), null, null, null, null, null);
}
catch (HibernateException ex)
{
OM.LOG.error("Could not create proxy factory for " + getEntityName(), ex);
pf = null;
}
return pf;
}
@SuppressWarnings("unchecked")
public Class getMappedClass()
{
return InternalCDORevision.class;
}
@SuppressWarnings("unchecked")
public Class getConcreteProxyClass()
{
return InternalCDORevision.class;
}
public boolean isInstrumented()
{
return false;
}
}
|