use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLAnyAttributeMapping method writeSingleValue.
@Override
public void writeSingleValue(Object attributeValue, Object parent, XMLRecord row, AbstractSession session) {
ContainerPolicy cp = this.getContainerPolicy();
if ((attributeValue == null) || (cp.sizeFor(attributeValue) == 0)) {
return;
}
DOMRecord record = (DOMRecord) row;
if (record.getDOM().getNodeType() != Node.ELEMENT_NODE) {
return;
}
DOMRecord recordToModify = record;
Element root = (Element) record.getDOM();
if (field != null) {
root = (Element) XPathEngine.getInstance().create((XMLField) getField(), root, session);
recordToModify = new DOMRecord(root);
}
List extraNamespaces = new ArrayList();
NamespaceResolver nr = recordToModify.getNamespaceResolver();
for (Object iter = cp.iteratorFor(attributeValue); cp.hasNext(iter); ) {
Map.Entry entry = (Map.Entry) cp.nextEntry(iter, session);
Object key = entry.getKey();
if ((key != null) && key instanceof QName) {
Object value = entry.getValue();
QName attributeName = (QName) key;
String namespaceURI = attributeName.getNamespaceURI();
String qualifiedName = attributeName.getLocalPart();
if (nr != null) {
String prefix = nr.resolveNamespaceURI(attributeName.getNamespaceURI());
if ((prefix != null) && prefix.length() > 0) {
qualifiedName = prefix + XMLConstants.COLON + qualifiedName;
} else if (attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) {
String generatedPrefix = nr.generatePrefix();
qualifiedName = generatedPrefix + XMLConstants.COLON + qualifiedName;
nr.put(generatedPrefix, attributeName.getNamespaceURI());
extraNamespaces.add(new Namespace(generatedPrefix, attributeName.getNamespaceURI()));
recordToModify.getNamespaceResolver().put(generatedPrefix, attributeName.getNamespaceURI());
}
}
if (namespaceURI != null) {
root.setAttributeNS(namespaceURI, qualifiedName, value.toString());
} else {
root.setAttribute(attributeName.getLocalPart(), value.toString());
}
}
}
((XMLObjectBuilder) descriptor.getObjectBuilder()).writeExtraNamespaces(extraNamespaces, recordToModify);
recordToModify.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class DirectCollectionChangeRecord method internalRecreateOriginalCollection.
/**
* Recreates the original state of the collection.
*/
@Override
public void internalRecreateOriginalCollection(Object currentCollection, AbstractSession session) {
ContainerPolicy cp = this.mapping.getContainerPolicy();
if (this.removeObjectMap != null) {
Iterator it = this.removeObjectMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object obj = entry.getKey();
int n = (Integer) entry.getValue();
for (int i = 0; i < n; i++) {
cp.addInto(obj, currentCollection, session);
}
}
}
if (this.addObjectMap != null) {
Iterator it = this.addObjectMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object obj = entry.getKey();
int n = (Integer) entry.getValue();
for (int i = 0; i < n; i++) {
cp.removeFrom(obj, currentCollection, session);
}
}
}
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLFragmentCollectionMapping method valueFromRow.
/**
* INTERNAL:
* Build the nested collection from the database row.
*/
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
ContainerPolicy cp = this.getContainerPolicy();
Object fieldValue = ((DOMRecord) row).getValuesIndicatingNoEntry(this.getField(), true);
Vector nestedRows = null;
if (fieldValue instanceof Vector) {
nestedRows = (Vector) fieldValue;
}
if ((nestedRows == null) || nestedRows.isEmpty()) {
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();
}
for (Enumeration stream = nestedRows.elements(); stream.hasMoreElements(); ) {
Object next = stream.nextElement();
if (next instanceof Element) {
XMLPlatformFactory.getInstance().getXMLPlatform().namespaceQualifyFragment((Element) next);
}
cp.addInto(next, result, executionSession);
}
return result;
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class MappingTestProject method buildMappingTestObjectDescriptor.
public ClassDescriptor buildMappingTestObjectDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(testObjectClass);
descriptor.setDefaultRootElement("mapping-test-object");
// any attribute mapping
XMLAnyAttributeMapping aamapping = new XMLAnyAttributeMapping();
aamapping.setAttributeName("anyAttribute");
aamapping.setGetMethodName("getAnyAttribute");
aamapping.setSetMethodName("setAnyAttribute");
ContainerPolicy mapPolicy;
try {
mapPolicy = (ContainerPolicy) mapContainerPolicyClass.getConstructor().newInstance();
Method meth = mapContainerPolicyClass.getDeclaredMethod("setContainerClassName", String.class);
meth.invoke(mapPolicy, "java.util.HashMap");
} catch (Exception e) {
throw new RuntimeException(e);
}
aamapping.setContainerPolicy(mapPolicy);
descriptor.addMapping(aamapping);
// any object mapping
XMLAnyObjectMapping aomapping = new XMLAnyObjectMapping();
aomapping.setAttributeName("anyObject");
aomapping.setGetMethodName("getAnyObject");
aomapping.setSetMethodName("setAnyObject");
descriptor.addMapping(aomapping);
// any collection mapping
XMLAnyCollectionMapping acmapping = new XMLAnyCollectionMapping();
acmapping.setXPath("any-collection");
acmapping.setAttributeName("anyCollection");
acmapping.setGetMethodName("getAnyCollection");
acmapping.setSetMethodName("setAnyCollection");
descriptor.addMapping(acmapping);
// setup binary data and choice mapping, but only add the mappings in certain cases
// binary data mapping
XMLBinaryDataMapping photoMapping = new XMLBinaryDataMapping();
photoMapping.setAttributeName("photo");
XMLField field = new XMLField("photo");
field.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
photoMapping.setField(field);
photoMapping.setShouldInlineBinaryData(false);
photoMapping.setSwaRef(false);
photoMapping.setMimeType("image");
// binary data collection mapping
XMLBinaryDataCollectionMapping photosMapping = new XMLBinaryDataCollectionMapping();
photosMapping.setAttributeName("photos");
XMLField xfield = new XMLField("photos/photo");
xfield.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
photosMapping.setField(xfield);
photosMapping.setShouldInlineBinaryData(false);
photosMapping.setSwaRef(false);
photosMapping.setMimeType("image");
photosMapping.setCollectionContentType(ClassConstants.APBYTE);
// choice mapping
XMLChoiceObjectMapping choiceMapping = new XMLChoiceObjectMapping();
choiceMapping.setAttributeName("choice");
choiceMapping.setGetMethodName("getChoice");
choiceMapping.setSetMethodName("setChoice");
choiceMapping.addChoiceElement("string-choice/text()", String.class);
choiceMapping.addChoiceElement("int-choice/text()", Integer.class);
// choice collection mapping
XMLChoiceCollectionMapping choiceColMapping = new XMLChoiceCollectionMapping();
choiceColMapping.setAttributeName("choices");
choiceColMapping.setGetMethodName("getChoices");
choiceColMapping.setSetMethodName("setChoices");
choiceColMapping.addChoiceElement("street-choice/text()", String.class);
choiceColMapping.addChoiceElement("address-choice", addressClass);
choiceColMapping.addChoiceElement("integer-choice/text()", Integer.class);
// 3- binary is supported with DOM
if (metadata == OXTestCase.Metadata.JAVA) {
descriptor.addMapping(photoMapping);
descriptor.addMapping(photosMapping);
}
if (platform == OXTestCase.Platform.SAX) {
descriptor.addMapping(choiceMapping);
descriptor.addMapping(choiceColMapping);
}
// object reference mapping
XMLObjectReferenceMapping orMapping = new XMLObjectReferenceMapping();
orMapping.setAttributeName("address");
orMapping.setReferenceClassName(addressClass.getName());
orMapping.addSourceToTargetKeyFieldAssociation("billing-address-ref/@address-id", "@aid");
descriptor.addMapping(orMapping);
// collection reference mapping
XMLCollectionReferenceMapping addressesMapping = new XMLCollectionReferenceMapping();
ContainerPolicy listPolicy;
try {
listPolicy = (ContainerPolicy) listContainerPolicyClass.getConstructor().newInstance();
Method meth = listContainerPolicyClass.getDeclaredMethod("setContainerClassName", String.class);
meth.invoke(listPolicy, "java.util.ArrayList");
} catch (Exception e) {
throw new RuntimeException(e);
}
addressesMapping.setContainerPolicy(listPolicy);
addressesMapping.setAttributeName("addresses");
addressesMapping.setReferenceClassName(addressClass.getName());
addressesMapping.addSourceToTargetKeyFieldAssociation("shipping-address-ref/@address-id", "@aid");
descriptor.addMapping(addressesMapping);
// direct collection mapping
XMLCompositeDirectCollectionMapping responsibilitiesMapping = new XMLCompositeDirectCollectionMapping();
responsibilitiesMapping.setAttributeName("responsibilities");
ContainerPolicy listPolicy1;
try {
listPolicy1 = (ContainerPolicy) listContainerPolicyClass.getConstructor().newInstance();
Method meth = listContainerPolicyClass.getDeclaredMethod("setContainerClassName", String.class);
meth.invoke(listPolicy1, "java.util.ArrayList");
} catch (Exception e) {
throw new RuntimeException(e);
}
responsibilitiesMapping.setContainerPolicy(listPolicy1);
responsibilitiesMapping.setXPath("responsibilities/list/@responsibility");
descriptor.addMapping(responsibilitiesMapping);
return descriptor;
}
use of org.eclipse.persistence.internal.queries.ContainerPolicy in project eclipselink by eclipse-ee4j.
the class MappingTestProject method buildRootObjectDescriptor.
/**
* The root descriptor contains a single mapping test object and a
* list of address objects
*/
public ClassDescriptor buildRootObjectDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(rootObjectClass);
descriptor.setDefaultRootElement("root-object");
// create test object mapping
XMLCompositeObjectMapping testObjectMapping = new XMLCompositeObjectMapping();
testObjectMapping.setAttributeName("testObject");
testObjectMapping.setXPath("mapping-test-object");
testObjectMapping.setReferenceClassName(testObjectClass.getName());
testObjectMapping.setGetMethodName("getTestObject");
testObjectMapping.setSetMethodName("setTestObject");
descriptor.addMapping(testObjectMapping);
// create address mapping
XMLCompositeCollectionMapping addMapping = new XMLCompositeCollectionMapping();
addMapping.setAttributeName("addresses");
addMapping.setXPath("addresses/address");
addMapping.setReferenceClassName(addressClass.getName());
addMapping.setGetMethodName("getAddresses");
addMapping.setSetMethodName("setAddresses");
ContainerPolicy listPolicy;
try {
listPolicy = (ContainerPolicy) listContainerPolicyClass.getConstructor().newInstance();
Method meth = listContainerPolicyClass.getDeclaredMethod("setContainerClassName", String.class);
meth.invoke(listPolicy, "java.util.ArrayList");
} catch (Exception e) {
throw new RuntimeException(e);
}
addMapping.setContainerPolicy(listPolicy);
descriptor.addMapping(addMapping);
return descriptor;
}
Aggregations