Skip to content

Commit

Permalink
Merge pull request #148 from keeps/ap_alpha5_bugfixing
Browse files Browse the repository at this point in the history
Bug fixing
  • Loading branch information
adrapereira committed Apr 11, 2016
2 parents c222c18 + 6e38f7c commit 008c3cb
Show file tree
Hide file tree
Showing 18 changed files with 332 additions and 338 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/roda/rodain/creation/BagitSipCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private long nodeSize(TreeNode node) throws IOException {
Path nodePath = node.getPath();
long result = 0;
if (Files.isDirectory(nodePath)) {
for (TreeNode tn : node.getAllFiles().values()) {
for (TreeNode tn : node.getChildren().values()) {
result += nodeSize(tn);
}
} else {
Expand All @@ -208,7 +208,7 @@ private void recCreateFiles(TreeNode node, Path dest) throws IOException {
if (Files.isDirectory(nodePath)) {
Path directory = dest.resolve(nodePath.getFileName().toString());
new File(directory.toString()).mkdir();
for (TreeNode tn : node.getAllFiles().values()) {
for (TreeNode tn : node.getChildren().values()) {
recCreateFiles(tn, directory);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/roda/rodain/creation/EarkSipCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private void addFileToRepresentation(TreeNode tn, List<String> relativePath, IPR
List<String> newRelativePath = new ArrayList<>(relativePath);
newRelativePath.add(tn.getPath().getFileName().toString());
// recursive call to all the node's children
for (TreeNode node : tn.getAllFiles().values()) {
for (TreeNode node : tn.getChildren().values()) {
addFileToRepresentation(node, newRelativePath, rep);
}
} else {
Expand All @@ -182,7 +182,7 @@ private void addDocToZip(TreeNode tn, List<String> relativePath, SIP earkSip) {
List<String> newRelativePath = new ArrayList<>(relativePath);
newRelativePath.add(tn.getPath().getFileName().toString());
// recursive call to all the node's children
for (TreeNode node : tn.getAllFiles().values()) {
for (TreeNode node : tn.getChildren().values()) {
addDocToZip(node, newRelativePath, earkSip);
}
} else {
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/roda/rodain/inspection/InspectionPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,8 @@ private void createContent() {
HBox.setHgrow(space, Priority.ALWAYS);

sipDocumentation = new SipDocumentationTreeView();

docsRoot = new SipContentDirectory(new TreeNode(Paths.get("")), null);
sipDocumentation.setRoot(docsRoot);
toggleDocumentation = new ToggleButton();
toggleDocumentation.setTooltip(new Tooltip(I18n.t("documentation")));
Platform.runLater(() -> {
Expand Down Expand Up @@ -693,7 +694,9 @@ private void createContent() {
}
} else { // from the documentation to the representations
toggleDocumentation.setTooltip(new Tooltip(I18n.t("documentation")));
dataBox.getChildren().clear();
dataBox.getChildren().add(sipFiles);
content.setCenter(dataBox);
content.setBottom(contentBottom);
}
});
Expand Down Expand Up @@ -827,6 +830,8 @@ protected Void call() throws Exception {
sipRoot = newRoot;
if (active) {
sipFiles.setRoot(sipRoot);
dataBox.getChildren().clear();
dataBox.getChildren().add(sipFiles);
content.setCenter(dataBox);
content.setBottom(contentBottom);
}
Expand All @@ -835,8 +840,7 @@ protected Void call() throws Exception {
}

private void createDocumentation(SipPreviewNode sip, boolean active) {
SipContentDirectory newRoot = new SipContentDirectory(new TreeNode(Paths.get("")), null);

docsRoot.getChildren().clear();
if (active) {
content.setCenter(loadingPane);
content.setBottom(new HBox());
Expand All @@ -846,19 +850,21 @@ private void createDocumentation(SipPreviewNode sip, boolean active) {
@Override
protected Void call() throws Exception {
for (TreeNode treeNode : sip.getSip().getDocumentation()) {
TreeItem<Object> startingItem = recCreateSipContent(treeNode, newRoot);
TreeItem<Object> startingItem = recCreateSipContent(treeNode, docsRoot);
startingItem.setExpanded(true);
newRoot.getChildren().add(startingItem);
docsRoot.getChildren().add(startingItem);
}
newRoot.sortChildren();
docsRoot.sortChildren();
return null;
}
};
docsTask.setOnSucceeded(event -> {
docsRoot = newRoot;
if (active) {
sipDocumentation.setRoot(docsRoot);
if (!docsRoot.getChildren().isEmpty()) {
if (!dataBox.getChildren().contains(sipDocumentation)) {
dataBox.getChildren().clear();
dataBox.getChildren().add(sipDocumentation);
}
content.setCenter(dataBox);
content.setBottom(docsBottom);
} else {
Expand All @@ -867,6 +873,7 @@ protected Void call() throws Exception {
}
}
});

new Thread(docsTask).start();
}

Expand Down Expand Up @@ -1047,15 +1054,6 @@ public void update(SipPreviewNode sip) {
boolean documentation = toggleDocumentation.isSelected();
createContent(sip, !documentation);
createDocumentation(sip, documentation);
if (documentation) {
if (docsRoot.getChildren().isEmpty()) {
content.setBottom(new HBox());
} else {
content.setBottom(docsBottom);
}
} else {
content.setBottom(contentBottom);
}

center.getChildren().addAll(metadata, content);
setCenter(center);
Expand Down Expand Up @@ -1139,8 +1137,10 @@ private void showMetadataHelp() {
}

private void updateTextArea(String content) {
metaText.replaceText(content);
metaText.setStyleSpans(0, XMLEditor.computeHighlighting(content));
Platform.runLater(() -> {
metaText.replaceText(content);
metaText.setStyleSpans(0, XMLEditor.computeHighlighting(content));
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void skip() {
TreeNode parentTreeNode = par.getTreeNode();
parentTreeNode.remove(treeNode.getPath()); // remove this treeNode from the
// parent
parentTreeNode.addAll(treeNode.getAllFiles()); // add this treeNode's
parentTreeNode.addAll(treeNode.getChildren()); // add this treeNode's
// children to this node's
// parent
par.sortChildren();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/roda/rodain/rules/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public RuleTypes getAssocType() {
* @see SipSingle
*/
public TreeVisitor apply() {
sips = new HashMap<>();
sips = new ConcurrentHashMap<>();
sipNodes = new ConcurrentHashMap<>();
schemaNodes = Collections.synchronizedSet(new HashSet<>());

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/roda/rodain/rules/TreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Set<Path> getFullTreePathsAsPaths() {
/**
* @return The direct children of the TreeNode.
*/
public Map<String, TreeNode> getAllFiles() {
public Map<String, TreeNode> getChildren() {
return files;
}

Expand Down
99 changes: 1 addition & 98 deletions src/main/java/org/roda/rodain/rules/sip/SipPerFile.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package org.roda.rodain.rules.sip;

import org.apache.commons.io.FilenameUtils;
import org.roda.rodain.core.PathCollection;
import org.roda.rodain.rules.MetadataTypes;
import org.roda.rodain.rules.TreeNode;
import org.roda.rodain.rules.filters.ContentFilter;
import org.roda.rodain.schema.DescObjMetadata;
import org.roda.rodain.source.ui.items.SourceTreeItemState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -28,8 +22,6 @@ public class SipPerFile extends SipPreviewCreator {
private static final int UPDATEFREQUENCY = 500; // in milliseconds
private long lastUIUpdate = 0;

private Map<String, Set<Path>> metadata;

/**
* Creates a new SipPreviewCreator where there's a new SIP created for each
* visited file.
Expand All @@ -48,28 +40,6 @@ public class SipPerFile extends SipPreviewCreator {
public SipPerFile(String id, Set<ContentFilter> filters, MetadataTypes metaType, Path metadataPath,
String templateType, String templateVersion) {
super(id, filters, metaType, metadataPath, templateType, templateVersion);
metadata = new HashMap<>();

if (metadataPath != null && metaType == MetadataTypes.DIFF_DIRECTORY) {
try {
Files.walkFileTree(metadataPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String key = FilenameUtils.removeExtension(file.getFileName().toString());
Set<Path> paths = metadata.get(key);
if (paths == null)
paths = new HashSet<>();
paths.add(file);
metadata.put(key, paths);
return FileVisitResult.CONTINUE;
}
});
} catch (AccessDeniedException e) {
log.info("Access denied to file", e);
} catch (IOException e) {
log.error("Error walking the file tree", e);
}
}
}

@Override
Expand Down Expand Up @@ -148,27 +118,8 @@ public void visitFile(Path path, BasicFileAttributes attrs) {
if (filter(path) || cancelled)
return;

Path metaPath = getMetadataPath(path);
TreeNode node = new TreeNode(path);
Set<TreeNode> files = new HashSet<>();
files.add(node);

DescObjMetadata dom = null;
if (metaType == MetadataTypes.TEMPLATE)
dom = new DescObjMetadata(metaType, templateType, templateVersion);
else if (metaPath != null)
dom = new DescObjMetadata(metaType, metaPath);

SipRepresentation rep = new SipRepresentation("rep1");
rep.setFiles(files);
Set<SipRepresentation> repSet = new HashSet<>();
repSet.add(rep);
SipPreview sipPreview = new SipPreview(path.getFileName().toString(), repSet, dom);
node.addObserver(sipPreview);

sips.add(sipPreview);
sipsMap.put(sipPreview.getId(), sipPreview);
added++;
createSip(path, node);

long now = System.currentTimeMillis();
if (now - lastUIUpdate > UPDATEFREQUENCY) {
Expand All @@ -177,52 +128,4 @@ else if (metaPath != null)
lastUIUpdate = now;
}
}

private Path getMetadataPath(Path path) {
Path result;
switch (metaType) {
case SINGLE_FILE:
result = metadataPath;
break;
case DIFF_DIRECTORY:
result = getFileFromDir(path);
break;
case SAME_DIRECTORY:
result = searchMetadata(path);
break;
default:
return null;
}
return result;
}

private Path searchMetadata(Path sipPath) {
File dir = sipPath.toFile();
if (!dir.isDirectory())
dir = sipPath.getParent().toFile();

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + templateType);
File[] foundFiles = dir.listFiles((dir1, name) -> matcher.matches(Paths.get(name)));

if (foundFiles != null && foundFiles.length > 0) {
return foundFiles[0].toPath();
}
return null;
}

private Path getFileFromDir(Path path) {
String fileNameWithExtension = path.getFileName().toString();
String fileName = FilenameUtils.removeExtension(fileNameWithExtension);

Set<Path> paths = metadata.get(fileName);
Path result = null;
if (paths != null) {
for (Path p : paths) {
if (!p.getFileName().toString().equals(fileNameWithExtension))
result = p;

}
}
return result;
}
}
54 changes: 0 additions & 54 deletions src/main/java/org/roda/rodain/rules/sip/SipPerSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import org.roda.rodain.rules.MetadataTypes;
import org.roda.rodain.rules.TreeNode;
import org.roda.rodain.rules.filters.ContentFilter;
import org.roda.rodain.schema.DescObjMetadata;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -109,30 +105,6 @@ public void postVisitDirectory(Path path) {
}
}

private void createSip(Path path, TreeNode node) {
Path metaPath = getMetadataPath(path);
// create a new Sip
Set<TreeNode> files = new HashSet<>();
files.add(node);

DescObjMetadata metadata;
if (metaType == MetadataTypes.TEMPLATE)
metadata = new DescObjMetadata(metaType, templateType, templateVersion);
else
metadata = new DescObjMetadata(metaType, metaPath);

SipRepresentation rep = new SipRepresentation("rep1");
rep.setFiles(files);
Set<SipRepresentation> repSet = new HashSet<>();
repSet.add(rep);
SipPreview sipPreview = new SipPreview(path.getFileName().toString(), repSet, metadata);
node.addObserver(sipPreview);

sips.add(sipPreview);
sipsMap.put(sipPreview.getId(), sipPreview);
added++;
}

/**
* If the path is in the selected set of paths creates a new SIP using the
* file, otherwise, adds the visited file to its parent.
Expand All @@ -155,30 +127,4 @@ public void visitFile(Path path, BasicFileAttributes attrs) {
nodes.peekLast().add(path);
}
}

private Path getMetadataPath(Path sipPath) {
Path result = null;
if (metaType == MetadataTypes.SINGLE_FILE) {
result = metadataPath;
} else if (metaType == MetadataTypes.SAME_DIRECTORY) {
result = searchMetadata(sipPath);
}
return result;
}

private Path searchMetadata(Path sipPath) {
File dir = sipPath.toFile();
if (!dir.isDirectory())
dir = sipPath.getParent().toFile();

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + templateType);
File[] foundFiles = dir.listFiles((dir1, name) -> {
return matcher.matches(Paths.get(name));
});

if (foundFiles != null && foundFiles.length > 0) {
return foundFiles[0].toPath();
}
return null;
}
}
Loading

0 comments on commit 008c3cb

Please sign in to comment.