Author: adamw
Date: 2008-08-14 07:12:23 -0400 (Thu, 14 Aug 2008)
New Revision: 103
Added:
trunk/src/main/org/jboss/envers/configuration/metadata/
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java
trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/
Removed:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java
trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionType.java
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/
trunk/src/main/org/jboss/envers/metadata/
trunk/src/main/org/jboss/envers/tools/Triple.java
Modified:
trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java
trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/PersistentClassVersioningData.java
trunk/src/main/org/jboss/envers/configuration/metadata/data/PropertyStoreInfo.java
Log:
ENVERS-37: moving metadata to configuration
Modified: trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java 2008-08-14
10:19:45 UTC (rev 102)
+++ trunk/src/main/org/jboss/envers/configuration/EntityConfiguration.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -21,7 +21,7 @@
*/
package org.jboss.envers.configuration;
-import org.jboss.envers.metadata.data.IdMappingData;
+import org.jboss.envers.configuration.metadata.data.IdMappingData;
import org.jboss.envers.mapper.ExtendedPropertyMapper;
import org.jboss.envers.mapper.id.IdMapper;
Modified: trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-08-14
10:19:45 UTC (rev 102)
+++ trunk/src/main/org/jboss/envers/configuration/VersionsConfiguration.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -24,10 +24,10 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.MappingException;
-import org.jboss.envers.metadata.VersionsMetadataGenerator;
-import org.jboss.envers.metadata.RevisionsInfoMetadataGenerator;
-import org.jboss.envers.metadata.AnnotationsMetadataReader;
-import org.jboss.envers.metadata.data.PersistentClassVersioningData;
+import org.jboss.envers.configuration.metadata.VersionsMetadataGenerator;
+import org.jboss.envers.configuration.metadata.RevisionsInfoMetadataGenerator;
+import org.jboss.envers.configuration.metadata.AnnotationsMetadataReader;
+import org.jboss.envers.configuration.metadata.data.PersistentClassVersioningData;
import org.jboss.envers.synchronization.VersionsSyncManager;
import org.jboss.envers.mapper.ExtendedPropertyMapper;
import org.jboss.envers.mapper.id.IdMapper;
Copied: trunk/src/main/org/jboss/envers/configuration/metadata (from rev 95,
trunk/src/main/org/jboss/envers/metadata)
Deleted:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/AnnotationsMetadataReader.java 2008-07-16
09:40:37 UTC (rev 95)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,105 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-import org.jboss.envers.reflection.YClass;
-import org.jboss.envers.reflection.YProperty;
-import org.jboss.envers.reflection.YReflectionManager;
-import org.jboss.envers.metadata.data.PersistentClassVersioningData;
-import org.jboss.envers.Versioned;
-import org.jboss.envers.VersionsTable;
-import org.jboss.envers.SecondaryVersionsTable;
-import org.jboss.envers.SecondaryVersionsTables;
-import org.hibernate.mapping.PersistentClass;
-import org.hibernate.MappingException;
-
-/**
- * A helper class to read versioning meta-data from annotations on persistent classes.
- * @author Adam Warski (adam at warski dot org)
- */
-public class AnnotationsMetadataReader {
- private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData
versioningData) {
- YClass superclazz = clazz.getSuperclass();
- if (!"java.lang.Object".equals(superclazz.getName())) {
- addPropertiesFromClass(superclazz, versioningData);
- }
-
- for (YProperty property : clazz.getDeclaredProperties("field")) {
- Versioned ver = property.getAnnotation(Versioned.class);
- if (ver != null) {
- versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
- }
- }
-
- for (YProperty property : clazz.getDeclaredProperties("property")) {
- Versioned ver = property.getAnnotation(Versioned.class);
- if (ver != null) {
- versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
- }
- }
- }
-
- public PersistentClassVersioningData getVersioningData(PersistentClass pc,
YReflectionManager reflectionManager) {
- if (pc.getClassName() == null) {
- return null;
- }
-
- PersistentClassVersioningData versioningData = new
PersistentClassVersioningData();
-
- try {
- YClass clazz = reflectionManager.classForName(pc.getClassName(),
this.getClass());
- Versioned defaultVersioned = clazz.getAnnotation(Versioned.class);
-
- if (defaultVersioned != null) {
- versioningData.propertyStoreInfo.defaultStore =
defaultVersioned.modStore();
- }
-
- addPropertiesFromClass(clazz, versioningData);
-
- VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class);
- if (versionsTable != null) {
- versioningData.versionsTableName = versionsTable.value();
- versioningData.schema = versionsTable.schema();
- versioningData.catalog = versionsTable.catalog();
- }
-
- // Getting information on secondary tables
- SecondaryVersionsTable secondaryVersionsTable1 =
clazz.getAnnotation(SecondaryVersionsTable.class);
- if (secondaryVersionsTable1 != null) {
-
versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(),
- secondaryVersionsTable1.secondaryVersionsTableName());
- }
-
- SecondaryVersionsTables secondaryVersionsTables =
clazz.getAnnotation(SecondaryVersionsTables.class);
- if (secondaryVersionsTables != null) {
- for (SecondaryVersionsTable secondaryVersionsTable2 :
secondaryVersionsTables.value()) {
-
versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(),
- secondaryVersionsTable2.secondaryVersionsTableName());
- }
- }
- } catch (ClassNotFoundException e) {
- throw new MappingException(e);
- }
-
- return versioningData;
- }
-}
Copied:
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
(from rev 102, trunk/src/main/org/jboss/envers/metadata/AnnotationsMetadataReader.java)
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/AnnotationsMetadataReader.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -0,0 +1,105 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.jboss.envers.tools.reflection.YClass;
+import org.jboss.envers.tools.reflection.YProperty;
+import org.jboss.envers.tools.reflection.YReflectionManager;
+import org.jboss.envers.configuration.metadata.data.PersistentClassVersioningData;
+import org.jboss.envers.Versioned;
+import org.jboss.envers.VersionsTable;
+import org.jboss.envers.SecondaryVersionsTable;
+import org.jboss.envers.SecondaryVersionsTables;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.MappingException;
+
+/**
+ * A helper class to read versioning meta-data from annotations on persistent classes.
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class AnnotationsMetadataReader {
+ private void addPropertiesFromClass(YClass clazz, PersistentClassVersioningData
versioningData) {
+ YClass superclazz = clazz.getSuperclass();
+ if (!"java.lang.Object".equals(superclazz.getName())) {
+ addPropertiesFromClass(superclazz, versioningData);
+ }
+
+ for (YProperty property : clazz.getDeclaredProperties("field")) {
+ Versioned ver = property.getAnnotation(Versioned.class);
+ if (ver != null) {
+ versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
+ }
+ }
+
+ for (YProperty property : clazz.getDeclaredProperties("property")) {
+ Versioned ver = property.getAnnotation(Versioned.class);
+ if (ver != null) {
+ versioningData.propertyStoreInfo.propertyStores.put(property.getName(),
ver.modStore());
+ }
+ }
+ }
+
+ public PersistentClassVersioningData getVersioningData(PersistentClass pc,
YReflectionManager reflectionManager) {
+ if (pc.getClassName() == null) {
+ return null;
+ }
+
+ PersistentClassVersioningData versioningData = new
PersistentClassVersioningData();
+
+ try {
+ YClass clazz = reflectionManager.classForName(pc.getClassName(),
this.getClass());
+ Versioned defaultVersioned = clazz.getAnnotation(Versioned.class);
+
+ if (defaultVersioned != null) {
+ versioningData.propertyStoreInfo.defaultStore =
defaultVersioned.modStore();
+ }
+
+ addPropertiesFromClass(clazz, versioningData);
+
+ VersionsTable versionsTable = clazz.getAnnotation(VersionsTable.class);
+ if (versionsTable != null) {
+ versioningData.versionsTableName = versionsTable.value();
+ versioningData.schema = versionsTable.schema();
+ versioningData.catalog = versionsTable.catalog();
+ }
+
+ // Getting information on secondary tables
+ SecondaryVersionsTable secondaryVersionsTable1 =
clazz.getAnnotation(SecondaryVersionsTable.class);
+ if (secondaryVersionsTable1 != null) {
+
versioningData.secondaryTableDictionary.put(secondaryVersionsTable1.secondaryTableName(),
+ secondaryVersionsTable1.secondaryVersionsTableName());
+ }
+
+ SecondaryVersionsTables secondaryVersionsTables =
clazz.getAnnotation(SecondaryVersionsTables.class);
+ if (secondaryVersionsTables != null) {
+ for (SecondaryVersionsTable secondaryVersionsTable2 :
secondaryVersionsTables.value()) {
+
versioningData.secondaryTableDictionary.put(secondaryVersionsTable2.secondaryTableName(),
+ secondaryVersionsTable2.secondaryVersionsTableName());
+ }
+ }
+ } catch (ClassNotFoundException e) {
+ throw new MappingException(e);
+ }
+
+ return versioningData;
+ }
+}
Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/InheritanceType.java 2008-07-16 09:40:37 UTC
(rev 95)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,60 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-import org.hibernate.mapping.*;
-import org.hibernate.MappingException;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public enum InheritanceType {
- NONE,
- JOINED,
- SINGLE,
- TABLE_PER_CLASS;
-
- /**
- * @param pc The class for which to get the inheritance type.
- * @return The inheritance type of this class. NONE, if this class does not inherit
from
- * another persisten class.
- */
- public static InheritanceType get(PersistentClass pc) {
- PersistentClass superclass = pc.getSuperclass();
- if (superclass == null) {
- return InheritanceType.NONE;
- }
-
- // We assume that every subclass is of the same type.
- Subclass subclass = (Subclass) superclass.getSubclassIterator().next();
-
- if (subclass instanceof SingleTableSubclass) {
- return InheritanceType.SINGLE;
- } else if (subclass instanceof JoinedSubclass) {
- return InheritanceType.JOINED;
- } else if (subclass instanceof UnionSubclass) {
- return InheritanceType.TABLE_PER_CLASS;
- }
-
- throw new MappingException("Unknown subclass class: " +
subclass.getClass());
- }
-}
Copied: trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java (from
rev 102, trunk/src/main/org/jboss/envers/metadata/InheritanceType.java)
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/InheritanceType.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -0,0 +1,60 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.hibernate.mapping.*;
+import org.hibernate.MappingException;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public enum InheritanceType {
+ NONE,
+ JOINED,
+ SINGLE,
+ TABLE_PER_CLASS;
+
+ /**
+ * @param pc The class for which to get the inheritance type.
+ * @return The inheritance type of this class. NONE, if this class does not inherit
from
+ * another persisten class.
+ */
+ public static InheritanceType get(PersistentClass pc) {
+ PersistentClass superclass = pc.getSuperclass();
+ if (superclass == null) {
+ return InheritanceType.NONE;
+ }
+
+ // We assume that every subclass is of the same type.
+ Subclass subclass = (Subclass) superclass.getSubclassIterator().next();
+
+ if (subclass instanceof SingleTableSubclass) {
+ return InheritanceType.SINGLE;
+ } else if (subclass instanceof JoinedSubclass) {
+ return InheritanceType.JOINED;
+ } else if (subclass instanceof UnionSubclass) {
+ return InheritanceType.TABLE_PER_CLASS;
+ }
+
+ throw new MappingException("Unknown subclass class: " +
subclass.getClass());
+ }
+}
Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/MetadataTools.java 2008-07-16 09:40:37 UTC
(rev 95)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,125 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-import org.dom4j.Element;
-import org.dom4j.Document;
-import org.jboss.envers.tools.StringTools;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class MetadataTools {
- public static Element addNativelyGeneratedId(Element parent, String name, String
type) {
- Element id_mapping = parent.addElement("id");
- id_mapping.addAttribute("name", name).addAttribute("type",
type);
-
- Element generator_mapping = id_mapping.addElement("generator");
- generator_mapping.addAttribute("class", "native");
-
- return id_mapping;
- }
-
- public static Element addProperty(Element parent, String name, String type, boolean
key) {
- Element prop_mapping;
- if (key) {
- prop_mapping = parent.addElement("key-property");
- } else {
- prop_mapping = parent.addElement("property");
- }
-
- prop_mapping.addAttribute("name", name).addAttribute("type",
type);
-
- return prop_mapping;
- }
-
- public static Element addColumn(Element parent, String name, Integer length) {
- Element column_mapping = parent.addElement("column");
-
- column_mapping.addAttribute("name", name);
- if (length != null) {
- column_mapping.addAttribute("length", length.toString());
- }
-
- return column_mapping;
- }
-
- private static Element createEntityCommon(Document document, String type, String
entityName,
- String tableName, String schema, String
catalog,
- String discriminatorValue) {
- Element hibernate_mapping = document.addElement("hibernate-mapping");
-
- Element class_mapping = hibernate_mapping.addElement(type);
- class_mapping.addAttribute("entity-name", entityName);
-
- if (discriminatorValue != null) {
- class_mapping.addAttribute("discriminator-value",
discriminatorValue);
- }
-
- if (!StringTools.isEmpty(tableName)) {
- class_mapping.addAttribute("table", tableName);
- }
-
- if (!StringTools.isEmpty(schema)) {
- class_mapping.addAttribute("schema", schema);
- }
-
- if (!StringTools.isEmpty(catalog)) {
- class_mapping.addAttribute("catalog", catalog);
- }
-
- return class_mapping;
- }
-
- public static Element createEntity(Document document, String entityName, String
tableName,
- String schema, String catalog, String
discriminatorValue) {
- return createEntityCommon(document, "class", entityName, tableName,
schema, catalog, discriminatorValue);
- }
-
- public static Element createSubclassEntity(Document document, String entityName,
String tableName,
- String schema, String catalog, String
extendsEntityName,
- String discriminatorValue) {
- Element class_mapping = createEntityCommon(document, "subclass",
entityName, tableName, schema, catalog,
- discriminatorValue);
-
- class_mapping.addAttribute("extends", extendsEntityName);
-
- return class_mapping;
- }
-
- public static Element createJoin(Element parent, String tableName,
- String schema, String catalog) {
- Element join_mapping = parent.addElement("join");
-
- join_mapping.addAttribute("table", tableName);
-
- if (!StringTools.isEmpty(schema)) {
- join_mapping.addAttribute("schema", schema);
- }
-
- if (!StringTools.isEmpty(catalog)) {
- join_mapping.addAttribute("catalog", catalog);
- }
-
- return join_mapping;
- }
-}
Copied: trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java (from
rev 102, trunk/src/main/org/jboss/envers/metadata/MetadataTools.java)
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java
(rev 0)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/MetadataTools.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -0,0 +1,125 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Element;
+import org.dom4j.Document;
+import org.jboss.envers.tools.StringTools;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class MetadataTools {
+ public static Element addNativelyGeneratedId(Element parent, String name, String
type) {
+ Element id_mapping = parent.addElement("id");
+ id_mapping.addAttribute("name", name).addAttribute("type",
type);
+
+ Element generator_mapping = id_mapping.addElement("generator");
+ generator_mapping.addAttribute("class", "native");
+
+ return id_mapping;
+ }
+
+ public static Element addProperty(Element parent, String name, String type, boolean
key) {
+ Element prop_mapping;
+ if (key) {
+ prop_mapping = parent.addElement("key-property");
+ } else {
+ prop_mapping = parent.addElement("property");
+ }
+
+ prop_mapping.addAttribute("name", name).addAttribute("type",
type);
+
+ return prop_mapping;
+ }
+
+ public static Element addColumn(Element parent, String name, Integer length) {
+ Element column_mapping = parent.addElement("column");
+
+ column_mapping.addAttribute("name", name);
+ if (length != null) {
+ column_mapping.addAttribute("length", length.toString());
+ }
+
+ return column_mapping;
+ }
+
+ private static Element createEntityCommon(Document document, String type, String
entityName,
+ String tableName, String schema, String
catalog,
+ String discriminatorValue) {
+ Element hibernate_mapping = document.addElement("hibernate-mapping");
+
+ Element class_mapping = hibernate_mapping.addElement(type);
+ class_mapping.addAttribute("entity-name", entityName);
+
+ if (discriminatorValue != null) {
+ class_mapping.addAttribute("discriminator-value",
discriminatorValue);
+ }
+
+ if (!StringTools.isEmpty(tableName)) {
+ class_mapping.addAttribute("table", tableName);
+ }
+
+ if (!StringTools.isEmpty(schema)) {
+ class_mapping.addAttribute("schema", schema);
+ }
+
+ if (!StringTools.isEmpty(catalog)) {
+ class_mapping.addAttribute("catalog", catalog);
+ }
+
+ return class_mapping;
+ }
+
+ public static Element createEntity(Document document, String entityName, String
tableName,
+ String schema, String catalog, String
discriminatorValue) {
+ return createEntityCommon(document, "class", entityName, tableName,
schema, catalog, discriminatorValue);
+ }
+
+ public static Element createSubclassEntity(Document document, String entityName,
String tableName,
+ String schema, String catalog, String
extendsEntityName,
+ String discriminatorValue) {
+ Element class_mapping = createEntityCommon(document, "subclass",
entityName, tableName, schema, catalog,
+ discriminatorValue);
+
+ class_mapping.addAttribute("extends", extendsEntityName);
+
+ return class_mapping;
+ }
+
+ public static Element createJoin(Element parent, String tableName,
+ String schema, String catalog) {
+ Element join_mapping = parent.addElement("join");
+
+ join_mapping.addAttribute("table", tableName);
+
+ if (!StringTools.isEmpty(schema)) {
+ join_mapping.addAttribute("schema", schema);
+ }
+
+ if (!StringTools.isEmpty(catalog)) {
+ join_mapping.addAttribute("catalog", catalog);
+ }
+
+ return join_mapping;
+ }
+}
Deleted: trunk/src/main/org/jboss/envers/configuration/metadata/RevisionType.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/RevisionType.java 2008-07-16 09:40:37 UTC
(rev 95)
+++ trunk/src/main/org/jboss/envers/configuration/metadata/RevisionType.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,33 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class RevisionType {
- private RevisionType() { }
-
- public final static Byte ADD = 0;
- public final static Byte MOD = 1;
- public final static Byte DEL = 2;
-}
Deleted:
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java
===================================================================
---
trunk/src/main/org/jboss/envers/metadata/RevisionsInfoMetadataGenerator.java 2008-07-16
09:40:37 UTC (rev 95)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,53 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-import org.dom4j.Document;
-import org.dom4j.DocumentHelper;
-import org.dom4j.Element;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class RevisionsInfoMetadataGenerator {
- private VersionsEntitiesConfiguration verEntCfg;
-
- public RevisionsInfoMetadataGenerator(VersionsEntitiesConfiguration verEntCfg) {
- this.verEntCfg = verEntCfg;
- }
-
- public Document generate() {
- Document document = DocumentHelper.createDocument();
-
- Element class_mapping = MetadataTools.createEntity(document,
verEntCfg.getRevisionsInfoEntityName(),
- null, null, null, null);
-
- MetadataTools.addNativelyGeneratedId(class_mapping,
verEntCfg.getRevisionsInfoIdName(),
- verEntCfg.getRevisionPropType());
-
- MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionsInfoTimestampName(),
- verEntCfg.getRevisionsInfoTimestampType(), false);
-
- return document;
- }
-}
Copied:
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java
(from rev 102,
trunk/src/main/org/jboss/envers/metadata/RevisionsInfoMetadataGenerator.java)
===================================================================
---
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/RevisionsInfoMetadataGenerator.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -0,0 +1,53 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Element;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class RevisionsInfoMetadataGenerator {
+ private VersionsEntitiesConfiguration verEntCfg;
+
+ public RevisionsInfoMetadataGenerator(VersionsEntitiesConfiguration verEntCfg) {
+ this.verEntCfg = verEntCfg;
+ }
+
+ public Document generate() {
+ Document document = DocumentHelper.createDocument();
+
+ Element class_mapping = MetadataTools.createEntity(document,
verEntCfg.getRevisionsInfoEntityName(),
+ null, null, null, null);
+
+ MetadataTools.addNativelyGeneratedId(class_mapping,
verEntCfg.getRevisionsInfoIdName(),
+ verEntCfg.getRevisionPropType());
+
+ MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionsInfoTimestampName(),
+ verEntCfg.getRevisionsInfoTimestampType(), false);
+
+ return document;
+ }
+}
Deleted:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java 2008-07-16
09:40:37 UTC (rev 95)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -1,622 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.metadata;
-
-import org.hibernate.mapping.*;
-import org.hibernate.type.*;
-import org.hibernate.util.StringHelper;
-import org.hibernate.MappingException;
-import org.dom4j.Element;
-import org.dom4j.Document;
-import org.dom4j.DocumentHelper;
-import org.dom4j.Attribute;
-import org.dom4j.tree.DefaultElement;
-import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
-import org.jboss.envers.configuration.EntityConfiguration;
-import org.jboss.envers.configuration.VersionsConfiguration;
-import org.jboss.envers.mapper.*;
-import org.jboss.envers.mapper.id.*;
-import org.jboss.envers.mapper.id.relation.ToOneIdMapper;
-import org.jboss.envers.mapper.id.relation.OneToOneIdMapper;
-import org.jboss.envers.mapper.id.relation.OneToManyIdMapper;
-import org.jboss.envers.tools.StringTools;
-import org.jboss.envers.tools.Tools;
-import org.jboss.envers.tools.HibernateVersion;
-import org.jboss.envers.ModificationStore;
-import org.jboss.envers.log.YLog;
-import org.jboss.envers.log.YLogManager;
-import org.jboss.envers.metadata.data.IdMappingData;
-import org.jboss.envers.metadata.data.PersistentClassVersioningData;
-import org.jboss.envers.metadata.data.PropertyStoreInfo;
-import org.jboss.envers.exception.VersionsException;
-
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Collections;
-import java.util.HashMap;
-
-/**
- * @author Adam Warski (adam at warski dot org)
- */
-public class VersionsMetadataGenerator {
- private final static Map<String, ModificationStore> EMPTY_STORE =
Collections.emptyMap();
-
- private VersionsConfiguration verCfg;
- private VersionsEntitiesConfiguration verEntCfg;
- private Map<String, EntityConfiguration> entitiesConfigurations;
-
- // Map entity name -> (join descriptor -> element describing the
"versioned" join)
- private Map<String, Map<Join, Element>> entitiesJoins;
-
- private YLog log =
YLogManager.getLogManager().getLog(VersionsMetadataGenerator.class);
-
- public VersionsMetadataGenerator(VersionsConfiguration verCfg,
VersionsEntitiesConfiguration verEntCfg) {
- this.verCfg = verCfg;
- this.verEntCfg = verEntCfg;
-
- entitiesConfigurations = new HashMap<String, EntityConfiguration>();
- entitiesJoins = new HashMap<String, Map<Join, Element>>();
- }
-
- private void addComponentClassName(Element any_mapping, Component comp) {
- if (StringHelper.isNotEmpty(comp.getComponentClassName())) {
- any_mapping.addAttribute("class", comp.getComponentClassName());
- }
- }
-
- private void addColumns(Element any_mapping, Iterator<Column> columns) {
- while (columns.hasNext()) {
- Column column = columns.next();
- MetadataTools.addColumn(any_mapping, column.getName(), column.getLength());
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addSimpleProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
- ModificationStore store, boolean key) {
- Element prop_mapping = MetadataTools.addProperty(parent, property.getName(),
- property.getType().getName(), key);
- addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
-
- // A null mapper means that we only want to add xml mappings (while building the
id mapping)
- if (mapper != null) {
- mapper.add(property.getName(), store);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addEnumProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
- ModificationStore store) {
- Element prop_mapping = parent.addElement("property");
- prop_mapping.addAttribute("name", property.getName());
-
- CustomType propertyType = (CustomType) property.getType();
-
- Element type_mapping = prop_mapping.addElement("type");
- type_mapping.addAttribute("name", propertyType.getName());
-
- Element type_param1 = type_mapping.addElement("param");
- type_param1.addAttribute("name", "enumClass");
- type_param1.setText(propertyType.getReturnedClass().getName());
-
- Element type_param2 = type_mapping.addElement("param");
- type_param2.addAttribute("name", "type");
- type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
-
- addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
-
- mapper.add(property.getName(), store);
- }
-
- @SuppressWarnings({"unchecked"})
- private void addComponent(Element parent, Property property, CompositeMapperBuilder
mapper, ModificationStore store,
- String entityName, boolean firstPass) {
- Element component_mapping = null;
- Component prop_component = (Component) property.getValue();
-
- if (!firstPass) {
- // The required element already exists.
- Iterator<Element> iter =
parent.elementIterator("component");
- while (iter.hasNext()) {
- Element child = iter.next();
- if
(child.attribute("name").getText().equals(property.getName())) {
- component_mapping = child;
- break;
- }
- }
-
- if (component_mapping == null) {
- throw new VersionsException("Element for component not found during
second pass!");
- }
- } else {
- component_mapping = parent.addElement("component");
- component_mapping.addAttribute("name", property.getName());
-
- addComponentClassName(component_mapping, prop_component);
- }
-
- addProperties(component_mapping, (Iterator<Property>)
prop_component.getPropertyIterator(),
- mapper.addComposite(property.getName()), new PropertyStoreInfo(store,
EMPTY_STORE), entityName,
- firstPass);
- }
-
- @SuppressWarnings({"unchecked"})
- private void changeNamesInColumnElement(Element element, Iterator<Column>
columnIterator) {
- Iterator<Element> properties = element.elementIterator();
- while (properties.hasNext()) {
- Element property = properties.next();
-
- if ("column".equals(property.getName())) {
- Attribute nameAttr = property.attribute("name");
- if (nameAttr != null) {
- nameAttr.setText(columnIterator.next().getName());
- }
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void prefixNamesInPropertyElement(Element element, String prefix,
Iterator<Column> columnIterator) {
- Iterator<Element> properties = element.elementIterator();
- while (properties.hasNext()) {
- Element property = properties.next();
-
- if ("property".equals(property.getName())) {
- Attribute nameAttr = property.attribute("name");
- if (nameAttr != null) {
- nameAttr.setText(prefix + nameAttr.getText());
- }
-
- changeNamesInColumnElement(property, columnIterator);
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addToOne(Element parent, Property property, CompositeMapperBuilder
mapper, String entityName) {
- String referencedEntityName = ((ToOne)
property.getValue()).getReferencedEntityName();
-
- EntityConfiguration configuration =
entitiesConfigurations.get(referencedEntityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
- }
-
- IdMappingData idMapping = configuration.getIdMappingData();
-
- String propertyName = property.getName();
- String lastPropertyPrefix = propertyName + "_";
-
- // Generating the id mapper for the relation
- IdMapper relMapper =
idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addToOneRelation(propertyName,
referencedEntityName, relMapper);
-
- // Adding an element to the mapping corresponding to the references entity
id's
- Element properties = (Element) idMapping.getXmlRelationMapping().clone();
- properties.addAttribute("name", propertyName);
-
- prefixNamesInPropertyElement(properties, lastPropertyPrefix,
property.getValue().getColumnIterator());
- parent.add(properties);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new ToOneIdMapper(relMapper, propertyName,
referencedEntityName));
- }
-
- @SuppressWarnings({"unchecked"})
- private void addOneToOne(Property property, CompositeMapperBuilder mapper, String
entityName) {
- OneToOne propertyValue = (OneToOne) property.getValue();
-
- String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
// mappedBy
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- IdMappingData ownedIdMapping = configuration.getIdMappingData();
-
- if (ownedIdMapping == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- String propertyName = property.getName();
- String lastPropertyPrefix = owningReferencePropertyName + "_";
- String referencedEntityName = propertyValue.getReferencedEntityName();
-
- // Generating the id mapper for the relation
- IdMapper ownedIdMapper =
ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addOneToOneRelation(propertyName,
owningReferencePropertyName,
- referencedEntityName, ownedIdMapper);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new
OneToOneIdMapper(owningReferencePropertyName,
- referencedEntityName, propertyName));
- }
-
- @SuppressWarnings({"unchecked"})
- private String getMappedBy(Collection collectionValue) {
- Iterator<Property> assocClassProps =
- ((OneToMany)
collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
-
- while (assocClassProps.hasNext()) {
- Property property = assocClassProps.next();
-
- if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
- collectionValue.getKey().getColumnIterator())) {
- return property.getName();
- }
- }
-
- return null;
- }
-
- @SuppressWarnings({"unchecked"})
- private void addOneToMany(Property property, CompositeMapperBuilder mapper, String
entityName) {
- Collection propertyValue = (Collection) property.getValue();
-
- String owningReferencePropertyName = getMappedBy(propertyValue);
- if (owningReferencePropertyName == null) {
- throw new MappingException("Unable to read the mapped by attribute for
" + property.getName());
- }
-
- EntityConfiguration configuration = entitiesConfigurations.get(entityName);
- if (configuration == null) {
- throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
- }
-
- IdMappingData referencingIdMapping = configuration.getIdMappingData();
-
- String owningEntityName = ((OneToMany)
propertyValue.getElement()).getReferencedEntityName();
- String propertyName = property.getName();
- String lastPropertyPrefix = owningReferencePropertyName + "_";
-
- // Generating the id mapper for the relation
- IdMapper ownedIdMapper =
referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
-
- // Storing information about this relation
- entitiesConfigurations.get(entityName).addOneToManyRelation(propertyName,
owningReferencePropertyName,
- owningEntityName, ownedIdMapper);
-
- // Adding mapper for the id
- mapper.addComposite(propertyName, new
OneToManyIdMapper(owningReferencePropertyName, owningEntityName,
- propertyName));
- }
-
- private ModificationStore getStoreForProperty(Property property, PropertyStoreInfo
propertyStoreInfo) {
- /*
- * Checks if a property is versioned, which is when:
- * - the whole entity is versioned, then the default store is not null
- * - there is a store defined for this entity, which is when this property is
annotated
- */
-
- ModificationStore store =
propertyStoreInfo.propertyStores.get(property.getName());
-
- if (store == null) {
- return propertyStoreInfo.defaultStore;
- }
-
- return store;
- }
-
- @SuppressWarnings({"unchecked"})
- private void addIdProperties(Element parent, Iterator<Property> properties,
SimpleMapperBuilder mapper, boolean key) {
- while (properties.hasNext()) {
- Property property = properties.next();
- Type propertyType = property.getType();
- if (!"_identifierMapper".equals(property.getName())) {
- if (propertyType instanceof ImmutableType) {
- addSimpleProperty(parent, property, mapper, ModificationStore.FULL,
key);
- } else {
- throw new MappingException("Type not supported: " +
propertyType.getClass().getName());
- }
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addProperties(Element parent, Iterator<Property> properties,
CompositeMapperBuilder currentMapper,
- PropertyStoreInfo propertyStoreInfo, String entityName,
boolean firstPass) {
- while (properties.hasNext()) {
- Property property = properties.next();
- Type propertyType = property.getType();
- if (!"_identifierMapper".equals(property.getName())) {
- ModificationStore store = getStoreForProperty(property,
propertyStoreInfo);
-
- if (store != null) {
- if (propertyType instanceof ComponentType) {
- // only first pass
- if (firstPass) {
- addComponent(parent, property, currentMapper, store,
entityName, firstPass);
- }
- } else if (propertyType instanceof ImmutableType || propertyType
instanceof MutableType) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if (propertyType instanceof CustomType &&
-
"org.hibernate.type.EnumType".equals(propertyType.getName())) {
- // only first pass
- if (firstPass) {
- addEnumProperty(parent, property, currentMapper, store);
- }
- } else if (propertyType instanceof ManyToOneType) {
- // only second pass
- if (!firstPass) {
- addToOne(parent, property, currentMapper, entityName);
- }
- } else if (propertyType instanceof OneToOneType) {
- // only second pass
- if (!firstPass) {
- addOneToOne(property, currentMapper, entityName);
- }
- } else if
("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
- propertyType.getClass().getName())) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if (propertyType instanceof CustomType &&
-
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(propertyType.getName())
||
-
"org.hibernate.type.StringClobType".equals(propertyType.getName()))) {
- // only first pass
- if (firstPass) {
- addSimpleProperty(parent, property, currentMapper, store,
false);
- }
- } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
- ((((Collection) property.getValue()).getElement() instanceof
OneToMany))) {
- // only second pass
- if (!firstPass) {
- addOneToMany(property, currentMapper, entityName);
- }
- } else {
- String message = "Type not supported for versioning: "
+ propertyType.getClass().getName() +
- ", on entity " + entityName + ", property
'" + property.getName() + "'.";
- if (verCfg.isWarnOnUnsupportedTypes()) {
- log.warn(message);
- } else {
- throw new MappingException(message);
- }
- }
- }
- }
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void createJoins(PersistentClass pc, Element parent,
PersistentClassVersioningData versioningData) {
- Iterator<Join> joins = pc.getJoinIterator();
-
- Map<Join, Element> joinElements = new HashMap<Join, Element>();
- entitiesJoins.put(pc.getEntityName(), joinElements);
-
- while (joins.hasNext()) {
- Join join = joins.next();
-
- // Determining the table name. If there is no entry in the dictionary, just
constructing the table name
- // as if it was an entity (by appending/prepending configured strings).
- String originalTableName = join.getTable().getName();
- String versionedTableName =
versioningData.secondaryTableDictionary.get(originalTableName);
- if (versionedTableName == null) {
- versionedTableName = verEntCfg.getVersionsEntityName(originalTableName);
- }
-
- String schema = versioningData.schema;
- if (StringTools.isEmpty(schema)) {
- schema = join.getTable().getSchema();
- }
-
- String catalog = versioningData.catalog;
- if (StringTools.isEmpty(catalog)) {
- catalog = join.getTable().getCatalog();
- }
-
- Element joinElement = MetadataTools.createJoin(parent, versionedTableName,
schema, catalog);
- joinElements.put(join, joinElement);
-
- Element joinKey = joinElement.addElement("key");
- addColumns(joinKey, join.getKey().getColumnIterator());
- MetadataTools.addColumn(joinKey, verEntCfg.getRevisionPropName(), null);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper,
PropertyStoreInfo propertyStoreInfo,
- String entityName, boolean firstPass) {
- Iterator<Join> joins = pc.getJoinIterator();
-
- while (joins.hasNext()) {
- Join join = joins.next();
- Element joinElement = entitiesJoins.get(entityName).get(join);
-
- addProperties(joinElement, join.getPropertyIterator(), currentMapper,
propertyStoreInfo, entityName,
- firstPass);
- }
- }
-
- @SuppressWarnings({"unchecked"})
- private IdMappingData addId(PersistentClass pc) {
- // Mapping which will be used for relations
- Element rel_id_mapping = new DefaultElement("properties");
- // Mapping which will be used for the primary key of the versions table
- Element orig_id_mapping = new DefaultElement("composite-id");
-
- Property id_prop = pc.getIdentifierProperty();
- Component id_mapper = pc.getIdentifierMapper();
-
- SimpleIdMapperBuilder mapper;
- if (id_mapper != null) {
- mapper = new MultipleIdMapper(((Component)
pc.getIdentifier()).getComponentClassName());
- addIdProperties(rel_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), mapper, false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addIdProperties(orig_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), null, true);
- } else if (id_prop.isComposite()) {
- Component id_component = (Component) id_prop.getValue();
-
- mapper = new EmbeddedIdMapper(id_prop.getName(),
id_component.getComponentClassName());
- addIdProperties(rel_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), mapper, false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addIdProperties(orig_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), null, true);
- } else {
- mapper = new SingleIdMapper();
-
- addSimpleProperty(rel_id_mapping, id_prop, mapper, ModificationStore.FULL,
false);
-
- // null mapper - the mapping where already added the first time, now we only
want to generate the xml
- addSimpleProperty(orig_id_mapping, id_prop, null, ModificationStore.FULL,
true);
- }
-
- orig_id_mapping.addAttribute("name",
verEntCfg.getOriginalIdPropName());
-
-
- // Adding the "revision number" property
- Element rev_mapping = MetadataTools.addProperty(orig_id_mapping,
verEntCfg.getRevisionPropName(),
- verEntCfg.getRevisionPropType(), true);
- MetadataTools.addColumn(rev_mapping, verEntCfg.getRevisionPropName(), null);
-
- return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping);
- }
-
- private void addPersisterHack(Element class_mapping) {
- String persisterClassName;
- if (HibernateVersion.get().startsWith("3.3")) {
- persisterClassName =
"org.jboss.envers.entity.VersionsInheritanceEntityPersisterFor33";
- } else {
- persisterClassName =
"org.jboss.envers.entity.VersionsInheritanceEntityPersisterFor32";
- }
-
- class_mapping.addAttribute("persister", persisterClassName);
- }
-
- @SuppressWarnings({"unchecked"})
- public Document generateFirstPass(PersistentClass pc, PersistentClassVersioningData
versioningData) {
- Document document = DocumentHelper.createDocument();
-
- String schema = versioningData.schema;
- if (StringTools.isEmpty(schema)) {
- schema = pc.getTable().getSchema();
- }
-
- String catalog = versioningData.catalog;
- if (StringTools.isEmpty(catalog)) {
- catalog = pc.getTable().getCatalog();
- }
-
- String entityName = pc.getEntityName();
- String versionsEntityName = verEntCfg.getVersionsEntityName(entityName);
- String versionsTableName = verEntCfg.getVersionsTableName(entityName,
pc.getTable().getName());
-
- // Generating a mapping for the id
- IdMappingData idMapper = addId(pc);
-
- Element class_mapping;
- ExtendedPropertyMapper propertyMapper;
-
- InheritanceType inheritanceType = InheritanceType.get(pc);
- String parentEntityName = null;
-
- switch (inheritanceType) {
- case NONE:
- class_mapping = MetadataTools.createEntity(document, versionsEntityName,
versionsTableName,
- schema, catalog, pc.getDiscriminatorValue());
- propertyMapper = new MultiPropertyMapper();
-
- // Checking if there is a discriminator column
- if (pc.getDiscriminator() != null) {
- Element discriminator_element =
class_mapping.addElement("discriminator");
- addColumns(discriminator_element,
pc.getDiscriminator().getColumnIterator());
- discriminator_element.addAttribute("type",
pc.getDiscriminator().getType().getName());
-
- // If so, there is some inheritance scheme -> using the persister
hack.
- addPersisterHack(class_mapping);
- }
-
- // Adding the id mapping
- class_mapping.add((Element) idMapper.getXmlMapping().clone());
-
- // Adding the "revision type" property
- MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionTypePropName(),
- verEntCfg.getRevisionTypePropType(), false);
-
- break;
- case SINGLE:
- String extendsEntityName =
verEntCfg.getVersionsEntityName(pc.getSuperclass().getEntityName());
- class_mapping = MetadataTools.createSubclassEntity(document,
versionsEntityName, versionsTableName,
- schema, catalog, extendsEntityName, pc.getDiscriminatorValue());
-
- addPersisterHack(class_mapping);
-
- // The id and revision type is already mapped in the parent
-
- // Getting the property mapper of the parent - when mapping properties,
they need to be included
- parentEntityName = pc.getSuperclass().getEntityName();
- ExtendedPropertyMapper parentPropertyMapper =
entitiesConfigurations.get(parentEntityName).getPropertyMapper();
- propertyMapper = new SubclassPropertyMapper(new MultiPropertyMapper(),
parentPropertyMapper);
-
- break;
- case JOINED:
- throw new MappingException("Joined inheritance strategy not
supported for versioning!");
- case TABLE_PER_CLASS:
- throw new MappingException("Table-per-class inheritance strategy not
supported for versioning!");
- default:
- throw new AssertionError("Impossible enum value.");
- }
-
- // Mapping unjoined properties
- addProperties(class_mapping, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(), propertyMapper,
- versioningData.propertyStoreInfo, pc.getEntityName(), true);
-
- // Creating and mapping joins (first pass)
- createJoins(pc, class_mapping, versioningData);
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), true);
-
- // Storing the generated configuration
- EntityConfiguration entityCfg = new EntityConfiguration(entityName,
versionsEntityName, idMapper,
- propertyMapper, parentEntityName);
- entitiesConfigurations.put(pc.getEntityName(), entityCfg);
-
- return document;
- }
-
- @SuppressWarnings({"unchecked"})
- public void generateSecondPass(PersistentClass pc, PersistentClassVersioningData
versioningData,
- Document document) {
- String entityName = pc.getEntityName();
-
- CompositeMapperBuilder propertyMapper =
entitiesConfigurations.get(entityName).getPropertyMapper();
-
- // Mapping unjoined properties
- addProperties(document.getRootElement().element("class"),
(Iterator<Property>) pc.getUnjoinedPropertyIterator(),
- propertyMapper, versioningData.propertyStoreInfo, entityName, false);
-
- // Mapping joins (second pass)
- addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
false);
- }
-
- public Map<String, EntityConfiguration> getEntitiesConfigurations() {
- return entitiesConfigurations;
- }
-}
Copied:
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
(from rev 102, trunk/src/main/org/jboss/envers/metadata/VersionsMetadataGenerator.java)
===================================================================
--- trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java
(rev 0)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/VersionsMetadataGenerator.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -0,0 +1,662 @@
+/*
+ * Envers.
http://www.jboss.org/envers
+ *
+ * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT A WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License, v.2.1 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Red Hat Author(s): Adam Warski
+ */
+package org.jboss.envers.configuration.metadata;
+
+import org.hibernate.mapping.*;
+import org.hibernate.type.*;
+import org.hibernate.util.StringHelper;
+import org.hibernate.MappingException;
+import org.dom4j.Element;
+import org.dom4j.Document;
+import org.dom4j.DocumentHelper;
+import org.dom4j.Attribute;
+import org.dom4j.tree.DefaultElement;
+import org.jboss.envers.configuration.VersionsEntitiesConfiguration;
+import org.jboss.envers.configuration.EntityConfiguration;
+import org.jboss.envers.configuration.VersionsConfiguration;
+import org.jboss.envers.mapper.*;
+import org.jboss.envers.mapper.id.*;
+import org.jboss.envers.mapper.id.relation.ToOneIdMapper;
+import org.jboss.envers.mapper.id.relation.OneToOneIdMapper;
+import org.jboss.envers.mapper.id.relation.OneToManyIdMapper;
+import org.jboss.envers.tools.StringTools;
+import org.jboss.envers.tools.Tools;
+import org.jboss.envers.tools.HibernateVersion;
+import org.jboss.envers.ModificationStore;
+import org.jboss.envers.tools.log.YLog;
+import org.jboss.envers.tools.log.YLogManager;
+import org.jboss.envers.configuration.metadata.data.IdMappingData;
+import org.jboss.envers.configuration.metadata.data.PersistentClassVersioningData;
+import org.jboss.envers.configuration.metadata.data.PropertyStoreInfo;
+import org.jboss.envers.exception.VersionsException;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Collections;
+import java.util.HashMap;
+
+/**
+ * @author Adam Warski (adam at warski dot org)
+ */
+public class VersionsMetadataGenerator {
+ private final static Map<String, ModificationStore> EMPTY_STORE =
Collections.emptyMap();
+
+ private VersionsConfiguration verCfg;
+ private VersionsEntitiesConfiguration verEntCfg;
+ private Map<String, EntityConfiguration> entitiesConfigurations;
+
+ // Map entity name -> (join descriptor -> element describing the
"versioned" join)
+ private Map<String, Map<Join, Element>> entitiesJoins;
+
+ private YLog log =
YLogManager.getLogManager().getLog(VersionsMetadataGenerator.class);
+
+ public VersionsMetadataGenerator(VersionsConfiguration verCfg,
VersionsEntitiesConfiguration verEntCfg) {
+ this.verCfg = verCfg;
+ this.verEntCfg = verEntCfg;
+
+ entitiesConfigurations = new HashMap<String, EntityConfiguration>();
+ entitiesJoins = new HashMap<String, Map<Join, Element>>();
+ }
+
+ private void addComponentClassName(Element any_mapping, Component comp) {
+ if (StringHelper.isNotEmpty(comp.getComponentClassName())) {
+ any_mapping.addAttribute("class", comp.getComponentClassName());
+ }
+ }
+
+ private void addColumns(Element any_mapping, Iterator<Column> columns) {
+ while (columns.hasNext()) {
+ Column column = columns.next();
+ MetadataTools.addColumn(any_mapping, column.getName(), column.getLength());
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addSimpleProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
+ ModificationStore store, boolean key) {
+ Element prop_mapping = MetadataTools.addProperty(parent, property.getName(),
+ property.getType().getName(), key);
+ addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
+
+ // A null mapper means that we only want to add xml mappings (while building the
id mapping)
+ if (mapper != null) {
+ mapper.add(property.getName(), store);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addEnumProperty(Element parent, Property property, SimpleMapperBuilder
mapper,
+ ModificationStore store) {
+ Element prop_mapping = parent.addElement("property");
+ prop_mapping.addAttribute("name", property.getName());
+
+ CustomType propertyType = (CustomType) property.getType();
+
+ Element type_mapping = prop_mapping.addElement("type");
+ type_mapping.addAttribute("name", propertyType.getName());
+
+ Element type_param1 = type_mapping.addElement("param");
+ type_param1.addAttribute("name", "enumClass");
+ type_param1.setText(propertyType.getReturnedClass().getName());
+
+ Element type_param2 = type_mapping.addElement("param");
+ type_param2.addAttribute("name", "type");
+ type_param2.setText(Integer.toString(propertyType.sqlTypes(null)[0]));
+
+ addColumns(prop_mapping, (Iterator<Column>) property.getColumnIterator());
+
+ mapper.add(property.getName(), store);
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addComponent(Element parent, Property property, CompositeMapperBuilder
mapper, ModificationStore store,
+ String entityName, boolean firstPass) {
+ Element component_mapping = null;
+ Component prop_component = (Component) property.getValue();
+
+ if (!firstPass) {
+ // The required element already exists.
+ Iterator<Element> iter =
parent.elementIterator("component");
+ while (iter.hasNext()) {
+ Element child = iter.next();
+ if
(child.attribute("name").getText().equals(property.getName())) {
+ component_mapping = child;
+ break;
+ }
+ }
+
+ if (component_mapping == null) {
+ throw new VersionsException("Element for component not found during
second pass!");
+ }
+ } else {
+ component_mapping = parent.addElement("component");
+ component_mapping.addAttribute("name", property.getName());
+
+ addComponentClassName(component_mapping, prop_component);
+ }
+
+ addProperties(component_mapping, (Iterator<Property>)
prop_component.getPropertyIterator(),
+ mapper.addComposite(property.getName()), new PropertyStoreInfo(store,
EMPTY_STORE), entityName,
+ firstPass);
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void changeNamesInColumnElement(Element element, Iterator<Column>
columnIterator) {
+ Iterator<Element> properties = element.elementIterator();
+ while (properties.hasNext()) {
+ Element property = properties.next();
+
+ if ("column".equals(property.getName())) {
+ Attribute nameAttr = property.attribute("name");
+ if (nameAttr != null) {
+ nameAttr.setText(columnIterator.next().getName());
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void prefixNamesInPropertyElement(Element element, String prefix,
Iterator<Column> columnIterator) {
+ Iterator<Element> properties = element.elementIterator();
+ while (properties.hasNext()) {
+ Element property = properties.next();
+
+ if ("property".equals(property.getName())) {
+ Attribute nameAttr = property.attribute("name");
+ if (nameAttr != null) {
+ nameAttr.setText(prefix + nameAttr.getText());
+ }
+
+ changeNamesInColumnElement(property, columnIterator);
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addToOne(Element parent, Property property, CompositeMapperBuilder
mapper, String entityName) {
+ String referencedEntityName = ((ToOne)
property.getValue()).getReferencedEntityName();
+
+ EntityConfiguration configuration =
entitiesConfigurations.get(referencedEntityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
+ }
+
+ IdMappingData idMapping = configuration.getIdMappingData();
+
+ String propertyName = property.getName();
+ String lastPropertyPrefix = propertyName + "_";
+
+ // Generating the id mapper for the relation
+ IdMapper relMapper =
idMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+ entitiesConfigurations.get(entityName).addToOneRelation(propertyName,
referencedEntityName, relMapper);
+
+ // Adding an element to the mapping corresponding to the references entity
id's
+ Element properties = (Element) idMapping.getXmlRelationMapping().clone();
+ properties.addAttribute("name", propertyName);
+
+ prefixNamesInPropertyElement(properties, lastPropertyPrefix,
property.getValue().getColumnIterator());
+ parent.add(properties);
+
+ // Adding mapper for the id
+ mapper.addComposite(propertyName, new ToOneIdMapper(relMapper, propertyName,
referencedEntityName));
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addOneToOneBidirectional(Property property, CompositeMapperBuilder
mapper, String entityName) {
+ OneToOne propertyValue = (OneToOne) property.getValue();
+
+ String owningReferencePropertyName = propertyValue.getReferencedPropertyName();
// mappedBy
+
+ EntityConfiguration configuration = entitiesConfigurations.get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ IdMappingData ownedIdMapping = configuration.getIdMappingData();
+
+ if (ownedIdMapping == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ String propertyName = property.getName();
+ String lastPropertyPrefix = owningReferencePropertyName + "_";
+ String referencedEntityName = propertyValue.getReferencedEntityName();
+
+ // Generating the id mapper for the relation
+ IdMapper ownedIdMapper =
ownedIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+ entitiesConfigurations.get(entityName).addOneToOneRelation(propertyName,
owningReferencePropertyName,
+ referencedEntityName, ownedIdMapper);
+
+ // Adding mapper for the id
+ mapper.addComposite(propertyName, new
OneToOneIdMapper(owningReferencePropertyName,
+ referencedEntityName, propertyName));
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private String getMappedBy(Collection collectionValue) {
+ Iterator<Property> assocClassProps =
+ ((OneToMany)
collectionValue.getElement()).getAssociatedClass().getPropertyIterator();
+
+ while (assocClassProps.hasNext()) {
+ Property property = assocClassProps.next();
+
+ if (Tools.iteratorsContentEqual(property.getValue().getColumnIterator(),
+ collectionValue.getKey().getColumnIterator())) {
+ return property.getName();
+ }
+ }
+
+ return null;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addOneToManyBidirectional(Property property, CompositeMapperBuilder
mapper, String entityName) {
+ Collection propertyValue = (Collection) property.getValue();
+
+ String owningReferencePropertyName = getMappedBy(propertyValue);
+ if (owningReferencePropertyName == null) {
+ throw new MappingException("Unable to read the mapped by attribute for
" + property.getName());
+ }
+
+ EntityConfiguration configuration = entitiesConfigurations.get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ IdMappingData referencingIdMapping = configuration.getIdMappingData();
+
+ String owningEntityName = ((OneToMany)
propertyValue.getElement()).getReferencedEntityName();
+ String propertyName = property.getName();
+ String lastPropertyPrefix = owningReferencePropertyName + "_";
+
+ // Generating the id mapper for the relation
+ IdMapper ownedIdMapper =
referencingIdMapping.getIdMapper().prefixMappedProperties(lastPropertyPrefix);
+
+ // Storing information about this relation
+ entitiesConfigurations.get(entityName).addOneToManyRelation(propertyName,
owningReferencePropertyName,
+ owningEntityName, ownedIdMapper);
+
+ // Adding mapper for the id
+ mapper.addComposite(propertyName, new
OneToManyIdMapper(owningReferencePropertyName, owningEntityName,
+ propertyName));
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addOneToManyUnidirectional(Property property, CompositeMapperBuilder
mapper, String entityName) {
+ Collection propertyValue = (Collection) property.getValue();
+ ManyToOne mto = (ManyToOne) propertyValue.getElement();
+
+ String referencedEntityName = mto.getReferencedEntityName();
+
+ EntityConfiguration configuration = entitiesConfigurations.get(entityName);
+ if (configuration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + entityName + "!");
+ }
+
+ EntityConfiguration referencedConfiguration =
entitiesConfigurations.get(referencedEntityName);
+ if (referencedConfiguration == null) {
+ throw new MappingException("A versioned relation to a non-versioned
entity " + referencedEntityName + "!");
+ }
+
+ IdMappingData referencingIdMapping = configuration.getIdMappingData();
+ IdMappingData referencedIdMapping = referencedConfiguration.getIdMappingData();
+
+ // Name of the entity that will be used to store the relation between the two
entities.
+ String middleEntityName = entityName + "_" + referencedEntityName;
+
+ MetadataTools.createEntity(null, middleEntityName, mto.getTable().getName(),
mto.getTable().getSchema(),
+ mto.getTable().getCatalog(), null);
+
+ // TODO: ???
+ }
+
+ private ModificationStore getStoreForProperty(Property property, PropertyStoreInfo
propertyStoreInfo) {
+ /*
+ * Checks if a property is versioned, which is when:
+ * - the whole entity is versioned, then the default store is not null
+ * - there is a store defined for this entity, which is when this property is
annotated
+ */
+
+ ModificationStore store =
propertyStoreInfo.propertyStores.get(property.getName());
+
+ if (store == null) {
+ return propertyStoreInfo.defaultStore;
+ }
+
+ return store;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addIdProperties(Element parent, Iterator<Property> properties,
SimpleMapperBuilder mapper, boolean key) {
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ Type propertyType = property.getType();
+ if (!"_identifierMapper".equals(property.getName())) {
+ if (propertyType instanceof ImmutableType) {
+ addSimpleProperty(parent, property, mapper, ModificationStore.FULL,
key);
+ } else {
+ throw new MappingException("Type not supported: " +
propertyType.getClass().getName());
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addProperties(Element parent, Iterator<Property> properties,
CompositeMapperBuilder currentMapper,
+ PropertyStoreInfo propertyStoreInfo, String entityName,
boolean firstPass) {
+ while (properties.hasNext()) {
+ Property property = properties.next();
+ Type propertyType = property.getType();
+ if (!"_identifierMapper".equals(property.getName())) {
+ ModificationStore store = getStoreForProperty(property,
propertyStoreInfo);
+
+ if (store != null) {
+ if (propertyType instanceof ComponentType) {
+ // only first pass
+ if (firstPass) {
+ addComponent(parent, property, currentMapper, store,
entityName, firstPass);
+ }
+ } else if (propertyType instanceof ImmutableType || propertyType
instanceof MutableType) {
+ // only first pass
+ if (firstPass) {
+ addSimpleProperty(parent, property, currentMapper, store,
false);
+ }
+ } else if (propertyType instanceof CustomType &&
+
"org.hibernate.type.EnumType".equals(propertyType.getName())) {
+ // only first pass
+ if (firstPass) {
+ addEnumProperty(parent, property, currentMapper, store);
+ }
+ } else if (propertyType instanceof ManyToOneType) {
+ // only second pass
+ if (!firstPass) {
+ addToOne(parent, property, currentMapper, entityName);
+ }
+ } else if (propertyType instanceof OneToOneType) {
+ // only second pass
+ if (!firstPass) {
+ addOneToOneBidirectional(property, currentMapper,
entityName);
+ }
+ } else if
("org.hibernate.type.PrimitiveByteArrayBlobType".equals(
+ propertyType.getClass().getName())) {
+ // only first pass
+ if (firstPass) {
+ addSimpleProperty(parent, property, currentMapper, store,
false);
+ }
+ } else if (propertyType instanceof CustomType &&
+
("org.hibernate.type.PrimitiveCharacterArrayClobType".equals(propertyType.getName())
||
+
"org.hibernate.type.StringClobType".equals(propertyType.getName()))) {
+ // only first pass
+ if (firstPass) {
+ addSimpleProperty(parent, property, currentMapper, store,
false);
+ }
+ } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
+ ((((Collection) property.getValue()).getElement() instanceof
OneToMany))) {
+ // only second pass
+ if (!firstPass) {
+ addOneToManyBidirectional(property, currentMapper,
entityName);
+ }
+ } else if ((propertyType instanceof BagType || propertyType
instanceof SetType) &&
+ ((((Collection) property.getValue()).getElement() instanceof
ManyToOne))) {
+ // only second pass
+ if (!firstPass) {
+ addOneToManyUnidirectional(property, currentMapper,
entityName);
+ }
+ } else {
+ String message = "Type not supported for versioning: "
+ propertyType.getClass().getName() +
+ ", on entity " + entityName + ", property
'" + property.getName() + "'.";
+ if (verCfg.isWarnOnUnsupportedTypes()) {
+ log.warn(message);
+ } else {
+ throw new MappingException(message);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void createJoins(PersistentClass pc, Element parent,
PersistentClassVersioningData versioningData) {
+ Iterator<Join> joins = pc.getJoinIterator();
+
+ Map<Join, Element> joinElements = new HashMap<Join, Element>();
+ entitiesJoins.put(pc.getEntityName(), joinElements);
+
+ while (joins.hasNext()) {
+ Join join = joins.next();
+
+ // Determining the table name. If there is no entry in the dictionary, just
constructing the table name
+ // as if it was an entity (by appending/prepending configured strings).
+ String originalTableName = join.getTable().getName();
+ String versionedTableName =
versioningData.secondaryTableDictionary.get(originalTableName);
+ if (versionedTableName == null) {
+ versionedTableName = verEntCfg.getVersionsEntityName(originalTableName);
+ }
+
+ String schema = versioningData.schema;
+ if (StringTools.isEmpty(schema)) {
+ schema = join.getTable().getSchema();
+ }
+
+ String catalog = versioningData.catalog;
+ if (StringTools.isEmpty(catalog)) {
+ catalog = join.getTable().getCatalog();
+ }
+
+ Element joinElement = MetadataTools.createJoin(parent, versionedTableName,
schema, catalog);
+ joinElements.put(join, joinElement);
+
+ Element joinKey = joinElement.addElement("key");
+ addColumns(joinKey, join.getKey().getColumnIterator());
+ MetadataTools.addColumn(joinKey, verEntCfg.getRevisionPropName(), null);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private void addJoins(PersistentClass pc, CompositeMapperBuilder currentMapper,
PropertyStoreInfo propertyStoreInfo,
+ String entityName, boolean firstPass) {
+ Iterator<Join> joins = pc.getJoinIterator();
+
+ while (joins.hasNext()) {
+ Join join = joins.next();
+ Element joinElement = entitiesJoins.get(entityName).get(join);
+
+ addProperties(joinElement, join.getPropertyIterator(), currentMapper,
propertyStoreInfo, entityName,
+ firstPass);
+ }
+ }
+
+ @SuppressWarnings({"unchecked"})
+ private IdMappingData addId(PersistentClass pc) {
+ // Xml mapping which will be used for relations
+ Element rel_id_mapping = new DefaultElement("properties");
+ // Xml mapping which will be used for the primary key of the versions table
+ Element orig_id_mapping = new DefaultElement("composite-id");
+
+ Property id_prop = pc.getIdentifierProperty();
+ Component id_mapper = pc.getIdentifierMapper();
+
+ SimpleIdMapperBuilder mapper;
+ if (id_mapper != null) {
+ mapper = new MultipleIdMapper(((Component)
pc.getIdentifier()).getComponentClassName());
+ addIdProperties(rel_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), mapper, false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ addIdProperties(orig_id_mapping, (Iterator<Property>)
id_mapper.getPropertyIterator(), null, true);
+ } else if (id_prop.isComposite()) {
+ Component id_component = (Component) id_prop.getValue();
+
+ mapper = new EmbeddedIdMapper(id_prop.getName(),
id_component.getComponentClassName());
+ addIdProperties(rel_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), mapper, false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ addIdProperties(orig_id_mapping, (Iterator<Property>)
id_component.getPropertyIterator(), null, true);
+ } else {
+ mapper = new SingleIdMapper();
+
+ addSimpleProperty(rel_id_mapping, id_prop, mapper, ModificationStore.FULL,
false);
+
+ // null mapper - the mapping where already added the first time, now we only
want to generate the xml
+ addSimpleProperty(orig_id_mapping, id_prop, null, ModificationStore.FULL,
true);
+ }
+
+ orig_id_mapping.addAttribute("name",
verEntCfg.getOriginalIdPropName());
+
+ // Adding the "revision number" property
+ Element rev_mapping = MetadataTools.addProperty(orig_id_mapping,
verEntCfg.getRevisionPropName(),
+ verEntCfg.getRevisionPropType(), true);
+ MetadataTools.addColumn(rev_mapping, verEntCfg.getRevisionPropName(), null);
+
+ return new IdMappingData(mapper, orig_id_mapping, rel_id_mapping);
+ }
+
+ private void addPersisterHack(Element class_mapping) {
+ String persisterClassName;
+ if (HibernateVersion.get().startsWith("3.3")) {
+ persisterClassName =
"org.jboss.envers.entity.VersionsInheritanceEntityPersisterFor33";
+ } else {
+ persisterClassName =
"org.jboss.envers.entity.VersionsInheritanceEntityPersisterFor32";
+ }
+
+ class_mapping.addAttribute("persister", persisterClassName);
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public Document generateFirstPass(PersistentClass pc, PersistentClassVersioningData
versioningData) {
+ Document document = DocumentHelper.createDocument();
+
+ String schema = versioningData.schema;
+ if (StringTools.isEmpty(schema)) {
+ schema = pc.getTable().getSchema();
+ }
+
+ String catalog = versioningData.catalog;
+ if (StringTools.isEmpty(catalog)) {
+ catalog = pc.getTable().getCatalog();
+ }
+
+ String entityName = pc.getEntityName();
+ String versionsEntityName = verEntCfg.getVersionsEntityName(entityName);
+ String versionsTableName = verEntCfg.getVersionsTableName(entityName,
pc.getTable().getName());
+
+ // Generating a mapping for the id
+ IdMappingData idMapper = addId(pc);
+
+ Element class_mapping;
+ ExtendedPropertyMapper propertyMapper;
+
+ InheritanceType inheritanceType = InheritanceType.get(pc);
+ String parentEntityName = null;
+
+ switch (inheritanceType) {
+ case NONE:
+ class_mapping = MetadataTools.createEntity(document, versionsEntityName,
versionsTableName,
+ schema, catalog, pc.getDiscriminatorValue());
+ propertyMapper = new MultiPropertyMapper();
+
+ // Checking if there is a discriminator column
+ if (pc.getDiscriminator() != null) {
+ Element discriminator_element =
class_mapping.addElement("discriminator");
+ addColumns(discriminator_element,
pc.getDiscriminator().getColumnIterator());
+ discriminator_element.addAttribute("type",
pc.getDiscriminator().getType().getName());
+
+ // If so, there is some inheritance scheme -> using the persister
hack.
+ addPersisterHack(class_mapping);
+ }
+
+ // Adding the id mapping
+ class_mapping.add((Element) idMapper.getXmlMapping().clone());
+
+ // Adding the "revision type" property
+ Element revTypeProperty = MetadataTools.addProperty(class_mapping,
verEntCfg.getRevisionTypePropName(),
+ verEntCfg.getRevisionTypePropType(), false);
+ revTypeProperty.addAttribute("type",
"org.jboss.envers.entity.RevisionTypeType");
+
+ break;
+ case SINGLE:
+ String extendsEntityName =
verEntCfg.getVersionsEntityName(pc.getSuperclass().getEntityName());
+ class_mapping = MetadataTools.createSubclassEntity(document,
versionsEntityName, versionsTableName,
+ schema, catalog, extendsEntityName, pc.getDiscriminatorValue());
+
+ addPersisterHack(class_mapping);
+
+ // The id and revision type is already mapped in the parent
+
+ // Getting the property mapper of the parent - when mapping properties,
they need to be included
+ parentEntityName = pc.getSuperclass().getEntityName();
+ ExtendedPropertyMapper parentPropertyMapper =
entitiesConfigurations.get(parentEntityName).getPropertyMapper();
+ propertyMapper = new SubclassPropertyMapper(new MultiPropertyMapper(),
parentPropertyMapper);
+
+ break;
+ case JOINED:
+ throw new MappingException("Joined inheritance strategy not
supported for versioning!");
+ case TABLE_PER_CLASS:
+ throw new MappingException("Table-per-class inheritance strategy not
supported for versioning!");
+ default:
+ throw new AssertionError("Impossible enum value.");
+ }
+
+ // Mapping unjoined properties
+ addProperties(class_mapping, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(), propertyMapper,
+ versioningData.propertyStoreInfo, pc.getEntityName(), true);
+
+ // Creating and mapping joins (first pass)
+ createJoins(pc, class_mapping, versioningData);
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo,
pc.getEntityName(), true);
+
+ // Storing the generated configuration
+ EntityConfiguration entityCfg = new EntityConfiguration(entityName,
versionsEntityName, idMapper,
+ propertyMapper, parentEntityName);
+ entitiesConfigurations.put(pc.getEntityName(), entityCfg);
+
+ return document;
+ }
+
+ @SuppressWarnings({"unchecked"})
+ public void generateSecondPass(PersistentClass pc, PersistentClassVersioningData
versioningData,
+ Document document) {
+ String entityName = pc.getEntityName();
+
+ CompositeMapperBuilder propertyMapper =
entitiesConfigurations.get(entityName).getPropertyMapper();
+
+ // Mapping unjoined properties
+ Element parent = document.getRootElement().element("class");
+ if (parent == null) {
+ parent = document.getRootElement().element("subclass");
+ }
+
+ addProperties(parent, (Iterator<Property>)
pc.getUnjoinedPropertyIterator(),
+ propertyMapper, versioningData.propertyStoreInfo, entityName, false);
+
+ // Mapping joins (second pass)
+ addJoins(pc, propertyMapper, versioningData.propertyStoreInfo, entityName,
false);
+ }
+
+ public Map<String, EntityConfiguration> getEntitiesConfigurations() {
+ return entitiesConfigurations;
+ }
+}
Copied: trunk/src/main/org/jboss/envers/configuration/metadata/data (from rev 102,
trunk/src/main/org/jboss/envers/metadata/data)
Modified: trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/data/IdMappingData.java 2008-08-14 10:19:45
UTC (rev 102)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/data/IdMappingData.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -19,7 +19,7 @@
*
* Red Hat Author(s): Adam Warski
*/
-package org.jboss.envers.metadata.data;
+package org.jboss.envers.configuration.metadata.data;
import org.jboss.envers.mapper.id.IdMapper;
import org.dom4j.Element;
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/data/PersistentClassVersioningData.java
===================================================================
---
trunk/src/main/org/jboss/envers/metadata/data/PersistentClassVersioningData.java 2008-08-14
10:19:45 UTC (rev 102)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/data/PersistentClassVersioningData.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -19,7 +19,7 @@
*
* Red Hat Author(s): Adam Warski
*/
-package org.jboss.envers.metadata.data;
+package org.jboss.envers.configuration.metadata.data;
import org.jboss.envers.ModificationStore;
Modified:
trunk/src/main/org/jboss/envers/configuration/metadata/data/PropertyStoreInfo.java
===================================================================
--- trunk/src/main/org/jboss/envers/metadata/data/PropertyStoreInfo.java 2008-08-14
10:19:45 UTC (rev 102)
+++
trunk/src/main/org/jboss/envers/configuration/metadata/data/PropertyStoreInfo.java 2008-08-14
11:12:23 UTC (rev 103)
@@ -19,7 +19,7 @@
*
* Red Hat Author(s): Adam Warski
*/
-package org.jboss.envers.metadata.data;
+package org.jboss.envers.configuration.metadata.data;
import org.jboss.envers.ModificationStore;
Deleted: trunk/src/main/org/jboss/envers/tools/Triple.java
===================================================================
--- trunk/src/main/org/jboss/envers/tools/Triple.java 2008-08-14 10:19:45 UTC (rev 102)
+++ trunk/src/main/org/jboss/envers/tools/Triple.java 2008-08-14 11:12:23 UTC (rev 103)
@@ -1,78 +0,0 @@
-/*
- * Envers.
http://www.jboss.org/envers
- *
- * Copyright 2008 Red Hat Middleware, LLC. All rights reserved.
- *
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT A WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License, v.2.1 along with this distribution; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * Red Hat Author(s): Adam Warski
- */
-package org.jboss.envers.tools;
-
-/**
- * A triple of objects.
- * @param <T1>
- * @param <T2>
- * @param <T3>
- * @author Adam Warski (adamw(a)aster.pl)
- */
-public class Triple<T1, T2, T3> {
- private T1 obj1;
- private T2 obj2;
- private T3 obj3;
-
- public Triple(T1 obj1, T2 obj2, T3 obj3) {
- this.obj1 = obj1;
- this.obj2 = obj2;
- this.obj3 = obj3;
- }
-
- public T1 getFirst() {
- return obj1;
- }
-
- public T2 getSecond() {
- return obj2;
- }
-
- public T3 getThird() {
- return obj3;
- }
-
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof Triple)) return false;
-
- Triple triple = (Triple) o;
-
- if (obj1 != null ? !obj1.equals(triple.obj1) : triple.obj1 != null) return
false;
- if (obj2 != null ? !obj2.equals(triple.obj2) : triple.obj2 != null) return
false;
- if (obj3 != null ? !obj3.equals(triple.obj3) : triple.obj3 != null) return
false;
-
- return true;
- }
-
- public int hashCode() {
- int result;
- result = (obj1 != null ? obj1.hashCode() : 0);
- result = 31 * result + (obj2 != null ? obj2.hashCode() : 0);
- result = 31 * result + (obj3 != null ? obj3.hashCode() : 0);
- return result;
- }
-
- public static <T1, T2, T3> Triple<T1, T2, T3> make(T1 obj1, T2 obj2, T3
obj3) {
- return new Triple<T1, T2, T3>(obj1, obj2, obj3);
- }
-}
\ No newline at end of file