public static void main(String [] args) {
RepatriateData rt = new RepatriateData();
rt.setupContext();
Long minStudyId = 0L; // Skip studies with ID numbers less than this
SubmissionHome sh = ContextManager.getSubmissionHome();
if (args.length > 0) {
minStudyId = Long.parseLong(args[0]);
}
for (TBPersistable tbS : ContextManager.getStudyHome().findAll(Study.class)) {
Study s = (Study) tbS;
if (s.getId() < minStudyId) { continue; }
Submission sub = s.getSubmission();
Collection<TreeBlock> tbs = sub.getSubmittedTreeBlocksReadOnly();
Collection<Matrix> ms = sub.getSubmittedMatricesReadOnly();
System.err.println("Study " + s.getId() + ": ");
for (Analysis an : s.getAnalyses()) {
for (AnalysisStep as : an.getAnalysisStepsReadOnly()) {
Transaction tr = null;
for (AnalyzedData ad : as.getDataSetReadOnly()) {
if (tr == null) {
tr = rt.beginTransaction();
}
Matrix m = ad.getMatrixData();
PhyloTree t = ad.getTreeData();
if (m != null) {
System.err.println(" Repatriating matrix " + m.getId()
+ " to study " + s.getId() + " submission " + sub.getId());
if (m.getStudy() != s) {
String nexusFileName = m.getNexusFileName();
Map<String,String> nexusMap = m.getStudy().getNexusFiles();
String nexusFile = nexusMap.remove(nexusFileName);
s.getNexusFiles().put(nexusFileName, nexusFile);
m.setStudy(s);
}
if (! ms.contains(m)) {
Submission oldSub = sh.findByMatrix(m);
if (oldSub != null) {
oldSub.removeMatrix(m);
sh.flush();
}
sub.addMatrix(m);
}
} else if (t != null) {
System.err.println(" Repatriating tree " + t.getId()
+ " to study " + s.getId() + " submission " + sub.getId());
if (t.getStudy() != s) {
String nexusFileName = t.getNexusFileName();
Map<String,String> nexusMap = t.getStudy().getNexusFiles();
String nexusFile = nexusMap.remove(nexusFileName);
s.getNexusFiles().put(nexusFileName, nexusFile);
t.setStudy(s);
}
TreeBlock tb = t.getTreeBlock();
if (tb == null) {
System.err.println(" No tree block!");
} else if (! tbs.contains(tb)) {
Submission oldSub = sh.findByTreeBlock(tb);
if (oldSub != null) {
oldSub.removePhyloTreeBlock(tb);
sh.flush();
}
sub.addPhyloTreeBlock(tb);
}
}
}