PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/icat3-ws-test/src/main/java/Session.java

http://icatproject.googlecode.com/
Java | 449 lines | 410 code | 39 blank | 0 comment | 28 complexity | 7d98cbae5c943006a76064a027fa0b42 MD5 | raw file
Possible License(s): GPL-2.0
  1. import java.lang.reflect.Field;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import javax.xml.datatype.XMLGregorianCalendar;
  10. import javax.xml.namespace.QName;
  11. import org.icatproject.Application;
  12. import org.icatproject.BadParameterException_Exception;
  13. import org.icatproject.Datafile;
  14. import org.icatproject.DatafileFormat;
  15. import org.icatproject.Dataset;
  16. import org.icatproject.DatasetParameter;
  17. import org.icatproject.DatasetType;
  18. import org.icatproject.DestType;
  19. import org.icatproject.EntityBaseBean;
  20. import org.icatproject.EntityInfo;
  21. import org.icatproject.Facility;
  22. import org.icatproject.Group;
  23. import org.icatproject.ICAT;
  24. import org.icatproject.ICATService;
  25. import org.icatproject.IcatInternalException_Exception;
  26. import org.icatproject.InputDatafile;
  27. import org.icatproject.InputDataset;
  28. import org.icatproject.InsufficientPrivilegesException_Exception;
  29. import org.icatproject.Investigation;
  30. import org.icatproject.InvestigationType;
  31. import org.icatproject.Job;
  32. import org.icatproject.NoSuchObjectFoundException_Exception;
  33. import org.icatproject.NotificationRequest;
  34. import org.icatproject.ObjectAlreadyExistsException_Exception;
  35. import org.icatproject.OutputDatafile;
  36. import org.icatproject.OutputDataset;
  37. import org.icatproject.ParameterType;
  38. import org.icatproject.ParameterValueType;
  39. import org.icatproject.Rule;
  40. import org.icatproject.SessionException_Exception;
  41. import org.icatproject.User;
  42. import org.icatproject.UserGroup;
  43. import org.icatproject.ValidationException_Exception;
  44. class Session {
  45. public enum ParameterApplicability {
  46. DATASET, DATAFILE, SAMPLE, INVESTIGATION
  47. };
  48. public String dump(EntityBaseBean bean, int i) throws IllegalArgumentException,
  49. IllegalAccessException, SecurityException, NoSuchMethodException,
  50. InvocationTargetException {
  51. StringBuilder result = new StringBuilder();
  52. for (Field field : new ArrayList<Field>(Arrays.asList(bean.getClass().getDeclaredFields()))) {
  53. String name = field.getName();
  54. String getterName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
  55. Method m = bean.getClass().getMethod(getterName);
  56. Object o = m.invoke(bean);
  57. if (result.length() != 0) {
  58. result.append(", ");
  59. }
  60. if (o instanceof EntityBaseBean) {
  61. if (i > 0) {
  62. result.append(name + ":[" + dump((EntityBaseBean) o, i - 1) + "]");
  63. } else {
  64. result.append(name + ":...");
  65. }
  66. } else if (o instanceof List) {
  67. result.append(name + ":" + ((List<?>) o).size());
  68. } else {
  69. result.append(name + ": '" + o + "'");
  70. }
  71. }
  72. return result.toString();
  73. }
  74. private final ICAT icatEP;
  75. private final String sessionId;
  76. public Session() throws MalformedURLException, SessionException_Exception,
  77. IcatInternalException_Exception {
  78. final String urlString = "http://localhost:8080";
  79. final URL icatUrl = new URL(urlString + "/ICATService/ICAT?wsdl");
  80. final ICATService icatService = new ICATService(icatUrl, new QName(
  81. "http://icatproject.org", "ICATService"));
  82. this.icatEP = icatService.getICATPort();
  83. this.sessionId = this.icatEP.login("root", "password");
  84. }
  85. public void addInputDatafile(Job job, Datafile df) throws IcatInternalException_Exception,
  86. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  87. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  88. ValidationException_Exception {
  89. final InputDatafile idf = new InputDatafile();
  90. idf.setDatafile(df);
  91. idf.setJob(job);
  92. this.icatEP.create(this.sessionId, idf);
  93. }
  94. public void addInputDataset(Job job, Dataset ds) throws IcatInternalException_Exception,
  95. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  96. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  97. ValidationException_Exception {
  98. final InputDataset ids = new InputDataset();
  99. ids.setDataset(ds);
  100. ids.setJob(job);
  101. this.icatEP.create(this.sessionId, ids);
  102. }
  103. public void addOutputDatafile(Job job, Datafile df) throws IcatInternalException_Exception,
  104. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  105. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  106. ValidationException_Exception {
  107. final OutputDatafile odf = new OutputDatafile();
  108. odf.setDatafile(df);
  109. odf.setJob(job);
  110. this.icatEP.create(this.sessionId, odf);
  111. }
  112. public void addOutputDataset(Job job, Dataset ds) throws IcatInternalException_Exception,
  113. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  114. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  115. ValidationException_Exception {
  116. final OutputDataset ods = new OutputDataset();
  117. ods.setDataset(ds);
  118. ods.setJob(job);
  119. this.icatEP.create(this.sessionId, ods);
  120. }
  121. public void addRule(String groupName, String what, String crudFlags) throws Exception {
  122. Rule rule = new Rule();
  123. if (groupName != null) {
  124. Group g = (Group) this.icatEP.get(this.sessionId, "Group", groupName);
  125. rule.setGroup(g);
  126. }
  127. rule.setWhat(what);
  128. rule.setCrudFlags(crudFlags);
  129. this.icatEP.create(this.sessionId, rule);
  130. }
  131. public void addUserGroupMember(String groupName, String userName) throws Exception {
  132. Group group = null;
  133. if (groupName != null) {
  134. try {
  135. group = (Group) this.icatEP.get(this.sessionId, "Group", groupName);
  136. } catch (NoSuchObjectFoundException_Exception e) {
  137. group = new Group();
  138. group.setName(groupName);
  139. this.icatEP.create(sessionId, group);
  140. }
  141. }
  142. User user = null;
  143. try {
  144. user = (User) this.icatEP.get(this.sessionId, "User", userName);
  145. } catch (NoSuchObjectFoundException_Exception e) {
  146. user = new User();
  147. user.setName(userName);
  148. this.icatEP.create(sessionId, user);
  149. }
  150. UserGroup userGroup = new UserGroup();
  151. userGroup.setUser(user);
  152. userGroup.setGroup(group);
  153. this.icatEP.create(sessionId, userGroup);
  154. }
  155. public void clear() throws Exception {
  156. List<Object> lo = this.search("Job");
  157. for (final Object o : lo) {
  158. System.out.println("Deleting " + o);
  159. this.delete((Job) o);
  160. }
  161. lo = this.search("Application");
  162. for (final Object o : lo) {
  163. System.out.println("Deleting " + o);
  164. this.delete((Application) o);
  165. }
  166. lo = this.search("Investigation");
  167. for (final Object o : lo) {
  168. System.out.println("Deleting " + o);
  169. this.delete((Investigation) o);
  170. }
  171. lo = this.search("DatasetType");
  172. for (final Object o : lo) {
  173. System.out.println("Deleting " + o);
  174. this.delete((DatasetType) o);
  175. }
  176. lo = this.search("DatafileFormat");
  177. for (final Object o : lo) {
  178. System.out.println("Deleting " + o);
  179. this.delete((DatafileFormat) o);
  180. }
  181. lo = this.search("InvestigationType");
  182. for (final Object o : lo) {
  183. System.out.println("Deleting " + o);
  184. this.delete((InvestigationType) o);
  185. }
  186. lo = this.search("ParameterType");
  187. for (final Object o : lo) {
  188. System.out.println("Deleting " + o);
  189. this.delete((ParameterType) o);
  190. }
  191. lo = this.search("Facility");
  192. for (final Object o : lo) {
  193. System.out.println("Deleting " + o);
  194. this.delete((Facility) o);
  195. }
  196. lo = this.search("NotificationRequest");
  197. for (final Object o : lo) {
  198. System.out.println("Deleting " + o);
  199. this.delete((NotificationRequest) o);
  200. }
  201. }
  202. public void clearAuthz() throws Exception {
  203. List<Object> lo1 = this.search("Rule");
  204. List<Object> lo2 = this.search("UserGroup");
  205. List<Object> lo3 = this.search("User");
  206. List<Object> lo4 = this.search("Group");
  207. for (final Object o : lo1) {
  208. System.out.println("Deleting " + o);
  209. this.delete((Rule) o);
  210. }
  211. for (final Object o : lo2) {
  212. System.out.println("Deleting " + o);
  213. this.delete((UserGroup) o);
  214. }
  215. for (final Object o : lo3) {
  216. System.out.println("Deleting " + o);
  217. this.delete((User) o);
  218. }
  219. for (final Object o : lo4) {
  220. System.out.println("Deleting " + o);
  221. this.delete((Group) o);
  222. }
  223. }
  224. public Application createApplication(String name, String version)
  225. throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  226. NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
  227. SessionException_Exception, ValidationException_Exception {
  228. final Application application = new Application();
  229. application.setName(name);
  230. application.setVersion(version);
  231. application.setId((Long) this.icatEP.create(this.sessionId, application));
  232. return application;
  233. }
  234. public Datafile createDatafile(String name, DatafileFormat format, Dataset ds)
  235. throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  236. NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
  237. SessionException_Exception, ValidationException_Exception {
  238. final Datafile datafile = new Datafile();
  239. datafile.setDatafileFormat(format);
  240. datafile.setName(name);
  241. datafile.setDataset(ds);
  242. datafile.setId((Long) this.icatEP.create(this.sessionId, datafile));
  243. return datafile;
  244. }
  245. public DatafileFormat createDatafileFormat(Facility facility, String name, String formatType)
  246. throws Exception {
  247. final DatafileFormat dff = new DatafileFormat();
  248. dff.setName(name);
  249. dff.setVersion("1");
  250. dff.setType(formatType);
  251. dff.setFacility(facility);
  252. dff.setId((Long) this.icatEP.create(this.sessionId, dff));
  253. return dff;
  254. }
  255. public Dataset createDataset(String name, DatasetType type, Investigation inv)
  256. throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  257. NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
  258. SessionException_Exception, ValidationException_Exception {
  259. final Dataset dataset = new Dataset();
  260. dataset.setName(name);
  261. dataset.setType(type);
  262. dataset.setInvestigation(inv);
  263. dataset.setId((Long) this.icatEP.create(this.sessionId, dataset));
  264. return dataset;
  265. }
  266. public DatasetParameter createDatasetParameter(Object value, ParameterType p, Dataset ds)
  267. throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  268. NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
  269. SessionException_Exception, ValidationException_Exception {
  270. final DatasetParameter dsp = new DatasetParameter();
  271. if (p.getValueType() == ParameterValueType.DATE_AND_TIME) {
  272. dsp.setDateTimeValue((XMLGregorianCalendar) value);
  273. }
  274. dsp.setType(p);
  275. dsp.setDataset(ds);
  276. this.icatEP.create(this.sessionId, dsp);
  277. return dsp;
  278. }
  279. public DatasetType createDatasetType(Facility facility, String name) throws Exception {
  280. final DatasetType dst = new DatasetType();
  281. dst.setName(name);
  282. dst.setFacility(facility);
  283. dst.setId((Long) this.icatEP.create(this.sessionId, dst));
  284. return dst;
  285. }
  286. public Facility createFacility(String shortName, int daysUntilRelease) throws Exception {
  287. final Facility f = new Facility();
  288. f.setName(shortName);
  289. f.setDaysUntilRelease(daysUntilRelease);
  290. this.icatEP.create(this.sessionId, f);
  291. return f;
  292. }
  293. public Investigation createInvestigation(Facility facility, String invNumber, String title,
  294. InvestigationType invType) throws Exception {
  295. final Investigation i = new Investigation();
  296. i.setFacility(facility);
  297. i.setName(invNumber);
  298. i.setTitle(title);
  299. i.setType(invType);
  300. i.setId((Long) this.icatEP.create(this.sessionId, i));
  301. return i;
  302. }
  303. public InvestigationType createInvestigationType(Facility facility, String name)
  304. throws Exception {
  305. final InvestigationType type = new InvestigationType();
  306. type.setFacility(facility);
  307. type.setName(name);
  308. type.setId((Long) this.icatEP.create(this.sessionId, type));
  309. return type;
  310. }
  311. public Job createJob(Application application) throws IcatInternalException_Exception,
  312. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  313. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  314. ValidationException_Exception {
  315. final Job job = new Job();
  316. job.setApplication(application);
  317. job.setId((Long) this.icatEP.create(this.sessionId, job));
  318. return job;
  319. }
  320. public void delete(EntityBaseBean bean) throws IcatInternalException_Exception,
  321. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  322. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  323. ValidationException_Exception {
  324. this.icatEP.delete(this.sessionId, bean);
  325. }
  326. public EntityBaseBean get(String query, Object key) throws BadParameterException_Exception,
  327. IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  328. NoSuchObjectFoundException_Exception, SessionException_Exception {
  329. return this.icatEP.get(this.sessionId, query, key);
  330. }
  331. public List<Object> search(String query) throws BadParameterException_Exception,
  332. IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  333. SessionException_Exception {
  334. return this.icatEP.search(this.sessionId, query);
  335. }
  336. public void setAuthz() throws Exception {
  337. try {
  338. this.addUserGroupMember("root", "root");
  339. } catch (ObjectAlreadyExistsException_Exception e) {
  340. System.out.println("root is already a member of root - carry on");
  341. }
  342. this.addRule("root", "Rule", "CRUD");
  343. this.addRule("root", "User", "CRUD");
  344. this.addRule("root", "Group", "CRUD");
  345. this.addRule("root", "UserGroup", "CRUD");
  346. this.addRule("root", "DatafileFormat", "CRUD");
  347. this.addRule("root", "DatasetType", "CRUD");
  348. this.addRule("root", "Facility", "CRUD");
  349. this.addRule("root", "Investigation", "CRUD");
  350. this.addRule("root", "InvestigationType", "CRUD");
  351. this.addRule("root", "ParameterType", "CRUD");
  352. this.addRule("root", "Investigation", "CRUD");
  353. this.addRule("root", "Dataset", "CRUD");
  354. this.addRule("root", "ParameterType", "CRUD");
  355. this.addRule("root", "DatasetParameter", "CRUD");
  356. this.addRule("root", "Datafile", "CRUD");
  357. this.addRule("root", "DatafileFormat", "CRUD");
  358. this.addRule("root", "DatasetType", "CRUD");
  359. this.addRule("root", "Application", "CRUD");
  360. this.addRule("root", "Job", "CRUD");
  361. this.addRule("root", "InputDataset", "CRUD");
  362. this.addRule("root", "OutputDataset", "CRUD");
  363. this.addRule("root", "InputDatafile", "CRUD");
  364. this.addRule("root", "OutputDatafile", "CRUD");
  365. this.addRule("root", "NotificationRequest", "CRUD");
  366. this.addRule("root", "InvestigationParameter", "CRUD");
  367. }
  368. public void update(EntityBaseBean df) throws IcatInternalException_Exception,
  369. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  370. SessionException_Exception, ValidationException_Exception {
  371. this.icatEP.update(this.sessionId, df);
  372. }
  373. public NotificationRequest createNotificationRequest(String name, DestType destType,
  374. String what, String crudFlags, String jmsOptions, String dataTypes)
  375. throws IcatInternalException_Exception, InsufficientPrivilegesException_Exception,
  376. NoSuchObjectFoundException_Exception, ObjectAlreadyExistsException_Exception,
  377. SessionException_Exception, ValidationException_Exception {
  378. NotificationRequest notificationRequest = new NotificationRequest();
  379. notificationRequest.setName(name);
  380. notificationRequest.setDestType(destType);
  381. notificationRequest.setWhat(what);
  382. notificationRequest.setCrudFlags(crudFlags);
  383. notificationRequest.setJmsOptions(jmsOptions);
  384. notificationRequest.setDatatypes(dataTypes);
  385. icatEP.create(this.sessionId, notificationRequest);
  386. return notificationRequest;
  387. }
  388. public void registerInvestigation(Investigation inv) throws IcatInternalException_Exception,
  389. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  390. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  391. ValidationException_Exception {
  392. inv.setId((Long) this.icatEP.create(this.sessionId, inv));
  393. }
  394. public EntityInfo getEntityInfo(String beanName) throws BadParameterException_Exception,
  395. IcatInternalException_Exception {
  396. return icatEP.getEntityInfo(beanName);
  397. }
  398. public Object create(EntityBaseBean bean) throws IcatInternalException_Exception,
  399. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  400. ObjectAlreadyExistsException_Exception, SessionException_Exception,
  401. ValidationException_Exception {
  402. return this.icatEP.create(this.sessionId, bean);
  403. }
  404. public List<Object> createMany(List<EntityBaseBean> beans)
  405. throws ValidationException_Exception, IcatInternalException_Exception,
  406. InsufficientPrivilegesException_Exception, NoSuchObjectFoundException_Exception,
  407. ObjectAlreadyExistsException_Exception, SessionException_Exception {
  408. return this.icatEP.createMany(this.sessionId, beans);
  409. }
  410. }