View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.persistence.hibernate.dao.extent;
18  
19  import java.lang.reflect.Method;
20  
21  import org.springframework.util.Assert;
22  
23  import static ch.elca.el4j.services.persistence.hibernate.dao.extent.ExtentEntity.entity;
24  
25  /**
26   * An ExtentCollection represents a collection of an entity.
27   * <br>
28   * Features: <br>
29   *  <ul>
30   *   <li> static method collection: create a new entity-collection.
31   *  </ul>
32   * For further details, see {@link DataExtent}. 
33   *
34   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/hibernate/src/main/java/ch/elca/el4j/services/persistence/hibernate/dao/extent/ExtentCollection.java $
35   *
36   * @author Andreas Rueedlinger (ARR)
37   */
38  public class ExtentCollection extends AbstractExtentPart {
39  	
40  	/** The sub-entity of the collection. */
41  	private ExtentEntity m_containedEntity;
42  	
43  	/** The id of the entity. */
44  	private String m_collectionId;
45  	
46  	/** Is the ExtentCollection frozen, eg. must not be changed anymore. */
47  	private boolean m_frozen;
48  	
49  	/**
50  	 * Default Creator.
51  	 * @param name	the name of the collection
52  	 */
53  	public ExtentCollection(String name) {
54  		m_name = name;
55  		m_collectionId = name;
56  	}
57  	/**
58  	 * Default Creator.
59  	 * @param name	the name of the collection
60  	 * @param c		the class of the contained entity
61  	 */
62  	public ExtentCollection(String name, Class<?> c) {
63  		m_name = name;
64  		m_containedEntity = entity(c);
65  		m_collectionId = m_name + "[" + m_containedEntity.toString() + "]";
66  		m_containedEntity.setParent(this);
67  	}
68  	
69  	/**
70  	 * Default Creator, hidden.
71  	 * @param c 		the class of the contained entity
72  	 * @param method	the method to get the collection
73  	 */
74  	public ExtentCollection(Class<?> c, Method method) {
75  		m_name = toFieldName(method);
76  		m_containedEntity = entity(c);
77  		m_collectionId = m_name + "[" + m_containedEntity.toString() + "]";
78  		m_containedEntity.setParent(this);
79  	}
80  	
81  	/** {@inheritDoc} */
82  	public String getId() {
83  		return m_collectionId;
84  	}
85  	
86  	/** {@inheritDoc} */
87  	protected void updateId() {
88  		String id = m_name + "[" + m_containedEntity.toString() + "]";
89  		if (!m_collectionId.equals(id)) {
90  			m_collectionId = id;
91  			if (m_parent != null) {
92  				m_parent.updateId();
93  			}
94  		}
95  	}
96  	
97  	/**
98  	 * Contained entity of the collection.
99  	 * @return the name of the field.
100 	 */
101 	public ExtentEntity getContainedEntity() {
102 		return m_containedEntity;
103 	}
104 	
105 	/**
106 	 * Set the entity contained in this collection.
107 	 * @param entity	 the contained entity of the collection.
108 	 */
109 	public void setContainedEntity(ExtentEntity entity) {
110 		Assert.state(!m_frozen, "DataExtent is frozen and cannot be changed anymore");
111 		// Method and Name of entity does not matter
112 		m_containedEntity = entity;
113 		m_containedEntity.setParent(this);
114 		m_collectionId = m_name + "[" + m_containedEntity.toString() + "]";
115 		
116 	}
117 	
118 	/**
119 	 * Returns a new Collection object, based on the given name and class.
120 	 * @param name	the name of the collection.
121 	 * @param c		the class of the contained entity.
122 	 * @return	the Collection object.
123 	 */
124 	public static ExtentCollection collection(String name, Class<?> c) {
125 		return new ExtentCollection(name, c);
126 	}
127 	
128 	/**
129 	 * Returns a new Collection object, based on the given class and method.
130 	 * @param c		the class of the contained entity.
131 	 * @param m		the method to get the collection.
132 	 * @return	the Collection object.
133 	 */
134 	public static ExtentCollection collection(Class<?> c, Method m) {
135 		return new ExtentCollection(c, m);
136 	}
137 	
138 	/**
139 	 * Returns a new Collection object, based on the given name and entity.
140 	 * @param name			the name of the collection.
141 	 * @param entity		the contained entity.
142 	 * @return	the Collection object.
143 	 */
144 	public static ExtentCollection collection(String name, ExtentEntity entity) {
145 		ExtentCollection c = new ExtentCollection(name);
146 		c.setContainedEntity(entity);
147 		return c;
148 		
149 	}
150 	
151 	/**
152 	 * Merge two ExtentCollections. Returns the union of the extents.
153 	 * The class of the two contained entities of the collections should be the same,
154 	 * the name and the parent is taken from this object.
155 	 * @param other	the collection to be merged with.
156 	 * @return	the merged collection.
157 	 */
158 	public ExtentCollection merge(ExtentCollection other) {
159 		Assert.state(!m_frozen, "DataExtent is frozen and cannot be changed anymore");
160 		if (m_containedEntity.getEntityClass().equals(other.m_containedEntity.getClass()) && !this.equals(other)) {
161 			m_containedEntity.merge(other.m_containedEntity);
162 		}
163 		return this;
164 	}
165 	
166 	/**
167 	 * Freeze the ExtentCollection, meaning that no further changes to it are possible.
168 	 * @return the frozen ExtentCollection.
169 	 */
170 	public ExtentCollection freeze() {
171 		if (!m_frozen) {
172 			m_frozen = true;
173 			m_containedEntity.freeze();
174 		}
175 		return this;
176 	}
177 	
178 }