090023922aa7aa57caab78085a9e7cc88280fe45
[vamsas.git] / src / org / vamsas / objects / utils / AppDataReference.java
1 /**
2  * 
3  */
4 package org.vamsas.objects.utils;
5 import java.util.Vector;
6
7 import org.vamsas.client.ClientHandle;
8 import org.vamsas.client.UserHandle;
9 import org.vamsas.objects.core.*;
10 /**
11  * Form, accessors and validation for ApplicationData references in
12  * vamsas document.
13  * TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
14  * TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
15  */
16 public class AppDataReference {
17   /**
18    * search interface for collecting particular types of AppDatas in a vamsas document
19    * @author jimp
20    *
21    */
22   interface IAppDSearch {
23     /**
24      * process the appData object d
25      * @param d
26      * @return true if appData should be collected
27      */
28     public boolean process(AppData d);
29   }
30   /**
31    * collect all appData reference strings in a vamsas document
32    * @param doc
33    * @return vector of String objects
34    */
35   static public Vector getAppDataReferences(VamsasDocument doc) {
36     if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
37       Vector apdrefs = new Vector();
38       ApplicationData[] appdatas = doc.getApplicationData();
39       for (int q=0; q<appdatas.length; q++) {
40         String refstring=appdatas[q].getDataReference();
41         if (refstring!=null) 
42           apdrefs.add(refstring);
43         User users[] = appdatas[q].getUser();
44         
45         if (users!=null)
46           for (int u=0; u<users.length; u++) {
47             refstring=users[u].getDataReference();
48             if (refstring!=null)
49               apdrefs.add(new String(refstring)); // avoid referencing.
50           }
51       }
52       if (apdrefs.size()>0)
53         return apdrefs;
54     }
55     return null;
56   }
57   /**
58    * General search through the set of AppData objects for a particular profile of Client and User handle.
59    * @param doc
60    * @param test interface implemented by the filter selecting particular AppDatas.
61    * @param cascade if true only User objects for ApplicationData objects that test.process returned true will be tested.
62    * @return set of org.vamsas.objects.core.AppData objects for which test.process returned true
63    */
64   static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test, boolean cascade) {
65     if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
66       Vector apdrefs = new Vector();
67       ApplicationData[] appdatas = doc.getApplicationData();
68       for (int q=0; q<appdatas.length; q++) {
69         boolean t;
70         if (t=test.process(appdatas[q])) 
71           apdrefs.add(appdatas[q]);
72         if (t || cascade) { 
73           User users[] = appdatas[q].getUser();
74           if (users!=null)
75             for (int u=0; u<users.length; u++) 
76               if (test.process(users[u]))
77                 apdrefs.add(users[u]);
78         }
79       }
80       if (apdrefs.size()>0)
81         return apdrefs;
82     }
83     return null;
84   }
85   static public boolean equals(User p, UserHandle u) {
86     if (p.getFullname().equals(u.getFullName())
87         && p.getOrganization().equals(u.getOrganization()))
88       return true;
89     return false;
90   }
91   /**
92    * 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)
93    * @param p
94    * @param c
95    * @return match of p on template c.
96    */
97   static public boolean equals(ApplicationData p, ClientHandle c) {
98     if (
99         ((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
100         &&
101         (p.getName().equals(c.getClientName()))
102         &&
103         ((c.getVersion()==null) || (p.getVersion().equals(c.getVersion())))
104         )
105       return true;
106     return false;
107   }
108   /**
109    * Searches document appData structure for particular combinations of client and user data
110    * @param doc the data 
111    * @param user template user data to match against
112    * @see AppDataReference.equals(org.vamsas.objects.core.User, org.vamsas.client.UserHandle)
113    * @param app
114    * @see AppDataReference.equals(org.vamsas.objects.core.ApplicationData, org.vamsas.client.ClientHandle)
115    * @return set of matching client app datas for this client and user combination
116    */
117   static public Vector getUserandApplicationsData(VamsasDocument doc, UserHandle user, ClientHandle app) {
118     if (doc==null) {
119       return null;
120     }
121     final UserHandle u = user;
122     final ClientHandle c = app;
123     
124     IAppDSearch match = new IAppDSearch() {
125       public boolean process(AppData p) {
126         if (p instanceof User) {
127           if (AppDataReference.equals((User) p, u))
128             return true;
129         } else 
130         if (p instanceof ApplicationData) {
131           if (AppDataReference.equals((ApplicationData) p, c))
132             return true;
133         }
134         return false;
135       }
136     };
137     
138     return searchAppDatas(doc, match, true); // only return AppDatas belonging to appdata app.
139   }
140 }