1 package org.vamsas.test.simpleclient;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.PrintWriter;
7 import java.text.DateFormat;
9 import java.util.Hashtable;
10 import java.util.Vector;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
15 import uk.ac.vamsas.objects.core.Alignment;
16 import uk.ac.vamsas.objects.core.ApplicationData;
17 import uk.ac.vamsas.objects.core.Entry;
18 import uk.ac.vamsas.objects.core.Instance;
19 import uk.ac.vamsas.objects.core.Provenance;
20 import uk.ac.vamsas.objects.core.VAMSAS;
21 import uk.ac.vamsas.objects.core.VamsasDocument;
22 import uk.ac.vamsas.objects.ut
23 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
24 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
27 public class ArchiveWriter {
30 * Test program for writing archive files.
31 * form is ArchiveWriter new/modified argive command list
34 static Log log = LogFactory.getLog(ArchiveWriter.class);
36 private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
38 for (i=0; i<svec1.length; i++)
39 destvec[i] = svec1[i];
40 for (int j=0; j<svec2.length; i++, j++)
41 destvec[i] = svec2[j];
43 // Merge appDataReferences require transfer of jar entries, perhaps with a renaming of the entry.
44 // Merge appDatas require eventually unique URNS
45 // TODO: merging global appdata from different documents where same app has written them causes conflict
47 public static Hashtable hashOfAppDatas(Hashtable ht, Instance[] appdatas) {
50 for (int i=0, j=appdatas.length; i<j; i++) {
51 if (!ht.containsKey(appdatas[i].getUrn())) {
52 Hashtable aphash = new Hashtable();
53 ht.put(appdatas[i].getUrn(), aphash);
54 aphash.put(appdatas[i], appdatas[i].getDataReference());
56 // ensure urns and references are unique
65 * safely copies an appData from one archive to another.
66 * @param darc destination archive
67 * @param dest destination document Vobject
68 * @param sarc source archive reader
69 * @param entry application data to be copied from source archive
71 public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, ApplicationData entry) {
72 // TODO: fix instances
73 // check uniqueness of instance's[] entry.urn amongst dest.ApplicationData[].getInstances[].urn
74 // check uniqueness of entry.user[].urn amongst dest.ApplicationData[].user[].urn
75 // check uniqueness of entry.user
76 // entry.getAppDataChoice().getData() or getDataReference is unique
77 ApplicationData newo = new ApplicationData();
78 for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
79 ApplicationData o = dest.getApplicationData()[i];
80 // ensure new urn is really unique
81 //String urn = entry.getUrn();
83 //while (o.getUrn().equals(urn)) {
84 // urn = entry.getUrn()+v++;
87 // check each user ApplicationData
89 // copy over valid objects
94 * Copy new datasets and appdatas from one vamsas document to another.
99 * @return true if merge was successful.
101 public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, VamsasDocument source) {
102 log.debug("mergeDocs entered.");
103 // search for appDatas in cdoc
104 VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount()+source.getVAMSASCount()];
105 mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
106 dest.setVAMSAS(newr);
107 /** TODO: LATER: should verify that all ids really are unique in newly merged document. If not then what ?
108 * investigate possibility of having an id translation between appDatas and the core document -
109 * the mapping is stored when an external application performs a merge, but when the owning
110 * Application accesses the Vobject, the vorba_id is updated to the new one when it writes its
111 * references in to its appdata again
113 if (source.getApplicationDataCount()>0) {
114 ApplicationData[] newdat = new ApplicationData[source.getApplicationDataCount()+dest.getApplicationDataCount()];
115 ApplicationData[] sappd = source.getApplicationData();
116 // check refs and update/modify if necessary
117 for (int i=0; i<sappd.length; i++) {
118 addAppDataEntry(darc, dest, sarc, sappd[i]);
123 return true; // success
126 private static CommandProcessor cproc;
128 cproc.addCommand("new", 0, "no args");
129 cproc.addCommand("add", 1, "Need another vamsas document archive filename as argument.");
130 cproc.addCommand("repair", 0, "no args");
131 cproc.addCommand("list", 0, "no args");
132 cproc.addCommand("monitor", 0, "no args");
135 public static void main(String argv[]) {
137 * TODO: switches for setting user identities for writing to vamsas document
140 log.fatal("Usage : <archive to create> [(commands)]");
143 File newarch = new File(argv[0]);
146 // test fully fledged doc construction
147 VamsasArchive varc = new VamsasArchive(newarch, true);
148 VamsasDocument docroot;
149 docroot = new VamsasDocument();
150 docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter", "user", "Created new Vamsas Document"));
151 while (++argpos<argv.length) {
152 File archive = new File(argv[argpos]);
154 if (archive.exists()) {
155 VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
156 if (vdoc.isValid()) {
157 istream = vdoc.getVamsasDocumentStream();
159 VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
161 mergeDocs(varc, docroot, vdoc, cdocroot);
163 log.warn("Unexpectedly null document stream from existing document "+archive);
165 // updating an oldformat stream ?
166 if ((istream = vdoc.getVamsasXmlStream())!=null) {
167 // make a new vamsas document from the vamsas.xml entry
168 VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO: verify only one VAMSAS element per vamsas.xml entry.
169 docroot.getProvenance().addEntry(ProvenanceStuff.newProvenanceEntry("ArchiveWriter", "user", "added vamsas.xml from "+argv[argpos-1]));
170 docroot.addVAMSAS(root);
174 // Begin a new vamsas document
175 PrintWriter docwriter = varc.getDocumentOutputStream();
178 } catch (Exception e) {
179 log.error("Whilst manipulating "+argv[0], e);