1 56 package org.objectstyle.cayenne.pref; 57 58 import java.io.ByteArrayInputStream ; 59 import java.io.ByteArrayOutputStream ; 60 import java.io.IOException ; 61 import java.util.Properties ; 62 63 import org.objectstyle.cayenne.DataObjectUtils; 64 import org.objectstyle.cayenne.PersistenceState; 65 import org.objectstyle.cayenne.util.Util; 66 67 70 public class DomainPreference extends _DomainPreference { 71 72 protected DomainProperties properties; 73 74 protected Properties getProperties() { 75 76 if (properties == null) { 77 properties = new DomainProperties(); 78 79 String values = getKeyValuePairs(); 80 if (values != null && values.length() > 0) { 81 try { 82 properties.load(new ByteArrayInputStream (values.getBytes())); 83 } 84 catch (IOException ex) { 85 throw new PreferenceException("Error loading properties.", ex); 86 } 87 } 88 } 89 90 return properties; 91 } 92 93 protected void encodeProperties() { 94 if (this.properties == null) { 95 setKeyValuePairs(null); 96 } 97 else { 98 ByteArrayOutputStream out = new ByteArrayOutputStream (); 99 100 try { 101 properties.store(out, null); 102 } 103 catch (IOException ex) { 104 throw new PreferenceException("Error storing properties.", ex); 105 } 106 107 setKeyValuePairs(out.toString()); 108 } 109 } 110 111 114 protected PreferenceDetail getPreference() { 115 PreferenceDetail preference = new PreferenceDetail(); 116 preference.setDomainPreference(this); 117 return preference; 118 } 119 120 123 protected PreferenceDetail getPreference(Class javaClass, boolean create) { 124 125 127 int pk = DataObjectUtils.intPKForObject(this); 128 PreferenceDetail preference = (PreferenceDetail) DataObjectUtils 129 .objectForPK(getDataContext(), javaClass, pk); 130 131 if (preference != null) { 132 preference.setDomainPreference(this); 133 } 134 135 if (preference != null || !create) { 136 return preference; 137 } 138 139 preference = (PreferenceDetail) getDataContext() 140 .createAndRegisterNewObject(javaClass); 141 142 preference.setDomainPreference(this); 143 getDataContext().commitChanges(); 144 return preference; 145 } 146 147 151 public void setPersistenceState(int state) { 152 153 if (state == PersistenceState.HOLLOW) { 155 properties = null; 156 } 157 158 super.setPersistenceState(state); 159 } 160 161 class DomainProperties extends Properties { 162 163 public Object setProperty(String key, String value) { 164 Object old = super.setProperty(key, value); 165 if (!Util.nullSafeEquals(old, value)) { 166 modified(); 167 } 168 169 return old; 170 } 171 172 void modified() { 173 encodeProperties(); 177 } 178 } 179 180 } 181 182 | Popular Tags |