Author: tomdz
Date: Tue Feb 14 15:34:18 2006
New Revision: 377885
URL: http://svn.apache.org/viewcvs?rev=377885&view=rev
Log:
Added check to the xdoclet module for whether the autoincrement value within
inheritance hierarchies via super-references over the pk
Modified:
db/ojb/branches/OJB_1_0_RELEASE/lib/xdoclet-ojb-module-1.2.3.jar
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/test/xdoclet/modules/ojb/tests/ModifyInheritedTagPrimarykeyAttributeTests.java
Modified: db/ojb/branches/OJB_1_0_RELEASE/lib/xdoclet-ojb-module-1.2.3.jar
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/lib/xdoclet-ojb-module-1.2.3.jar?rev=377885&r1=377884&r2=377885&view=diff
==============================================================================
Binary files - no diff available.
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java?rev=377885&r1=377884&r2=377885&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
Tue Feb 14 15:34:18 2006
@@ -1,6 +1,6 @@
package xdoclet.modules.ojb.constraints;
-/* Copyright 2004-2005 The Apache Software Foundation
+/* Copyright 2004-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,8 @@
import java.util.Collection;
import java.util.Iterator;
-import org.apache.commons.collections.SequencedHashMap;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.map.ListOrderedMap;
import xdoclet.modules.ojb.CommaListIterator;
import xdoclet.modules.ojb.LogHelper;
@@ -35,7 +36,7 @@
* Checks constraints that span deal with parts of the model, not just with
one class.
* This for instance means relationships (collections, references).
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Dudziak ([EMAIL
PROTECTED])</a>
+ * @author Thomas Dudziak
*/
public class ModelConstraints extends ConstraintsBase
{
@@ -52,6 +53,7 @@
checkReferenceForeignkeys(modelDef, checkLevel);
checkCollectionForeignkeys(modelDef, checkLevel);
checkKeyModifications(modelDef, checkLevel);
+ checkAutoIncrementedPKs(modelDef);
}
/**
@@ -202,7 +204,7 @@
ClassDescriptorDef elementClassDef =
modelDef.getClass(elementClassName);
String fkFieldNames =
collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
ArrayList missingFields = new ArrayList();
- SequencedHashMap fkFields = new SequencedHashMap();
+ ListOrderedMap fkFields = new ListOrderedMap();
// first we gather all field names
for (CommaListIterator it = new CommaListIterator(fkFieldNames);
it.hasNext();)
@@ -256,13 +258,12 @@
*/
private void ensurePKsFromHierarchy(ClassDescriptorDef classDef) throws
ConstraintException
{
- SequencedHashMap pks = new SequencedHashMap();
+ ListOrderedMap pks = new ListOrderedMap();
for (Iterator it = classDef.getAllExtentClasses(); it.hasNext();)
{
ClassDescriptorDef subTypeDef = (ClassDescriptorDef)it.next();
-
- ArrayList subPKs = subTypeDef.getPrimaryKeys();
+ ArrayList subPKs = subTypeDef.getPrimaryKeys();
// check against already present PKs
for (Iterator pkIt = subPKs.iterator(); pkIt.hasNext();)
@@ -288,7 +289,84 @@
ensureFields(classDef, pks.values());
}
-
+
+ /**
+ * Checks that the autoincrement status of a pk field within a hierarchy
of class descriptors
+ * connected by super-references over the primary key fields does not
change from 'ojb' to some
+ * other value.
+ *
+ * @param classDef The root of the hierarchy
+ * @throws ConstraintException If there is autoincrement change in a
hierarchy
+ */
+ private void checkAutoIncrementedPKs(ModelDef modelDef) throws
ConstraintException
+ {
+ for (Iterator it = modelDef.getClasses(); it.hasNext();)
+ {
+ checkAutoIncrementedPKs((ClassDescriptorDef)it.next());
+ }
+ }
+
+ /**
+ * Checks that the autoincrement status of a pk field within a hierarchy
of class descriptors
+ * connected by super-references over the primary key fields does not
change from 'ojb' to some
+ * other value.
+ *
+ * @param classDef The root of the hierarchy
+ * @throws ConstraintException If there is autoincrement change in the
hierarchy
+ */
+ private void checkAutoIncrementedPKs(ClassDescriptorDef classDef) throws
ConstraintException
+ {
+ ArrayList pks = classDef.getPrimaryKeys();
+
+ for (Iterator it = pks.iterator(); it.hasNext();)
+ {
+ FieldDescriptorDef fieldDef = (FieldDescriptorDef)it.next();
+ String autoIncr =
fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT);
+
+ if (!"ojb".equals(autoIncr))
+ {
+ it.remove();
+ }
+ }
+ if (!pks.isEmpty())
+ {
+ ArrayList queue = new ArrayList();
+
+ CollectionUtils.addAll(queue, classDef.getExtentClasses());
+ while (!queue.isEmpty())
+ {
+ ClassDescriptorDef subTypeDef =
(ClassDescriptorDef)queue.get(0);
+
+ queue.remove(0);
+ if (subTypeDef.getReference("super") != null)
+ {
+ if
(subTypeDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_REPOSITORY_INFO,
true))
+ {
+ // sub type has a repository mapping, so check its the
pk fields
+ for (Iterator pkIt = pks.iterator(); pkIt.hasNext();)
+ {
+ FieldDescriptorDef baseFieldDef =
(FieldDescriptorDef)pkIt.next();
+ FieldDescriptorDef subFieldDef =
subTypeDef.getField(baseFieldDef.getName());
+
+ if ((subFieldDef != null) &&
(subFieldDef.getOriginal() == null))
+ {
+ String autoIncr =
subFieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT);
+
+ if ("ojb".equals(autoIncr))
+ {
+ throw new ConstraintException("Both the
base type "+classDef.getQualifiedName()+" and its subtype "+
+
subTypeDef.getQualifiedName()+" specify 'ojb' for the autoincrement primary key
field "+
+
baseFieldDef.getName()+" which will lead to problems. Please remove the
specification at the subtype.");
+ }
+ }
+ }
+ }
+ CollectionUtils.addAll(queue,
subTypeDef.getExtentClasses());
+ }
+ }
+ }
+ }
+
/**
* Ensures that the specified fields are present in the given class.
*
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/test/xdoclet/modules/ojb/tests/ModifyInheritedTagPrimarykeyAttributeTests.java
URL:
http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/test/xdoclet/modules/ojb/tests/ModifyInheritedTagPrimarykeyAttributeTests.java?rev=377885&r1=377884&r2=377885&view=diff
==============================================================================
---
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/test/xdoclet/modules/ojb/tests/ModifyInheritedTagPrimarykeyAttributeTests.java
(original)
+++
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/test/xdoclet/modules/ojb/tests/ModifyInheritedTagPrimarykeyAttributeTests.java
Tue Feb 14 15:34:18 2006
@@ -1,6 +1,6 @@
package xdoclet.modules.ojb.tests;
-/* Copyright 2003-2005 The Apache Software Foundation
+/* Copyright 2004-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@
/**
* Tests for the ojb.modify-inherited tag with the primarykey attribute.
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Thomas Dudziak ([EMAIL
PROTECTED])</a>
+ * @author Thomas Dudziak
*/
public class ModifyInheritedTagPrimarykeyAttributeTests extends OjbTestBase
{
@@ -516,6 +516,51 @@
" * primarykey=\"false\"\n"+
" */\n"+
"public class C extends B {}\n");
+
+ assertNull(runOjbXDoclet(OJB_DEST_FILE));
+ assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
+ }
+
+ // Test: primarykey in baseclass and in subclass (via super-ref) both have
autoincrement=true
+ public void testPrimarykey13()
+ {
+ addClass(
+ "test.A",
+ "package test;\n"+
+ "/** @ojb.class */\n" +
+ "public class A {\n"+
+ " /** @ojb.field primarykey=\"true\"\n" +
+ " * autoincrement=\"ojb\"\n"+
+ " */\n"+
+ " private int id;\n"+
+ "}\n");
+ addClass(
+ "test.B",
+ "package test;\n"+
+ "/** @ojb.class\n" +
+ " * @ojb.reference class-ref=\"test.A\"\n"+
+ " * auto-retrieve=\"true\"\n"+
+ " * auto-update=\"true\"\n"+
+ " * auto-delete=\"true\"\n"+
+ " * foreignkey=\"id\"\n"+
+ " */\n"+
+ "public class B extends A {}\n");
+ addClass(
+ "test.C",
+ "package test;\n"+
+ "/** @ojb.class\n" +
+ " * @ojb.reference class-ref=\"test.B\"\n"+
+ " * auto-retrieve=\"true\"\n"+
+ " * auto-update=\"true\"\n"+
+ " * auto-delete=\"true\"\n"+
+ " * foreignkey=\"id\"\n"+
+ " */\n"+
+ "public class C extends B {\n"+
+ " /** @ojb.field primarykey=\"true\"\n" +
+ " * autoincrement=\"ojb\"\n"+
+ " */\n"+
+ " private int id;\n"+
+ "}\n");
assertNull(runOjbXDoclet(OJB_DEST_FILE));
assertNull(runTorqueXDoclet(TORQUE_DEST_FILE, "ojbtest"));
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]