/** Get the value from the wrapped POJO, wrapping in DataObjects as necessary. */
 public Object getDeclaredProperty(int propertyIndex) {
   SDOProperty declaredProperty =
       (SDOProperty) dataObject.getType().getDeclaredProperties().get(propertyIndex);
   if (declaredProperty.getType().isChangeSummaryType()) {
     return dataObject.getChangeSummary();
   }
   Mapping mapping = this.getJAXBMappingForProperty(declaredProperty);
   Object value = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
   if (declaredProperty.isMany()) {
     JAXBListWrapper listWrapper = listWrappers.get(declaredProperty);
     if (null != listWrapper) {
       return listWrapper;
     }
     listWrapper = new JAXBListWrapper(this, declaredProperty);
     listWrappers.put(declaredProperty, listWrapper);
     return listWrapper;
   } else if (null == value || declaredProperty.getType().isDataType()) {
     return value;
   } else {
     if (declaredProperty.isContainment()) {
       return jaxbHelperContext.wrap(value, declaredProperty, dataObject);
     } else {
       return jaxbHelperContext.wrap(value);
     }
   }
 }
  /** Set the value on the underlying POJO, unwrapping values as necessary. */
  public void setDeclaredProperty(int propertyIndex, Object value) {
    SDOProperty declaredProperty =
        (SDOProperty) dataObject.getType().getDeclaredProperties().get(propertyIndex);
    if (declaredProperty.getType().isChangeSummaryType()) {
      return;
    }

    Mapping mapping = this.getJAXBMappingForProperty(declaredProperty);

    Object newValue = value;
    Object oldValue = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);

    if (declaredProperty.getType().isDataType()) {
      if (!declaredProperty.isMany()) {
        AbstractSession session =
            ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
        XMLDirectMapping directMapping = (XMLDirectMapping) mapping;
        if (directMapping.hasConverter()) {
          newValue = directMapping.getConverter().convertDataValueToObjectValue(newValue, session);
        } else {
          CoreField field = mapping.getField();
          newValue =
              session
                  .getDatasourcePlatform()
                  .getConversionManager()
                  .convertObject(
                      newValue,
                      descriptor.getObjectBuilder().getFieldClassification((XMLField) field));
        }
      }
      mapping.setAttributeValueInObject(entity, newValue);
    } else if (declaredProperty.isMany()) {
      // Get a ListWrapper and set it's current elements
      ListWrapper listWrapper = (ListWrapper) getDeclaredProperty(propertyIndex);
      listWrapper.addAll((List) newValue);
    } else {
      // OLD VALUE
      if (mapping.isAbstractCompositeObjectMapping()) {
        XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) mapping;
        if (oldValue != null && compositeMapping.getContainerAccessor() != null) {
          compositeMapping.getContainerAccessor().setAttributeValueInObject(oldValue, null);
        }
      }

      // NEW VALUE
      newValue = jaxbHelperContext.unwrap((DataObject) value);
      mapping.getAttributeAccessor().setAttributeValueInObject(entity, newValue);
      if (mapping.isAbstractCompositeObjectMapping()) {
        XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) mapping;
        if (value != null && compositeMapping.getContainerAccessor() != null) {
          compositeMapping.getContainerAccessor().setAttributeValueInObject(newValue, entity);
        }
      }
    }
  }
 public JAXBValueStore(JAXBHelperContext aJAXBHelperContext, Object anEntity) {
   this.jaxbHelperContext = aJAXBHelperContext;
   this.listWrappers = new WeakHashMap<Property, JAXBListWrapper>();
   JAXBContext jaxbContext = (JAXBContext) jaxbHelperContext.getJAXBContext();
   this.descriptor =
       (XMLDescriptor) jaxbContext.getXMLContext().getSession(anEntity).getDescriptor(anEntity);
   this.entity = anEntity;
 }
  /**
   * For isMany=false properties set the value to null. For isMany=true set the value to an empty
   * container of the appropriate type.
   */
  public void unsetDeclaredProperty(int propertyIndex) {
    SDOProperty declaredProperty =
        (SDOProperty) dataObject.getType().getDeclaredProperties().get(propertyIndex);
    Mapping mapping = this.getJAXBMappingForProperty(declaredProperty);
    if (declaredProperty.isMany()) {
      ContainerMapping containerMapping = (ContainerMapping) mapping;
      ContainerPolicy containerPolicy = containerMapping.getContainerPolicy();

      // OLD VALUE
      if (mapping.isAbstractCompositeCollectionMapping()) {
        XMLCompositeCollectionMapping compositeMapping = (XMLCompositeCollectionMapping) mapping;
        if (compositeMapping.getContainerAccessor() != null) {

          Object oldContainer = mapping.getAttributeValueFromObject(entity);
          if (oldContainer != null) {
            AbstractSession session =
                ((JAXBContext) jaxbHelperContext.getJAXBContext())
                    .getXMLContext()
                    .getSession(entity);
            Object iterator = containerPolicy.iteratorFor(oldContainer);
            while (containerPolicy.hasNext(iterator)) {
              Object oldValue = containerPolicy.next(iterator, session);
              compositeMapping.getContainerAccessor().setAttributeValueInObject(oldValue, null);
            }
          }
        }
      }

      // NEW VALUE
      Object container = containerPolicy.containerInstance();
      mapping.getAttributeAccessor().setAttributeValueInObject(entity, container);
    } else {
      // OLD VALUE
      Object oldValue = mapping.getAttributeAccessor().getAttributeValueFromObject(entity);
      if (mapping.isAbstractCompositeObjectMapping()) {
        XMLCompositeObjectMapping compositeMapping = (XMLCompositeObjectMapping) mapping;
        if (compositeMapping.getContainerAccessor() != null) {
          if (oldValue != null) {
            compositeMapping.getContainerAccessor().setAttributeValueInObject(oldValue, null);
          }
        }
      }

      // NEW VALUE
      mapping.getAttributeAccessor().setAttributeValueInObject(entity, null);
    }
  }
  public ValueStore copy() {
    AbstractSession session =
        ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
    Object originalEntity = entity;
    entity = descriptor.getInstantiationPolicy().buildNewInstance();

    for (SDOProperty sdoProperty : (List<SDOProperty>) dataObject.getType().getProperties()) {
      if (!sdoProperty.getType().isChangeSummaryType()) {
        Mapping mapping = getJAXBMappingForProperty(sdoProperty);
        CoreAttributeAccessor attributeAccessor = mapping.getAttributeAccessor();
        Object attributeValue = attributeAccessor.getAttributeValueFromObject(originalEntity);
        if (mapping.isCollectionMapping()) {
          Object containerCopy = null;
          SDOChangeSummary sdoChangeSummary = dataObject.getChangeSummary();
          if (null != sdoChangeSummary) {
            List list = listWrappers.get(sdoProperty);
            containerCopy = sdoChangeSummary.getOriginalElements().get(list);
          }
          if (null == containerCopy) {
            CoreContainerPolicy containerPolicy = mapping.getContainerPolicy();
            containerCopy = containerPolicy.containerInstance();
            if (null != attributeValue) {
              Object attributeValueIterator = containerPolicy.iteratorFor(attributeValue);
              while (containerPolicy.hasNext(attributeValueIterator)) {
                containerPolicy.addInto(
                    containerPolicy.nextEntry(attributeValueIterator), containerCopy, session);
              }
            }
          }
          attributeAccessor.setAttributeValueInObject(entity, containerCopy);
        } else {
          attributeAccessor.setAttributeValueInObject(entity, attributeValue);
        }
      }
    }
    return new JAXBValueStore(
        jaxbHelperContext, originalEntity, descriptor, dataObject, listWrappers);
  }
 public JAXBValueStore(JAXBHelperContext aJAXBHelperContext, SDOType sdoType) {
   this.jaxbHelperContext = aJAXBHelperContext;
   this.listWrappers = new WeakHashMap<Property, JAXBListWrapper>();
   this.descriptor = jaxbHelperContext.getObjectDescriptor(sdoType);
   this.entity = this.descriptor.getInstantiationPolicy().buildNewInstance();
 }