Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 Clarion Media, LLC
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.clarionmedia.infinitum.orm.persistence;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 

Provides a runtime resolution policy for model persistence based on the Infinitum configuration. There are two types of persistence policies: annotation and XML.

Domain classes should be individually registered in infinitum.cfg.xml using <model resource="com.foo.domain.MyModel" /> in the domain element.

 
 public abstract class PersistencePolicy {

Used to indicate an entity's cascade mode. All means every related entity is cascaded, None means no related entities are cascaded, and Keys means only foreign keys will be cascaded.
 
 	public static enum Cascade {
 		ALL, NONE, KEYS
 	};
 
 	// This Map caches which fields are persistent
 	protected Map<Class<?>, List<Field>> mPersistenceCache;
 
 	// This Map caches the field-column map
 	protected Map<FieldStringmColumnCache;
 
 	// This Map caches the primary key Field for each persistent class
 	protected Map<Class<?>, FieldmPrimaryKeyCache;
 
 	// This Map caches the "nullability" of Fields
 
 	// This Map caches the uniqueness of Fields
 
 	// This Map caches the many-to-many relationships
 
 	// This Map caches the many-to-one relationships
 
 	// This Map caches the one-to-many relationships
 
 	// This Map caches the one-to-one relationships
 
 	// This Map caches the lazy-loading status for each persistent class
	// This Map caches the endpoint names for models
	// This Map caches the endpoint field names for model Fields
	protected Logger mLogger;

Constructs a new PersistencePolicy.
	public PersistencePolicy() {
	}

Indicates if the given Class is persistent or transient.

Parameters:
c the Class to check persistence for
Returns:
true if persistent, false if transient
	public abstract boolean isPersistent(Class<?> c);

Retrieves the name of the database table for the specified Class. If the Class is transient, this method will return null.

Parameters:
c the Class to retrieve the table name for
Returns:
the name of the database table for the specified domain model Class
Throws:
java.lang.IllegalArgumentException if the given Class is transient
com.clarionmedia.infinitum.orm.exception.InvalidMapFileException if the map file for the given Class is invalid
	public abstract String getModelTableName(Class<?> c)
Retrieves a List of all persistent Fields for the given Class.

Parameters:
c the Class to retrieve persistent Fields for
Returns:
List of all persistent Fields for the specified Class
	public abstract List<FieldgetPersistentFields(Class<?> c);

Retrieves the primary key Field for the given Class.

Parameters:
c the Class to retrieve the primary key Field for
Returns:
the primary key Field for the specified Class
Throws:
com.clarionmedia.infinitum.orm.exception.ModelConfigurationException if multiple primary keys are declared in c
	public abstract Field getPrimaryKeyField(Class<?> c)
Retrieves the name of the database column the specified Field maps to.

Parameters:
f the Field to retrieve the column for
Returns:
the name of the column
	public abstract String getFieldColumnName(Field f);

Determines if the given java.lang.reflect.Field is a primary key.

Parameters:
f the Field to check
Returns:
true if it is a primary key, false if it's not
	public abstract boolean isFieldPrimaryKey(Field f);

Determines if the given primary key java.lang.reflect.Field is set to autoincrement. This method assumes, as a precondition, that the Field being passed is guaranteed to be a primary key, whether implicitly or explicitly.

Parameters:
f the primary key Field to check if it's set to autoincrement
Returns:
true if it is set to autoincrement, false if it's not
Throws:
com.clarionmedia.infinitum.exception.InfinitumRuntimeException if an explicit primary key that is set to autoincrement is not of type int or long
	public abstract boolean isPrimaryKeyAutoIncrement(Field f)
Checks if the specified Field's associated column is nullable.

Parameters:
f the Field to check if nullable
Returns:
true if the field is nullable, false if it is not nullable
	public abstract boolean isFieldNullable(Field f);

Checks if the specified Field is unique, meaning each record must have a different value in the table. This is a way of implementing a unique constraint on a column.

Parameters:
f the Field to check for uniqueness
Returns:
true if it is unique, false if not
	public abstract boolean isFieldUnique(Field f);

Retrieves a java.util.Set of all com.clarionmedia.infinitum.orm.relationship.ManyToManyRelationship instances for the given java.lang.Class.

Parameters:
c the Class to get relationships for
Returns:
Set of all many-to-many relationships
			Class<?> c);

Retrieves the PersistencePolicy.Cascade mode for the given persistent java.lang.Class .

Parameters:
c the Class to retrieve Cascade mode for
Returns:
Cascade mode
	public abstract Cascade getCascadeMode(Class<?> c);

Indicates if the given persistent java.lang.reflect.Field is part of an entity relationship, either many-to-many, many-to-one, one-to-many, or one-to-one.

Parameters:
f the Field to check
Returns:
true if it is part of a relationship, false if not
	public abstract boolean isRelationship(Field f);

Indicates if the given persistent java.lang.reflect.Field is part of a many-to-many entity relationship.

Parameters:
f the Field to check
Returns:
true if it is part of a many-to-many relationship, false if not
	public abstract boolean isManyToManyRelationship(Field f);

Indicates if the given persistent java.lang.reflect.Field is part of a one-to-one entity relationship.

Parameters:
f the Field to check
Returns:
true if it is part of a one-to-one relationship, false if not
	public abstract boolean isOneToOneRelationship(Field f);

Indicates if the given persistent java.lang.reflect.Field is part of a many-to-one or one-to-one entity relationship.

Parameters:
f the Field to check
Returns:
true if it is part of a many-to-one or one-to-one relationship, false if not
	public abstract boolean isToOneRelationship(Field f);

Retrieves the com.clarionmedia.infinitum.orm.relationship.ModelRelationship the given java.lang.reflect.Field is a part of.

Parameters:
f the Field to retrieve the relationship for
Returns:
the ModelRelationship for f or null if there is none
	public abstract ModelRelationship getRelationship(Field f);

Retrieves the java.lang.reflect.Field pertaining to the given com.clarionmedia.infinitum.orm.relationship.ModelRelationship for the specified java.lang.Class. If no such Field exists, null is returned.

Parameters:
c the Class to retrieve the Field from
rel the ModelRelationship to retrieve the Field for
Returns:
Field pertaining to the relationship or null
	public abstract Field findRelationshipField(Class<?> c,
Indicates if the given persistent java.lang.Class has lazy loading enabled or not.

Parameters:
c the Class to check lazy-loading status
Returns:
true if lazy loading is enabled, false if not
	public abstract boolean isLazy(Class<?> c);

Retrieves the REST endpoint name for the given persistent java.lang.Class.

Parameters:
c the Class to retrieve the REST endpoint name for
Returns:
endpoint name
Throws:
java.lang.IllegalArgumentException if the given Class is not a domain model or persistent
	public abstract String getRestEndpoint(Class<?> c)
Retrieves the REST endpoint field name for the given persistent java.lang.reflect.Field.

Parameters:
f the Field to retrieve the endpoint field name for
Returns:
endpoint field name
Throws:
java.lang.IllegalArgumentException if the containing java.lang.Class of the given Field is transient or if the Field itself is marked transient
	public abstract String getEndpointFieldName(Field f)
Finds the persistent java.lang.reflect.Field for the given java.lang.Class which has the specified name. Returns null if no such Field exists.

Parameters:
c the Class containing the Field
name the name of the Field to retrieve
Returns:
Field with specified name
	public Field findPersistentField(Class<?> cString name) {
		for (Field f : fields) {
			if (f.getName().equalsIgnoreCase(name)) {
				return f;
			}
		}
		return null;
	}

Retrieves a List of all unique Fields for the given Class.

Parameters:
c the Class to retrieve unique Fields for
Returns:
List of all unique Fields for the specified Class
	public List<FieldgetUniqueFields(Class<?> c) {
		if (!isPersistent(c))
			throw new IllegalArgumentException("Class '" + c.getName()
"' is transient.");
		List<Fieldret = new ArrayList<Field>();
		for (Field f : fields) {
				ret.add(f);
			else {
				boolean unique = isFieldUnique(f);
				if (unique)
					ret.add(f);
			}
		}
		return ret;
	}

Calculates a hash code for the specified persistent model based on its java.lang.Class and primary key.

Parameters:
model the model entity to compute the hash for
Returns:
hash code for the model
	public int computeModelHash(Object model) {
		Serializable pk = null;
		try {
catch (ClassCastException e) {
			throw new ModelConfigurationException("Invalid primary key specified for '" + model.getClass().getName() + "'.");
		}
		return computeModelHash(model.getClass(), pk);
	}

Calculates a hash code based on the given java.lang.Class and primary key.

Parameters:
c the Class to compute the hash for
pk the primary key to compute hash for
Returns:
hash code
	public int computeModelHash(Class<?> cSerializable pk) {
		final int PRIME = 31;
		int hash = 7;
		hash *= PRIME + c.hashCode();
		hash *= PRIME + pk.hashCode();
		return hash;
	}

Retrieves the primary key value for the given persistent model.

Parameters:
model the model to retrieve the primary key for
Returns:
primary key value
	public Serializable getPrimaryKey(Object model) {
		if (AbstractProxy.isAopProxy(model)) {
			model = AbstractProxy.getProxy(model).getTarget();
		}
		Field pkField = getPrimaryKeyField(model.getClass());
		return (Serializable.getFieldValue(modelpkField);
	}

Indicates if the primary key java.lang.reflect.Field for the given model is 0 or null.

Parameters:
model the model to check the primary key value for
Returns:
true if it is 0 or null, false if not
	public boolean isPKNullOrZero(Object model) {
		if (pk == null)
			return true;
		if (pk instanceof Integer)
			return (((Integerpk) == 0);
		else if (pk instanceof Long)
			return (((Longpk) == 0);
		else if (pk instanceof Float)
			return (((Floatpk) == 0);
		else if (pk instanceof Double)
			return (((Doublepk) == 0);
		return false;
	}

Returns the many-to-many relationship cache.

Returns:
many-to-many cache
	}
	protected Field findPrimaryKeyField(Class<?> c) {
		for (Field f : fields) {
			if (f.getName().equals("mId") || f.getName().equals("mID")
				return f;
		}
		return null;
	}
New to GrepCode? Check out our FAQ X