4 package uk.ac.vamsas.objects.utils;
5 import java.util.Vector;
8 import uk.ac.vamsas.client.ClientHandle;
9 import uk.ac.vamsas.client.UserHandle;
10 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
11 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
12 import uk.ac.vamsas.objects.core.*;
14 * Form, accessors and validation for ApplicationData references in
16 * TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
17 * TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
19 public class AppDataReference {
21 * search interface for collecting particular types of AppDatas in a vamsas document
25 interface IAppDSearch {
27 * process the appData Vobject d
29 * @return true if appData should be collected
31 public boolean process(AppData d);
34 * collect all appData reference strings in a vamsas document
36 * @return vector of String objects
38 static public Vector getAppDataReferences(VamsasDocument doc) {
39 if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
40 Vector apdrefs = new Vector();
41 ApplicationData[] appdatas = doc.getApplicationData();
42 for (int q=0; q<appdatas.length; q++) {
43 String refstring=appdatas[q].getDataReference();
45 apdrefs.add(refstring);
46 User users[] = appdatas[q].getUser();
49 for (int u=0; u<users.length; u++) {
50 refstring=users[u].getDataReference();
52 apdrefs.add(new String(refstring)); // avoid referencing.
61 * General search through the set of AppData objects for a particular profile of Client and User handle.
63 * @param test interface implemented by the filter selecting particular AppDatas.
64 * @param cascade if true only User objects for ApplicationData objects that test.process returned true will be tested.
65 * @return set of uk.ac.vamsas.objects.core.AppData objects for which test.process returned true
67 static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test, boolean cascade) {
68 if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
69 Vector apdrefs = new Vector();
70 ApplicationData[] appdatas = doc.getApplicationData();
71 for (int q=0; q<appdatas.length; q++) {
73 if (t=test.process(appdatas[q]))
74 apdrefs.add(appdatas[q]);
76 User users[] = appdatas[q].getUser();
78 for (int u=0; u<users.length; u++)
79 if (test.process(users[u]))
80 apdrefs.add(users[u]);
88 static public boolean equals(User p, UserHandle u) {
89 if (p.getFullname().equals(u.getFullName())
90 && p.getOrganization().equals(u.getOrganization()))
95 * returns true if Name matches in c and p, and Urn's match (or c.getUrn()==null) and Version's match (or c.getVersion()==null)
98 * @return match of p on template c.
100 static public boolean equals(ApplicationData p, ClientHandle c) {
102 //((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
104 (p.getName().equals(c.getClientName()))
106 ((c.getVersion()==null) || (p.getVersion().equals(c.getVersion())))
112 * Searches document appData structure for particular combinations of client and user data
113 * @param doc the data
114 * @param user template user data to match against
115 * @see AppDataReference.equals(uk.ac.vamsas.objects.core.User, uk.ac.vamsas.client.UserHandle)
117 * @see AppDataReference.equals(uk.ac.vamsas.objects.core.ApplicationData, uk.ac.vamsas.client.ClientHandle)
118 * @return set of matching client app datas for this client and user combination
120 static public Vector getUserandApplicationsData(VamsasDocument doc, UserHandle user, ClientHandle app) {
124 final UserHandle u = user;
125 final ClientHandle c = app;
127 IAppDSearch match = new IAppDSearch() {
128 public boolean process(AppData p) {
129 if (p instanceof User) {
130 if (AppDataReference.equals((User) p, u))
133 if (p instanceof ApplicationData) {
134 if (AppDataReference.equals((ApplicationData) p, c))
141 return searchAppDatas(doc, match, true); // only return AppDatas belonging to appdata app.
144 * safely creates a new appData reference
145 * @param dest destination document Vobject
146 * @param entry base application reference to make unique
148 public static String uniqueAppDataReference(VamsasDocument dest,String base) {
149 String urn = new String(base);
151 for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
152 ApplicationData o = dest.getApplicationData()[i];
153 // ensure new urn is really unique
155 // TODO: while (o.getUrn().equals(urn)) {