-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from keeps/ap_previewsip
Preview and create SIPs
- Loading branch information
Showing
29 changed files
with
1,373 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.