/** * INTERNAL: The mapping is initialized with the given session. This mapping is fully initialized * after this. */ public void initialize(AbstractSession session) throws DescriptorException { // modified so that reference class on composite mappings is no longer mandatory String referenceClassName = getReferenceClassName(); if (this.referenceClass == null && referenceClassName != null) { if (!referenceClassName.equals(XMLConstants.UNKNOWN_OR_TRANSIENT_CLASS)) { setReferenceClass( session .getDatasourcePlatform() .getConversionManager() .convertClassNameToClass(referenceClassName)); } } initializeReferenceDescriptorAndField(session); ContainerPolicy cp = getContainerPolicy(); if (cp != null) { if (cp.getContainerClass() == null) { Class cls = session .getDatasourcePlatform() .getConversionManager() .convertClassNameToClass(cp.getContainerClassName()); cp.setContainerClass(cls); } if (cp instanceof MapContainerPolicy) { initializeMapContainerPolicy(session, (MapContainerPolicy) cp); } } if (null != getContainerAccessor()) { getContainerAccessor().initializeAttributes(this.referenceClass); } }
/** INTERNAL: Get a value from the object and set that in the respective field of the row. */ @Override public Object valueFromObject(Object object, DatabaseField field, AbstractSession session) { // First check if the value can be obtained from the value holder's row. AbstractRecord referenceRow = getIndirectionPolicy().extractReferenceRow(getAttributeValueFromObject(object)); if (referenceRow != null) { Object value = referenceRow.get(field); // Must ensure the classification to get a cache hit. try { value = session.getDatasourcePlatform().convertObject(value, getFieldClassification(field)); } catch (ConversionException e) { throw ConversionException.couldNotBeConverted(this, getDescriptor(), e); } return value; } // 2.5.1.6 PWK. added to support batch reading on variable one to ones Object referenceObject = getRealAttributeValueFromObject(object, session); String queryKeyName = (String) getSourceToTargetQueryKeyNames().get(field); ClassDescriptor objectDescriptor = session.getDescriptor(referenceObject.getClass()); DatabaseField targetField = objectDescriptor.getObjectBuilder().getTargetFieldForQueryKeyName(queryKeyName); if (targetField == null) { return null; } return objectDescriptor .getObjectBuilder() .extractValueFromObjectForField(referenceObject, targetField, session); }
/** 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); } } } }
/** INTERNAL: Return the implementor for a specified type */ protected Object getImplementorForType(Object type, AbstractSession session) { if (type == null) { return getTypeIndicatorTranslation().get(Helper.getNullWrapper()); } // Must ensure the type is the same, i.e. Integer != BigDecimal. try { type = session.getDatasourcePlatform().convertObject(type, getTypeField().getType()); } catch (ConversionException e) { throw ConversionException.couldNotBeConverted(this, getDescriptor(), e); } return getTypeIndicatorTranslation().get(type); }
/** * INTERNAL: The mapping is initialized with the given session. This mapping is fully initialized * after this. */ public void initialize(AbstractSession session) throws DescriptorException { super.initialize(session); ContainerPolicy cp = getContainerPolicy(); if (cp != null) { if (cp.getContainerClass() == null) { Class cls = session .getDatasourcePlatform() .getConversionManager() .convertClassNameToClass(cp.getContainerClassName()); cp.setContainerClass(cls); } } }
/** * INTERNAL: Extract the primary key values from the row, then create an * org.eclipse.persistence.internal.oxm.Reference instance and stored it on the session's * org.eclipse.persistence.internal.oxm.ReferenceResolver. */ public Object readFromRowIntoObject( AbstractRecord databaseRow, JoinedAttributeManager joinManager, Object targetObject, ObjectBuildingQuery sourceQuery, AbstractSession executionSession) throws DatabaseException { ClassDescriptor descriptor = sourceQuery.getSession().getClassDescriptor(getReferenceClass()); ContainerPolicy cp = this.getContainerPolicy(); Vector pkFieldNames = referenceDescriptor.getPrimaryKeyFieldNames(); Vector primaryKeyValues = new Vector(); primaryKeyValues.setSize(pkFieldNames.size()); HashMap primaryKeyMap = new HashMap(); // for each source xmlField, get the value from the row and store for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) { XMLField fld = (XMLField) fieldIt.next(); XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(fld); Object fieldValue = databaseRow.getValues(fld); if ((fieldValue == null) || (fieldValue instanceof String) || !(fieldValue instanceof Vector)) { return cp.containerInstance(); } // fix for bug# 5687430 // need to get the actual type of the target (i.e. int, String, etc.) // and use the converted value when checking the cache. XMLConversionManager xmlConversionManager = (XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager(); Vector newValues = new Vector(); for (Iterator valIt = ((Vector) fieldValue).iterator(); valIt.hasNext(); ) { for (StringTokenizer stok = new StringTokenizer((String) valIt.next()); stok.hasMoreTokens(); ) { Object value = xmlConversionManager.convertObject( stok.nextToken(), descriptor.getTypedField(tgtFld).getType()); if (value != null) { newValues.add(value); } } } primaryKeyMap.put(tgtFld.getXPath(), newValues); } // store the Reference instance on the resolver for use during mapping resolution phase ReferenceResolver resolver = ReferenceResolver.getInstance(sourceQuery.getSession()); if (resolver != null) { resolver.addReference(new Reference(this, targetObject, referenceClass, primaryKeyMap)); } return null; }
private Object convertToSimpleTypeIfPresent( Object objectToAdd, AbstractRecord nestedRow, AbstractSession executionSession) { String stringValue = null; Element theElement = ((Element) ((DOMRecord) nestedRow).getDOM()); Node textchild = theElement.getFirstChild(); if ((textchild != null) && (textchild.getNodeType() == Node.TEXT_NODE)) { stringValue = ((Text) textchild).getNodeValue(); if (stringValue != null && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT) { objectToAdd = stringValue; } } if ((stringValue == null) || stringValue.length() == 0) { return objectToAdd; } String type = theElement.getAttributeNS( javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE); if ((null != type) && type.length() > 0) { XPathFragment typeFragment = new XPathFragment(type); String namespaceURI = ((DOMRecord) nestedRow).resolveNamespacePrefix(typeFragment.getPrefix()); typeFragment.setNamespaceURI(namespaceURI); QName schemaTypeQName = new QName(namespaceURI, typeFragment.getLocalName()); ConversionManager conversionManager = (ConversionManager) executionSession.getDatasourcePlatform().getConversionManager(); Class theClass = conversionManager.javaType(schemaTypeQName); if (theClass != null) { objectToAdd = conversionManager.convertObject(stringValue, theClass, schemaTypeQName); } } return objectToAdd; }
/** * INTERNAL: Create (if necessary) and populate a reference object that will be used during the * mapping reference resolution phase after unmarshalling is complete. * * @param record * @param xmlField * @param object * @param session * @return */ public void buildReference( UnmarshalRecord record, XMLField xmlField, Object object, AbstractSession session) { ReferenceResolver resolver = ReferenceResolver.getInstance(session); if (resolver == null) { return; } Object srcObject = record.getCurrentObject(); Reference reference = resolver.getReference(this, srcObject); // if reference is null, create a new instance and set it on the resolver if (reference == null) { reference = new Reference(this, srcObject, getReferenceClass(), new HashMap()); resolver.addReference(reference); } XMLField tgtFld = (XMLField) getSourceToTargetKeyFieldAssociations().get(xmlField); String tgtXPath = tgtFld.getXPath(); Vector pks; HashMap primaryKeyMap = reference.getPrimaryKeyMap(); if (primaryKeyMap.containsKey(tgtXPath)) { pks = (Vector) primaryKeyMap.get(tgtXPath); } else { pks = new Vector(); primaryKeyMap.put(tgtXPath, pks); } ClassDescriptor descriptor = session.getClassDescriptor(getReferenceClass()); Class type = descriptor.getTypedField(tgtFld).getType(); XMLConversionManager xmlConversionManager = (XMLConversionManager) session.getDatasourcePlatform().getConversionManager(); for (StringTokenizer stok = new StringTokenizer((String) object); stok.hasMoreTokens(); ) { Object value = xmlConversionManager.convertObject(stok.nextToken(), type); if (value != null) { pks.add(value); } } }
protected String getValueToWrite(QName schemaType, Object value, AbstractSession session) { return (String) ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()) .convertObject(value, ClassConstants.STRING, schemaType); }
/** INTERNAL: */ public void validationAfterDescriptorInitialization(AbstractSession session) { Hashtable mapped = new Hashtable(); for (int operation = INSERT; operation <= UPDATE; operation++) { if ((main[operation][MAPPED] != null) && !main[operation][MAPPED].isEmpty()) { Iterator it = main[operation][MAPPED].iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); mapped.put(field, field); } } } if (!mapped.isEmpty()) { for (Enumeration fields = getDescriptor().getFields().elements(); fields.hasMoreElements(); ) { DatabaseField fieldInDescriptor = (DatabaseField) fields.nextElement(); DatabaseField fieldInMain = (DatabaseField) mapped.get(fieldInDescriptor); if (fieldInMain != null) { if (fieldInMain.getType() == null) { if (getDescriptor().isReturnTypeRequiredForReturningPolicy()) { session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyMappedFieldTypeNotSet( fieldInMain.getName(), getDescriptor())); } } else if (isThereATypeConflict(fieldInMain, fieldInDescriptor)) { session .getIntegrityChecker() .handleError( DescriptorException.returningPolicyAndDescriptorFieldTypeConflict( fieldInMain.getName(), fieldInMain.getType().getName(), fieldInDescriptor.getType().getName(), getDescriptor())); } } } } if (!(session.getDatasourcePlatform() instanceof DatabasePlatform)) { // don't attempt further diagnostics on non-relational platforms return; } WriteObjectQuery[] query = { getDescriptor().getQueryManager().getInsertQuery(), getDescriptor().getQueryManager().getUpdateQuery() }; String[] queryTypeName = {"InsertObjectQuery", "UpdateObjectQuery"}; for (int operation = INSERT; operation <= UPDATE; operation++) { if ((main[operation][ALL] != null) && !main[operation][ALL].isEmpty()) { // this operation requires some fields to be returned if ((query[operation] == null) || (query[operation].getDatasourceCall() == null)) { if (!session.getPlatform().canBuildCallWithReturning()) { session .getIntegrityChecker() .handleError( DescriptorException.noCustomQueryForReturningPolicy( queryTypeName[operation], Helper.getShortClassName(session.getPlatform()), getDescriptor())); } } else if (query[operation].getDatasourceCall() instanceof StoredProcedureCall) { // SQLCall with custom SQL calculates its outputRowFields later (in prepare() method) - // that's why SQLCall can't be verified here. DatabaseCall customCall = (DatabaseCall) query[operation].getDatasourceCall(); Enumeration outputRowFields = customCall.getOutputRowFields().elements(); Collection notFoundInOutputRow = createCollection(); notFoundInOutputRow.addAll(main[operation][ALL]); while (outputRowFields.hasMoreElements()) { notFoundInOutputRow.remove(outputRowFields.nextElement()); } if (!notFoundInOutputRow.isEmpty()) { Iterator it = notFoundInOutputRow.iterator(); while (it.hasNext()) { DatabaseField field = (DatabaseField) it.next(); session .getIntegrityChecker() .handleError( DescriptorException.customQueryAndReturningPolicyFieldConflict( field.getName(), queryTypeName[operation], getDescriptor())); } } } } } }
public Object valueFromRow( AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery query, AbstractSession executionSession) { ContainerPolicy cp = this.getContainerPolicy(); Object fieldValue = row.getValues(this.getField()); if (fieldValue == null) { if (reuseContainer) { Object currentObject = ((XMLRecord) row).getCurrentObject(); Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject); return container != null ? container : cp.containerInstance(); } else { return cp.containerInstance(); } } Vector fieldValues = this.getDescriptor().buildDirectValuesFromFieldValue(fieldValue); if (fieldValues == null) { if (reuseContainer) { Object currentObject = ((XMLRecord) row).getCurrentObject(); Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject); return container != null ? container : cp.containerInstance(); } else { return cp.containerInstance(); } } Object result = null; if (reuseContainer) { Object currentObject = ((XMLRecord) row).getCurrentObject(); Object container = getAttributeAccessor().getAttributeValueFromObject(currentObject); result = container != null ? container : cp.containerInstance(); } else { result = cp.containerInstance(fieldValues.size()); } for (Enumeration stream = fieldValues.elements(); stream.hasMoreElements(); ) { Object element = stream.nextElement(); // PERF: Direct variable access. // Object value = row.get(field); // Object fieldValue = null; XMLUnmarshaller unmarshaller = ((XMLRecord) row).getUnmarshaller(); if (element instanceof String) { if (this.isSwaRef() && (unmarshaller.getAttachmentUnmarshaller() != null)) { fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler((String) element); } else if (!this.isSwaRef()) { // should be base64 byte[] bytes = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()) .convertSchemaBase64ToByteArray(element); fieldValue = bytes; } } else { // this was an element, so do the XOP/SWAREF/Inline binary cases for an element XMLRecord record = (XMLRecord) element; record.setSession(executionSession); if ((unmarshaller.getAttachmentUnmarshaller() != null) && unmarshaller.getAttachmentUnmarshaller().isXOPPackage() && !this.isSwaRef() && !this.shouldInlineBinaryData()) { // look for the include element: String xpath = XMLConstants.EMPTY_STRING; // need a prefix for XOP String prefix = null; NamespaceResolver descriptorResolver = ((XMLDescriptor) getDescriptor()).getNamespaceResolver(); if (descriptorResolver != null) { prefix = descriptorResolver.resolveNamespaceURI(XMLConstants.XOP_URL); } if (prefix == null) { prefix = XMLConstants.XOP_PREFIX; } NamespaceResolver tempResolver = new NamespaceResolver(); tempResolver.put(prefix, XMLConstants.XOP_URL); xpath = prefix + XMLConstants.COLON + INCLUDE + "/@href"; XMLField field = new XMLField(xpath); field.setNamespaceResolver(tempResolver); String includeValue = (String) record.get(field); if (element != null && includeValue != null) { if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) { fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsByteArray(includeValue); } else { fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(includeValue); } } else { // If we didn't find the Include element, check for inline fieldValue = record.get(XMLConstants.TEXT); // should be a base64 string fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()) .convertSchemaBase64ToByteArray(fieldValue); } } else if ((unmarshaller.getAttachmentUnmarshaller() != null) && isSwaRef()) { String refValue = (String) record.get(XMLConstants.TEXT); if (refValue != null) { fieldValue = unmarshaller.getAttachmentUnmarshaller().getAttachmentAsDataHandler(refValue); } } else { fieldValue = record.get(XMLConstants.TEXT); // should be a base64 string fieldValue = ((XMLConversionManager) executionSession.getDatasourcePlatform().getConversionManager()) .convertSchemaBase64ToByteArray(fieldValue); } } Object attributeValue = fieldValue; if (getValueConverter() != null) { if (getValueConverter() instanceof XMLConverter) { attributeValue = ((XMLConverter) getValueConverter()) .convertDataValueToObjectValue(fieldValue, executionSession, unmarshaller); } else { attributeValue = getValueConverter().convertDataValueToObjectValue(fieldValue, executionSession); } } cp.addInto(attributeValue, result, query.getSession()); } return result; }
public Object getValueToWrite( Object value, Object parent, XMLRecord record, XMLField field, XMLField includeField, AbstractSession session) { XMLMarshaller marshaller = record.getMarshaller(); Object element = value; boolean isAttribute = ((XMLField) getField()).getLastXPathFragment().isAttribute(); if (getValueConverter() != null) { Converter converter = getValueConverter(); if (converter instanceof XMLConverter) { element = ((XMLConverter) converter) .convertObjectValueToDataValue(element, session, record.getMarshaller()); } else { element = converter.convertObjectValueToDataValue(element, session); } } if (isAttribute) { if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) { // should be a DataHandler here try { String id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) element); element = id; } catch (ClassCastException cce) { throw XMLMarshalException.invalidSwaRefAttribute(getAttributeClassification().getName()); } } else { // inline case XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( element, record.getMarshaller(), mimeTypePolicy.getMimeType(parent)); String base64Value = ((XMLConversionManager) session.getDatasourcePlatform().getConversionManager()) .buildBase64StringFromBytes(data.getData()); element = base64Value; } } else { if (record.isXOPPackage() && !isSwaRef() && !shouldInlineBinaryData()) { // write as attachment String c_id = XMLConstants.EMPTY_STRING; byte[] bytes = null; if ((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE)) { if (getAttributeElementClass() == ClassConstants.ABYTE) { element = session .getDatasourcePlatform() .getConversionManager() .convertObject(element, ClassConstants.APBYTE); } bytes = (byte[]) element; c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( bytes, 0, bytes.length, this.mimeTypePolicy.getMimeType(parent), field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI()); } else if (getAttributeElementClass() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) { c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( (DataHandler) element, field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI()); if (c_id == null) { XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( element, marshaller, this.mimeTypePolicy.getMimeType(parent)); bytes = data.getData(); } } else { XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( element, marshaller, this.mimeTypePolicy.getMimeType(parent)); bytes = data.getData(); c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( bytes, 0, bytes.length, data.getMimeType(), field.getLastXPathFragment().getLocalName(), field.getLastXPathFragment().getNamespaceURI()); } if (c_id == null) { element = bytes; } else { DOMRecord include = new DOMRecord(field.getLastXPathFragment().getLocalName()); include.setSession(session); include.put(includeField, c_id); element = include; // Need to call setAttributeNS on the record, unless the xop prefix // is defined on the descriptor's resolver already NamespaceResolver resolver = ((XMLField) getField()).getNamespaceResolver(); if (resolver == null || resolver.resolveNamespaceURI(XMLConstants.XOP_URL) == null) { resolver = new NamespaceResolver(); resolver.put(XMLConstants.XOP_PREFIX, XMLConstants.XOP_URL); String xpath = XMLConstants.XOP_PREFIX + XMLConstants.COLON + INCLUDE; XMLField incField = new XMLField(xpath); incField.setNamespaceResolver(resolver); Object obj = include.getIndicatingNoEntry(incField); if (obj != null && obj instanceof DOMRecord) { if (((DOMRecord) obj).getDOM().getNodeType() == Node.ELEMENT_NODE) { ((Element) ((DOMRecord) obj).getDOM()) .setAttributeNS( XMLConstants.XMLNS_URL, XMLConstants.XMLNS + XMLConstants.COLON + XMLConstants.XOP_PREFIX, XMLConstants.XOP_URL); } } } } } else if (isSwaRef() && (marshaller.getAttachmentMarshaller() != null)) { // element should be a data-handler try { String c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) element); element = c_id; } catch (Exception ex) { } } else { // inline if (!((getAttributeElementClass() == ClassConstants.ABYTE) || (getAttributeElementClass() == ClassConstants.APBYTE))) { element = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( element, marshaller, this.mimeTypePolicy.getMimeType(parent)) .getData(); } } } return element; }