Author: aadamchik
Date: Sun Sep 10 15:54:55 2006
New Revision: 442027
URL: http://svn.apache.org/viewvc?view=rev&rev=442027
Log:
removing validation delegate methods - they are not needed in JPA and are
optional in Cayenne core
Modified:
incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectDelegate.java
Modified:
incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectDelegate.java
URL:
http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectDelegate.java?view=diff&rev=442027&r1=442026&r2=442027
==============================================================================
---
incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectDelegate.java
(original)
+++
incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/enhancer/DataObjectDelegate.java
Sun Sep 10 15:54:55 2006
@@ -17,35 +17,18 @@
* under the License.
****************************************************************/
-
package org.apache.cayenne.jpa.enhancer;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
import org.apache.cayenne.CayenneRuntimeException;
import org.apache.cayenne.DataObject;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.access.DataContext;
-import org.apache.cayenne.access.DataNode;
-import org.apache.cayenne.access.types.ExtendedTypeMap;
-import org.apache.cayenne.map.DbAttribute;
-import org.apache.cayenne.map.DbJoin;
-import org.apache.cayenne.map.DbRelationship;
-import org.apache.cayenne.map.EntityResolver;
-import org.apache.cayenne.map.ObjAttribute;
import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
import org.apache.cayenne.property.ClassDescriptor;
import org.apache.cayenne.property.Property;
import org.apache.cayenne.property.PropertyUtils;
-import org.apache.cayenne.validation.BeanValidationFailure;
-import org.apache.cayenne.validation.ValidationFailure;
-import org.apache.cayenne.validation.ValidationResult;
/**
* A static delegate for DataObject callbacks. To obtain class descriptors,
@@ -55,8 +38,6 @@
*
* @author Andrus Adamchik
*/
-// TODO: andrus, 5/2/2006 - going ahead, the delegate should only impement the
validation
-// methods, as property access by the framework should be done via
ClassDescriptor.
public final class DataObjectDelegate {
public static void beforeGetProperty(Persistent object, String
propertyName) {
@@ -103,14 +84,6 @@
return PropertyUtils.getProperty(object, path);
}
- /**
- * @since 1.1
- * @deprecated since 1.2 use 'getObjectContext().prepareForAccess(object)'
- */
- public static void resolveFault(DataObject object) {
- throw new UnsupportedOperationException("Not supported");
- }
-
public static Object readProperty(DataObject object, String property) {
return getPropertyDescriptor(object, property).readProperty(object);
}
@@ -171,12 +144,6 @@
throw new UnsupportedOperationException("Not supported");
}
- public static void validateForInsert(
- DataObject object,
- ValidationResult validationResult) {
- validateForSave(object, validationResult);
- }
-
/**
* Returns a Cayenne ClassDescriptor for the enhanced DataObject class. To
obtain
* class descriptors, uses a DataObject context first, and then falls back
to the
@@ -239,157 +206,4 @@
return p;
}
-
- public static void validateForUpdate(
- DataObject object,
- ValidationResult validationResult) {
- validateForSave(object, validationResult);
- }
-
- public static void validateForDelete(
- DataObject object,
- ValidationResult validationResult) {
- // does nothing
- }
-
- protected static void validateForSave(
- DataObject object,
- ValidationResult validationResult) {
-
- EntityResolver resolver =
object.getObjectContext().getEntityResolver();
- ObjEntity objEntity = resolver.lookupObjEntity(object);
- if (objEntity == null) {
- throw new CayenneRuntimeException(
- "No ObjEntity mapping found for DataObject "
- + object.getClass().getName());
- }
-
- DataNode node =
getDataContext(object).getParentDataDomain().lookupDataNode(
- objEntity.getDataMap());
- if (node == null) {
- throw new CayenneRuntimeException("No DataNode found for
objEntity: "
- + objEntity.getName());
- }
-
- ExtendedTypeMap types = node.getAdapter().getExtendedTypes();
-
- // validate mandatory attributes
-
- // handling a special case - meaningful mandatory FK... defer failures
until
- // relationship validation is done... This is just a temporary
solution, as
- // handling meaningful keys within the object lifecycle requires
something more,
- // namely read/write methods for relationships and direct values
should be
- // synchronous with each other..
- Map failedDbAttributes = null;
-
- ClassDescriptor descriptor =
resolver.getClassDescriptor(objEntity.getName());
- Iterator attributes = objEntity.getAttributes().iterator();
- while (attributes.hasNext()) {
- ObjAttribute objAttribute = (ObjAttribute) attributes.next();
- DbAttribute dbAttribute = objAttribute.getDbAttribute();
-
- Object value = descriptor
- .getDeclaredProperty(objAttribute.getName())
- .readPropertyDirectly(object);
- if (dbAttribute.isMandatory()) {
- ValidationFailure failure =
BeanValidationFailure.validateNotNull(
- object,
- objAttribute.getName(),
- value);
-
- if (failure != null) {
-
- if (failedDbAttributes == null) {
- failedDbAttributes = new HashMap();
- }
-
- failedDbAttributes.put(dbAttribute.getName(), failure);
- continue;
- }
- }
-
- if (value != null) {
-
- // TODO: should we pass null values for validation as well?
- // if so, class can be obtained from ObjAttribute...
-
- types.getRegisteredType(value.getClass()).validateProperty(
- object,
- objAttribute.getName(),
- value,
- dbAttribute,
- validationResult);
- }
- }
-
- // validate mandatory relationships
- Iterator relationships = objEntity.getRelationships().iterator();
- while (relationships.hasNext()) {
- ObjRelationship relationship = (ObjRelationship)
relationships.next();
-
- if (relationship.isSourceIndependentFromTargetChange()) {
- continue;
- }
-
- List dbRels = relationship.getDbRelationships();
- if (dbRels.isEmpty()) {
- // Wha?
- continue;
- }
-
- // if db relationship is not based on a PK and is based on
mandatory
- // attributes, see if we have a target object set
- boolean validate = true;
- DbRelationship dbRelationship = (DbRelationship) dbRels.get(0);
- Iterator joins = dbRelationship.getJoins().iterator();
- while (joins.hasNext()) {
- DbJoin join = (DbJoin) joins.next();
- DbAttribute source = join.getSource();
-
- if (source.isMandatory()) {
- // clear attribute failures...
- if (failedDbAttributes != null &&
!failedDbAttributes.isEmpty()) {
- failedDbAttributes.remove(source.getName());
-
- // loop through all joins if there were previous
mandatory
-
- // attribute failures....
- if (!failedDbAttributes.isEmpty()) {
- continue;
- }
- }
- }
- else {
- // do not validate if the relation is based on
- // multiple keys with some that can be nullable.
- validate = false;
- }
- }
-
- if (validate) {
- Object value = descriptor
- .getDeclaredProperty(relationship.getName())
- .readPropertyDirectly(object);
- ValidationFailure failure =
BeanValidationFailure.validateNotNull(
- object,
- relationship.getName(),
- value);
-
- if (failure != null) {
- validationResult.addFailure(failure);
- continue;
- }
- }
-
- }
-
- // deal with previously found attribute failures...
- if (failedDbAttributes != null && !failedDbAttributes.isEmpty()) {
- Iterator failedAttributes = failedDbAttributes.values().iterator();
- while (failedAttributes.hasNext()) {
- validationResult.addFailure((ValidationFailure)
failedAttributes.next());
- }
- }
- }
-
}