applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / test / simpleclient / ArchiveWriter.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.test.simpleclient;\r
23 \r
24 import java.io.File;\r
25 import java.io.InputStream;\r
26 import java.io.InputStreamReader;\r
27 import java.io.PrintWriter;\r
28 import java.text.DateFormat;\r
29 import java.util.Date;\r
30 import java.util.Hashtable;\r
31 import java.util.Vector;\r
32 \r
33 import org.apache.commons.logging.Log;\r
34 import org.apache.commons.logging.LogFactory;\r
35 import uk.ac.vamsas.objects.core.Alignment;\r
36 import uk.ac.vamsas.objects.core.ApplicationData;\r
37 import uk.ac.vamsas.objects.core.Entry;\r
38 import uk.ac.vamsas.objects.core.Instance;\r
39 import uk.ac.vamsas.objects.core.Provenance;\r
40 import uk.ac.vamsas.objects.core.VAMSAS;\r
41 import uk.ac.vamsas.objects.core.VamsasDocument;\r
42 import uk.ac.vamsas.objects.utils.ProvenanceStuff;\r
43 import uk.ac.vamsas.client.simpleclient.VamsasArchive;\r
44 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;\r
45 \r
46 public class ArchiveWriter {\r
47 \r
48   /**\r
49    * Test program for writing archive files. form is ArchiveWriter new/modified\r
50    * argive command list\r
51    */\r
52 \r
53   static Log log = LogFactory.getLog(ArchiveWriter.class);\r
54 \r
55   private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {\r
56     int i;\r
57     for (i = 0; i < svec1.length; i++)\r
58       destvec[i] = svec1[i];\r
59     for (int j = 0; j < svec2.length; i++, j++)\r
60       destvec[i] = svec2[j];\r
61   }\r
62 \r
63   // Merge appDataReferences require transfer of jar entries, perhaps with a\r
64   // renaming of the entry.\r
65   // Merge appDatas require eventually unique URNS\r
66   // TODO: merging global appdata from different documents where same app has\r
67   // written them causes conflict\r
68 \r
69   public static Hashtable hashOfAppDatas(Hashtable ht, Instance[] appdatas) {\r
70     if (ht == null)\r
71       ht = new Hashtable();\r
72     for (int i = 0, j = appdatas.length; i < j; i++) {\r
73       if (!ht.containsKey(appdatas[i].getUrn())) {\r
74         Hashtable aphash = new Hashtable();\r
75         ht.put(appdatas[i].getUrn(), aphash);\r
76         aphash.put(appdatas[i], appdatas[i].getDataReference());\r
77       } else {\r
78         // ensure urns and references are unique\r
79 \r
80       }\r
81 \r
82     }\r
83     return ht;\r
84   }\r
85 \r
86   /**\r
87    * safely copies an appData from one archive to another.\r
88    * \r
89    * @param darc\r
90    *          destination archive\r
91    * @param dest\r
92    *          destination document Vobject\r
93    * @param sarc\r
94    *          source archive reader\r
95    * @param entry\r
96    *          application data to be copied from source archive\r
97    */\r
98   public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest,\r
99       VamsasArchiveReader sarc, ApplicationData entry) {\r
100     // TODO: fix instances\r
101     // check uniqueness of instance's[] entry.urn amongst\r
102     // dest.ApplicationData[].getInstances[].urn\r
103     // check uniqueness of entry.user[].urn amongst\r
104     // dest.ApplicationData[].user[].urn\r
105     // check uniqueness of entry.user\r
106     // entry.getAppDataChoice().getData() or getDataReference is unique\r
107     ApplicationData newo = new ApplicationData();\r
108     for (int i = 0, j = dest.getApplicationDataCount(); i < j; i++) {\r
109       ApplicationData o = dest.getApplicationData()[i];\r
110       // ensure new urn is really unique\r
111       // String urn = entry.getUrn();\r
112       int v = 1;\r
113       // while (o.getUrn().equals(urn)) {\r
114       // urn = entry.getUrn()+v++;\r
115       // }\r
116       // uniqueness of urn\r
117       // check each user ApplicationData\r
118       // uniqueness (again)\r
119       // copy over valid objects\r
120       // \r
121     }\r
122   }\r
123 \r
124   /**\r
125    * Copy new datasets and appdatas from one vamsas document to another.\r
126    * \r
127    * @param darc\r
128    * @param dest\r
129    * @param sarc\r
130    * @param source\r
131    * @return true if merge was successful.\r
132    */\r
133   public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest,\r
134       VamsasArchiveReader sarc, VamsasDocument source) {\r
135     log.debug("mergeDocs entered.");\r
136     // search for appDatas in cdoc\r
137     VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount() + source.getVAMSASCount()];\r
138     mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());\r
139     dest.setVAMSAS(newr);\r
140     /**\r
141      * TODO: LATER: should verify that all ids really are unique in newly merged\r
142      * document. If not then what ? investigate possibility of having an id\r
143      * translation between appDatas and the core document - the mapping is\r
144      * stored when an external application performs a merge, but when the owning\r
145      * Application accesses the Vobject, the vorba_id is updated to the new one\r
146      * when it writes its references in to its appdata again\r
147      */\r
148     if (source.getApplicationDataCount() > 0) {\r
149       ApplicationData[] newdat = new ApplicationData[source\r
150           .getApplicationDataCount()\r
151           + dest.getApplicationDataCount()];\r
152       ApplicationData[] sappd = source.getApplicationData();\r
153       // check refs and update/modify if necessary\r
154       for (int i = 0; i < sappd.length; i++) {\r
155         addAppDataEntry(darc, dest, sarc, sappd[i]);\r
156       }\r
157 \r
158     }\r
159 \r
160     return true; // success\r
161   }\r
162 \r
163   private static CommandProcessor cproc;\r
164   static {\r
165     cproc.addCommand("new", 0, "no args");\r
166     cproc.addCommand("add", 1,\r
167         "Need another vamsas document archive filename as argument.");\r
168     cproc.addCommand("repair", 0, "no args");\r
169     cproc.addCommand("list", 0, "no args");\r
170     cproc.addCommand("monitor", 0, "no args");\r
171   }\r
172 \r
173   public static void main(String argv[]) {\r
174     /**\r
175      * TODO: switches for setting user identities for writing to vamsas document\r
176      */\r
177     if (argv.length < 1) {\r
178       log.fatal("Usage : <archive to create> [(commands)]");\r
179       return;\r
180     }\r
181     File newarch = new File(argv[0]);\r
182     int argpos = 0;\r
183     try {\r
184       // test fully fledged doc construction\r
185       VamsasArchive varc = new VamsasArchive(newarch, true);\r
186       VamsasDocument docroot;\r
187       docroot = new VamsasDocument();\r
188       docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter",\r
189           "user", "Created new Vamsas Document"));\r
190       while (++argpos < argv.length) {\r
191         File archive = new File(argv[argpos]);\r
192         InputStream istream;\r
193         if (archive.exists()) {\r
194           VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);\r
195           if (vdoc.isValid()) {\r
196             istream = vdoc.getVamsasDocumentStream();\r
197             if (istream != null) {\r
198               VamsasDocument cdocroot = VamsasDocument\r
199                   .unmarshal(new InputStreamReader(istream));\r
200               if (cdocroot != null)\r
201                 mergeDocs(varc, docroot, vdoc, cdocroot);\r
202             } else\r
203               log\r
204                   .warn("Unexpectedly null document stream from existing document "\r
205                       + archive);\r
206           } else {\r
207             // updating an oldformat stream ?\r
208             if ((istream = vdoc.getVamsasXmlStream()) != null) {\r
209               // make a new vamsas document from the vamsas.xml entry\r
210               VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO:\r
211                                                                               // verify\r
212                                                                               // only\r
213                                                                               // one\r
214                                                                               // VAMSAS\r
215                                                                               // element\r
216                                                                               // per\r
217                                                                               // vamsas.xml\r
218                                                                               // entry.\r
219               docroot.getProvenance().addEntry(\r
220                   ProvenanceStuff.newProvenanceEntry("ArchiveWriter", "user",\r
221                       "added vamsas.xml from " + argv[argpos - 1]));\r
222               docroot.addVAMSAS(root);\r
223             }\r
224           }\r
225         } else {\r
226           // Begin a new vamsas document\r
227           PrintWriter docwriter = varc.getDocumentOutputStream();\r
228         }\r
229       }\r
230     } catch (Exception e) {\r
231       log.error("Whilst manipulating " + argv[0], e);\r
232     }\r
233   }\r
234 }\r