{
m_loader = loader;
m_helper = loader.getHelper();
final VirtualMapping mapping = new VirtualMapping();
final XMLMetadata metadata = loader.getMetadata();
final String sURLPrefix = "class:" + metaclass.getName() + ".persistence";
int nCookie = m_helper.pushMarker(MetadataValidationException.TYPE_NAME, "ServiceMapping");
m_helper.pushMarker("class", metaclass.getName());
try
{
mapping.setMetaclass(metaclass);
mapping.setDataSource(dataSource);
XMLUtil.withFirstChildElement(element, "ServiceMapping",
true, new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
final ArrayList keyTypeList = new ArrayList(4);
XMLUtil.withFirstChildElement(element, "KeyParts", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
XMLUtil.forEachChildElement(element, "KeyPart", new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
keyTypeList.add(Primitive.find(XMLUtil.getReqStringAttr(element, "type")));
String sAttributeName = XMLUtil.getStringAttr(element, "attribute");
if (sAttributeName != null)
{
Attribute attribute = metaclass.getAttribute(sAttributeName);
VirtualPrimitiveMapping primitiveMapping = new VirtualPrimitiveMapping();
primitiveMapping.setObjectKeyPart(keyTypeList.size() - 1);
primitiveMapping.setAttribute(attribute);
mapping.addAttributeMapping(primitiveMapping);
primitiveMapping.validate(metadata, m_helper.getWarnings());
}
}
});
mapping.setKey((Primitive[])keyTypeList.toArray(new Primitive[keyTypeList.size()]));
}
});
XMLUtil.withFirstChildElement(element, "AttributeMappings", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
XMLUtil.forEachChildElement(element, "AttributeMapping", new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
String sName = XMLUtil.getReqStringAttr(element, "name");
int nMaxLength = XMLUtil.getIntAttr(element, "maxLength", 0);
final Attribute attribute = metaclass.getAttribute(sName);
AttributeMapping attrMapping;
if (attribute.getType().isPrimitive())
{
VirtualPrimitiveMapping primitiveMapping = new VirtualPrimitiveMapping();
attrMapping = primitiveMapping;
if (element.hasAttribute("attributes"))
{
throw new MetadataException("err.meta.persistence.virtual.invalidPropertySpecified",
new Object[]{"attributes", sName, metaclass.getName()});
}
if (XMLUtil.getBooleanAttr(element, "objectSourceKey", false))
{
throw new MetadataException("err.meta.persistence.virtual.invalidPropertySpecified",
new Object[]{"objectSourceKey", sName, metaclass.getName()});
}
if (element.hasAttribute("destinationKey"))
{
throw new MetadataException("err.meta.persistence.virtual.invalidPropertySpecified",
new Object[]{"destinationKey", sName, metaclass.getName()});
}
if (nMaxLength != 0)
{
if (attribute.getType() != Primitive.BINARY && attribute.getType() != Primitive.STRING)
{
throw new MetadataException("err.meta.persistence.virtual.maxLengthSpecified",
new Object[]{attribute.getName(), metaclass.getName()});
}
primitiveMapping.setMaxLength(nMaxLength);
}
}
else
{
final VirtualClassMapping classMapping = new VirtualClassMapping();
attrMapping = classMapping;
classMapping.setComposition((Pair)m_helper.parse(
XMLUtil.getStringAttr(element, "attributes"),
true, null, null, metadata.getGlobalEnvironment()
));
final boolean bObjectSourceKey = XMLUtil.getBooleanAttr(element, "objectSourceKey", false);
final String sDestinationKeyName = XMLUtil.getStringAttr(element, "destinationKey");
if (nMaxLength != 0)
{
throw new MetadataException("err.meta.persistence.virtual.maxLengthSpecified",
new Object[]{attribute.getName(), metaclass.getName()});
}
loader.addPersistenceMappingFixup(new ContextFixup(m_helper)
{
public void fixup()
{
Metaclass type = (Metaclass)attribute.getType();
PersistenceMapping assocClassMapping = type.getPersistenceMapping();
if (assocClassMapping == null)
{
throw new MetadataException("err.meta.missingAssocPersistenceMapping",
new Object[]{attribute.getName(), metaclass.getName(), type.getName()});
}
classMapping.setSourceKey((bObjectSourceKey) ? mapping.getObjectKey() : new VirtualKey(attribute));
classMapping.setDestinationKey(assocClassMapping.addForeignKey(sDestinationKeyName, classMapping));
}
});
loader.addComponentFixup(new ContextFixup(m_helper)
{
public void fixup()
{
classMapping.validate(metadata, m_helper.getWarnings());
}
});
}
attrMapping.setAttribute(attribute);
mapping.addAttributeMapping(attrMapping);
}
});
}
});
XMLUtil.withFirstChildElement(element, "SortKeys", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
XMLUtil.forEachChildElement(element, "SortKey", new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
mapping.addSortKey(
(Pair)m_helper.parse(XMLUtil.getReqStringAttr(element, "attributes"), true,
mapping.getTextPositionMap(), null, metadata.getGlobalEnvironment()),
XMLUtil.getBooleanAttr(element, "unique", false)
);
}
});
}
});
XMLUtil.withFirstChildElement(element, "ReadMapping", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
final ReadMapping readMapping = mapping.getReadMapping();
XMLUtil.forEachChildElement(element, "Case", new XMLUtil.ElementHandler()
{
private int m_nOrdinal = 0;
public void handleElement(Element element)
{
final String sReadPrefix = sURLPrefix + ".read." + m_nOrdinal;
Object whereExpr = m_helper.parse(
XMLUtil.getStringAttr(element, "where"),
false, sReadPrefix + "$where", mapping.getTextPositionMap(), Symbol.ELSE, metadata.getGlobalEnvironment()
);
Pair variables = (Pair)m_helper.parse(
XMLUtil.getStringAttr(element, "variables"),
true, mapping.getTextPositionMap(), null, metadata.getGlobalEnvironment()
);
final ReadMappingCase readMappingCase = new ReadMappingCase();
readMappingCase.setWhere(whereExpr);
readMappingCase.setVariables(variables);
XMLUtil.withFirstChildElement(element, "Read", true, new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
readMappingCase.setReadScript(loadScript(element, sReadPrefix + "$read", mapping.getTextPositionMap()));
}
});
XMLUtil.withFirstChildElement(element, "Close", false, new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
readMappingCase.setCloseScript(loadScript(element, sReadPrefix + "$close", mapping.getTextPositionMap()));
}
});
readMapping.addCase(readMappingCase);
m_nOrdinal += 1;
}
});
}
});
XMLUtil.withFirstChildElement(element, "CreateMapping", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
final WorkMapping operation = new WorkMapping(metaclass);
operation.setBatch(XMLUtil.getBooleanAttr(element, "batch", operation.isBatch()));
operation.setScript(loadScript(element, sURLPrefix + ".create", mapping.getTextPositionMap()));
mapping.setCreateMapping(operation);
}
});
XMLUtil.withFirstChildElement(element, "UpdateMapping", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
final UpdateMapping updateMapping = new UpdateMapping(mapping);
XMLUtil.forEachChildElement(element, "Case", new XMLUtil.ElementHandler()
{
private int m_nOrdinal = 0;
public void handleElement(Element element)
{
final UpdateMappingCase update = new UpdateMappingCase(metaclass);
update.setBatch(XMLUtil.getBooleanAttr(element, "batch", update.isBatch()));
update.setDirty(XMLUtil.getBooleanAttr(element, "dirty", update.isDirty()));
update.setFull(XMLUtil.getBooleanAttr(element, "full", update.isFull()));
Pair attributes = (Pair)m_helper.parse(
XMLUtil.getStringAttr(element, "attributes"),
true, mapping.getTextPositionMap(), null, metadata.getGlobalEnvironment()
);
update.setScript(loadScript(element, sURLPrefix + ".update." + m_nOrdinal, mapping.getTextPositionMap()));
updateMapping.addCase(update, attributes);
m_nOrdinal += 1;
}
});
mapping.setUpdateMapping(updateMapping);
}
});
XMLUtil.withFirstChildElement(element, "DeleteMapping", false,
new XMLUtil.ElementHandler()
{
public void handleElement(Element element)
{
final WorkMapping operation = new WorkMapping(metaclass);
operation.setBatch(XMLUtil.getBooleanAttr(element, "batch", operation.isBatch()));
operation.setScript(loadScript(element, sURLPrefix + ".delete", mapping.getTextPositionMap()));
mapping.setDeleteMapping(operation);
}
});
// Key Generator
final String sKeyGeneratorName = XMLUtil.getStringAttr(element, "keyGenerator");
if (sKeyGeneratorName != null)
{
loader.addComponentFixup(new ContextFixup(m_helper)
{
public void fixup()
{
mapping.setKeyGenerator(metadata.getComponent(sKeyGeneratorName));
}
});
}
mapping.setDerived(XMLUtil.getBooleanAttr(element, "derived", mapping.isDerived()));