if (log.isDebugEnabled())
log.debug("Restore: " + nodePath.getAsString() + ", removeExisting=" + removeExisting);
PropertyData frozenIdentifier =
(PropertyData)dataManager.getItemData(frozen, new QPathEntry(Constants.JCR_FROZENUUID, 1), ItemType.PROPERTY);
String fidentifier = null;
NodeData existing = null;
// make new node from frozen
try
{
fidentifier = new String(frozenIdentifier.getValues().get(0).getAsByteArray());
NodeData sameIdentifierNodeRestored = (NodeData)findDelegated(fidentifier);
if (sameIdentifierNodeRestored != null)
{
// already restored from delegated call, remove it as we interested in
// this version state
deleteDelegated(sameIdentifierNodeRestored.getQPath());
}
else
{
NodeData sameIdentifierNode = (NodeData)dataManager.getItemData(fidentifier);
if (sameIdentifierNode != null)
{
QPath sameIdentifierPath = sameIdentifierNode.getQPath();
if (sameIdentifierPath.makeParentPath().equals(nodePath.makeParentPath()) && // same
// parent
sameIdentifierPath.getName().equals(nodePath.getName()))
{ // same
// name
if (sameIdentifierPath.getIndex() != nodePath.getIndex())
// but different index, see below... fix it
nodePath = QPath.makeChildPath(parentData.getQPath(), name, sameIdentifierPath.getIndex());
// if it's a target node
existing = sameIdentifierNode;
// remove existed node, with validation
ItemDataRemoveVisitor removeVisitor = new RemoveVisitor();
removeVisitor.visit(existing);
changes.addAll(removeVisitor.getRemovedStates());
}
else if (!sameIdentifierPath.isDescendantOf(nodePath))
{
if (removeExisting)
{
final QPath restorePath = nodePath;
// remove same uuid node, with validation
class RemoveVisitor extends ItemDataRemoveVisitor
{
RemoveVisitor() throws RepositoryException
{
super(userSession.getTransientNodesManager(), null, nodeTypeDataManager, userSession
.getAccessManager(), userSession.getUserState());
}
@Override
protected boolean isRemoveDescendant(ItemData item) throws RepositoryException
{
return item.getQPath().isDescendantOf(removedRoot.getQPath())
|| item.getQPath().isDescendantOf(restorePath);
}
};
ItemDataRemoveVisitor removeVisitor = new RemoveVisitor();
removeVisitor.visit(sameIdentifierNode);
changes.addAll(removeVisitor.getRemovedStates());
}
else
{
throw new ItemExistsException("Item with the same UUID as restored node " + nodePath.getAsString()
+ " already exists and removeExisting=false. Existed "
+ userSession.getLocationFactory().createJCRPath(sameIdentifierPath).getAsString(false) + " "
+ sameIdentifierNode.getIdentifier());
}
}
}
}
}
catch (IllegalStateException e)
{
throw new RepositoryException("jcr:frozenUuid, error of data read "
+ userSession.getLocationFactory().createJCRPath(frozenIdentifier.getQPath()).getAsString(false), e);
}
catch (IOException e)
{
throw new RepositoryException("jcr:frozenUuid, error of data read "
+ userSession.getLocationFactory().createJCRPath(frozenIdentifier.getQPath()).getAsString(false), e);
}
PropertyData frozenPrimaryType =
(PropertyData)dataManager.getItemData(frozen, new QPathEntry(Constants.JCR_FROZENPRIMARYTYPE, 0),
ItemType.PROPERTY);
PropertyData frozenMixinTypes =
(PropertyData)dataManager.getItemData(frozen, new QPathEntry(Constants.JCR_FROZENMIXINTYPES, 0),
ItemType.PROPERTY);
List<AccessControlEntry> accessList =
new ArrayList<AccessControlEntry>(parentData.getACL().getPermissionEntries());
String owner = parentData.getACL().getOwner();
InternalQName[] mixins = null;
if (frozenMixinTypes != null)
{
try
{
List<ValueData> mvs = frozenMixinTypes.getValues();
mixins = new InternalQName[mvs.size()];
for (int i = 0; i < mvs.size(); i++)
{
ValueData mvd = mvs.get(i);
mixins[i] = InternalQName.parse(new String(mvd.getAsByteArray()));
if (mixins[i].equals(Constants.EXO_PRIVILEGEABLE))
{
PropertyData aclData =
(PropertyData)dataManager.getItemData(frozen, new QPathEntry(Constants.EXO_PERMISSIONS, 0),
ItemType.PROPERTY);
AccessControlList acl = new AccessControlList();
acl.removePermissions(SystemIdentity.ANY);
for (ValueData value : aclData.getValues())
{
acl.addPermissions(new String(value.getAsByteArray(), Constants.DEFAULT_ENCODING));
}
accessList = acl.getPermissionEntries();
}
else if (mixins[i].equals(Constants.EXO_OWNEABLE))
{
PropertyData ownerData =
(PropertyData)dataManager.getItemData(frozen, new QPathEntry(Constants.EXO_OWNER, 0),
ItemType.PROPERTY);
owner = new String(ownerData.getValues().get(0).getAsByteArray(), Constants.DEFAULT_ENCODING);
}
}