1 17 package org.alfresco.jcr.item; 18 19 import java.io.InputStream ; 20 import java.util.Calendar ; 21 22 import javax.jcr.PropertyType; 23 import javax.jcr.RepositoryException; 24 import javax.jcr.Value; 25 import javax.jcr.ValueFormatException; 26 27 import org.alfresco.jcr.session.SessionImpl; 28 import org.alfresco.jcr.util.JCRProxyFactory; 29 import org.alfresco.service.cmr.repository.ContentReader; 30 31 32 37 public class ValueImpl implements Value 38 { 39 private enum ValueState {Stream, Value, None}; 40 private ValueState state = ValueState.None; 41 42 private SessionImpl session; 43 private int datatype; 44 private Object value; 45 private InputStream stream = null; 46 47 private Value proxy; 48 49 50 55 public ValueImpl(SessionImpl session, int datatype, Object value) 56 { 57 this.session = session; 58 this.datatype = datatype; 59 this.value = value; 60 } 61 62 67 public Value getProxy() 68 { 69 if (proxy == null) 70 { 71 proxy = (Value)JCRProxyFactory.create(this, Value.class, session); 72 } 73 return proxy; 74 } 75 76 79 public String getString() throws ValueFormatException, IllegalStateException , RepositoryException 80 { 81 isValidState(ValueState.Value); 82 String typedValue = session.getTypeConverter().stringValue(getInternalValue()); 83 value = typedValue; 84 enterState(ValueState.Value); 85 return typedValue; 86 } 87 88 91 public InputStream getStream() throws IllegalStateException , RepositoryException 92 { 93 isValidState(ValueState.Stream); 94 if (stream == null) 95 { 96 stream = session.getTypeConverter().streamValue(value); 97 } 98 enterState(ValueState.Stream); 99 return stream; 100 } 101 102 105 public long getLong() throws ValueFormatException, IllegalStateException , RepositoryException 106 { 107 isValidState(ValueState.Value); 108 long typedValue = session.getTypeConverter().longValue(getInternalValue()); 109 value = typedValue; 110 enterState(ValueState.Value); 111 return typedValue; 112 } 113 114 117 public double getDouble() throws ValueFormatException, IllegalStateException , RepositoryException 118 { 119 isValidState(ValueState.Value); 120 double typedValue = session.getTypeConverter().doubleValue(getInternalValue()); 121 value = typedValue; 122 enterState(ValueState.Value); 123 return typedValue; 124 } 125 126 129 public Calendar getDate() throws ValueFormatException, IllegalStateException , RepositoryException 130 { 131 isValidState(ValueState.Value); 132 Calendar typedValue = session.getTypeConverter().dateValue(getInternalValue()); 133 value = typedValue.getTime(); 134 enterState(ValueState.Value); 135 return typedValue; 136 } 137 138 141 public boolean getBoolean() throws ValueFormatException, IllegalStateException , RepositoryException 142 { 143 isValidState(ValueState.Value); 144 boolean typedValue = session.getTypeConverter().booleanValue(getInternalValue()); 145 value = typedValue; 146 enterState(ValueState.Value); 147 return typedValue; 148 } 149 150 153 public int getType() 154 { 155 return datatype; 156 } 157 158 164 public static Object getValue(Value value) throws RepositoryException 165 { 166 Object objValue = null; 167 int valueType = value.getType(); 168 169 switch(valueType) 170 { 171 case PropertyType.STRING: 172 case PropertyType.NAME: 173 case PropertyType.PATH: 174 objValue = value.getString(); 175 break; 176 case PropertyType.LONG: 177 objValue = value.getLong(); 178 break; 179 case PropertyType.DOUBLE: 180 objValue = value.getDouble(); 181 break; 182 case PropertyType.BOOLEAN: 183 objValue = value.getBoolean(); 184 break; 185 case PropertyType.DATE: 186 objValue = value.getDate(); 187 break; 188 case PropertyType.BINARY: 189 objValue = value.getStream(); 190 break; 191 default: 192 objValue = ((ValueImpl)value).value; 194 break; 195 } 196 197 return objValue; 198 } 199 200 206 public static Object getValue(JCRTypeConverter typeConverter, int requiredType, Value value) throws RepositoryException 207 { 208 Object objValue = null; 209 210 switch(requiredType) 211 { 212 case PropertyType.STRING: 213 objValue = value.getString(); 214 break; 215 case PropertyType.LONG: 216 objValue = value.getLong(); 217 break; 218 case PropertyType.DOUBLE: 219 objValue = value.getDouble(); 220 break; 221 case PropertyType.BOOLEAN: 222 objValue = value.getBoolean(); 223 break; 224 case PropertyType.DATE: 225 objValue = value.getDate(); 226 break; 227 case PropertyType.BINARY: 228 objValue = value.getStream(); 229 break; 230 case PropertyType.NAME: 231 objValue = typeConverter.nameValue(ValueImpl.getValue(value)); 232 break; 233 case PropertyType.PATH: 234 objValue = typeConverter.pathValue(ValueImpl.getValue(value)); 235 break; 236 default: 237 throw new ValueFormatException("Unsupported Value Type " + requiredType); 238 } 239 240 return objValue; 241 } 242 243 251 private Object getInternalValue() 252 { 253 if (value instanceof ContentReader && state == ValueState.Value) 254 { 255 value = ((ContentReader)value).getReader(); 256 } 257 return value; 258 } 259 260 266 private void isValidState(ValueState state) 267 { 268 if (this.state != ValueState.None && this.state != state) 269 { 270 throw new IllegalStateException ("This value has already been retrieved as a " + state + " and cannot be retrieved as a " + ValueState.Stream + "."); 271 } 272 } 273 274 279 private void enterState(ValueState state) 280 { 281 this.state = state; 282 } 283 284 @Override 285 public boolean equals(Object obj) 286 { 287 if (obj == this) 288 { 289 return true; 290 } 291 if (!(obj instanceof ValueImpl)) 292 { 293 return false; 294 } 295 ValueImpl other = (ValueImpl)obj; 296 297 if (datatype != other.datatype) 299 { 300 return false; 301 } 302 303 if (value instanceof ContentReader) 305 { 306 String thisUrl = ((ContentReader)value).getContentUrl(); 307 String otherUrl = ((ContentReader)other).getContentUrl(); 308 return thisUrl.equals(otherUrl); 309 } 310 311 return value.equals(other.value); 313 } 314 315 @Override 316 public int hashCode() 317 { 318 return value.hashCode() * 32 + datatype; 319 } 320 321 } 322 | Popular Tags |