extracted serial command processing for use by other test classes
[vamsas.git] / src / org / vamsas / test / simpleclient / ArchiveWriter.java
1 package org.vamsas.test.simpleclient;
2
3 import java.io.File;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.io.PrintWriter;
7 import java.text.DateFormat;
8 import java.util.Date;
9 import java.util.Hashtable;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.vamsas.client.simpleclient.VamsasArchive;
14 import org.vamsas.client.simpleclient.VamsasArchiveReader;
15 import org.vamsas.objects.core.ApplicationData;
16 import org.vamsas.objects.core.Entry;
17 import org.vamsas.objects.core.Provenance;
18 import org.vamsas.objects.core.VAMSAS;
19 import org.vamsas.objects.core.VamsasDocument;
20
21 public class ArchiveWriter {
22   
23   /**
24    * Test program for writing archive files.
25    */
26   
27   static Log log = LogFactory.getLog("org.vamsas.test.simpleclient.ArchiveWriter");
28   
29   /**
30    * @param action
31    *          text for action entry
32    * @return new Provenance entry for ArchiveWriter created docs.
33    */
34   public static Entry newProvenanceEntry(String user, String action) { 
35     log.debug("Adding ProvenanceEntry("+user+","+action+")");
36     Entry e = new Entry();
37     e.setAction(action);
38     e.setUser(user);
39     e.setDate(new org.exolab.castor.types.Date(new Date()));
40     return e;
41   }
42   
43   public static Provenance newProvenance() {
44     Provenance list = new Provenance();
45     list.addEntry(newProvenanceEntry("ArchiveWriter", "Created new Vamsas Document"));
46     return list;
47   }
48   private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
49     int i;
50     for (i=0; i<svec1.length; i++)
51       destvec[i] = svec1[i];
52     for (int j=0; j<svec2.length; i++, j++)
53       destvec[i] = svec2[j];
54   }
55   // Merge appDataReferences require transfer of jar entries, perhaps with a renaming of the entry.
56   // Merge appDatas require eventually unique URNS
57   
58   public static Hashtable hashOfAppDatas(Hashtable ht, ApplicationData[] appdatas) {
59     if (ht==null)
60       ht = new Hashtable();
61     for (int i=0, j=appdatas.length; i<j; i++) {
62       if (!ht.containsKey(appdatas[i].getUrn())) {
63         Hashtable aphash = new Hashtable();
64         ht.put(appdatas[i].getUrn(), aphash);
65         aphash.put(appdatas[i], appdatas[i].getAppDataChoice().getDataReference());
66       } else {
67         // ensure urns and references are unique
68         
69         
70       }
71           
72     }
73     return ht;
74   }
75   public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest,  VamsasArchiveReader sarc, ApplicationData entry) {
76     
77     // check uniqueness of entry.urn amongst dest.ApplicationData[].urn
78     //  check uniqueness of entry.user[].urn amongst dest.ApplicationData[].user[].urn
79     // check uniqueness of entry.user
80     // entry.getAppDataChoice().getData() or getDataReference is unique
81     ApplicationData newo = new ApplicationData();
82     for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
83       ApplicationData o = dest.getApplicationData()[i];
84       // ensure new urn is really unique
85       String urn = entry.getUrn();
86       int v = 1;
87       while (o.getUrn().equals(urn)) {
88         urn = entry.getUrn()+v++;      
89       }
90       // uniqueness of urn
91       // check each user ApplicationData
92       // uniqueness (again)
93       // copy over valid objects
94       // 
95     }
96   }
97   public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest,  VamsasArchiveReader sarc, VamsasDocument source) {
98     log.debug("mergeDocs entered.");
99     // search for appDatas in cdoc
100     VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount()+source.getVAMSASCount()];
101     mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
102     dest.setVAMSAS(newr);
103     // TODO: should verify that all ids really are unique in newly merged document. If not then what ?
104     
105     if (source.getApplicationDataCount()>0) {
106       ApplicationData[] newdat = new ApplicationData[source.getApplicationDataCount()+dest.getApplicationDataCount()];
107       ApplicationData[] sappd = source.getApplicationData();
108       // check refs and update/modify if necessary
109       for (int i=0; i<sappd.length; i++) {
110         addAppDataEntry(darc, dest, sarc, sappd[i]);
111       }
112       
113     }
114     
115     return true; // success    
116   }
117   
118   
119   public static void main(String argv[]) {
120     if (argv.length<1) {
121       log.fatal("Usage : <archive to create> [(commands)]");
122       return;
123     }
124     File newarch = new File(argv[0]);
125     int argpos = 0;
126     try {
127       // test fully fledged doc construction
128       VamsasArchive varc = new VamsasArchive(newarch, true);
129       VamsasDocument docroot;
130       docroot = new VamsasDocument();
131       docroot.setProvenance(newProvenance());
132       while (++argpos<argv.length) {
133         File archive = new File(argv[argpos]);
134         if (archive.exists()) {
135           VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
136           if (vdoc.isValid()) {
137             InputStream istream = vdoc.getVamsasDocumentStream(); 
138             if (istream!=null) {
139               VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
140               if (cdocroot!=null) 
141                 mergeDocs(varc, docroot, vdoc, cdocroot);
142             } else {
143               if ((istream = vdoc.getVamsasXmlStream())!=null) {
144                 // make a new vamsas document from the vamsas.xml entry
145                 VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream));
146                 docroot.getProvenance().addEntry(newProvenanceEntry("user", "added vamsas.xml from "+argv[argpos-1]));
147                 docroot.addVAMSAS(root);
148               }
149             }
150             
151           }
152           // Write a dummy vamsas document
153         
154           PrintWriter docwriter = varc.getDocumentOutputStream();
155         }
156       }
157     } catch (Exception e) {
158       log.error("Whilst manipulating "+argv[0], e);
159     }
160   }
161 }