public void addRefValue(Attribute attribute, ReferenceInfo info) throws RaplaException { if (info == null) { return; } if (attribute.getRefType() != info.getType()) { throw new RaplaException( "Different reference type exepcted " + attribute.getRefType() + " but was " + info.getType()); } String attributeKey = attribute.getKey(); final String id = info.getId(); addValue(attributeKey, id); }
@Override public Iterable<ReferenceInfo> getReferenceInfo() { List<ReferenceInfo> result = new ArrayList<ReferenceInfo>(); String parentId = getParentId(); result.add(new ReferenceInfo(parentId, DynamicType.class)); DynamicTypeImpl type = getType(); for (Map.Entry<String, List<String>> entry : data.entrySet()) { String key = entry.getKey(); Attribute attribute = type.getAttribute(key); if (attribute == null) { continue; } Class<? extends Entity> refType = attribute.getRefType(); if (refType == null) { continue; } List<String> values = entry.getValue(); if (values != null) { for (String value : values) { result.add(new ReferenceInfo(value, refType)); } } } return result; }
private Object fromString(Attribute attribute, EntityResolver resolver, String value) throws EntityNotFoundException, IllegalStateException { Class<? extends Entity> refType = attribute.getRefType(); if (refType != null) { Entity resolved = resolver.resolve(value, refType); return resolved; } try { Object result = AttributeImpl.parseAttributeValueWithoutRef(attribute, value); return result; } catch (RaplaException exception) { throw new IllegalStateException(exception.getMessage(), exception); } }
/** * returns the string representation of the given value. if attribute is a reference then the id * of the referenced object is returned. */ private String toStringValue(Attribute attribute, Object value) { String stringValue = null; Class<? extends Entity> refType = attribute.getRefType(); AttributeType attributeType = attribute.getType(); if (refType != null) { if (value instanceof Entity && ((Entity) value).getTypeClass() == refType) { stringValue = ((Entity) value).getId(); } else { throw new IllegalArgumentException( "entity expected. but id used please use addRefValue instead of addValue in reading"); } } else if (attributeType.equals(AttributeType.DATE)) { return new SerializableDateTimeFormat().formatDate((Date) value); } else if (value != null) { stringValue = value.toString(); } return stringValue; }
@Override public void replace(ReferenceInfo origId, ReferenceInfo newId) { final Set<Entry<String, List<String>>> entrySet = data.entrySet(); for (Entry<String, List<String>> entry : entrySet) { final String attributeKey = entry.getKey(); final Attribute attribute = getAttribute(attributeKey); if (attribute.getRefType() == Allocatable.class) { final List<String> list = entry.getValue(); final String origIdString = origId.getId(); if (list.contains(origIdString)) { list.remove(origIdString); final String newIdString = newId.getId(); if (!list.contains(newIdString)) { list.add(newIdString); } } } } }