pl.edu.agh.cast.data.model.general
Class ManyToManyRelation

java.lang.Object
  extended by pl.edu.agh.cast.data.model.property.PropertyContainer
      extended by pl.edu.agh.cast.data.model.AbstractElement
          extended by pl.edu.agh.cast.data.model.general.ManyToManyRelation
All Implemented Interfaces:
Serializable, Observer, IDisposable, IReplicable, IValidatable, IDomainElement, IGeneralElement, IManyToManyRelation, Identifiable, IElement, IPropertyContainer
Direct Known Subclasses:
OneToOneRelation, TimedManyToManyRelation

public class ManyToManyRelation
extends AbstractElement
implements IManyToManyRelation

Default implementation of IManyToManyRelation interface.

All public constructors set the element type to IManyToManyRelation.TYPE. Protected constructors should only be used by direct subclasses to provide the concrete type to the super constructor.

Author:
AGH CAST Team
See Also:
IManyToManyRelation, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface pl.edu.agh.cast.data.model.general.IManyToManyRelation
IManyToManyRelation.Properties
 
Field Summary
 
Fields inherited from interface pl.edu.agh.cast.data.model.general.IManyToManyRelation
TYPE
 
Constructor Summary
  ManyToManyRelation()
          Default constructor.
  ManyToManyRelation(IGeneralDataSet<? extends IGeneralElement> dataSet, List<? extends IEntity> sources, List<? extends IEntity> targets, boolean directed)
          Creates new many-to-many relation with given data set, source and target entities, direction and newly-generated ID.
protected ManyToManyRelation(Type type)
          Initializes element with type.
protected ManyToManyRelation(Type type, UUID id, IGeneralDataSet<? extends IGeneralElement> dataSet, List<? extends IEntity> sources, List<? extends IEntity> targets, boolean directed)
          Initializes element with type, ID, data set, source and target entities lists and direction flag.
protected ManyToManyRelation(Type type, UUID id, List<? extends IEntity> sources, List<? extends IEntity> targets, boolean directed)
          Initializes element with type, ID, source and target entities lists and direction flag.
  ManyToManyRelation(UUID id, IGeneralDataSet<? extends IGeneralElement> dataSet, List<? extends IEntity> sources, List<? extends IEntity> targets, boolean directed)
          Creates new many-to-many relation with given ID, data set, source and target entities and direction.
  ManyToManyRelation(UUID id, List<? extends IEntity> sources, List<? extends IEntity> targets, boolean directed)
          Creates new many-to-many relation with given ID, source and target entities and direction.
 
Method Summary
 void addSourceEntitiy(IEntity entity)
          Adds a source entity associated with the relation.
 void addTargetEntitiy(IEntity entity)
          Adds a target entity associated with the relation.
protected  String getDescString()
          Returns a string with short description of element.
 List<? extends IEntity> getSourceEntities()
          Returns source entities associated with the relation.
 IEntity getSourceEntitiy(int index)
          Gets source entity associated with the relation with given index in the list.
 List<? extends IEntity> getTargetEntities()
          Returns target entities associated with the relation.
 IEntity getTargetEntitiy(int index)
          Gets target entity associated with the relation with given index in the list.
 Boolean isDirected()
          Checks if given relation is directed (from source to target entities) or not (in that case it is bi-directional).
protected  boolean isEntityListValid(List<? extends IEntity> entities)
          Checks if given list of entities is valid for the relation's source and target entities list.
protected  boolean isEntityValid(IEntity entity)
          Checks if given entity is valid for the relation's source and target entities list.
protected  boolean isSourceEntitiesResolved()
          Checks if the list of source entities is resolved (loaded).
protected  boolean isSourceEntityListValid(List<? extends IEntity> entities)
          Checks if given list of entities is valid for the relation's source entities list.
protected  boolean isTargetEntitiesResolved()
          Checks if the list of target entities is resolved (loaded).
protected  boolean isTargetEntityListValid(List<? extends IEntity> entities)
          Checks if given list of entities is valid for the relations source and target entities list.
 boolean removeSourceEntitiy(IEntity entity)
          Removes the specified source entity associated with the relation.
 boolean removeTargetEntitiy(IEntity entity)
          Removes the specified target entity associated with the relation.
 ManyToManyRelation replicate()
          Returns a replicated instance of this instance.
 ManyToManyRelation replicateTo(Object replica)
          Replicates this instance state to given replica object.
 void setDirected(Boolean directed)
          Sets the directed flag.
 void setSourceEntities(List<? extends IEntity> entities)
          Sets source entities associated with the relation.
 void setTargetEntities(List<? extends IEntity> entities)
          Sets target entities associated with the relation.
 
Methods inherited from class pl.edu.agh.cast.data.model.AbstractElement
afterSetMetaPropertyManager, canReplicateTo, checkIfValid, equals, getDataSet, getId, getType, hashCode, isValid, setDataSet, setId, setType, toString
 
Methods inherited from class pl.edu.agh.cast.data.model.property.PropertyContainer
addProperty, createProperty, dispose, getCustomProperties, getMetaPropertyManager, getPermanentProperties, getProperties, getProperty, getTransientProperties, initializeProperties, matches, removeProperty, setMetaPropertyManager, setProperty, update
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface pl.edu.agh.cast.data.model.IElement
getDataSet
 
Methods inherited from interface pl.edu.agh.cast.data.model.Identifiable
getId, getType
 
Methods inherited from interface pl.edu.agh.cast.data.model.property.IPropertyContainer
addProperty, getCustomProperties, getMetaPropertyManager, getPermanentProperties, getProperties, getProperty, getTransientProperties, matches, removeProperty, setProperty
 
Methods inherited from interface java.util.Observer
update
 
Methods inherited from interface pl.edu.agh.cast.common.IValidatable
isValid
 
Methods inherited from interface pl.edu.agh.cast.common.IDisposable
dispose
 

Constructor Detail

ManyToManyRelation

public ManyToManyRelation()
Default constructor.

Usage of this constructor is discouraged, since it does not provide any integrity control. If, however, this constructor is used, the AbstractElement.isValid() method should be called in order to check the integrity.


ManyToManyRelation

public ManyToManyRelation(UUID id,
                          List<? extends IEntity> sources,
                          List<? extends IEntity> targets,
                          boolean directed)
Creates new many-to-many relation with given ID, source and target entities and direction.

If the sources or targets list is an instance of IReferenceList then it is used internally, otherwise all entities from this list are added to internal collection.

Parameters:
id - ID of the element
sources - list of source entities - must contain at least one reference
targets - list of target entities - must contain at least one reference
directed - the flag indicating whether the relation is directed or not (see IManyToManyRelation.isDirected())

ManyToManyRelation

public ManyToManyRelation(IGeneralDataSet<? extends IGeneralElement> dataSet,
                          List<? extends IEntity> sources,
                          List<? extends IEntity> targets,
                          boolean directed)
Creates new many-to-many relation with given data set, source and target entities, direction and newly-generated ID.

If the sources or targets list is an instance of IReferenceList then it is used internally, otherwise all entities from this list are added to internal collection.

Parameters:
dataSet - the general data set this relation is in
sources - list of source entities - must contain at least one reference
targets - list of target entities - must contain at least one reference
directed - the flag indicating whether the relation is directed or not (see IManyToManyRelation.isDirected())

ManyToManyRelation

public ManyToManyRelation(UUID id,
                          IGeneralDataSet<? extends IGeneralElement> dataSet,
                          List<? extends IEntity> sources,
                          List<? extends IEntity> targets,
                          boolean directed)
Creates new many-to-many relation with given ID, data set, source and target entities and direction.

If the sources or targets list is an instance of IReferenceList then it is used internally, otherwise all entities from this list are added to internal collection.

Parameters:
id - ID of the element
dataSet - the general data set this relation is in
sources - list of source entities - must contain at least one reference
targets - list of target entities - must contain at least one reference
directed - the flag indicating whether the relation is directed or not (see IManyToManyRelation.isDirected())

ManyToManyRelation

protected ManyToManyRelation(Type type)
Initializes element with type.

This constructor should be used only by extending classes to implement the default constructor. The incoming and outgoing relation lists are initialized with empty lists.

Parameters:
type - type of the element
See Also:
AbstractElement.AbstractElement(Type)

ManyToManyRelation

protected ManyToManyRelation(Type type,
                             UUID id,
                             List<? extends IEntity> sources,
                             List<? extends IEntity> targets,
                             boolean directed)
Initializes element with type, ID, source and target entities lists and direction flag.

Parameters:
type - type of element which extends this one
id - ID of the element
sources - list of source entities - must contain at least one reference
targets - list of target entities - must contain at least one reference
directed - the flag indicating whether the relation is directed or not (see IManyToManyRelation.isDirected())

ManyToManyRelation

protected ManyToManyRelation(Type type,
                             UUID id,
                             IGeneralDataSet<? extends IGeneralElement> dataSet,
                             List<? extends IEntity> sources,
                             List<? extends IEntity> targets,
                             boolean directed)
Initializes element with type, ID, data set, source and target entities lists and direction flag.

Parameters:
type - type of element which extends this one
id - ID of the element
dataSet - the general data set this relation is in
sources - list of source entities - must contain at least one reference
targets - list of target entities - must contain at least one reference
directed - the flag indicating whether the relation is directed or not (see IManyToManyRelation.isDirected())
Method Detail

isEntityListValid

protected boolean isEntityListValid(List<? extends IEntity> entities)
                             throws ModelException
Checks if given list of entities is valid for the relation's source and target entities list.

This method is called by the setSourceEntities(List) and {setTargetEntities(List) methods. By default it always returns true , however it can be overridden in order to define new validation rules. In such case the implementation from base class should be called inside the overridden method in order to preserve data consistency.

Parameters:
entities - the entities list to validate
Returns:
true if the entity list is valid, false otherwise
Throws:
ModelException - if there is a severe validity violation

isSourceEntityListValid

protected boolean isSourceEntityListValid(List<? extends IEntity> entities)
                                   throws ModelException
Checks if given list of entities is valid for the relation's source entities list.

This method is called by the setSourceEntities(List) method. By default it always returns true , however it can be overridden in order to define new validation rules. In such case the implementation from base class should be called inside the overridden method in order to preserve data consistency.

Parameters:
entities - the entities list to validate
Returns:
true if the entity list is valid for source entities, false otherwise
Throws:
ModelException - if there is a severe validity violation

isTargetEntityListValid

protected boolean isTargetEntityListValid(List<? extends IEntity> entities)
                                   throws ModelException
Checks if given list of entities is valid for the relations source and target entities list.

This method is called by the {setTargetEntities(List) method. By default it always returns true , however it can be overridden in order to define new validation rules. In such case the implementation from base class should be called inside the overridden method in order to preserve data consistency.

Parameters:
entities - the entities list to validate
Returns:
true if the entity list is valid for target entities, false otherwise
Throws:
ModelException - if there is a severe validity violation

isEntityValid

protected boolean isEntityValid(IEntity entity)
                         throws ModelException
Checks if given entity is valid for the relation's source and target entities list.

This method is called by the {addSourceEntitiy(IEntity) and addTargetEntitiy(IEntity) methods. By default it always returns true , however it can be overridden in order to define new validation rules. In such case the implementation from base class should be called inside the overridden method in order to preserve data consistency.

Parameters:
entities - the entity to validate
Returns:
true if the entity is valid, false otherwise
Throws:
ModelException - if there is a severe validity violation

getSourceEntities

public final List<? extends IEntity> getSourceEntities()
Returns source entities associated with the relation. Result must contain at least one entity.

Specified by:
getSourceEntities in interface IManyToManyRelation
Returns:
list of source entities
See Also:
IManyToManyRelation.getSourceEntities()

setSourceEntities

public final void setSourceEntities(List<? extends IEntity> entities)
Sets source entities associated with the relation.

Specified by:
setSourceEntities in interface IManyToManyRelation
Parameters:
entities - collection containing at least one entity
See Also:
pl.edu.agh.cast.data.model.general.IManyToManyRelation#setSourceEntities(java.lang.Iterable)

addSourceEntitiy

public final void addSourceEntitiy(IEntity entity)
                            throws ModelException
Adds a source entity associated with the relation.

Specified by:
addSourceEntitiy in interface IManyToManyRelation
Parameters:
entity - entity to add
Throws:
ModelException - when entity is already the relation's source entity
See Also:
#addSourceEntitiy(pl.edu.agh.cast.data.model.general.IEntity)

removeSourceEntitiy

public final boolean removeSourceEntitiy(IEntity entity)
Removes the specified source entity associated with the relation.

Specified by:
removeSourceEntitiy in interface IManyToManyRelation
Parameters:
entity - entity to remove
Returns:
true if the source entity was present and successfully removed
See Also:
#removeSourceEntitiy(pl.edu.agh.cast.data.model.general.IEntity)

getSourceEntitiy

public final IEntity getSourceEntitiy(int index)
Gets source entity associated with the relation with given index in the list.

Specified by:
getSourceEntitiy in interface IManyToManyRelation
Parameters:
index - index of entity in the source entities list
Returns:
entity from given index or null if not present
See Also:
IManyToManyRelation.getSourceEntitiy(int)

isSourceEntitiesResolved

protected final boolean isSourceEntitiesResolved()
Checks if the list of source entities is resolved (loaded).

Returns:
true if the source reference list is resolved
See Also:
IReferenceList.isResolved()

getTargetEntities

public final List<? extends IEntity> getTargetEntities()
Returns target entities associated with the relation. Result must contain at least one entity.

Specified by:
getTargetEntities in interface IManyToManyRelation
Returns:
list of target entities
See Also:
IManyToManyRelation.getTargetEntities()

setTargetEntities

public final void setTargetEntities(List<? extends IEntity> entities)
Sets target entities associated with the relation.

Specified by:
setTargetEntities in interface IManyToManyRelation
Parameters:
entities - collection containing at least one entity
See Also:
pl.edu.agh.cast.data.model.general.IManyToManyRelation#setTargetEntities(java.lang.Iterable)

addTargetEntitiy

public final void addTargetEntitiy(IEntity entity)
                            throws ModelException
Adds a target entity associated with the relation.

Specified by:
addTargetEntitiy in interface IManyToManyRelation
Parameters:
entity - entity to add
Throws:
ModelException - when entity is already the relation's target entity
See Also:
#addTargetEntitiy(pl.edu.agh.cast.data.model.general.IEntity)

removeTargetEntitiy

public final boolean removeTargetEntitiy(IEntity entity)
Removes the specified target entity associated with the relation.

Specified by:
removeTargetEntitiy in interface IManyToManyRelation
Parameters:
entity - entity to remove
Returns:
true if the target entity was present and successfully removed
See Also:
#removeTargetEntitiy(pl.edu.agh.cast.data.model.general.IEntity)

getTargetEntitiy

public final IEntity getTargetEntitiy(int index)
Gets target entity associated with the relation with given index in the list.

Specified by:
getTargetEntitiy in interface IManyToManyRelation
Parameters:
index - index of entity in the target entities list
Returns:
entity from given index or null if not present
See Also:
IManyToManyRelation.getTargetEntitiy(int)

isTargetEntitiesResolved

protected final boolean isTargetEntitiesResolved()
Checks if the list of target entities is resolved (loaded).

Returns:
true if the target reference list is resolved
See Also:
IReferenceList.isResolved()

isDirected

public final Boolean isDirected()
Checks if given relation is directed (from source to target entities) or not (in that case it is bi-directional).

Specified by:
isDirected in interface IManyToManyRelation
Returns:
true if this relation is directed, false if it is bi-directional
See Also:
IManyToManyRelation.isDirected()

setDirected

public final void setDirected(Boolean directed)
Sets the directed flag.

Specified by:
setDirected in interface IManyToManyRelation
Parameters:
directed - the flag
See Also:
IManyToManyRelation.setDirected(java.lang.Boolean)

getDescString

protected String getDescString()
Returns a string with short description of element. This method is used by AbstractElement.toString() and may be overridden in order to customize its results.

Overrides:
getDescString in class AbstractElement
Returns:
a description string
See Also:
AbstractElement.getDescString()

replicateTo

public ManyToManyRelation replicateTo(Object replica)
                               throws IllegalArgumentException,
                                      ReplicationException
Replicates this instance state to given replica object. The replica should be an instance of replicated instance's class (same class or subclass).

Replicates element type and ID.

Replicates relation direction, the lists of source and target entities are empty.

Specified by:
replicateTo in interface IReplicable
Overrides:
replicateTo in class AbstractElement
Parameters:
replica - the object to replicate to
Returns:
the replica object (the same instance as given)
Throws:
IllegalArgumentException - if given object is null or of invalid type
ReplicationException - if replication fails
See Also:
AbstractElement.replicateTo(java.lang.Object)

replicate

public ManyToManyRelation replicate()
                             throws ReplicationException
Returns a replicated instance of this instance.

Specified by:
replicate in interface IReplicable
Overrides:
replicate in class AbstractElement
Returns:
replicated instance
Throws:
ReplicationException - if replication fails
See Also:
IReplicable.replicate()


Copyright © 2007-2009 IISG AGH-UST Krakow, Poland. All Rights Reserved.