KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > PropertyHolderBuilder


1 //$Id: PropertyHolderBuilder.java,v 1.6 2005/06/20 17:11:01 epbernard Exp $
2
package org.hibernate.cfg;
3
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.lang.reflect.AnnotatedElement JavaDoc;
7 import javax.persistence.Column;
8 import javax.persistence.AttributeOverride;
9 import javax.persistence.AttributeOverrides;
10
11 import org.hibernate.mapping.Collection;
12 import org.hibernate.mapping.Component;
13 import org.hibernate.mapping.PersistentClass;
14
15 /**
16  * This factory is here ot build a PropertyHolder and prevent .mapping interface adding
17  *
18  * @author Emmanuel Bernard
19  */

20 public final class PropertyHolderBuilder {
21     private PropertyHolderBuilder() {}
22
23     public static PropertyHolder buildPropertyHolder(PersistentClass persistentClass, Map JavaDoc<String JavaDoc, Column[]> columnOverride) {
24         return (PropertyHolder) new ClassPropertyHolder(persistentClass, columnOverride);
25     }
26
27     /**
28      * build a component property holder
29      *
30      * @param component component to wrap
31      * @param path component path
32      * @return PropertyHolder
33      */

34     public static PropertyHolder buildPropertyHolder(Component component, String JavaDoc path, Map JavaDoc<String JavaDoc, Column[]> columnOverride) {
35         return (PropertyHolder) new ComponentPropertyHolder(component, path, columnOverride);
36     }
37
38     /** buid a property holder on top of a collection */
39     public static PropertyHolder buildPropertyHolder(Collection collection, String JavaDoc path) {
40         return new CollectionPropertyHolder( collection, path );
41     }
42
43     public static PropertyHolder buildPropertyHolder(PersistentClass persistentClass) {
44         return buildPropertyHolder( persistentClass, new HashMap JavaDoc<String JavaDoc, Column[]>() );
45     }
46
47     public static Map JavaDoc<String JavaDoc, Column[]> buildColumnOverride(AnnotatedElement JavaDoc element) {
48         AttributeOverride singleOverride = element.getAnnotation(AttributeOverride.class);
49         AttributeOverrides multipleOverrides = element.getAnnotation(AttributeOverrides.class);
50         AttributeOverride[] overrides;
51         if (singleOverride != null) {
52             overrides = new AttributeOverride[] { singleOverride };
53         }
54         else if (multipleOverrides != null) {
55             overrides = multipleOverrides.value();
56         }
57         else {
58             overrides = null;
59         }
60         Map JavaDoc<String JavaDoc, Column[]> columnOverride = new HashMap JavaDoc<String JavaDoc, Column[]>();
61         //fill overriden columns
62
if (overrides != null) {
63             for ( AttributeOverride depAttr : overrides ) {
64                 columnOverride.put( depAttr.name(), new Column[] { depAttr.column() } );
65             }
66         }
67         return columnOverride;
68     }
69 }
70
Popular Tags