use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceMaterialController method cloneWorkspaceNode.
private WorkspaceNode cloneWorkspaceNode(WorkspaceNode workspaceNode, WorkspaceNode parent, boolean cloneMaterials, boolean overrideCloneMaterials) {
WorkspaceNode newNode;
boolean isHtmlMaterial = false;
Integer index = workspaceNodeDAO.getMaximumOrderNumber(parent);
index = index == null ? 0 : ++index;
if (workspaceNode instanceof WorkspaceMaterial) {
WorkspaceMaterial workspaceMaterial = (WorkspaceMaterial) workspaceNode;
Material material = getMaterialForWorkspaceMaterial(workspaceMaterial);
isHtmlMaterial = material instanceof HtmlMaterial;
Material clonedMaterial = cloneMaterials && !overrideCloneMaterials ? materialController.cloneMaterial(material) : material;
// Implementation of feature #1232 (front and help pages should always be copies)
if (isHtmlMaterial && !cloneMaterials) {
WorkspaceNode parentNode = workspaceMaterial.getParent();
if (parentNode instanceof WorkspaceFolder) {
WorkspaceFolder parentFolder = (WorkspaceFolder) parentNode;
if (parentFolder.getFolderType() == WorkspaceFolderType.FRONT_PAGE || parentFolder.getFolderType() == WorkspaceFolderType.HELP_PAGE) {
clonedMaterial = materialController.cloneMaterial(material);
}
}
}
newNode = createWorkspaceMaterial(parent, clonedMaterial, workspaceMaterial.getTitle(), generateUniqueUrlName(parent, workspaceMaterial.getUrlName()), index, workspaceMaterial.getHidden(), workspaceMaterial.getAssignmentType(), workspaceMaterial.getCorrectAnswers());
} else if (workspaceNode instanceof WorkspaceFolder) {
newNode = createWorkspaceFolder(parent, ((WorkspaceFolder) workspaceNode).getTitle(), generateUniqueUrlName(parent, workspaceNode.getUrlName()), index, workspaceNode.getHidden(), ((WorkspaceFolder) workspaceNode).getFolderType(), ((WorkspaceFolder) workspaceNode).getViewRestrict());
} else {
throw new IllegalArgumentException("Uncloneable workspace node " + workspaceNode.getClass());
}
List<WorkspaceNode> childNodes = workspaceNodeDAO.listByParentSortByOrderNumber(workspaceNode);
for (WorkspaceNode childNode : childNodes) {
cloneWorkspaceNode(childNode, newNode, cloneMaterials, isHtmlMaterial);
}
return newNode;
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceMaterialController method flattenWorkspaceNodes.
public List<FlattenedWorkspaceNode> flattenWorkspaceNodes(List<WorkspaceNode> workspaceNodes, int level, boolean includeHidden) {
List<FlattenedWorkspaceNode> result = new ArrayList<>();
for (WorkspaceNode workspaceNode : workspaceNodes) {
if (workspaceNode.getType() == WorkspaceNodeType.FOLDER) {
WorkspaceFolder workspaceFolder = (WorkspaceFolder) workspaceNode;
List<WorkspaceNode> children = includeHidden ? workspaceNodeDAO.listByParentSortByOrderNumber(workspaceFolder) : workspaceNodeDAO.listByParentAndHiddenSortByOrderNumber(workspaceFolder, Boolean.FALSE);
result.add(new FlattenedWorkspaceNode(true, workspaceFolder.getTitle(), workspaceFolder, level, workspaceFolder.getParent().getId(), workspaceFolder.getHidden()));
result.addAll(flattenWorkspaceNodes(children, level + 1, includeHidden));
} else {
result.add(new FlattenedWorkspaceNode(false, null, workspaceNode, level, workspaceNode.getParent().getId(), workspaceNode.getHidden()));
}
}
return result;
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceMaterialController method generateUniqueUrlName.
public synchronized String generateUniqueUrlName(WorkspaceNode parent, WorkspaceNode targetNode, String title) {
String urlName = generateUrlName(title);
String fileName = StringUtils.substringBeforeLast(urlName, ".");
String extension = StringUtils.substringAfterLast(urlName, ".");
int extensionLength = StringUtils.length(extension);
boolean isFileName = StringUtils.isAlphanumeric(extension) && extensionLength < StringUtils.length(urlName) - 1;
// use urlName as base and uniqueName as final result
String uniqueName = urlName;
if (parent != null) {
// if parent node is given, ensure that the generated url name is unique amongst its child nodes
int i = 1;
while (true) {
// find child node with uniqueName
WorkspaceNode workspaceNode = workspaceNodeDAO.findByParentAndUrlName(parent, uniqueName);
if (workspaceNode != null) {
if (targetNode != null && workspaceNode.getId().equals(targetNode.getId())) {
// uniqueName is in use but by the target node itself, so it's okay
break;
}
// uniqueName in use, try again with the next candidate (name, name-2, name-3, etc.)
uniqueName = isFileName ? String.format("%s-%d.%s", fileName, ++i, extension) : String.format("%s-%d", urlName, ++i);
} else {
// Current uniqueName is available
break;
}
}
}
return uniqueName;
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceMaterialController method listWorkspaceMaterialsAsContentNodes.
public List<ContentNode> listWorkspaceMaterialsAsContentNodes(WorkspaceEntity workspaceEntity, boolean includeHidden) throws WorkspaceMaterialException {
List<ContentNode> contentNodes = new ArrayList<>();
WorkspaceRootFolder rootFolder = findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
List<WorkspaceNode> rootMaterialNodes = includeHidden ? listWorkspaceNodesByParentAndFolderTypeSortByOrderNumber(rootFolder, WorkspaceFolderType.DEFAULT) : listVisibleWorkspaceNodesByParentAndFolderTypeSortByOrderNumber(rootFolder, WorkspaceFolderType.DEFAULT);
for (WorkspaceNode rootMaterialNode : rootMaterialNodes) {
ContentNode node = createContentNode(rootMaterialNode, 1, true, includeHidden);
contentNodes.add(node);
}
return contentNodes;
}
use of fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode in project muikku by otavanopisto.
the class WorkspaceMaterialController method findWorkspaceNodeByWorkspaceEntityAndPath.
public WorkspaceNode findWorkspaceNodeByWorkspaceEntityAndPath(WorkspaceEntity workspaceEntity, String path) {
String[] pathElements = path.split("/");
WorkspaceNode parent = findWorkspaceRootFolderByWorkspaceEntity(workspaceEntity);
for (int i = 0, l = pathElements.length; i < l - 1; i++) {
String pathElement = pathElements[i];
parent = findWorkspaceNodeByParentAndUrlName(parent, pathElement);
}
return findWorkspaceNodeByParentAndUrlName(parent, pathElements[pathElements.length - 1]);
}
Aggregations