1 package org.infoglue.cms.util.workflow.hibernate; 2 3 import java.io.UnsupportedEncodingException ; 4 import java.util.Map ; 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 15 public class InfoGlueHibernatePropertySet extends HibernatePropertySet 16 { 17 20 private static final String UTF8_ENCODING = "utf-8"; 21 22 private static final String CONFIGURATION_PROVIDER_ARGUMENT = "configurationProvider"; 23 24 private HibernateConfigurationProvider configProvider; 25 private Long entityId; 26 private String entityName; 27 28 31 public InfoGlueHibernatePropertySet() 32 { 33 super(); 34 } 35 36 39 public boolean supportsType(int type) 40 { 41 return (type == PropertySet.DATA) ? true : super.supportsType(type); 42 } 43 44 47 public Object getAsActualType(String key) throws PropertyException 48 { 49 return (getType(key) == PropertySet.DATA) ? getDataString(key) : super.getAsActualType(key); 50 } 51 52 55 public String getDataString(final String key) throws PropertyException 56 { 57 try 58 { 59 final byte[] b = getData(key); 60 final String value = (b == null) ? null : new String (b, UTF8_ENCODING); 61 return value; 62 } 63 catch(UnsupportedEncodingException e) 64 { 65 throw new PropertyException("Unable to get data for [" + key + "]."); 66 } 67 } 68 69 72 public void init(Map config, Map args) 73 { 74 super.init(config, args); 75 this.configProvider = (HibernateConfigurationProvider) args.get(CONFIGURATION_PROVIDER_ARGUMENT); 76 this.entityId = (Long ) args.get("entityId"); 77 this.entityName = (String ) args.get("entityName"); 78 } 79 80 83 private PropertySetItem findByKey(String key) throws PropertyException 84 { 85 return configProvider.getPropertySetDAO().findByKey(entityName, entityId, key); 86 } 87 88 91 protected void setImpl(int type, String key, Object 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 106 protected void setDataImpl(int type, String key, Object 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 128 protected Object get(int type, String 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 140 private Object getData(int type, String 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 |