KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > hibernate > InfoGlueHibernatePropertySet


1 package org.infoglue.cms.util.workflow.hibernate;
2
3 import java.io.UnsupportedEncodingException JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import com.opensymphony.module.propertyset.PropertyException;
7 import com.opensymphony.module.propertyset.PropertySet;
8 import com.opensymphony.module.propertyset.hibernate.HibernateConfigurationProvider;
9 import com.opensymphony.module.propertyset.hibernate.HibernatePropertySet;
10 import com.opensymphony.module.propertyset.hibernate.PropertySetItem;
11
12 /**
13  * Quickfix
14  */

15 public class InfoGlueHibernatePropertySet extends HibernatePropertySet
16 {
17     /**
18      *
19      */

20     private static final String JavaDoc UTF8_ENCODING = "utf-8";
21     
22     private static final String JavaDoc CONFIGURATION_PROVIDER_ARGUMENT = "configurationProvider";
23     
24     private HibernateConfigurationProvider configProvider;
25     private Long JavaDoc entityId;
26     private String JavaDoc entityName;
27     
28     /**
29      *
30      */

31     public InfoGlueHibernatePropertySet()
32     {
33         super();
34     }
35
36     /**
37      *
38      */

39     public boolean supportsType(int type)
40     {
41         return (type == PropertySet.DATA) ? true : super.supportsType(type);
42     }
43
44     /**
45      *
46      */

47     public Object JavaDoc getAsActualType(String JavaDoc key) throws PropertyException
48     {
49         return (getType(key) == PropertySet.DATA) ? getDataString(key) : super.getAsActualType(key);
50     }
51
52     /**
53      *
54      */

55     public String JavaDoc getDataString(final String JavaDoc key) throws PropertyException
56     {
57         try
58         {
59             final byte[] b = getData(key);
60             final String JavaDoc value = (b == null) ? null : new String JavaDoc(b, UTF8_ENCODING);
61             return value;
62         }
63         catch(UnsupportedEncodingException JavaDoc e)
64         {
65             throw new PropertyException("Unable to get data for [" + key + "].");
66         }
67     }
68     
69     /**
70      *
71      */

72     public void init(Map JavaDoc config, Map JavaDoc args)
73     {
74         super.init(config, args);
75         this.configProvider = (HibernateConfigurationProvider) args.get(CONFIGURATION_PROVIDER_ARGUMENT);
76         this.entityId = (Long JavaDoc) args.get("entityId");
77         this.entityName = (String JavaDoc) args.get("entityName");
78     }
79     
80     /**
81      *
82      */

83     private PropertySetItem findByKey(String JavaDoc key) throws PropertyException
84     {
85         return configProvider.getPropertySetDAO().findByKey(entityName, entityId, key);
86     }
87
88     /**
89      *
90      */

91     protected void setImpl(int type, String JavaDoc key, Object JavaDoc value) throws PropertyException
92     {
93         if(type != PropertySet.DATA)
94         {
95             super.setImpl(type, key, value);
96         }
97         else
98         {
99             setDataImpl(type, key, value);
100         }
101     }
102
103     /**
104      *
105      */

106     protected void setDataImpl(int type, String JavaDoc key, Object JavaDoc value) throws PropertyException
107     {
108         PropertySetItem item = configProvider.getPropertySetDAO().findByKey(entityName, entityId, key);
109         boolean update = (item == null) ? false : true;
110         if (item == null)
111         {
112             item = configProvider.getPropertySetDAO().create(entityName, entityId.longValue(), key);
113         }
114         else if (item.getType() != type)
115         {
116             throw new PropertyException("Existing key '" + key + "' does not have matching type of " + type);
117         }
118         
119         ((InfogluePropertySetItem) item).setDataVal(((com.opensymphony.util.Data) value).getBytes());
120         
121         item.setType(type);
122         configProvider.getPropertySetDAO().setImpl(item, update);
123     }
124
125     /**
126      *
127      */

128     protected Object JavaDoc get(int type, String JavaDoc key) throws PropertyException
129     {
130         if(type != PropertySet.DATA)
131         {
132             return super.get(type, key);
133         }
134         return getData(type, key);
135     }
136     
137     /**
138      *
139      */

140     private Object JavaDoc getData(int type, String JavaDoc key) throws PropertyException
141     {
142         final PropertySetItem item = findByKey(key);
143         if (item == null)
144         {
145             return null;
146         }
147         if (item.getType() != type)
148         {
149             throw new PropertyException("key '" + key + "' does not have matching type of " + type);
150         }
151         return new com.opensymphony.util.Data(((InfogluePropertySetItem) item).getDataVal());
152     }
153 }
Popular Tags