/icat3-ws-test/src/main/java/Session.java
Java | 449 lines | 410 code | 39 blank | 0 comment | 28 complexity | 7d98cbae5c943006a76064a027fa0b42 MD5 | raw file
Possible License(s): GPL-2.0
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import javax.xml.datatype.XMLGregorianCalendar;
- import javax.xml.namespace.QName;
- import org.icatproject.Application;
- import org.icatproject.BadParameterException_Exception;
- import org.icatproject.Datafile;
- import org.icatproject.DatafileFormat;
- import org.icatproject.Dataset;
- import org.icatproject.DatasetParameter;
- import org.icatproject.DatasetType;
- import org.icatproject.DestType;
- import org.icatproject.EntityBaseBean;
- import org.icatproject.EntityInfo;
- import org.icatproject.Facility;
- import org.icatproject.Group;
- import org.icatproject.ICAT;
- import org.icatproject.ICATService;
- import org.icatproject.IcatInternalException_Exception;
- import org.icatproject.InputDatafile;
- import org.icatproject.InputDataset;
- import org.icatproject.InsufficientPrivilegesException_Exception;
- import org.icatproject.Investigation;
- import org.icatproject.InvestigationType;
- import org.icatproject.Job;
- import org.icatproject.NoSuchObjectFoundException_Exception;
- import org.icatproject.NotificationRequest;
- import org.icatproject.ObjectAlreadyExistsException_Exception;
- import org.icatproject.OutputDatafile;
- import org.icatproject.OutputDataset;
- import org.icatproject.ParameterType;
- import org.icatproject.ParameterValueType;
- import org.icatproject.Rule;
- import org.icatproject.SessionException_Exception;
- import org.icatproject.User;
- import org.icatproject.UserGroup;
- import org.icatproject.ValidationException_Exception;
- class Session {
- public enum ParameterApplicability {
- DATASET, DATAFILE, SAMPLE, INVESTIGATION
- };
- public String dump(EntityBaseBean bean, int i) throws IllegalArgumentException,
- IllegalAccessException, SecurityException, NoSuchMethodException,
- InvocationTargetException {
- StringBuilder result = new StringBuilder();
- for (Field field : new ArrayList<Field>(Arrays.asList(bean.getClass().getDeclaredFields()))) {
- String name = field.getName();
- String getterName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
- Method m = bean.getClass().getMethod(getterName);
- Object o = m.invoke(bean);
- if (result.length() != 0) {
- result.append(", ");
- }
- if (o instanceof EntityBaseBean) {
- if (i > 0) {
- result.append(name + ":[" + dump((EntityBaseBean) o, i - 1) + "]");
- } else {
- result.append(name + ":...");
- }
- } else if (o instanceof List) {
- result.append(name + ":" + ((List<?>) o).size());
- } else {
- result.append(name + ": '" + o + "'");
- }
- }
- return result.toString();
- }
- private final ICAT icatEP;
- private final String sessionId;
- public Session() throws MalformedURLException, SessionException_Exception,
- IcatInternalException_Exception {
- final String urlString = "http://localhost:8080";
- final URL icatUrl = new URL(urlString + "/ICATService/ICAT?wsdl");
- final ICATService icatService = new ICATService(icatUrl, new QName(
- "http://icatproject.org", "ICATService"));
- this.icatEP = icatService.getICATPort();
- this.sessionId = this.icatEP.login("root", "password");
- }
- public void addInputDatafile(Job job, Datafile df) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- final InputDatafile idf = new InputDatafile();
- idf.setDatafile(df);
- idf.setJob(job);
- this.icatEP.create(this.sessionId, idf);
- }
- public void addInputDataset(Job job, Dataset ds) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- final InputDataset ids = new InputDataset();
- ids.setDataset(ds);
- ids.setJob(job);
- this.icatEP.create(this.sessionId, ids);
- }
- public void addOutputDatafile(Job job, Datafile df) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- final OutputDatafile odf = new OutputDatafile();
- odf.setDatafile(df);
- odf.setJob(job);
- this.icatEP.create(this.sessionId, odf);
- }
- public void addOutputDataset(Job job, Dataset ds) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- final OutputDataset ods = new OutputDataset();
- ods.setDataset(ds);
- ods.setJob(job);
- this.icatEP.create(this.sessionId, ods);
- }
- public void addRule(String groupName, String what, String crudFlags) throws Exception {
- Rule rule = new Rule();
- if (groupName != null) {
- Group g = (Group) this.icatEP.get(this.sessionId, "Group", groupName);
- rule.setGroup(g);
- }
- rule.setWhat(what);
- rule.setCrudFlags(crudFlags);
- this.icatEP.create(this.sessionId, rule);
- }
- public void addUserGroupMember(String groupName, String userName) throws Exception {
- Group group = null;
- if (groupName != null) {
- try {
- group = (Group) this.icatEP.get(this.sessionId, "Group", groupName);
- } catch (NoSuchObjectFoundException_Exception e) {
- group = new Group();
- group.setName(groupName);
- this.icatEP.create(sessionId, group);
- }
- }
- User user = null;
- try {
- user = (User) this.icatEP.get(this.sessionId, "User", userName);
- } catch (NoSuchObjectFoundException_Exception e) {
- user = new User();
- user.setName(userName);
- this.icatEP.create(sessionId, user);
- }
- UserGroup userGroup = new UserGroup();
- userGroup.setUser(user);
- userGroup.setGroup(group);
- this.icatEP.create(sessionId, userGroup);
- }
- public void clear() throws Exception {
- List<Object> lo = this.search("Job");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((Job) o);
- }
- lo = this.search("Application");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((Application) o);
- }
- lo = this.search("Investigation");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((Investigation) o);
- }
- lo = this.search("DatasetType");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((DatasetType) o);
- }
- lo = this.search("DatafileFormat");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((DatafileFormat) o);
- }
- lo = this.search("InvestigationType");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((InvestigationType) o);
- }
- lo = this.search("ParameterType");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((ParameterType) o);
- }
- lo = this.search("Facility");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((Facility) o);
- }
- lo = this.search("NotificationRequest");
- for (final Object o : lo) {
- System.out.println("Deleting " + o);
- this.delete((NotificationRequest) o);
- }
- }
- public void clearAuthz() throws Exception {
- List<Object> lo1 = this.search("Rule");
- List<Object> lo2 = this.search("UserGroup");
- List<Object> lo3 = this.search("User");
- List<Object> lo4 = this.search("Group");
- for (final Object o : lo1) {
- System.out.println("Deleting " + o);
- this.delete((Rule) o);
- }
- for (final Object o : lo2) {
- System.out.println("Deleting " + o);
- this.delete((UserGroup) o);
- }
- for (final Object o : lo3) {
- System.out.println("Deleting " + o);
- this.delete((User) o);
- }
- for (final Object o : lo4) {
- System.out.println("Deleting " + o);
- this.delete((Group) o);
- }
- }
- public Application createApplication(String name, String version)
- throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
- SessionException_Exception, ValidationException_Exception {
- final Application application = new Application();
- application.setName(name);
- application.setVersion(version);
- application.setId((Long) this.icatEP.create(this.sessionId, application));
- return application;
- }
- public Datafile createDatafile(String name, DatafileFormat format, Dataset ds)
- throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
- SessionException_Exception, ValidationException_Exception {
- final Datafile datafile = new Datafile();
- datafile.setDatafileFormat(format);
- datafile.setName(name);
- datafile.setDataset(ds);
- datafile.setId((Long) this.icatEP.create(this.sessionId, datafile));
- return datafile;
- }
- public DatafileFormat createDatafileFormat(Facility facility, String name, String formatType)
- throws Exception {
- final DatafileFormat dff = new DatafileFormat();
- dff.setName(name);
- dff.setVersion("1");
- dff.setType(formatType);
- dff.setFacility(facility);
- dff.setId((Long) this.icatEP.create(this.sessionId, dff));
- return dff;
- }
- public Dataset createDataset(String name, DatasetType type, Investigation inv)
- throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
- SessionException_Exception, ValidationException_Exception {
- final Dataset dataset = new Dataset();
- dataset.setName(name);
- dataset.setType(type);
- dataset.setInvestigation(inv);
- dataset.setId((Long) this.icatEP.create(this.sessionId, dataset));
- return dataset;
- }
- public DatasetParameter createDatasetParameter(Object value, ParameterType p, Dataset ds)
- throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
- SessionException_Exception, ValidationException_Exception {
- final DatasetParameter dsp = new DatasetParameter();
- if (p.getValueType() == ParameterValueType.DATE_AND_TIME) {
- dsp.setDateTimeValue((XMLGregorianCalendar) value);
- }
- dsp.setType(p);
- dsp.setDataset(ds);
- this.icatEP.create(this.sessionId, dsp);
- return dsp;
- }
- public DatasetType createDatasetType(Facility facility, String name) throws Exception {
- final DatasetType dst = new DatasetType();
- dst.setName(name);
- dst.setFacility(facility);
- dst.setId((Long) this.icatEP.create(this.sessionId, dst));
- return dst;
- }
- public Facility createFacility(String shortName, int daysUntilRelease) throws Exception {
- final Facility f = new Facility();
- f.setName(shortName);
- f.setDaysUntilRelease(daysUntilRelease);
- this.icatEP.create(this.sessionId, f);
- return f;
- }
- public Investigation createInvestigation(Facility facility, String invNumber, String title,
- InvestigationType invType) throws Exception {
- final Investigation i = new Investigation();
- i.setFacility(facility);
- i.setName(invNumber);
- i.setTitle(title);
- i.setType(invType);
- i.setId((Long) this.icatEP.create(this.sessionId, i));
- return i;
- }
- public InvestigationType createInvestigationType(Facility facility, String name)
- throws Exception {
- final InvestigationType type = new InvestigationType();
- type.setFacility(facility);
- type.setName(name);
- type.setId((Long) this.icatEP.create(this.sessionId, type));
- return type;
- }
- public Job createJob(Application application) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- final Job job = new Job();
- job.setApplication(application);
- job.setId((Long) this.icatEP.create(this.sessionId, job));
- return job;
- }
- public void delete(EntityBaseBean bean) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- this.icatEP.delete(this.sessionId, bean);
- }
- public EntityBaseBean get(String query, Object key) throws BadParameterException_Exception,
- IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, SessionException_Exception {
- return this.icatEP.get(this.sessionId, query, key);
- }
- public List<Object> search(String query) throws BadParameterException_Exception,
- IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- SessionException_Exception {
- return this.icatEP.search(this.sessionId, query);
- }
- public void setAuthz() throws Exception {
- try {
- this.addUserGroupMember("root", "root");
- } catch (ObjectAlreadyExistsException_Exception e) {
- System.out.println("root is already a member of root - carry on");
- }
- this.addRule("root", "Rule", "CRUD");
- this.addRule("root", "User", "CRUD");
- this.addRule("root", "Group", "CRUD");
- this.addRule("root", "UserGroup", "CRUD");
- this.addRule("root", "DatafileFormat", "CRUD");
- this.addRule("root", "DatasetType", "CRUD");
- this.addRule("root", "Facility", "CRUD");
- this.addRule("root", "Investigation", "CRUD");
- this.addRule("root", "InvestigationType", "CRUD");
- this.addRule("root", "ParameterType", "CRUD");
- this.addRule("root", "Investigation", "CRUD");
- this.addRule("root", "Dataset", "CRUD");
- this.addRule("root", "ParameterType", "CRUD");
- this.addRule("root", "DatasetParameter", "CRUD");
- this.addRule("root", "Datafile", "CRUD");
- this.addRule("root", "DatafileFormat", "CRUD");
- this.addRule("root", "DatasetType", "CRUD");
- this.addRule("root", "Application", "CRUD");
- this.addRule("root", "Job", "CRUD");
- this.addRule("root", "InputDataset", "CRUD");
- this.addRule("root", "OutputDataset", "CRUD");
- this.addRule("root", "InputDatafile", "CRUD");
- this.addRule("root", "OutputDatafile", "CRUD");
- this.addRule("root", "NotificationRequest", "CRUD");
- this.addRule("root", "InvestigationParameter", "CRUD");
- }
- public void update(EntityBaseBean df) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- SessionException_Exception, ValidationException_Exception {
- this.icatEP.update(this.sessionId, df);
- }
- public NotificationRequest createNotificationRequest(String name, DestType destType,
- String what, String crudFlags, String jmsOptions, String dataTypes)
- throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
- NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
- SessionException_Exception, ValidationException_Exception {
- NotificationRequest notificationRequest = new NotificationRequest();
- notificationRequest.setName(name);
- notificationRequest.setDestType(destType);
- notificationRequest.setWhat(what);
- notificationRequest.setCrudFlags(crudFlags);
- notificationRequest.setJmsOptions(jmsOptions);
- notificationRequest.setDatatypes(dataTypes);
- icatEP.create(this.sessionId, notificationRequest);
- return notificationRequest;
- }
- public void registerInvestigation(Investigation inv) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- inv.setId((Long) this.icatEP.create(this.sessionId, inv));
- }
- public EntityInfo getEntityInfo(String beanName) throws BadParameterException_Exception,
- IcatInternalException_Exception {
- return icatEP.getEntityInfo(beanName);
- }
- public Object create(EntityBaseBean bean) throws IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception,
- ValidationException_Exception {
- return this.icatEP.create(this.sessionId, bean);
- }
- public List<Object> createMany(List<EntityBaseBean> beans)
- throws ValidationException_Exception, IcatInternalException_Exception,
- InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
- ObjectAlreadyExistsException_Exception, SessionException_Exception {
- return this.icatEP.createMany(this.sessionId, beans);
- }
- }