package org.vamsas.objects.utils;
import java.util.Vector;
+import org.vamsas.client.ClientHandle;
+import org.vamsas.client.UserHandle;
import org.vamsas.objects.core.*;
/**
* Form, accessors and validation for ApplicationData references in
* vamsas document.
* TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
- * TODO: implement methods for searching appData structure for particular combinations of client and user data
* TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
*/
public class AppDataReference {
+ /**
+ * search interface for collecting particular types of AppDatas in a vamsas document
+ * @author jimp
+ *
+ */
+ interface IAppDSearch {
+ /**
+ * process the appData object d
+ * @param d
+ * @return true if appData should be collected
+ */
+ public boolean process(AppData d);
+ }
+ /**
+ * collect all appData reference strings in a vamsas document
+ * @param doc
+ * @return vector of String objects
+ */
static public Vector getAppDataReferences(VamsasDocument doc) {
if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
Vector apdrefs = new Vector();
User users[] = appdatas[q].getUser();
if (users!=null)
- for (int u=0; u<users.length; u++)
+ for (int u=0; u<users.length; u++) {
refstring=users[u].getDataReference();
if (refstring!=null)
apdrefs.add(new String(refstring)); // avoid referencing.
+ }
+ }
+ if (apdrefs.size()>0)
+ return apdrefs;
+ }
+ return null;
+ }
+ /**
+ * General search through the set of AppData objects for a particular profile of Client and User handle.
+ * @param doc
+ * @param test interface implemented by the filter selecting particular AppDatas.
+ * @param cascade if true only User objects for ApplicationData objects that test.process returned true will be tested.
+ * @return set of org.vamsas.objects.core.AppData objects for which test.process returned true
+ */
+ static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test, boolean cascade) {
+ if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
+ Vector apdrefs = new Vector();
+ ApplicationData[] appdatas = doc.getApplicationData();
+ for (int q=0; q<appdatas.length; q++) {
+ boolean t;
+ if (t=test.process(appdatas[q]))
+ apdrefs.add(appdatas[q]);
+ if (t || cascade) {
+ User users[] = appdatas[q].getUser();
+ if (users!=null)
+ for (int u=0; u<users.length; u++)
+ if (test.process(users[u]))
+ apdrefs.add(users[u]);
+ }
}
if (apdrefs.size()>0)
return apdrefs;
}
return null;
}
+ static public boolean equals(User p, UserHandle u) {
+ if (p.getFullname().equals(u.getFullName())
+ && p.getOrganization().equals(u.getOrganization()))
+ return true;
+ return false;
+ }
+ /**
+ * 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)
+ * @param p
+ * @param c
+ * @return match of p on template c.
+ */
+ static public boolean equals(ApplicationData p, ClientHandle c) {
+ if (
+ ((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
+ &&
+ (p.getName().equals(c.getClientName()))
+ &&
+ ((c.getVersion()==null) || (p.getVersion().equals(c.getVersion())))
+ )
+ return true;
+ return false;
+ }
+ /**
+ * Searches document appData structure for particular combinations of client and user data
+ * @param doc the data
+ * @param user template user data to match against
+ * @see AppDataReference.equals(org.vamsas.objects.core.User, org.vamsas.client.UserHandle)
+ * @param app
+ * @see AppDataReference.equals(org.vamsas.objects.core.ApplicationData, org.vamsas.client.ClientHandle)
+ * @return set of matching client app datas for this client and user combination
+ */
+ static public Vector getUserandApplicationsData(VamsasDocument doc, UserHandle user, ClientHandle app) {
+ if (doc==null) {
+ return null;
+ }
+ final UserHandle u = user;
+ final ClientHandle c = app;
+
+ IAppDSearch match = new IAppDSearch() {
+ public boolean process(AppData p) {
+ if (p instanceof User) {
+ if (AppDataReference.equals((User) p, u))
+ return true;
+ } else
+ if (p instanceof ApplicationData) {
+ if (AppDataReference.equals((ApplicationData) p, c))
+ return true;
+ }
+ return false;
+ }
+ };
+
+ return searchAppDatas(doc, match, true); // only return AppDatas belonging to appdata app.
+ }
}