1 package eu
.etaxonomy
.cdm
.persistence
.hibernate
.replace
.impl
;
3 import java
.lang
.reflect
.Field
;
4 import java
.lang
.reflect
.Modifier
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
10 import javax
.persistence
.OneToMany
;
12 import org
.hibernate
.MappingException
;
13 import org
.hibernate
.SessionFactory
;
14 import org
.hibernate
.engine
.SessionFactoryImplementor
;
15 import org
.hibernate
.impl
.SessionFactoryImpl
;
16 import org
.hibernate
.metadata
.ClassMetadata
;
17 import org
.hibernate
.type
.AnyType
;
18 import org
.hibernate
.type
.AssociationType
;
19 import org
.hibernate
.type
.BagType
;
20 import org
.hibernate
.type
.CollectionType
;
21 import org
.hibernate
.type
.EntityType
;
22 import org
.hibernate
.type
.ListType
;
23 import org
.hibernate
.type
.MapType
;
24 import org
.hibernate
.type
.SetType
;
25 import org
.hibernate
.type
.Type
;
26 import org
.hibernate
.validator
.xml
.PropertyType
;
27 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
28 import org
.springframework
.stereotype
.Component
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
32 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
33 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.replace
.ReferringObjectMetadata
;
34 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.replace
.ReferringObjectMetadataFactory
;
37 public class ReferringObjectMetadataFactoryImpl
implements ReferringObjectMetadataFactory
{
40 private SessionFactory sessionFactory
;
42 private Map
<Class
<?
extends CdmBase
>, Set
<ReferringObjectMetadata
>> referringObjectMap
= new HashMap
<Class
<?
extends CdmBase
>, Set
<ReferringObjectMetadata
>>();
44 public Set
<ReferringObjectMetadata
> get(Class
<?
extends CdmBase
> toClass
) {
45 if(!referringObjectMap
.containsKey(toClass
)) {
46 ClassMetadata toClassMetadata
= sessionFactory
.getClassMetadata(toClass
);
47 Map
<Class
,Set
<String
>> bidirectionalRelationships
= new HashMap
<Class
,Set
<String
>>();
48 for(String propertyName
: toClassMetadata
.getPropertyNames()) {
49 Type propertyType
= toClassMetadata
.getPropertyType(propertyName
);
50 if(propertyType
.isAssociationType() && !propertyType
.isAnyType()) {
51 AssociationType associationType
= (AssociationType
)propertyType
;
55 field
= toClass
.getDeclaredField(propertyName
);
56 } catch(NoSuchFieldException nsfe
) {
57 Class superClass
= toClass
.getSuperclass();
58 while(!superClass
.equals(CdmBase
.class)) {
60 field
= superClass
.getDeclaredField(propertyName
);
62 } catch(NoSuchFieldException nsfe1
) { }
63 superClass
= superClass
.getSuperclass();
70 field
.setAccessible(true);
71 if(field
.isAnnotationPresent(OneToMany
.class)){
72 OneToMany oneToMany
= field
.getAnnotation(OneToMany
.class);
73 if(oneToMany
.mappedBy() != null && oneToMany
.mappedBy().length() > 0) {
74 String associatedEntityName
= associationType
.getAssociatedEntityName((SessionFactoryImpl
) sessionFactory
.getCurrentSession().getSessionFactory());
75 Class associatedEntity
;
77 associatedEntity
= Class
.forName(associatedEntityName
);
78 if(!bidirectionalRelationships
.containsKey(associatedEntity
)) {
79 bidirectionalRelationships
.put(associatedEntity
, new HashSet
<String
>());
81 bidirectionalRelationships
.get(associatedEntity
).add(oneToMany
.mappedBy());
82 } catch (ClassNotFoundException e
) {
83 // TODO Auto-generated catch block
86 // System.out.println(oneToMany.mappedBy() + " " + associatedEntityName);
93 Map
<Object
,ClassMetadata
> allClassMetadata
= sessionFactory
.getAllClassMetadata();
94 Set
<ReferringObjectMetadata
> referringObjectMetadata
= new HashSet
<ReferringObjectMetadata
>();
96 for(Object fromClass
: allClassMetadata
.keySet()) {
97 String entityName
= (String
) fromClass
;
98 if(!entityName
.endsWith("_AUD")) {
100 // System.out.println(entityName);
101 Class entityClass
= Class
.forName(entityName
);
102 ClassMetadata classMetadata
= allClassMetadata
.get(entityName
);
104 for(String propertyName
: classMetadata
.getPropertyNames()) {
105 if(bidirectionalRelationships
.containsKey(entityClass
) && bidirectionalRelationships
.get(entityClass
).contains(propertyName
)) {
106 // System.out.println("Excluding " + entityClass.getName() + " " + propertyName);
108 Type propertyType
= classMetadata
.getPropertyType(propertyName
);
109 if (propertyType
.isAssociationType()){
110 // System.out.println(entityName+"."+propertyName);
111 AssociationType associationType
= (AssociationType
)propertyType
;
113 if(!propertyType
.isAnyType()) {
115 String associatedEntityName
= associationType
.getAssociatedEntityName((SessionFactoryImpl
) sessionFactory
.getCurrentSession().getSessionFactory());
116 Class associatedClass
= Class
.forName(associatedEntityName
);
117 if (associatedClass
.isAssignableFrom(toClass
)){
120 if(associationType
.isEntityType()) {
122 referringObjectMetadata
.add(new ToOneReferringObjectMetadata(entityClass
,propertyName
, toClass
));
123 // System.out.println(propertyName + " " + fromClass + " " + toClass);
124 } else if(associationType
.isCollectionType()) {
125 CollectionType collectionType
= (CollectionType
)propertyType
;
127 if(propertyType
instanceof BagType
|| propertyType
instanceof SetType
) {
128 referringObjectMetadata
.add(new SetReferringObjectMetadata(entityClass
,propertyName
, toClass
));
129 // System.out.println(propertyName + " " + fromClass + " " + toClass);
130 } else if(propertyType
instanceof ListType
) {
131 referringObjectMetadata
.add(new ListReferringObjectMetadata(entityClass
,propertyName
, toClass
));
132 // System.out.println(propertyName + " " + fromClass + " " + toClass);
135 } catch(NoSuchFieldException nsfe
) { }
137 } catch(MappingException me
) { }
143 } catch (ClassNotFoundException e
) {
149 referringObjectMap
.put(toClass
, referringObjectMetadata
);
152 return referringObjectMap
.get(toClass
);