request.setError(new InvalidWorkspaceException(msg));
return;
}
}
String fromWorkspaceName = request.nameOfWorkspaceToBeCloned();
WorkspaceEntity fromWorkspace = workspaces.get(fromWorkspaceName, false);
if (fromWorkspace == null) {
switch (request.cloneConflictBehavior()) {
case SKIP_CLONE:
break;
case DO_NOT_CLONE:
default:
String msg = JpaConnectorI18n.workspaceDoesNotExist.text(getSourceName(), fromWorkspaceName);
request.setError(new InvalidRequestException(msg));
return;
}
}
// Create the workspace ...
WorkspaceEntity intoWorkspace = workspaces.create(name);
String newWorkspaceName = intoWorkspace.getName();
request.setActualWorkspaceName(newWorkspaceName);
if (fromWorkspace != null) {
// Copy the workspace into the new workspace, via bulk insert statements ..
Long fromWorkspaceId = fromWorkspace.getId();
Long intoWorkspaceId = intoWorkspace.getId();
Query query = entities.createNamedQuery("ChildEntity.findInWorkspace");
query.setParameter("workspaceId", fromWorkspaceId);
List<ChildEntity> childEntities = query.getResultList();
for (ChildEntity child : childEntities) {
ChildId origId = child.getId();
ChildId copyId = new ChildId(intoWorkspaceId, origId.getParentUuidString(), origId.getChildUuidString());
ChildEntity copy = new ChildEntity(copyId, child.getIndexInParent(), child.getChildNamespace(),
child.getChildName());
copy.setAllowsSameNameChildren(child.getAllowsSameNameChildren());
copy.setSameNameSiblingIndex(child.getSameNameSiblingIndex());
entities.persist(copy);
}
entities.flush();
query = entities.createNamedQuery("PropertiesEntity.findInWorkspace");
query.setParameter("workspaceId", fromWorkspaceId);
List<PropertiesEntity> properties = query.getResultList();
for (PropertiesEntity property : properties) {
NodeId copyId = new NodeId(intoWorkspaceId, property.getId().getUuidString());
PropertiesEntity copy = new PropertiesEntity(copyId);
copy.setCompressed(property.isCompressed());
copy.setData(property.getData());
copy.setPropertyCount(property.getPropertyCount());
copy.setReferentialIntegrityEnforced(property.isReferentialIntegrityEnforced());
Collection<LargeValueId> ids = property.getLargeValues();
if (ids.size() != 0) {
copy.getLargeValues().addAll(ids);
}
entities.persist(copy);
}
entities.flush();
query = entities.createNamedQuery("ReferenceEntity.findInWorkspace");
query.setParameter("workspaceId", fromWorkspaceId);
List<ReferenceEntity> references = query.getResultList();
for (ReferenceEntity reference : references) {
ReferenceId from = reference.getId();
ReferenceId copy = new ReferenceId(fromWorkspaceId, from.getFromUuidString(), from.getToUuidString());
entities.persist(new ReferenceEntity(copy));
}
entities.flush();
}
// Finish up the request ...
Location root = Location.create(pathFactory.createRootPath(), rootNodeUuid);
request.setActualRootLocation(getActualLocation(intoWorkspace.getId(), root).location);
}