From 608b8a2bd1a75dafd07c5dd81b5d5df7f0f653ef Mon Sep 17 00:00:00 2001 From: Martin Taal Date: Mon, 18 Nov 2013 00:08:43 +0100 Subject: [420464] - Add new functionality to Teneo to select teneo annotations --- .../org/eclipse/emf/teneo/PersistenceOptions.java | 19 ++++++++ .../parser/EAnnotationParserImporter.java | 23 ++++++++- .../annotations/HbEAnnotationParserImporter.java | 6 ++- .../emf/teneo/hibernate/HbEntityDataStore.java | 54 +++++++++++----------- .../emf/teneo/test/emf/sample/ClaimAction.java | 2 + 5 files changed, 74 insertions(+), 30 deletions(-) diff --git a/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/PersistenceOptions.java b/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/PersistenceOptions.java index 8c744657f..e4ad013ac 100755 --- a/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/PersistenceOptions.java +++ b/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/PersistenceOptions.java @@ -482,6 +482,16 @@ public class PersistenceOptions implements ExtensionPoint { */ public static final String EXTRA_ANNOTATION_SOURCES = MAPPING_PREFIX + "extra_annotation_sources"; + /** + * If set to true then the extra annotation source will override/replace the default annotation + * sources, so they are used if present, but if the extra annotation is not present on the model + * element then the default annotation is checked. + * + * Default is false for backward compatibility. + */ + public static final String EXTRA_ANNOTATIONS_OVERRIDES_DEFAULT = MAPPING_PREFIX + + "use_default_if_no_extra_annotation_sources"; + public final static String ECONTAINER_CLASS_COLUMN = "econtainer_class_column"; public final static String ECONTAINER_COLUMN = "e_container_column"; public final static String ECONTAINER_FEATURE_NAME_COLUMN = "e_container_feature_name_column"; @@ -563,6 +573,7 @@ public class PersistenceOptions implements ExtensionPoint { props.setProperty(ECONTAINER_FEATURE_NAME_COLUMN, Constants.COLUMN_ECONTAINER_FEATURE_NAME); props.setProperty(FEATUREMAP_AS_COMPONENT, "false"); props.setProperty(EXTRA_ANNOTATION_SOURCES, ""); + props.setProperty(EXTRA_ANNOTATIONS_OVERRIDES_DEFAULT, "false"); props.setProperty(AUTO_ADAPT_MANUAL_SET_SQL_NAMES, "true"); return props; @@ -580,6 +591,14 @@ public class PersistenceOptions implements ExtensionPoint { return Boolean.valueOf(properties.getProperty(ENABLE_AUDITING)).booleanValue(); } + /** + * @return value of {@link #EXTRA_ANNOTATIONS_OVERRIDES_DEFAULT} + */ + public boolean isExtraAnnotationsOverridesDefault() { + return Boolean.valueOf(properties.getProperty(EXTRA_ANNOTATIONS_OVERRIDES_DEFAULT)) + .booleanValue(); + } + /** * @return value of {@link #HIBERNATE_VERSION} */ diff --git a/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/annotations/parser/EAnnotationParserImporter.java b/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/annotations/parser/EAnnotationParserImporter.java index 5f522c488..6c96c1c56 100755 --- a/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/annotations/parser/EAnnotationParserImporter.java +++ b/core/org.eclipse.emf.teneo/src/org/eclipse/emf/teneo/annotations/parser/EAnnotationParserImporter.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EAnnotation; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.ENamedElement; @@ -58,6 +59,8 @@ public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint private String[] extraAnnotationsSources = new String[] {}; + private PersistenceOptions persistenceOptions = null; + /** Parse an pamodel */ public void process(PAnnotatedModel paModel) { for (PAnnotatedEPackage pap : paModel.getPaEPackages()) { @@ -165,10 +168,11 @@ public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint private ArrayList process(EAnnotation ea, ENamedElement ene) { final ArrayList result = new ArrayList(); - if (!isValidSource(ea.getSource())) { + if (!isValidSource(ea.getSource(), ene.getEAnnotations())) { return result; } + log.debug("Processing annotations "); for (Map.Entry pAnnotationDetails : ea.getDetails().entrySet()) { final String fName = pAnnotationDetails.getKey(); @@ -215,7 +219,7 @@ public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint } /** Is a valid source */ - protected boolean isValidSource(String source) { + protected boolean isValidSource(String source, EList eAnnotations) { if (source == null) { return false; } @@ -226,6 +230,18 @@ public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint return true; } } + + // check if there is an extra annotation which could have been used + // if so then return false, it should already have been caught in the above if + if (persistenceOptions != null && persistenceOptions.isExtraAnnotationsOverridesDefault()) { + for (EAnnotation eAnnotation : eAnnotations) { + for (String annotationSource : extraAnnotationsSources) { + if (eAnnotation.getSource().equals(annotationSource)) { + return false; + } + } + } + } } if (source.equals(Constants.ANNOTATION_SOURCE_TENEO_JPA_AUDITING)) { @@ -236,12 +252,15 @@ public class EAnnotationParserImporter implements EClassResolver, ExtensionPoint || source.startsWith(Constants.ANNOTATION_SOURCE_TENEO_MAPPING); } + + /** Find the efeature */ public EStructuralFeature getEStructuralFeature(EClass eClass, String name) { return ParserUtil.getEStructuralFeature(eClass, name); } public void setExtraAnnotationSources(PersistenceOptions po) { + persistenceOptions = po; if (po.getExtraAnnotationSources() != null && po.getExtraAnnotationSources().trim().length() > 0) { extraAnnotationsSources = po.getExtraAnnotationSources().split(","); diff --git a/hibernate/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/annotations/HbEAnnotationParserImporter.java b/hibernate/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/annotations/HbEAnnotationParserImporter.java index 0574d4343..93b7295cb 100755 --- a/hibernate/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/annotations/HbEAnnotationParserImporter.java +++ b/hibernate/org.eclipse.emf.teneo.hibernate.mapper/src/org/eclipse/emf/teneo/hibernate/annotations/HbEAnnotationParserImporter.java @@ -15,6 +15,8 @@ */ package org.eclipse.emf.teneo.hibernate.annotations; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EAnnotation; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.emf.teneo.annotations.parser.EAnnotationParserImporter; @@ -30,11 +32,11 @@ public class HbEAnnotationParserImporter extends EAnnotationParserImporter { /** Returns true if the source is a hibernate source or a generic source */ @Override - protected boolean isValidSource(String source) { + protected boolean isValidSource(String source, EList eAnnotations) { if (source == null) { return false; } - return source.startsWith("teneo.hibernate") || super.isValidSource(source); + return source.startsWith("teneo.hibernate") || super.isValidSource(source, eAnnotations); } /* diff --git a/hibernate/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbEntityDataStore.java b/hibernate/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbEntityDataStore.java index 67b686cac..114adf2df 100755 --- a/hibernate/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbEntityDataStore.java +++ b/hibernate/org.eclipse.emf.teneo.hibernate/src/org/eclipse/emf/teneo/hibernate/HbEntityDataStore.java @@ -23,13 +23,11 @@ import java.lang.reflect.Field; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import javax.persistence.Cache; -import javax.persistence.EntityGraph; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -479,9 +477,10 @@ public class HbEntityDataStore extends HbDataStore implements EntityManagerFacto return delegate.isOpen(); } - public void addNamedEntityGraph(String arg0, EntityGraph arg1) { - delegate.addNamedEntityGraph(arg0, arg1); - } + // JPA 2.1 + // public void addNamedEntityGraph(String arg0, EntityGraph arg1) { + // delegate.addNamedEntityGraph(arg0, arg1); + // } public void addNamedQuery(String arg0, Query arg1) { delegate.addNamedQuery(arg0, arg1); @@ -701,13 +700,14 @@ public class HbEntityDataStore extends HbDataStore implements EntityManagerFacto this.delegateEntityManager = delegateEntityManager; } - public EntityGraph createEntityGraph(Class arg0) { - return delegateEntityManager.createEntityGraph(arg0); - } - - public EntityGraph createEntityGraph(String arg0) { - return delegateEntityManager.createEntityGraph(arg0); - } + // JPA 2.1 + // public EntityGraph createEntityGraph(Class arg0) { + // return delegateEntityManager.createEntityGraph(arg0); + // } + // + // public EntityGraph createEntityGraph(String arg0) { + // return delegateEntityManager.createEntityGraph(arg0); + // } public StoredProcedureQuery createNamedStoredProcedureQuery(String arg0) { return delegateEntityManager.createNamedStoredProcedureQuery(arg0); @@ -734,13 +734,14 @@ public class HbEntityDataStore extends HbDataStore implements EntityManagerFacto return delegateEntityManager.createStoredProcedureQuery(arg0, arg1); } - public EntityGraph getEntityGraph(String arg0) { - return delegateEntityManager.getEntityGraph(arg0); - } - - public List> getEntityGraphs(Class arg0) { - return delegateEntityManager.getEntityGraphs(arg0); - } + // JPA 2.1 + // public EntityGraph getEntityGraph(String arg0) { + // return delegateEntityManager.getEntityGraph(arg0); + // } + // + // public List> getEntityGraphs(Class arg0) { + // return delegateEntityManager.getEntityGraphs(arg0); + // } public boolean isJoinedToTransaction() { return delegateEntityManager.isJoinedToTransaction(); @@ -823,13 +824,14 @@ public class HbEntityDataStore extends HbDataStore implements EntityManagerFacto } } - public void addNamedEntityGraph(String arg0, EntityGraph arg1) { - getEntityManagerFactory().addNamedEntityGraph(arg0, arg1); - } - - public void addNamedQuery(String arg0, Query arg1) { - getEntityManagerFactory().addNamedQuery(arg0, arg1); - } + // JPA 2.1 + // public void addNamedEntityGraph(String arg0, EntityGraph arg1) { + // getEntityManagerFactory().addNamedEntityGraph(arg0, arg1); + // } + // + // public void addNamedQuery(String arg0, Query arg1) { + // getEntityManagerFactory().addNamedQuery(arg0, arg1); + // } public EntityManager createEntityManager(SynchronizationType arg0) { return getEntityManagerFactory().createEntityManager(arg0); diff --git a/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/sample/ClaimAction.java b/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/sample/ClaimAction.java index 1dae89ca0..c1388f1d7 100755 --- a/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/sample/ClaimAction.java +++ b/tests/org.eclipse.emf.teneo.commontest/src/org/eclipse/emf/teneo/test/emf/sample/ClaimAction.java @@ -52,6 +52,8 @@ public class ClaimAction extends AbstractTestAction { final Properties props = new Properties(); props.setProperty(PersistenceOptions.EXTRA_ANNOTATION_SOURCES, "teneo.extra, teneo.extra.test, teneo.extra.test2"); + props.setProperty(PersistenceOptions.EXTRA_ANNOTATIONS_OVERRIDES_DEFAULT, + "true"); return props; } -- cgit v1.2.3