1 package org.splat.dal.bo.kernel;
4 * Abstract root class of persistent objects supporting relations to other persistent objects.<br/>
5 * Relations are instances of concrete subclasses of Relation that are assigned to Entity objects at run time.<br/>
7 * Entity objects also support dynamic attributes provided by the Any class.
10 * @author Daniel Brunier-Coulin
11 * @copyright OPEN CASCADE 2012-2015
14 import java.util.ArrayList;
15 import java.util.HashSet;
16 import java.util.List;
19 import org.splat.kernel.InvalidPropertyException;
20 import org.splat.kernel.MissedPropertyException;
21 import org.splat.kernel.MultiplyDefinedException;
22 import org.splat.kernel.ObjectProperties;
23 import org.splat.log.AppLogger;
26 * Persistent entity class. Entity can have relations to other persistent objects.
28 public abstract class Entity extends Any {
33 public final static AppLogger LOG = AppLogger.getLogger(Entity.class);
36 * Persistent collection of relations to other persistent objects.
38 private final Set<Relation> relations = new HashSet<Relation>(); // NOPMD:RKV: mapped as field
41 * Database fetch constructor.
48 * Initialization constructor.
52 * @throws MissedPropertyException
53 * if some mandatory property is missed
54 * @throws InvalidPropertyException
55 * if some property has invalid value
56 * @throws MultiplyDefinedException
57 * if some property is defined several times
59 protected Entity(final ObjectProperties prop)
60 throws MissedPropertyException, InvalidPropertyException,
61 MultiplyDefinedException {
65 // ==============================================================================================================================
66 // Public member functions
67 // ==============================================================================================================================
70 * Get first relation of the given type.
73 * the relation type to look for
74 * @return the found relation or null
76 public Relation getFirstRelation(final Class<? extends Relation> type) {
77 Relation found = null;
78 for (Relation link : relations) {
79 if (link.getClass().equals(type)) {
87 * Get list of all relations of the given type.
90 * the relation type to look for
91 * @return the list if found relations
93 public List<Relation> getRelations(final Class<? extends Relation> type) {
94 List<Relation> result = new ArrayList<Relation>();
96 for (Relation link : relations) {
97 if (link.getClass().equals(type)) {
104 // ==============================================================================================================================
105 // Protected services
106 // ==============================================================================================================================
109 * Get persistent collection of relations.
111 * @return entity relations set
113 public Set<Relation> getAllRelations() {
118 * Add a relation for the entity. If the link is bidirectional then also add the reverse link to the referenced object.
121 * the relation from this entity to some persistent object
122 * @return the reverse link if the given link is bidirectional otherwise returns the given link
124 public Relation addRelation(final Relation link) {
126 Relation back = link;
129 if (link.isBidirectional()) {
130 Entity to = (Entity) link.getTo(); // Bidirectional relation are necessarily between entities
132 back = link.getReverse();
133 to.relations.add(back);
139 * Remove the first relation of the given type to the given object.
142 * the type of the relation to remove
144 * the referenced object to identify the relation to remove
145 * @return the removed relation
147 public Relation removeRelation(final Class<? extends Relation> type,
148 final Persistent to) {
150 if (LOG.isDebugEnabled()) {
151 LOG.debug("Try to remove the link " + type.getSimpleName()
152 + " from " + this.toString() + " to " + to.toString());
154 for (Relation link : relations) {
155 if (LOG.isDebugEnabled()) {
156 LOG.debug("Compare the link " + link.toString());
157 LOG.debug("link.getClass().equals(type): "
158 + link.getClass().equals(type));
160 if (link.getClass().equals(type) && link.getTo().equals(to)) {
161 if (LOG.isDebugEnabled()) {
162 LOG.debug("Remove the link "
163 + link.getClass().getSimpleName() + " from "
164 + this.toString() + " to " + to.toString());
171 if (LOG.isDebugEnabled()) {
172 LOG.debug("Contains: " + this.getAllRelations().contains(res));
173 LOG.debug("Nb relations of this before: "
174 + this.getAllRelations().size());
176 this.getAllRelations().remove(res);
177 if (LOG.isDebugEnabled()) {
178 LOG.debug("Nb relations of this after: "
179 + this.getAllRelations().size());
181 if (res.isBidirectional()) {
182 if (LOG.isDebugEnabled()) {
183 LOG.debug("Nb relations of reverse before: "
184 + ((Entity) res.getTo()).getAllRelations().size());
186 ((Entity) res.getTo()).getAllRelations().remove(
188 if (LOG.isDebugEnabled()) {
189 LOG.debug("Nb relations of reverse after: "
190 + ((Entity) res.getTo()).getAllRelations().size());
201 * @see org.splat.dal.bo.kernel.Any#evict()
204 public void evict() {
206 // Evict all attributes of the persistent object
207 Set<Relation> tmpSet = new HashSet<Relation>();
208 tmpSet.addAll(relations);
211 for (Relation rel : tmpSet) {
212 if (rel.isSaved()) { // to avoid recursive evict