4 package eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
;
6 import java
.io
.Serializable
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collection
;
9 import java
.util
.Collections
;
10 import java
.util
.HashMap
;
11 import java
.util
.HashSet
;
12 import java
.util
.List
;
16 import org
.apache
.commons
.beanutils
.BeanUtils
;
17 import org
.apache
.commons
.beanutils
.BeanUtilsBean2
;
18 import org
.apache
.commons
.lang
.StringUtils
;
19 import org
.hibernate
.collection
.internal
.AbstractPersistentCollection
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 public class BeanInitNode
implements Comparable
<BeanInitNode
>{
29 // ************************ ATTRIBUTES *******************************/
31 private BeanInitNode parent
;
33 private Map
<String
, BeanInitNode
> children
= new HashMap
<String
, BeanInitNode
>();
37 private boolean isToManyInitialized
= false;
39 private boolean isToOneInitialized
= false;
41 private BeanInitNode wildcardChildCache
;
44 private Map
<Class
<?
>, Set
<Object
>> beans
= new HashMap
<Class
<?
>, Set
<Object
>>();
46 private Map
<Class
<?
>, Set
<Serializable
>> lazyBeans
= new HashMap
<Class
<?
>, Set
<Serializable
>>();
48 private Map
<Class
<?
>, Map
<String
, Set
<Serializable
>>> lazyCollections
= new HashMap
<Class
<?
>, Map
<String
,Set
<Serializable
>>>();
50 //java.util.Set is not possible here to avoid initializing of collection during .add()
51 private List
<AbstractPersistentCollection
> uninitializedCollections
= new ArrayList
<AbstractPersistentCollection
>();
53 // *************************** STATIC METHODS *****************************************/
55 public static BeanInitNode
createInitTree(List
<String
> propertyPaths
) {
57 //sort paths //TODO needed?
58 Collections
.sort(propertyPaths
);
60 BeanInitNode root
= new BeanInitNode(null, "");
62 for (String fullPath
: propertyPaths
){
63 String
[] parts
= fullPath
.split("\\.");
64 BeanInitNode lastNode
= root
;
65 for (String part
: parts
){
66 BeanInitNode child
= lastNode
.children
.get(part
);
68 child
= new BeanInitNode(lastNode
, part
);
77 private static boolean isWildcard(String pathPart
) {
78 return pathPart
.equals(AbstractBeanInitializer
.LOAD_2ONE_WILDCARD
) ||
79 pathPart
.equals(AbstractBeanInitializer
.LOAD_2ONE_2MANY_WILDCARD
);
83 //***************************** CONSTRUCTOR RELATED ****************************************/
85 public BeanInitNode(BeanInitNode parent
, String part
) {
86 this.path
= CdmUtils
.Nz(part
);
88 this.isToManyInitialized
= this.path
.equals(AbstractBeanInitializer
.LOAD_2ONE_2MANY_WILDCARD
);
89 this.isToOneInitialized
= this.path
.equals(AbstractBeanInitializer
.LOAD_2ONE_WILDCARD
) || this.isToManyInitialized
;
91 parent
.addChild(part
, this);
95 private void addChild(String part
, BeanInitNode child
) {
96 children
.put(part
, child
);
97 if (child
.isWildcard()){
98 //set wildcard child if not exists or if child is stronger then existing wildcard child
99 if (this.wildcardChildCache
== null || (! this.wildcardChildCache
.isToManyInitialized
) && child
.isToManyInitialized
){
100 this.wildcardChildCache
= child
;
105 // ************************** ***********************************************************/
107 public BeanInitNode
getChild(String param
){
108 return children
.get(param
);
113 public List
<BeanInitNode
> getChildrenList() {
114 List
<BeanInitNode
> result
= new ArrayList
<BeanInitNode
>(children
.values());
115 Collections
.sort(result
);
119 public BeanInitNode
getSibling(String param
) {
120 if (getPath().equals(param
) || parent
== null){
123 return parent
.getChild(param
);
127 public String
getPath() {
131 public boolean isRoot() {
132 return StringUtils
.isBlank(path
);
135 public boolean hasChildren() {
136 return children
.size() > 0;
139 public BeanInitNode
getWildcardChild(){
140 return this.wildcardChildCache
;
142 public boolean isWildcard() {
143 return this.isToManyInitialized
|| this.isToOneInitialized
;
145 public boolean hasWildcardChild() {
146 return this.wildcardChildCache
!= null;
148 public boolean hasToManyWildcardChild() {
149 return this.wildcardChildCache
!= null;
152 public boolean hasWildcardToManySibling() {
153 BeanInitNode sibl
= getWildcardSibling();
154 return (sibl
!= null && sibl
.isToManyInitialized
);
157 private BeanInitNode
getWildcardSibling() {
158 if (!isWildcard() && parent
== null){
159 return parent
.getWildcardChild();
166 public boolean isToManyWildcard() {
167 return path
.equals(AbstractBeanInitializer
.LOAD_2ONE_2MANY_WILDCARD
);
171 // ******************* LAZY COLLECTION *****************************************/
173 public void putLazyCollection(AbstractPersistentCollection collection
) {
174 String parameter
= collection
.getRole();
175 parameter
= parameter
.substring(parameter
.lastIndexOf(".")+1);
176 putLazyCollection(collection
.getOwner().getClass(), parameter
, collection
.getKey());
177 this.uninitializedCollections
.add(collection
);
181 private void putLazyCollection(Class
<?
> ownerClazz
, String parameter
, Serializable id
) {
182 if (ownerClazz
!= null && parameter
!= null && id
!= null){
183 Map
<String
, Set
<Serializable
>> lazyParams
= lazyCollections
.get(ownerClazz
);
184 if (lazyParams
== null){
185 lazyParams
= new HashMap
<String
, Set
<Serializable
>>();
186 lazyCollections
.put(ownerClazz
, lazyParams
);
188 Set
<Serializable
> layzIds
= lazyParams
.get(parameter
);
189 if (layzIds
== null){
190 layzIds
= new HashSet
<Serializable
>();
191 lazyParams
.put(parameter
, layzIds
);
195 throw new IllegalArgumentException("Class, parameter and id should not be null");
198 public Map
<Class
<?
>, Map
<String
, Set
<Serializable
>>> getLazyCollections(){
199 return this.lazyCollections
;
201 public List
<AbstractPersistentCollection
> getUninitializedCollections(){
202 return this.uninitializedCollections
;
204 public void resetLazyCollections(){
205 this.lazyCollections
.clear();
206 this.uninitializedCollections
.clear();
210 // ******************* LAZY BEAN *****************************************/
212 public void putLazyBean(Class
<?
> clazz
, Serializable id
) {
213 if (clazz
!= null && id
!= null){
214 Set
<Serializable
> classedLazies
= lazyBeans
.get(clazz
);
215 if (classedLazies
== null){
216 classedLazies
= new HashSet
<Serializable
>();
217 lazyBeans
.put(clazz
, classedLazies
);
219 classedLazies
.add(id
);
221 throw new IllegalArgumentException("Class and id should not be null");
224 public Map
<Class
<?
>, Set
<Serializable
>> getLazyBeans(){
225 return this.lazyBeans
;
227 public void resetLazyBeans(){
228 this.lazyBeans
.clear();
231 // ********************* BEANS ******************************/
233 private Map
<Class
<?
>, Set
<Object
>> getClassedBeans(){
238 public Map
<Class
<?
>, Set
<Object
>> getBeans() {
242 public Map
<Class
<?
>, Set
<Object
>> getParentBeans() {
244 return new HashMap
<Class
<?
>, Set
<Object
>>();
246 return parent
.getClassedBeans();
250 public void addBean(Object bean
) {
253 Class
<?
> key
= bean
.getClass();
254 Set
<Object
> classedBeans
= beans
.get(key
);
255 if (classedBeans
== null){
256 classedBeans
= new HashSet
<Object
>();
257 beans
.put(key
, classedBeans
);
259 classedBeans
.add(bean
);
263 public void addBeans(Collection
<?
> beans
) {
264 for (Object bean
: beans
){
269 public void resetBeans(){
274 // ************************* OVERRIDES **********************************/
277 public int compareTo(BeanInitNode o
) {
278 String toMany
= AbstractBeanInitializer
.LOAD_2ONE_2MANY_WILDCARD
;
279 String toOne
= AbstractBeanInitializer
.LOAD_2ONE_WILDCARD
;
280 if (this.path
.equals(toMany
)){
282 }else if (o
.path
.equals(toMany
)){
284 }else if (this.path
.equals(toOne
)){
286 }else if (o
.path
.equals(toOne
)){
289 return path
.compareTo(o
.path
);
295 public String
toString() {
298 return "/"; //for root node
300 String result
= parent
.toString() + ((parent
.parent
== null) ?
"" : ".") + path
;
305 public String
toStringNoWildcard(){
306 return toString().replace(".$", "").replace(".*", "");
309 public String
toStringTree() {
311 String result
= (path
.equals("") ?
"/" : path
) + "\n";
313 for (BeanInitNode child
: getChildrenList()){
314 result
+= toString() + (result
.endsWith("/\n") ?
"" : ".") + child
.toStringTree();
320 public boolean hasWildcardToOneSibling() {
321 BeanInitNode sibl
= getWildcardSibling();
322 return (sibl
!= null && sibl
.isToOneInitialized
);