applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / AppDataReference.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.objects.utils;\r
23 \r
24 import java.util.Vector;\r
25 \r
26 import uk.ac.vamsas.client.ClientHandle;\r
27 import uk.ac.vamsas.client.UserHandle;\r
28 import uk.ac.vamsas.client.simpleclient.VamsasArchive;\r
29 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;\r
30 import uk.ac.vamsas.objects.core.*;\r
31 \r
32 /**\r
33  * Form, accessors and validation for ApplicationData references in vamsas\r
34  * document. TODO: LATER:extend XML Schema to properly validate against the same\r
35  * forms required by this class TODO: VAMSAS: URNS for appDatas are supposed to\r
36  * be unique, aren't they ?\r
37  */\r
38 public class AppDataReference {\r
39   /**\r
40    * search interface for collecting particular types of AppDatas in a vamsas\r
41    * document\r
42    * \r
43    * @author jimp\r
44    * \r
45    */\r
46   interface IAppDSearch {\r
47     /**\r
48      * process the appData Vobject d\r
49      * \r
50      * @param d\r
51      * @return true if appData should be collected\r
52      */\r
53     public boolean process(AppData d);\r
54   }\r
55 \r
56   /**\r
57    * collect all appData reference strings in a vamsas document\r
58    * \r
59    * @param doc\r
60    * @return vector of String objects\r
61    */\r
62   static public Vector getAppDataReferences(VamsasDocument doc) {\r
63     if ((doc != null) && (doc.getApplicationDataCount() > 0)) {\r
64       Vector apdrefs = new Vector();\r
65       ApplicationData[] appdatas = doc.getApplicationData();\r
66       for (int q = 0; q < appdatas.length; q++) {\r
67         String refstring = appdatas[q].getDataReference();\r
68         if (refstring != null)\r
69           apdrefs.add(refstring);\r
70         User users[] = appdatas[q].getUser();\r
71 \r
72         if (users != null)\r
73           for (int u = 0; u < users.length; u++) {\r
74             refstring = users[u].getDataReference();\r
75             if (refstring != null)\r
76               apdrefs.add(new String(refstring)); // avoid referencing.\r
77           }\r
78       }\r
79       if (apdrefs.size() > 0)\r
80         return apdrefs;\r
81     }\r
82     return null;\r
83   }\r
84 \r
85   /**\r
86    * General search through the set of AppData objects for a particular profile\r
87    * of Client and User handle.\r
88    * \r
89    * @param doc\r
90    * @param test\r
91    *          interface implemented by the filter selecting particular AppDatas.\r
92    * @param cascade\r
93    *          if true only User objects for ApplicationData objects that\r
94    *          test.process returned true will be tested.\r
95    * @return set of uk.ac.vamsas.objects.core.AppData objects for which\r
96    *         test.process returned true\r
97    */\r
98   static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test,\r
99       boolean cascade) {\r
100     if ((doc != null) && (doc.getApplicationDataCount() > 0)) {\r
101       Vector apdrefs = new Vector();\r
102       ApplicationData[] appdatas = doc.getApplicationData();\r
103       for (int q = 0; q < appdatas.length; q++) {\r
104         boolean t;\r
105         if (t = test.process(appdatas[q]))\r
106           apdrefs.add(appdatas[q]);\r
107         if (t || cascade) {\r
108           User users[] = appdatas[q].getUser();\r
109           if (users != null)\r
110             for (int u = 0; u < users.length; u++)\r
111               if (test.process(users[u]))\r
112                 apdrefs.add(users[u]);\r
113         }\r
114       }\r
115       if (apdrefs.size() > 0)\r
116         return apdrefs;\r
117     }\r
118     return null;\r
119   }\r
120 \r
121   static public boolean equals(User p, UserHandle u) {\r
122     if (p.getFullname().equals(u.getFullName())\r
123         && p.getOrganization().equals(u.getOrganization()))\r
124       return true;\r
125     return false;\r
126   }\r
127 \r
128   /**\r
129    * returns true if Name matches in c and p, and Urn's match (or\r
130    * c.getUrn()==null) and Version's match (or c.getVersion()==null)\r
131    * \r
132    * @param p\r
133    * @param c\r
134    * @return match of p on template c.\r
135    */\r
136   static public boolean equals(ApplicationData p, ClientHandle c) {\r
137     if (\r
138     // ((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))\r
139     // &&\r
140     (p.getName().equals(c.getClientName()))\r
141         && ((c.getVersion() == null) || (p.getVersion().equals(c.getVersion()))))\r
142       return true;\r
143     return false;\r
144   }\r
145 \r
146   /**\r
147    * Searches document appData structure for particular combinations of client\r
148    * and user data\r
149    * \r
150    * @param doc\r
151    *          the data\r
152    * @param user\r
153    *          template user data to match against\r
154    * @see AppDataReference.equals(uk.ac.vamsas.objects.core.User,\r
155    *      uk.ac.vamsas.client.UserHandle)\r
156    * @param app\r
157    * @see AppDataReference.equals(uk.ac.vamsas.objects.core.ApplicationData,\r
158    *      uk.ac.vamsas.client.ClientHandle)\r
159    * @return set of matching client app datas for this client and user\r
160    *         combination\r
161    */\r
162   static public Vector getUserandApplicationsData(VamsasDocument doc,\r
163       UserHandle user, ClientHandle app) {\r
164     if (doc == null) {\r
165       return null;\r
166     }\r
167     final UserHandle u = user;\r
168     final ClientHandle c = app;\r
169 \r
170     IAppDSearch match = new IAppDSearch() {\r
171       public boolean process(AppData p) {\r
172         if (p instanceof User) {\r
173           if (AppDataReference.equals((User) p, u))\r
174             return true;\r
175         } else if (p instanceof ApplicationData) {\r
176           if (AppDataReference.equals((ApplicationData) p, c))\r
177             return true;\r
178         }\r
179         return false;\r
180       }\r
181     };\r
182 \r
183     return searchAppDatas(doc, match, true); // only return AppDatas belonging\r
184                                              // to appdata app.\r
185   }\r
186 \r
187   /**\r
188    * safely creates a new appData reference\r
189    * \r
190    * @param dest\r
191    *          destination document Vobject\r
192    * @param entry\r
193    *          base application reference to make unique\r
194    */\r
195   public static String uniqueAppDataReference(VamsasDocument dest, String base) {\r
196     String urn = base.replace('/', '_').replace('\\', '_').replace(':', '_')\r
197         .replace('.', '_');\r
198     int v = 1;\r
199     for (int i = 0, j = dest.getApplicationDataCount(); i < j; i++) {\r
200       ApplicationData o = dest.getApplicationData()[i];\r
201       // ensure new urn is really unique\r
202       while (o.getDataReference() != null && o.getDataReference().equals(urn)) {\r
203         urn = base + "_" + v++;\r
204       }\r
205     }\r
206     return urn;\r
207   }\r
208 }\r