Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 84d5c23526d3b23a808422c9eb3552766e4f19a9 (plain) (blame)
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
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, 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:
 *    Martin Taal - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.hibernate.internal.teneo;

import org.eclipse.emf.cdo.common.model.CDOModelUtil;
import org.eclipse.emf.cdo.eresource.EresourcePackage;
import org.eclipse.emf.cdo.etypes.EtypesPackage;
import org.eclipse.emf.cdo.server.hibernate.internal.teneo.bundle.OM;
import org.eclipse.emf.cdo.server.hibernate.teneo.CDOMappingGenerator;
import org.eclipse.emf.cdo.server.internal.hibernate.CDOHibernateConstants;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateMappingProvider;
import org.eclipse.emf.cdo.server.internal.hibernate.HibernateStore;
import org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.CDOBlobUserType;
import org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.CDOClobUserType;

import org.eclipse.net4j.util.om.trace.ContextTracer;

import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.teneo.Constants;
import org.eclipse.emf.teneo.PackageRegistryProvider;
import org.eclipse.emf.teneo.PersistenceOptions;

import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;

/**
 * Uses the ecore string in the ePackages of the store to generate a mapping.
 * 
 * @author Martin Taal
 * @author Eike Stepper
 * @since 3.0
 */
public class TeneoHibernateMappingProvider extends HibernateMappingProvider
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, TeneoHibernateMappingProvider.class);

  private Map<String, String> extensions = new HashMap<String, String>();

  private Properties mappingProviderProperties = new Properties();

  public TeneoHibernateMappingProvider()
  {
  }

  public void putExtension(String extensionClassName, String extendingClassName)
  {
    extensions.put(extensionClassName, extendingClassName);
  }

  @Override
  public HibernateStore getHibernateStore()
  {
    return (HibernateStore)super.getHibernateStore();
  }

  public String getMapping()
  {
    final String mapping = generateMapping();
    if (TRACER.isEnabled())
    {
      TRACER.trace("Generated hibernate mapping:"); //$NON-NLS-1$
      TRACER.trace(mapping);
    }

    return mapping;
  }

  // the passed modelObjects collection is defined as a collection of Objects
  // to prevent binary dependency on emf.
  public String generateMapping()
  {
    if (TRACER.isEnabled())
    {
      TRACER.trace("Generating Hibernate Mapping"); //$NON-NLS-1$
    }

    final Properties storeProperties = getHibernateStore().getProperties();

    // merge the store properties with the mapping provider properties
    // the mapping provider props take precedence
    final Properties properties = new Properties();

    properties.putAll(storeProperties);
    properties.putAll(mappingProviderProperties);

    PackageRegistryProvider.getInstance().setThreadPackageRegistry(
        getHibernateStore().getRepository().getPackageRegistry());

    // translate the list of EPackages to an array
    boolean hasXMLTypePackage = false;
    final List<EPackage> epacks = getHibernateStore().getPackageHandler().getEPackages();
    final ListIterator<EPackage> iterator = epacks.listIterator();
    while (iterator.hasNext())
    {
      final EPackage epack = iterator.next();
      if (CDOModelUtil.isSystemPackage(epack) && epack != EtypesPackage.eINSTANCE)
      {
        iterator.remove();
      }
      else if (epack == XMLTypePackage.eINSTANCE)
      {
        hasXMLTypePackage = true;
        // iterator.remove();
      }
    }

    if (hasXMLTypePackage)
    {
      addTypeAnnotationToXMLTypes();
    }

    addUniqueConstraintAnnotation();

    final EPackage[] ePackageArray = epacks.toArray(new EPackage[epacks.size()]);
    // remove the persistence xml if no epackages as this won't work without
    // epackages
    if (ePackageArray.length == 0 && properties.getProperty(PersistenceOptions.PERSISTENCE_XML) != null)
    {
      properties.remove(PersistenceOptions.PERSISTENCE_XML);
    }

    // add some annotations to the CDO model so that the mapping gets generated correctly
    addTransientAnnotationToEClass(EtypesPackage.eINSTANCE.getModelElement());
    addTransientAnnotationToEClass(EtypesPackage.eINSTANCE.getAnnotation());
    addTypeAnnotationToEDataType(EtypesPackage.eINSTANCE.getBlob(), CDOBlobUserType.class.getName());
    addTypeAnnotationToEDataType(EtypesPackage.eINSTANCE.getClob(), CDOClobUserType.class.getName());

    final CDOMappingGenerator mappingGenerator = new CDOMappingGenerator();
    mappingGenerator.getExtensions().putAll(extensions);
    String hbm = mappingGenerator.generateMapping(ePackageArray, properties);
    // to solve an issue with older versions of teneo
    hbm = hbm.replaceAll("_cont", "cont"); //$NON-NLS-1$ //$NON-NLS-2$

    // System.err.println(hbm);

    return hbm;
  }

  private void addTypeAnnotationToXMLTypes()
  {
    for (EClassifier eClassifier : XMLTypePackage.eINSTANCE.getEClassifiers())
    {
      if (eClassifier instanceof EDataType)
      {
        final EDataType eDataType = (EDataType)eClassifier;
        if (eDataType.getEAnnotation(Constants.ANNOTATION_SOURCE_TENEO_JPA) != null)
        {
          continue;
        }

        final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
        eAnnotation.setSource(Constants.ANNOTATION_SOURCE_TENEO_JPA);
        final String typeAnnotation = "@Type(type=\"org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.XMLUserType$"
            + eDataType.getName() + "\")";
        eAnnotation.getDetails().put("value", typeAnnotation);
        eDataType.getEAnnotations().add(eAnnotation);
      }
    }
  }

  private void addTypeAnnotationToEDataType(EDataType eDataType, String type)
  {
    if (eDataType.getEAnnotation(Constants.ANNOTATION_SOURCE_TENEO_JPA) != null)
    {
      return;
    }

    final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
    eAnnotation.setSource(Constants.ANNOTATION_SOURCE_TENEO_JPA);
    final String typeAnnotation = "@Type(type=\"" + type + "\")";
    eAnnotation.getDetails().put("value", typeAnnotation);
    eDataType.getEAnnotations().add(eAnnotation);
  }

  private void addTransientAnnotationToEClass(EClass eClass)
  {
    if (eClass.getEAnnotation(Constants.ANNOTATION_SOURCE_TENEO_JPA) != null)
    {
      return;
    }

    final EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
    eAnnotation.setSource(Constants.ANNOTATION_SOURCE_TENEO_JPA);
    final String transientAnnotation = "@Transient";
    eAnnotation.getDetails().put("value", transientAnnotation);
    eClass.getEAnnotations().add(eAnnotation);
  }

  // see the CDOEntityMapper, there an explicit unique-key is added to
  // a column also
  private void addUniqueConstraintAnnotation()
  {
    final EClass eClass = EresourcePackage.eINSTANCE.getCDOResourceNode();
    // already been here
    if (eClass.getEAnnotation("teneo.jpa") != null) //$NON-NLS-1$
    {
      return;
    }

    final EAnnotation annotation = EcoreFactory.eINSTANCE.createEAnnotation();
    annotation.setSource("teneo.jpa"); //$NON-NLS-1$
    final String tableAnnotation = "@Table(uniqueConstraints={@UniqueConstraint(columnNames={\"" //$NON-NLS-1$
        + CDOHibernateConstants.CONTAINER_PROPERTY_COLUMN + "\", \"" //$NON-NLS-1$
        + EresourcePackage.eINSTANCE.getCDOResourceNode_Name().getName() + "\"})})"; //$NON-NLS-1$
    annotation.getDetails().put("value", tableAnnotation); //$NON-NLS-1$
    eClass.getEAnnotations().add(annotation);
  }

  public Properties getMappingProviderProperties()
  {
    return mappingProviderProperties;
  }

  public void setMappingProviderProperties(Properties mappingProviderProperties)
  {
    this.mappingProviderProperties = mappingProviderProperties;
  }
}

Back to the top