if (resolved == null) {
LOGGER.debug("**** uuid {0} is still unresolved during last phase of writing model", entry.getKey());
continue;
}
final UnresolvedReference unresolved = entry.getValue();
final ValueFactory valueFactory = resolved.getSession().getValueFactory();
// add mixins
for (final String mixin : unresolved.getMixins()) {
resolved.addMixin(mixin);
LOGGER.debug("adding mixin {0} to resolved node {1}", mixin, resolved.getName());
}
{ // add properties
for (final String propName : unresolved.getProperties().keySet()) {
UnresolvedProperty property = unresolved.getProperties().get(propName);
assert (property != null);
boolean multiValued = property.isMulti();
if (multiValued) {
Value[] propertyValues = new Value[property.getValues().size()];
int i = 0;
for (String value : property.getValues()) {
propertyValues[i++] = valueFactory.createValue(value);
}
} else {
// single valued
final String mappedName = this.medHelper.getMappedPropertyName(propName);
resolved.setProperty(mappedName, property.getValue());
LOGGER.debug("setting property '{0}' with value '{1}' to resolved node {2}",
propName,
property.getValue(),
resolved.getName());
}
}
}
{ // add weakreferences
for (final String propertyName : unresolved.getReferences().keySet()) {
final Collection<String> refs = unresolved.getReferences().get(propertyName);
if ((refs == null) || refs.isEmpty()) {
continue;
}
boolean multiValued = false;
if (resolved.hasProperty(propertyName)) {
multiValued = resolved.getProperty(propertyName).isMultiple();
if (multiValued) {
Value[] values = new Value[refs.size()];
int i = 0;
for (final String value : refs) {
Node referencedNode = this.resolver.getNode(value);
if (referencedNode == null) {
this.resolver.addUnresolvedReference(value);
} else {
values[i++] = valueFactory.createValue(referencedNode, true);
}
}
resolved.setProperty(propertyName, values);
} else {
// single valued property so just use first value
resolved.setProperty(propertyName, refs.iterator().next());
}
} else {
LOGGER.debug("**** resolved property does not have property '{0}'. The value has {1} reference(s) and first reference is '{2}'",
propertyName,
refs.size(),
refs.iterator().next());
}
}
}
{ // add referenced node name to referencer property
for (final String propertyName : unresolved.getReferenceNames().keySet()) {
final Collection<String> referencerUuids = unresolved.getReferenceNames().get(propertyName);
if ((referencerUuids == null) || referencerUuids.isEmpty()) {
continue;
}
for (final String uuid : referencerUuids) {
Node referencerNode = this.resolver.getNode(uuid);
if (referencerNode == null) {
// referencer node is unresolved
UnresolvedReference unresolvedReferencer = this.resolver.addUnresolvedReference(uuid);
unresolvedReferencer.addProperty(propertyName, resolved.getName(), true);
} else {
referencerNode.setProperty(propertyName,
new Value[] { this.context.valueFactory().createValue(
resolved.getName()) });
}
}
}
}
{ // referencer references
Multimap<String, String> refRefs = unresolved.getReferencerReferences();
for (final String propertyName : refRefs.keySet()) {
if (!resolved.isNodeType(JcrConstants.MIX_REFERENCEABLE)) {
resolved.addMixin(JcrConstants.MIX_REFERENCEABLE);
}
Value weakRef = valueFactory.createValue(resolved, true);
// property needs to get set with the weak reference of the resolved node
for (final String referencerUuuid : refRefs.get(propertyName)) {
Node referencer = this.resolver.getNode(referencerUuuid);
if (referencer == null) {
UnresolvedReference unresolvedReferencer = this.resolver.addUnresolvedReference(referencerUuuid);
unresolvedReferencer.addReference(propertyName, entry.getKey());
} else {
if (referencer.hasProperty(propertyName)) {
Property prop = referencer.getProperty(propertyName);
if (prop.isMultiple()) {