Search in sources :

Example 26 with AbstractSession

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);
    }
}
Also used : CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 27 with AbstractSession

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);
}
Also used : CoreAttributeAccessor(org.eclipse.persistence.core.mappings.CoreAttributeAccessor) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) List(java.util.List) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) ContainerMapping(org.eclipse.persistence.mappings.ContainerMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) SDOProperty(org.eclipse.persistence.sdo.SDOProperty) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreContainerPolicy(org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)

Example 28 with AbstractSession

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);
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) SDOSetting(org.eclipse.persistence.sdo.SDOSetting) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Namespace(org.eclipse.persistence.internal.oxm.Namespace) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) Vector(java.util.Vector) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 29 with AbstractSession

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);
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SDOType(org.eclipse.persistence.sdo.SDOType) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) SDODataObject(org.eclipse.persistence.sdo.SDODataObject)

Example 30 with AbstractSession

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());
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) IsolatedClientSession(org.eclipse.persistence.internal.sessions.IsolatedClientSession) Employee(org.eclipse.persistence.testing.models.employee.domain.Employee) ByteArrayInputStream(java.io.ByteArrayInputStream) PhoneNumber(org.eclipse.persistence.testing.models.employee.domain.PhoneNumber) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) IsolatedClientSession(org.eclipse.persistence.internal.sessions.IsolatedClientSession) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)215 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)52 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)28 ArrayList (java.util.ArrayList)26 AbstractRecord (org.eclipse.persistence.internal.sessions.AbstractRecord)26 Vector (java.util.Vector)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)22 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)21 EntityManager (jakarta.persistence.EntityManager)19 List (java.util.List)18 Session (org.eclipse.persistence.sessions.Session)18 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)15 InvalidObject (org.eclipse.persistence.internal.helper.InvalidObject)14 UnitOfWorkImpl (org.eclipse.persistence.internal.sessions.UnitOfWorkImpl)14 HashMap (java.util.HashMap)12 Map (java.util.Map)12 Collection (java.util.Collection)11 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)11 SQLException (java.sql.SQLException)10 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)9