/** INTERNAL: Recursive method to set a field value in the given key instance. */ protected void setFieldValue( KeyElementAccessor accessor, Object keyInstance, DatabaseMapping mapping, AbstractSession session, int[] elementIndex, Object... keyElements) { if (mapping.isAggregateMapping()) { Object nestedObject = mapping.getRealAttributeValueFromObject(keyInstance, session); if (nestedObject == null) { nestedObject = getClassInstance(mapping.getReferenceDescriptor().getJavaClass()); mapping.setRealAttributeValueInObject(keyInstance, nestedObject); } // keep drilling down the nested mappings ... setFieldValue( accessor, nestedObject, mapping .getReferenceDescriptor() .getObjectBuilder() .getMappingForField(accessor.getDatabaseField()), session, elementIndex, keyElements); } else { Object fieldValue = null; if (mapping.isDirectToFieldMapping()) { fieldValue = keyElements[elementIndex[0]]; Converter converter = ((DirectToFieldMapping) mapping).getConverter(); if (converter != null) { fieldValue = converter.convertDataValueToObjectValue(fieldValue, session); } ++elementIndex[0]; } else if (mapping.isObjectReferenceMapping()) { // what if mapping comes from derived ID. need to get the derived mapping. // get reference descriptor and extract pk from target cmp policy fieldValue = mapping .getReferenceDescriptor() .getCMPPolicy() .createPrimaryKeyInstanceFromPrimaryKeyValues(session, elementIndex, keyElements); } accessor.setValue(keyInstance, fieldValue); } }
@Test public void testConvertersEstabished_autoApply() throws Exception { ClassDescriptor classDescriptor = jpaMetadataProvider.getClassDescriptor(TestDataObject.class); DatabaseMapping attribute = classDescriptor.getMappingForAttributeName("currencyProperty"); assertEquals("attribute data type mismatch", DirectToFieldMapping.class, attribute.getClass()); Converter converter = ((org.eclipse.persistence.mappings.DirectToFieldMapping) attribute).getConverter(); assertNotNull("converter not assigned", converter); assertEquals( "Mismatch - converter should have been the EclipseLink JPA wrapper class", ConverterClass.class, converter.getClass()); Field f = ConverterClass.class.getDeclaredField("attributeConverterClassName"); f.setAccessible(true); String attributeConverterClassName = (String) f.get(converter); assertNotNull("attributeConverterClassName missing", attributeConverterClassName); assertEquals( "Converter class incorrect", "org.kuali.rice.krad.data.jpa.converters.KualiDecimalConverter", attributeConverterClassName); }
/** * INTERNAL: Create an instance of the composite primary key class for the key object. Yes the * elementIndex looks strange but this is just a simple way to get the index to be * pass-by-reference */ public Object createPrimaryKeyInstanceFromPrimaryKeyValues( AbstractSession session, int[] elementIndex, Object... keyElements) { Object keyInstance = null; KeyElementAccessor[] pkElementArray = getKeyClassFields(); if ((pkElementArray.length == 1) && (pkElementArray[0] instanceof KeyIsElementAccessor)) { DatabaseMapping mapping = getDescriptor() .getObjectBuilder() .getMappingForAttributeName(pkElementArray[0].getAttributeName()); if (mapping.isDirectToFieldMapping()) { Converter converter = ((DirectToFieldMapping) mapping).getConverter(); if (converter != null) { return converter.convertDataValueToObjectValue(keyElements[elementIndex[0]], session); } keyInstance = keyElements[elementIndex[0]]; } else if (mapping .isObjectReferenceMapping()) { // what if mapping comes from derived ID. need to get the // derived mapping. // get reference descriptor and extract pk from target cmp policy keyInstance = mapping .getReferenceDescriptor() .getCMPPolicy() .createPrimaryKeyInstanceFromPrimaryKeyValues(session, elementIndex, keyElements); } ++elementIndex[0]; // remove processed key in case keys are complex and derived } else { keyInstance = getPKClassInstance(); // get clone of Key so we can remove values. for (int index = 0; index < pkElementArray.length; index++) { KeyElementAccessor accessor = pkElementArray[index]; DatabaseMapping mapping = getDescriptor() .getObjectBuilder() .getMappingForAttributeName(accessor.getAttributeName()); if (mapping == null) { mapping = getDescriptor().getObjectBuilder().getMappingForField(accessor.getDatabaseField()); } if (accessor.isNestedAccessor()) { // Need to recursively build all the nested objects. setFieldValue( accessor, keyInstance, mapping .getReferenceDescriptor() .getObjectBuilder() .getMappingForField(accessor.getDatabaseField()), session, elementIndex, keyElements); } else { // Not nested but may be a single layer aggregate so check. if (mapping.isAggregateMapping()) { mapping = mapping .getReferenceDescriptor() .getObjectBuilder() .getMappingForField(accessor.getDatabaseField()); } setFieldValue(accessor, keyInstance, mapping, session, elementIndex, keyElements); } } } return keyInstance; }
public boolean marshalSingleValue( XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object objectValue, AbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) { if (objectValue == null) { return false; } boolean addDeclaration = false; boolean removePrefix = false; String xopPrefix = null; if (namespaceResolver != null) { xopPrefix = namespaceResolver.resolveNamespaceURI(XMLConstants.XOP_URL); } if (xopPrefix == null) { // check for it in the MarshalRecord's NamespaceResolver addDeclaration = true; xopPrefix = marshalRecord.getNamespaceResolver().resolveNamespaceURI(XMLConstants.XOP_URL); if (xopPrefix == null) { // if it's still null, add it, and make a note to remove it later removePrefix = true; xopPrefix = XMLConstants.XOP_PREFIX; marshalRecord.getNamespaceResolver().put(xopPrefix, XMLConstants.XOP_URL); } namespaceResolver = marshalRecord.getNamespaceResolver(); } XMLMarshaller marshaller = marshalRecord.getMarshaller(); if (xmlBinaryDataCollectionMapping.getValueConverter() != null) { Converter converter = xmlBinaryDataCollectionMapping.getValueConverter(); if (converter instanceof XMLConverter) { objectValue = ((XMLConverter) converter) .convertObjectValueToDataValue(objectValue, session, marshaller); } else { objectValue = converter.convertObjectValueToDataValue(objectValue, session); } } XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver); marshalRecord.closeStartGroupingElements(groupingFragment); marshalRecord.openStartElement(xPathFragment, namespaceResolver); marshalRecord.closeStartElement(); if (xmlBinaryDataCollectionMapping.isSwaRef() && marshaller.getAttachmentMarshaller() != null) { // object value should be a DataHandler String c_id = null; byte[] bytes = null; if (xmlBinaryDataCollectionMapping.getAttributeElementClass() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) { c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment((DataHandler) objectValue); if (c_id == null) { bytes = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( // objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object)) .getData(); } } else { XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( // objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object)); bytes = data.getData(); c_id = marshaller.getAttachmentMarshaller().addSwaRefAttachment(bytes, 0, bytes.length); } if (c_id != null) { marshalRecord.characters(c_id); } else { String value = getValueToWrite( ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(), bytes, session); marshalRecord.characters(value); } } else { if (marshalRecord.isXOPPackage() && !xmlBinaryDataCollectionMapping.shouldInlineBinaryData()) { XPathFragment lastFrag = ((XMLField) xmlBinaryDataCollectionMapping.getField()).getLastXPathFragment(); String c_id = XMLConstants.EMPTY_STRING; byte[] bytes = null; if (objectValue.getClass() == ClassConstants.APBYTE) { bytes = (byte[]) objectValue; c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( bytes, 0, bytes.length, this.xmlBinaryDataCollectionMapping.getMimeType(object), lastFrag.getLocalName(), lastFrag.getNamespaceURI()); } else if (xmlBinaryDataCollectionMapping.getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) { c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( (DataHandler) objectValue, lastFrag.getLocalName(), lastFrag.getNamespaceURI()); if (c_id == null) { bytes = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( // objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeType(object)) .getData(); } } else { XMLBinaryDataHelper.EncodedData data = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( // objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeTypePolicy().getMimeType(object)); bytes = data.getData(); c_id = marshaller .getAttachmentMarshaller() .addMtomAttachment( bytes, 0, bytes.length, // data.getMimeType(), lastFrag.getLocalName(), lastFrag.getNamespaceURI()); } if (c_id == null) { String value = getValueToWrite( ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(), bytes, session); marshalRecord.characters(value); } else { XPathFragment xopInclude = new XPathFragment(xopPrefix + ":Include"); xopInclude.setNamespaceURI(XMLConstants.XOP_URL); marshalRecord.openStartElement(xopInclude, namespaceResolver); marshalRecord.attribute("", "href", "href", c_id); if (addDeclaration) { marshalRecord.attribute( XMLConstants.XMLNS_URL, xopPrefix, XMLConstants.XMLNS + XMLConstants.COLON + xopPrefix, XMLConstants.XOP_URL); // marshalRecord.attribute(new XPathFragment("@xmlns:" + xopPrefix), namespaceResolver, // XMLConstants.XOP_URL); } marshalRecord.closeStartElement(); marshalRecord.endElement(xPathFragment, namespaceResolver); // marshal as an attachment. } } else { String value = XMLConstants.EMPTY_STRING; if (objectValue.getClass() == ClassConstants.ABYTE || objectValue.getClass() == ClassConstants.APBYTE) { value = getValueToWrite( ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(), objectValue, session); } else { byte[] bytes = XMLBinaryDataHelper.getXMLBinaryDataHelper() .getBytesForBinaryValue( // objectValue, marshaller, xmlBinaryDataCollectionMapping.getMimeTypePolicy().getMimeType(object)) .getData(); value = getValueToWrite( ((XMLField) xmlBinaryDataCollectionMapping.getField()).getSchemaType(), bytes, session); } marshalRecord.characters(value); } } marshalRecord.endElement(xPathFragment, namespaceResolver); if (removePrefix) { marshalRecord.getNamespaceResolver().removeNamespace(XMLConstants.XOP_PREFIX); } return true; }
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; }