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;
14 import org.vamsas.client.simpleclient.VamsasArchive;
15 import org.vamsas.client.simpleclient.VamsasArchiveReader;
16 import org.vamsas.objects.core.ApplicationData;
17 import org.vamsas.objects.core.Entry;
18 import org.vamsas.objects.core.Provenance;
19 import org.vamsas.objects.core.VAMSAS;
20 import org.vamsas.objects.core.VamsasDocument;
21 import org.vamsas.objects.utils.ProvenanceStuff;
23 public class ArchiveWriter {
26 * Test program for writing archive files.
27 * form is ArchiveWriter new/modified argive command list
30 static Log log = LogFactory.getLog(ArchiveWriter.class);
32 private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
34 for (i=0; i<svec1.length; i++)
35 destvec[i] = svec1[i];
36 for (int j=0; j<svec2.length; i++, j++)
37 destvec[i] = svec2[j];
39 // Merge appDataReferences require transfer of jar entries, perhaps with a renaming of the entry.
40 // Merge appDatas require eventually unique URNS
42 public static Hashtable hashOfAppDatas(Hashtable ht, ApplicationData[] appdatas) {
45 for (int i=0, j=appdatas.length; i<j; i++) {
46 if (!ht.containsKey(appdatas[i].getUrn())) {
47 Hashtable aphash = new Hashtable();
48 ht.put(appdatas[i].getUrn(), aphash);
49 aphash.put(appdatas[i], appdatas[i].getDataReference());
51 // ensure urns and references are unique
60 * safely copies an appData from one archive to another.
61 * @param darc destination archive
62 * @param dest destination document Vobject
63 * @param sarc source archive reader
64 * @param entry application data to be copied from source archive
66 public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, ApplicationData entry) {
68 // check uniqueness of entry.urn amongst dest.ApplicationData[].urn
69 // check uniqueness of entry.user[].urn amongst dest.ApplicationData[].user[].urn
70 // check uniqueness of entry.user
71 // entry.getAppDataChoice().getData() or getDataReference is unique
72 ApplicationData newo = new ApplicationData();
73 for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
74 ApplicationData o = dest.getApplicationData()[i];
75 // ensure new urn is really unique
76 String urn = entry.getUrn();
78 while (o.getUrn().equals(urn)) {
79 urn = entry.getUrn()+v++;
82 // check each user ApplicationData
84 // copy over valid objects
89 * Copy new datasets and appdatas from one vamsas document to another.
94 * @return true if merge was successful.
96 public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest, VamsasArchiveReader sarc, VamsasDocument source) {
97 log.debug("mergeDocs entered.");
98 // search for appDatas in cdoc
99 VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount()+source.getVAMSASCount()];
100 mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
101 dest.setVAMSAS(newr);
102 /** TODO: LATER: should verify that all ids really are unique in newly merged document. If not then what ?
103 * investigate possibility of having an id translation between appDatas and the core document -
104 * the mapping is stored when an external application performs a merge, but when the owning
105 * Application accesses the Vobject, the vorba_id is updated to the new one when it writes its
106 * references in to its appdata again
108 if (source.getApplicationDataCount()>0) {
109 ApplicationData[] newdat = new ApplicationData[source.getApplicationDataCount()+dest.getApplicationDataCount()];
110 ApplicationData[] sappd = source.getApplicationData();
111 // check refs and update/modify if necessary
112 for (int i=0; i<sappd.length; i++) {
113 addAppDataEntry(darc, dest, sarc, sappd[i]);
118 return true; // success
121 private static CommandProcessor cproc;
123 cproc.addCommand("new", 0, "no args");
124 cproc.addCommand("add", 1, "Need another vamsas document archive filename as argument.");
125 cproc.addCommand("repair", 0, "no args");
126 cproc.addCommand("list", 0, "no args");
127 cproc.addCommand("monitor", 0, "no args");
130 public static void main(String argv[]) {
132 * TODO: switches for setting user identities for writing to vamsas document
135 log.fatal("Usage : <archive to create> [(commands)]");
138 File newarch = new File(argv[0]);
141 // test fully fledged doc construction
142 VamsasArchive varc = new VamsasArchive(newarch, true);
143 VamsasDocument docroot;
144 docroot = new VamsasDocument();
145 docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter", "Created new Vamsas Document"));
146 while (++argpos<argv.length) {
147 File archive = new File(argv[argpos]);
149 if (archive.exists()) {
150 VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
151 if (vdoc.isValid()) {
152 istream = vdoc.getVamsasDocumentStream();
154 VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
156 mergeDocs(varc, docroot, vdoc, cdocroot);
158 log.warn("Unexpectedly null document stream from existing document "+archive);
160 // updating an oldformat stream ?
161 if ((istream = vdoc.getVamsasXmlStream())!=null) {
162 // make a new vamsas document from the vamsas.xml entry
163 VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO: verify only one VAMSAS element per vamsas.xml entry.
164 docroot.getProvenance().addEntry(ProvenanceStuff.newProvenanceEntry("user", "added vamsas.xml from "+argv[argpos-1]));
165 docroot.addVAMSAS(root);
169 // Begin a new vamsas document
170 PrintWriter docwriter = varc.getDocumentOutputStream();
173 } catch (Exception e) {
174 log.error("Whilst manipulating "+argv[0], e);