use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.
the class JAXBValueStore method unsetDeclaredProperty.
/**
* For isMany=false properties set the value to null. For isMany=true set
* the value to an empty container of the appropriate type.
*/
@Override
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;
XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != 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);
inverseReferenceMapping.getAttributeAccessor().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;
final XMLInverseReferenceMapping inverseReferenceMapping = compositeMapping.getInverseReferenceMapping();
if (inverseReferenceMapping != null && inverseReferenceMapping.getAttributeAccessor() != null) {
if (oldValue != null) {
inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(oldValue, null);
}
}
}
// NEW VALUE
mapping.getAttributeAccessor().setAttributeValueInObject(entity, null);
}
}
use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.
the class JAXBValueStore method copy.
@Override
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);
}
use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.
the class SDOMarshalListener method beforeMarshal.
@Override
public void beforeMarshal(Object obj) {
if (obj instanceof SDOChangeSummary) {
SDOChangeSummary changeSummary = ((SDOChangeSummary) obj);
// CREATED - build a list of xpaths to write to the created attribute
// this must be done dynamically because the xpath is relative to the marshalledObject
// so it can't be calculated until we know what object is being marshalled
List createdSet = changeSummary.getCreated();
List xpaths = new ArrayList(createdSet.size());
String rootElementName = this.marshalledObjectRootQName.getLocalPart();
String rootNamespaceUri = this.marshalledObjectRootQName.getNamespaceURI();
if (rootNamespaceUri != null && !rootNamespaceUri.equals(XMLConstants.EMPTY_STRING)) {
org.eclipse.persistence.internal.oxm.NamespaceResolver resolver = getRootMarshalRecord().getNamespaceResolver();
if (resolver != null) {
String prefix = resolver.resolveNamespaceURI(this.marshalledObjectRootQName.getNamespaceURI());
if (prefix != null) {
rootElementName = prefix + XMLConstants.COLON + rootElementName;
}
}
}
if ((createdSet != null) && (createdSet.size() > 0)) {
Iterator anIterator = createdSet.iterator();
SDODataObject nextCreatedDO = null;
while (anIterator.hasNext()) {
// get path to the changeSummaryRoot (may not be the root marshalled object - may be internal)
nextCreatedDO = ((SDODataObject) anIterator.next());
String nextPath = getPathFromAncestor(nextCreatedDO, (SDODataObject) marshalledObject, changeSummary);
// Add sdoRef attribute...all modified objects written should have this
if (nextPath == SDOConstants.EMPTY_STRING) {
// if this is the root, just put the root element
xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + //
SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName);
} else {
xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + //
SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + nextPath);
}
}
}
changeSummary.setCreatedXPaths(xpaths);
// Build xpathToCS
String xpathMarshalledObjToCS = getPathFromAncestor(changeSummary.getRootObject(), (SDODataObject) marshalledObject, changeSummary);
String xpathChangeSumProp = getXPathForProperty(changeSummary.getRootObject().getType().getChangeSummaryProperty());
String xpathToCS = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName;
// check if the CS is at the local-cs-root or is in a child property
if ((xpathMarshalledObjToCS != null) && !xpathMarshalledObjToCS.equals(SDOConstants.EMPTY_STRING)) {
// SDO_XPATH_TO_ROOT)) {
// CS is not on the root
xpathToCS = xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathMarshalledObjToCS;
}
xpathToCS = //
xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathChangeSumProp + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
// MODIFIED && DELETED
List deletedXPaths = new ArrayList();
Document document = XMLPlatformFactory.getInstance().getXMLPlatform().createDocument();
Element csNode = null;
List modifiedItems = changeSummary.getModified();
int modifiedSize = modifiedItems.size();
List newNodes = new ArrayList(modifiedSize);
SDODataObject nextModifiedDO = null;
// Iterate through CS modified items
for (int i = 0; i < modifiedSize; i++) {
nextModifiedDO = (SDODataObject) modifiedItems.get(i);
String sdoPrefix = typeHelper.getPrefix(SDOConstants.SDO_URL);
// List unsetPropNames = new ArrayList();
String uri = getURI(nextModifiedDO);
String qualifiedName = getQualifiedName(nextModifiedDO);
String sdoRefPrefix = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
if (uri == null) {
csNode = document.createElement(qualifiedName);
} else {
csNode = document.createElementNS(uri, qualifiedName);
}
String nextPath = getPathFromAncestor(nextModifiedDO, (SDODataObject) marshalledObject, changeSummary);
// Add sdoRef attribute...all modified objects written should have this
if (nextPath == SDOConstants.EMPTY_STRING) {
// if this is the root, just put the root element
csNode.setAttributeNS(SDOConstants.SDO_URL, //
sdoPrefix + //
SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + //
SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName);
} else {
csNode.setAttributeNS(SDOConstants.SDO_URL, //
sdoPrefix + //
SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + //
SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName + "/" + nextPath);
}
// Bug6346754 Add all namespaces if they are not yet declared above.
Vector namespaces = nextModifiedDO.getType().getXmlDescriptor().getNonNullNamespaceResolver().getNamespaces();
for (int j = 0; j < namespaces.size(); j++) {
Namespace next = (Namespace) namespaces.get(j);
if (declareNamespace(next.getNamespaceURI(), next.getPrefix(), changeSummary.getRootObject())) {
csNode.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + next.getPrefix(), next.getNamespaceURI());
}
}
List nextDOSettings = changeSummary.getOldValues(nextModifiedDO);
DOMRecord row = new DOMRecord(csNode);
Session session = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlContext().getSession();
row.setSession((AbstractSession) session);
// Iterate through SDOSettings for the current modified Object
SDOSetting nextSetting = null;
for (int j = 0; j < nextDOSettings.size(); j++) {
nextSetting = (SDOSetting) nextDOSettings.get(j);
if (nextSetting.isSet()) {
if (!nextSetting.getProperty().getType().isDataType()) {
if (nextSetting.getProperty().isMany()) {
List values = (List) nextSetting.getValue();
for (int k = 0; k < values.size(); k++) {
doMarshal(//
nextSetting.getProperty(), //
(DataObject) values.get(k), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
}
} else {
doMarshal(//
nextSetting.getProperty(), //
(DataObject) nextSetting.getValue(), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
}
} else {
// This writes out simple values
Object value = nextSetting.getValue();
if (value == null) {
// Marshal out xsi:nil=true
marshalNilAttribute(nextSetting.getProperty(), row);
} else {
String xPath = getXPathForProperty(nextSetting.getProperty());
XMLField field = new XMLField(xPath);
field.setNamespaceResolver(typeHelper.getNamespaceResolver());
row.put(field, value);
}
}
}
}
List unsetPropNames = changeSummary.getUnsetProps(nextModifiedDO);
if (!unsetPropNames.isEmpty()) {
XMLConversionManager xmlConversionManager = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlConversionManager();
String unsetPropsString = xmlConversionManager.convertObject(unsetPropNames, String.class);
csNode.setAttributeNS(SDOConstants.SDO_URL, //
sdoPrefix + //
SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + SDOConstants.CHANGESUMMARY_UNSET, unsetPropsString);
}
newNodes.add(csNode);
}
changeSummary.setDeletedXPaths(deletedXPaths);
changeSummary.setModifiedDoms(newNodes);
}
}
use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.
the class SDOMarshalListener method doMarshal.
private //
void doMarshal(//
SDOProperty prop, //
DataObject value, //
SDOChangeSummary cs, Element csNode, SDODataObject modifiedObject, List deletedXPaths, String xpathToCS, String sdoPrefix, String rootElementName) {
if (value == null) {
// Marshal out xsi:nil=true
DOMRecord row = new DOMRecord(csNode);
Session session = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlContext().getSession();
row.setSession((AbstractSession) session);
marshalNilAttribute(prop, row);
return;
}
boolean isDeleted = false;
// Do we need a second map or can we just check the values of the deleted map
Object original = cs.getReverseDeletedMap().get(value);
if ((original != null) && cs.isDeleted((DataObject) original)) {
isDeleted = true;
}
String qualifiedName = getXPathForProperty(prop);
String uri = null;
if (prop.isOpenContent()) {
uri = prop.getUri();
} else {
uri = ((XMLField) prop.getXmlMapping().getField()).getXPathFragment().getNamespaceURI();
}
if (isDeleted) {
String pathToNode = getPathFromAncestor(((SDODataObject) original), modifiedObject, cs);
String containerPath = null;
containerPath = getQualifiedName(modifiedObject);
deletedXPaths.add(xpathToCS + containerPath + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + pathToNode);
XMLRoot xmlroot = new XMLRoot();
// set the object to the deep copy
xmlroot.setObject(value);
xmlroot.setNamespaceURI(uri);
xmlroot.setLocalName(qualifiedName);
xmlMarshaller.marshal(xmlroot, csNode);
} else {
// need sdoref
Element modifiedElement = null;
if (uri == null) {
modifiedElement = csNode.getOwnerDocument().createElement(qualifiedName);
} else {
modifiedElement = csNode.getOwnerDocument().createElementNS(uri, qualifiedName);
}
csNode.appendChild(modifiedElement);
String nextPath = getPathFromAncestor((SDODataObject) original, (SDODataObject) marshalledObject, cs);
// Add sdoRef attribute...all modified objects written should have this
if (nextPath == SDOConstants.EMPTY_STRING) {
// if this is the root, just put the root element
modifiedElement.setAttributeNS(SDOConstants.SDO_URL, //
sdoPrefix + //
SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + //
SDOConstants.CHANGESUMMARY_REF, SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + //
SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName);
} else {
modifiedElement.setAttributeNS(SDOConstants.SDO_URL, //
sdoPrefix + //
SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + //
SDOConstants.CHANGESUMMARY_REF, SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + //
SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName + "/" + nextPath);
}
// Added for bug 6346754
if ((((SDODataObject) original).getContainmentProperty() != null) && ((SDODataObject) original).getContainmentProperty().getType().isDataObjectType()) {
// may also need xsi:type
String schemaContext = ((SDOType) value.getType()).getXmlDescriptor().getSchemaReference().getSchemaContext();
QName schemaContextQName = ((SDOType) value.getType()).getXmlDescriptor().getSchemaReference().getSchemaContextAsQName(((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver());
if (schemaContext != null) {
String typeValue = schemaContext.substring(1, schemaContext.length());
String schemaInstancePrefix = ((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver().resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
// may or may not need the xmlns declaration added.
String schemaContextUri = schemaContextQName.getNamespaceURI();
String schemaContextPrefix = ((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver().resolveNamespaceURI(schemaContextUri);
if (schemaContextPrefix != null) {
modifiedElement.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + schemaContextPrefix, schemaContextQName.getNamespaceURI());
}
modifiedElement.setAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, schemaInstancePrefix + XMLConstants.COLON + XMLConstants.SCHEMA_TYPE_ATTRIBUTE, typeValue);
}
}
}
}
use of org.eclipse.persistence.internal.sessions.AbstractSession in project eclipselink by eclipse-ee4j.
the class DeepMergeCloneSerializedTest method test.
/*
* This test creates an object and registers it with a unit of work. It then serializes that
* object and deserializes it. Adds an object onto the origional then performs serialization
* sequence again. Then deepMergeClone is attempted and the results are compared to verify that
* the merge worked.
*/
@Override
public void test() {
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteStream);
// create the phoneNumber object
Employee empClone;
Session session = getSession();
org.eclipse.persistence.sessions.UnitOfWork uow = session.acquireUnitOfWork();
this.empObject = (Employee) session.readObject(Employee.class, new org.eclipse.persistence.expressions.ExpressionBuilder().get("firstName").equal("Bob"));
ClassDescriptor descriptor = session.getDescriptor(this.empObject);
if (descriptor.isProtectedIsolation() && descriptor.shouldIsolateProtectedObjectsInUnitOfWork() && session instanceof IsolatedClientSession) {
// this will have read a version of the protected Entity into the Isolated Cache even though the test wants to isolated to UOW
// replace with actual shared cache version
this.empObject = (Employee) ((AbstractSession) session).getParentIdentityMapSession(descriptor, false, true).getIdentityMapAccessor().getFromIdentityMap(this.empObject);
}
// force instantiations of value holders before serialization
this.empObject.getPhoneNumbers();
if (this.empObject.getManager() != null) {
this.empObject.getManager().getManagedEmployees();
}
this.empObject.getResponsibilitiesList();
// serialize object by writing to a stream
stream.writeObject(this.empObject);
stream.flush();
byte[] arr = byteStream.toByteArray();
ByteArrayInputStream inByteStream = new ByteArrayInputStream(arr);
ObjectInputStream inObjStream = new ObjectInputStream(inByteStream);
Employee deserialEmp;
// deserialize the object
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
// add a new manager, test 1-m's
Employee newManager = new org.eclipse.persistence.testing.models.employee.domain.Employee();
if (deserialEmp.getManager() != null) {
deserialEmp.getManager().removeManagedEmployee(deserialEmp);
this.removedPhone = (PhoneNumber) deserialEmp.getPhoneNumbers().firstElement();
deserialEmp.getPhoneNumbers().removeElement(deserialEmp.getPhoneNumbers().firstElement());
}
newManager.addManagedEmployee(deserialEmp);
// add the PhoneNumber object to the origional clone, test 1-1
PhoneNumber phone = new org.eclipse.persistence.testing.models.employee.domain.PhoneNumber();
phone.setNumber("5555897");
phone.setType("Fax");
phone.setOwner(deserialEmp);
deserialEmp.addPhoneNumber(phone);
this.addedPhone = phone;
deserialEmp.setLastName("Willford");
this.gender = deserialEmp.getGender();
if (deserialEmp.getGender().equals("Female")) {
deserialEmp.setMale();
} else {
deserialEmp.setFemale();
}
this.endDate = deserialEmp.getPeriod().getEndDate();
deserialEmp.getPeriod().setEndDate(new java.sql.Date(System.currentTimeMillis() + 300000L));
this.endTime = deserialEmp.getEndTime();
deserialEmp.setEndTime(Helper.timeFromHourMinuteSecond(15, 2, 3));
deserialEmp.addResponsibility("A Very New Respons");
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
// send the ammended object back through the serialization process
stream.writeObject(deserialEmp);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
// merge the ammended clone with the unit of work
empClone = (Employee) uow.deepMergeClone(deserialEmp);
uow.commit();
uow = session.acquireUnitOfWork();
// do the serialization for the second time
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
stream.writeObject(this.empObject);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
// attempt to deserialize the object
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
deserialEmp.setFirstName("Danny");
this.origional = deserialEmp;
byteStream = new ByteArrayOutputStream();
stream = new ObjectOutputStream(byteStream);
// send the ammended object back through the serialization process
stream.writeObject(deserialEmp);
stream.flush();
arr = byteStream.toByteArray();
inByteStream = new ByteArrayInputStream(arr);
inObjStream = new ObjectInputStream(inByteStream);
try {
deserialEmp = (Employee) inObjStream.readObject();
} catch (ClassNotFoundException e) {
throw new TestErrorException("Could not deserialize object " + e.toString());
}
deserialEmp = (Employee) uow.deepMergeClone(deserialEmp);
uow.commit();
this.mergedClone = deserialEmp;
} catch (IOException e) {
throw new TestErrorException("Error running Test " + e.toString());
}
}
Aggregations