Skip to content

Commit

Permalink
Merge pull request #4 from keeps/ap_previewsip
Browse files Browse the repository at this point in the history
Preview and create SIPs
  • Loading branch information
luis100 committed Oct 8, 2015
2 parents e7c30f4 + eea7ca0 commit b58dcb3
Show file tree
Hide file tree
Showing 29 changed files with 1,373 additions and 188 deletions.
47 changes: 47 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,51 @@
<artifactId>roda-in</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${version.java}</source>
<target>${version.java}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- <plugin>
&lt;!&ndash; Build an executable JAR &ndash;&gt;
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>core.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>-->
</plugins>
</build>

<profiles>
<profile>
<id>JDK_1.7</id>
<activation>
<jdk>1.7</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<version.java>1.7</version.java>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
Expand All @@ -29,6 +68,9 @@
<activation>
<jdk>1.8</jdk>
</activation>
<properties>
<version.java>1.8</version.java>
</properties>
</profile>
</profiles>

Expand Down Expand Up @@ -63,6 +105,11 @@
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>gov.loc</groupId>
<artifactId>bagit</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>

</project>
103 changes: 103 additions & 0 deletions src/main/java/core/CreateBagits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package core;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import org.slf4j.LoggerFactory;

import rules.Rule;
import rules.TreeNode;
import rules.VisitorStack;
import rules.VisitorState;
import schema.SipPreview;
import utils.TreeVisitor;
import gov.loc.repository.bagit.Bag;
import gov.loc.repository.bagit.BagFactory;
import gov.loc.repository.bagit.PreBag;
import gov.loc.repository.bagit.writer.impl.FileSystemWriter;

/**
* Created by adrapereira on 06-10-2015.
*/
public class CreateBagits extends Thread implements Observer {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(CreateBagits.class.getName());
private VisitorStack visitors;
private HashMap<String, Rule> unfinished;
private int successful = 0, error = 0;
private String startPath;

public CreateBagits(String path){
startPath = path;
visitors = new VisitorStack();
unfinished = new HashMap<String, Rule>();
visitors.addObserver(this);
}

public void run(){
for(Rule rule: Main.getRules()){
log.info(rule.getId());
TreeVisitor visitor = rule.apply();
visitors.add(rule.getSource().getPath(), visitor);
unfinished.put(rule.getId(), rule);
}
updateFooter();
}

public void update(Observable o, Object arg) {
for(String id: unfinished.keySet()){
if(visitors.isDone(id) == VisitorState.VISITOR_DONE){
createBagits(unfinished.get(id));
unfinished.remove(id);
}
}
updateFooter();
}

private void updateFooter(){
if(unfinished.size() == 0){
Footer.activeButton();
Footer.setStatus("Created " + successful + " Bagits. Errors creating " + error + " Bagits.");
}
}

private void createBagits(Rule rule){
ArrayList<SipPreview> sips = rule.getSips();

String path = startPath + "/" + rule.getId() + "/";
File ruleDir = new File(path);
ruleDir.mkdir();
int numSips = 0;
for(SipPreview sip: sips){
String name = path + "sip_" + rule.getId() + "_" + numSips;
//make the directory
new File(name).mkdir();
new File(name+"/data").mkdir();

try {
TreeNode node = sip.getFiles();
BagFactory bf = new BagFactory();

Bag b = bf.createBag();
b.addFileToPayload(new File(node.getPath()));
b.makeComplete();
b.close();

FileSystemWriter fsw = new FileSystemWriter(bf);
fsw.write(b, new File(name));

PreBag pb = bf.createPreBag(new File(name));
pb.makeBagInPlace(BagFactory.Version.V0_97, false);

numSips++;
}
catch (Exception e) {
e.printStackTrace();
error++;
}
}
successful += numSips;
}
}
44 changes: 38 additions & 6 deletions src/main/java/core/Footer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package core;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
Expand All @@ -9,19 +10,31 @@
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;

import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.slf4j.LoggerFactory;
import rules.Rule;
import rules.VisitorStack;
import utils.TreeVisitor;

import java.io.File;
import java.nio.file.Path;
import java.util.HashSet;


/**
* Created by adrapereira on 28-09-2015.
*/
public class Footer extends HBox{
public class Footer extends HBox {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(Footer.class.getName());
public static final Label status = new Label();
private Stage stage;
static Button btn;

public Footer(){
public Footer(Stage st){
super();
Button btn = new Button("Create SIPs");
this.stage = st;
btn = new Button("Create SIPs");

HBox space = new HBox();
HBox.setHgrow(space, Priority.ALWAYS);
Expand All @@ -33,14 +46,33 @@ public Footer(){

btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
Footer.setStatus("Carregou no \"Create SIPs\"");
DirectoryChooser chooser = new DirectoryChooser();
chooser.setTitle("Please choose a folder");
File selectedDirectory = chooser.showDialog(stage);
if (selectedDirectory == null) return;
Path path = selectedDirectory.toPath();
Footer.setStatus("Creating BagIts...");
btn.setDisable(true);
CreateBagits creator = new CreateBagits(path.toString());
creator.start();
}
});
}

public static void setStatus(String st){
status.setText(st);
public static void setStatus(final String st){
Platform.runLater(new Runnable() {
public void run() {
status.setText(st);
}
});
}

public static void activeButton(){
Platform.runLater(new Runnable() {
public void run() {
btn.setDisable(false);
}
});
}

}
28 changes: 23 additions & 5 deletions src/main/java/core/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;

import org.slf4j.LoggerFactory;

import rules.Rule;
import rules.ui.RuleComponent;
import rules.ui.RulesPane;
import schema.ui.SchemaNode;
import schema.ui.SchemaPane;
import source.ui.FileExplorerPane;
import source.ui.items.SourceTreeItem;

import java.io.IOException;
import java.util.HashSet;

public class Main extends Application {
private static final org.slf4j.Logger log = LoggerFactory.getLogger(Main.class.getName());
public Stage stage;
private Stage stage;

private static FileExplorerPane previewExplorer;
private static BorderPane rulesPane;
private static RulesPane rulesPane;
private static SchemaPane schemaPane;

public static void main(String[] args) {
Expand All @@ -37,7 +43,14 @@ public static void main(String[] args) {
public void start(Stage primaryStage) {
stage = primaryStage;

try {
stage.getIcons().add(new Image(ClassLoader.getSystemResource("roda2-logo.png").openStream()));
} catch (IOException e) {
log.error(e.getMessage());
}

createFrameStructure();
stage.show();
}

private void createFrameStructure(){
Expand All @@ -52,14 +65,14 @@ private void createFrameStructure(){

// Divide center pane in 3
SplitPane split = new SplitPane();
//StackPane previewExplorer = createPreviewExplorer();
previewExplorer = new FileExplorerPane(stage);
rulesPane = new RulesPane(stage);
schemaPane = new SchemaPane(stage);

split.getItems().addAll(previewExplorer, rulesPane, schemaPane);

//Create Footer
HBox footer = new Footer();
HBox footer = new Footer(stage);

BorderPane mainPane = new BorderPane();
mainPane.setCenter(split);
Expand All @@ -70,7 +83,6 @@ private void createFrameStructure(){
Scene scene = new Scene(mainPane, bounds.getWidth(), bounds.getHeight());
scene.getStylesheets().add(ClassLoader.getSystemResource("Modena.css").toExternalForm());
stage.setScene(scene);
stage.show();
}

public static SchemaNode getSchemaSelectedItem(){
Expand All @@ -79,4 +91,10 @@ public static SchemaNode getSchemaSelectedItem(){
public static SourceTreeItem getSourceSelectedItem(){
return previewExplorer.getSelectedItem();
}
public static void removeRule(RuleComponent rule){
rulesPane.removeChild(rule);
}
public static HashSet<Rule> getRules(){
return rulesPane.getRules();
}
}
Loading

0 comments on commit b58dcb3

Please sign in to comment.