refactored org to uk
[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 import java.util.Vector;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 der;
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;
25 ils.ProvenanceStuff;
26
27 public class ArchiveWriter {
28   
29   /**
30    * Test program for writing archive files.
31    * form is ArchiveWriter new/modified argive command list
32    */
33   
34   static Log log = LogFactory.getLog(ArchiveWriter.class);
35     
36   private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
37     int i;
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];
42   }
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
46   
47   public static Hashtable hashOfAppDatas(Hashtable ht, Instance[] appdatas) {
48     if (ht==null)
49       ht = new Hashtable();
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());
55       } else {
56         // ensure urns and references are unique
57         
58         
59       }
60           
61     }
62     return ht;
63   }
64   /**
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
70    */
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();
82       int v = 1;
83       //while (o.getUrn().equals(urn)) {
84       //  urn = entry.getUrn()+v++;      
85      // }
86       // uniqueness of urn
87       // check each user ApplicationData
88       // uniqueness (again)
89       // copy over valid objects
90       // 
91     }
92   }
93   /**
94    * Copy new datasets and appdatas from one vamsas document to another.
95    * @param darc
96    * @param dest
97    * @param sarc
98    * @param source
99    * @return true if merge was successful.
100    */
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
112      */
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]);
119       }
120       
121     }
122     
123     return true; // success    
124   }
125   
126   private static CommandProcessor cproc;
127   static {
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");    
133   }
134   
135   public static void main(String argv[]) {
136     /**
137      * TODO: switches for setting user identities for writing to vamsas document
138      */ 
139     if (argv.length<1) {
140       log.fatal("Usage : <archive to create> [(commands)]");
141       return;
142     }
143     File newarch = new File(argv[0]);
144     int argpos = 0;
145     try {
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]);
153         InputStream istream;
154         if (archive.exists()) {
155           VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
156           if (vdoc.isValid()) {
157             istream = vdoc.getVamsasDocumentStream(); 
158             if (istream!=null) {
159               VamsasDocument cdocroot = VamsasDocument.unmarshal(new InputStreamReader(istream));
160               if (cdocroot!=null) 
161                 mergeDocs(varc, docroot, vdoc, cdocroot);
162             } else 
163               log.warn("Unexpectedly null document stream from existing document "+archive);
164           } else {
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);
171               }
172           }
173         } else {
174           // Begin a new vamsas document
175           PrintWriter docwriter = varc.getDocumentOutputStream();
176         }
177       }
178     } catch (Exception e) {
179       log.error("Whilst manipulating "+argv[0], e);
180     }
181   }
182 }