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
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\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
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
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
22 package uk.ac.vamsas.objects.utils;
\r
24 import java.util.Vector;
\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
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
38 public class AppDataReference {
\r
40 * search interface for collecting particular types of AppDatas in a vamsas
\r
46 interface IAppDSearch {
\r
48 * process the appData Vobject d
\r
51 * @return true if appData should be collected
\r
53 public boolean process(AppData d);
\r
57 * collect all appData reference strings in a vamsas document
\r
60 * @return vector of String objects
\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
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
79 if (apdrefs.size() > 0)
\r
86 * General search through the set of AppData objects for a particular profile
\r
87 * of Client and User handle.
\r
91 * interface implemented by the filter selecting particular AppDatas.
\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
98 static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test,
\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
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
110 for (int u = 0; u < users.length; u++)
\r
111 if (test.process(users[u]))
\r
112 apdrefs.add(users[u]);
\r
115 if (apdrefs.size() > 0)
\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
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
134 * @return match of p on template c.
\r
136 static public boolean equals(ApplicationData p, ClientHandle c) {
\r
138 // ((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
\r
140 (p.getName().equals(c.getClientName()))
\r
141 && ((c.getVersion() == null) || (p.getVersion().equals(c.getVersion()))))
\r
147 * Searches document appData structure for particular combinations of client
\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
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
162 static public Vector getUserandApplicationsData(VamsasDocument doc,
\r
163 UserHandle user, ClientHandle app) {
\r
167 final UserHandle u = user;
\r
168 final ClientHandle c = app;
\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
175 } else if (p instanceof ApplicationData) {
\r
176 if (AppDataReference.equals((ApplicationData) p, c))
\r
183 return searchAppDatas(doc, match, true); // only return AppDatas belonging
\r
188 * safely creates a new appData reference
\r
191 * destination document Vobject
\r
193 * base application reference to make unique
\r
195 public static String uniqueAppDataReference(VamsasDocument dest, String base) {
\r
196 String urn = base.replace('/', '_').replace('\\', '_').replace(':', '_')
\r
197 .replace('.', '_');
\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