Source: tc-admin.service.js

  1. (function() {
  2. 'use strict';
  3. var app = angular.module('topcat');
  4. app.service('tcAdmin', function(helpers){
  5. this.create = function(facility){
  6. return new Admin(facility);
  7. };
  8. /**
  9. * @interface Admin
  10. */
  11. function Admin(facility){
  12. var that = this;
  13. this.facility = function(){
  14. return facility;
  15. };
  16. this.isValidSession = helpers.overload({
  17. /**
  18. * Returns whether or not the user's session has admin priviliges.
  19. *
  20. * @method
  21. * @name Admin#isValidSession
  22. * @param {string} sessionId the session id to be tested
  23. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  24. * @return {Promise<boolean>}
  25. */
  26. 'string, object': function(sessionId, options){
  27. return this.get('isValidSession', {
  28. facilityName: facility.config().name,
  29. sessionId: sessionId
  30. }, options);
  31. },
  32. /**
  33. * Returns whether or not the user's session has admin priviliges.
  34. *
  35. * @method
  36. * @name Admin#isValidSession
  37. * @param {string} sessionId the session id to be tested
  38. * @param {Promise} timeout if resolved will cancel the request
  39. * @return {Promise<boolean>}
  40. */
  41. 'string, promise': function(sessionId, timeout){
  42. return this.isValidSession(sessionId, {timeout: timeout});
  43. },
  44. /**
  45. * Returns whether or not the user's session has admin priviliges.
  46. *
  47. * @method
  48. * @name Admin#isValidSession
  49. * @param {string} sessionId the session id to be tested
  50. * @return {Promise<boolean>}
  51. */
  52. 'string': function(sessionId){
  53. return this.isValidSession(sessionId, {});
  54. }
  55. });
  56. this.downloads = helpers.overload({
  57. /**
  58. * Returns all downloads.
  59. *
  60. * @method
  61. * @name Admin#downloads
  62. * @param {array} queryOffset any JPQL from the where clause onwards
  63. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  64. * @return {Promise<object[]>} a deferred list of downloads
  65. */
  66. 'array, object': function(queryOffset, options){
  67. queryOffset = helpers.buildQuery(queryOffset);
  68. queryOffset = "where download.facilityName = " + helpers.jpqlSanitize(facility.config().name) + (queryOffset ? " AND " + queryOffset.replace(/^\s*where\s*/, '') : "");
  69. return this.get('downloads', {
  70. facilityName: facility.config().name,
  71. sessionId: facility.icat().session().sessionId,
  72. queryOffset: queryOffset
  73. }, options).then(function(downloads){
  74. _.each(downloads, function(download){
  75. download.delete = helpers.overload({
  76. 'object': function(options){
  77. return that.deleteDownload(this.id, options);
  78. },
  79. 'promise': function(timeout){
  80. return this.delete({timeout: timeout});
  81. },
  82. '': function(){
  83. return this.delete({});
  84. }
  85. });
  86. download.restore = helpers.overload({
  87. 'object': function(options){
  88. return that.restoreDownload(this.idd, options);
  89. },
  90. 'promise': function(timeout){
  91. return this.restore(this.id, {timeout: timeout});
  92. },
  93. '': function(){
  94. return this.restore(this.id, {});
  95. }
  96. });
  97. });
  98. return downloads;
  99. });
  100. },
  101. /**
  102. * Returns all downloads.
  103. *
  104. * @method
  105. * @name Admin#downloads
  106. * @param {Promise} timeout if resolved will cancel the request
  107. * @param {array} queryOffset any JPQL from the where clause onwards
  108. * @return {Promise<object[]>} a deferred list of downloads
  109. */
  110. 'promise, array': function(timeout, queryOffset){
  111. return this.downloads(queryOffset, {timeout: timeout});
  112. },
  113. /**
  114. * Returns all downloads.
  115. *
  116. * @method
  117. * @name Admin#downloads
  118. * @param {array} queryOffset any JPQL from the where clause onwards
  119. * @return {Promise<object[]>} a deferred list of downloads
  120. */
  121. 'array': function(queryOffset){
  122. return this.downloads(queryOffset, {});
  123. },
  124. /**
  125. * Returns all downloads.
  126. *
  127. * @method
  128. * @name Admin#downloads
  129. * @param {Promise} timeout if resolved will cancel the request
  130. * @param {string} queryOffset any JPQL from the where clause onwards
  131. * @return {Promise<object[]>} a deferred list of downloads
  132. */
  133. 'promise, string': function(timeout, queryOffset){
  134. return this.downloads([queryOffset], {timeout: timeout});
  135. },
  136. /**
  137. * Returns all downloads.
  138. *
  139. * @method
  140. * @name Admin#downloads
  141. * @param {string} queryOffset any JPQL from the where clause onwards
  142. * @return {Promise<object[]>} a deferred list of downloads
  143. */
  144. 'string': function(queryOffset){
  145. return this.downloads([queryOffset]);
  146. },
  147. /**
  148. * Returns all downloads.
  149. *
  150. * @method
  151. * @name Admin#downloads
  152. * @param {Promise} timeout if resolved will cancel the request
  153. * @return {Promise<object[]>} a deferred list of downloads
  154. */
  155. 'promise': function(timeout){
  156. return this.downloads(params, {timeout: timeout});
  157. },
  158. /**
  159. * Returns all downloads.
  160. *
  161. * @method
  162. * @name Admin#downloads
  163. * @return {Promise<object[]>} a deferred list of downloads
  164. */
  165. '': function(){
  166. return this.downloads({}, {});
  167. }
  168. });
  169. this.deleteDownload = helpers.overload({
  170. /**
  171. * Soft deletes a download.
  172. *
  173. * @method
  174. * @name Admin#deleteDownload
  175. * @param {string|number} id the id of the download
  176. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  177. * @return {Promise}
  178. */
  179. 'string, object': function(id, options){
  180. return this.put('download/' + id + '/isDeleted', {
  181. facilityName: facility.config().name,
  182. sessionId: facility.icat().session().sessionId,
  183. value: 'true'
  184. }, options);
  185. },
  186. /**
  187. * Soft deletes a download.
  188. *
  189. * @method
  190. * @name Admin#deleteDownload
  191. * @param {string|number} id the id of the download
  192. * @param {Promise} timeout if resolved will cancel the request
  193. * @return {Promise}
  194. */
  195. 'string, promise': function(id, timeout){
  196. return this.deleteDownload(id, {timeout: timeout});
  197. },
  198. /**
  199. * Soft deletes a download.
  200. *
  201. * @method
  202. * @name Admin#deleteDownload
  203. * @param {string|number} id the id of the download
  204. * @return {Promise}
  205. */
  206. 'string': function(id){
  207. return this.deleteDownload(id, {});
  208. },
  209. 'number, object': function(id, options){
  210. return this.deleteDownload("" + id, options);
  211. },
  212. 'number, promise': function(id, timeout){
  213. return this.deleteDownload("" + id, {timeout: timeout});
  214. },
  215. 'number': function(id){
  216. return this.deleteDownload("" + id, {});
  217. }
  218. });
  219. this.restoreDownload = helpers.overload({
  220. /**
  221. * Restores a (soft deleted) download.
  222. *
  223. * @method
  224. * @name Admin#restoreDownload
  225. * @param {string|number} id the id of the download
  226. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  227. * @return {Promise}
  228. */
  229. 'string, object': function(id, options){
  230. return this.put('download/' + id + '/isDeleted', {
  231. facilityName: facility.config().name,
  232. sessionId: facility.icat().session().sessionId,
  233. value: 'false'
  234. }, options);
  235. },
  236. /**
  237. * Restores a (soft deleted) download.
  238. *
  239. * @method
  240. * @name Admin#restoreDownload
  241. * @param {string|number} id the id of the download
  242. * @param {Promise} timeout if resolved will cancel the request
  243. * @return {Promise}
  244. */
  245. 'string, promise': function(id, timeout){
  246. return this.restoreDownload(id, {timeout: timeout});
  247. },
  248. /**
  249. * Restores a (soft deleted) download.
  250. *
  251. * @method
  252. * @name Admin#restoreDownload
  253. * @param {string|number} id the id of the download
  254. * @return {Promise}
  255. */
  256. 'string': function(id){
  257. return this.restoreDownload(id, {});
  258. },
  259. 'number, object': function(id, options){
  260. return this.restoreDownload("" + id, options);
  261. },
  262. 'number, promise': function(id, timeout){
  263. return this.restoreDownload("" + id, {timeout: timeout});
  264. },
  265. 'number': function(id){
  266. return this.restoreDownload("" + id, {});
  267. }
  268. });
  269. this.setDownloadStatus = helpers.overload({
  270. /**
  271. * Sets the status of a download.
  272. *
  273. * @method
  274. * @name Admin#setDownloadStatus
  275. * @param {string|number} id the id of the download
  276. * @sparam {string} status can be 'ONLINE', 'ARCHIVE' or 'RESTORING'
  277. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  278. * @return {Promise}
  279. */
  280. 'string, string, object': function(id, status, options){
  281. return this.put('download/' + id + '/status', {
  282. facilityName: facility.config().name,
  283. sessionId: facility.icat().session().sessionId,
  284. value: status
  285. }, options);
  286. },
  287. /**
  288. * Sets the status of a download.
  289. *
  290. * @method
  291. * @name Admin#setDownloadStatus
  292. * @param {string|number} id the id of the download
  293. * @param {string} status can be 'ONLINE', 'ARCHIVE' or 'RESTORING'
  294. * @param {Promise} timeout if resolved will cancel the request
  295. * @return {Promise}
  296. */
  297. 'string, string, promise': function(id, status, timeout){
  298. return this.setDownloadStatus(id, status, {timeout: timeout});
  299. },
  300. /**
  301. * Sets the status of a download.
  302. *
  303. * @method
  304. * @name Admin#setDownloadStatus
  305. * @param {string|number} id the id of the download
  306. * @param {string} status can be 'ONLINE', 'ARCHIVE' or 'RESTORING'
  307. * @return {Promise}
  308. */
  309. 'string, string': function(id, status){
  310. return this.setDownloadStatus(id, status, {});
  311. },
  312. 'number, string, object': function(id, status, options){
  313. return this.setDownloadStatus("" + id, status, options);
  314. },
  315. 'number, string, promise': function(id, status, timeout){
  316. return this.setDownloadStatus("" + id, status, {timeout: timeout});
  317. },
  318. 'number, string': function(id, status){
  319. return this.setDownloadStatus("" + id, status, {});
  320. }
  321. });
  322. this.setConfVar = helpers.overload({
  323. /**
  324. * Stores a value on Topcat.
  325. *
  326. * @method
  327. * @name Admin#setConfVar
  328. * @param {string} name the name of the value
  329. * @param {object} value the value to be stored
  330. * @param {object} options {@link https://docs.angularjs.org/api/ng/service/$http#usage|as specified in the Angular documentation}
  331. * @return {Promise}
  332. */
  333. 'string, object, object': function(name, value, options){
  334. return this.put('confVars/' + name, {
  335. facilityName: facility.config().name,
  336. sessionId: facility.icat().session().sessionId,
  337. value: JSON.stringify(value)
  338. }, options);
  339. },
  340. /**
  341. * Stores a value on Topcat.
  342. *
  343. * @method
  344. * @name Admin#setConfVar
  345. * @param {string} name the name of the value
  346. * @param {object} value the value to be stored
  347. * @param {Promise} timeout if resolved will cancel the request
  348. * @return {Promise}
  349. */
  350. 'string, object, promise': function(name, value, timeout){
  351. return this.setConfVar(name, value, {timeout: timeout});
  352. },
  353. /**
  354. * Stores a value on Topcat.
  355. *
  356. * @method
  357. * @name Admin#setConfVar
  358. * @param {string} name the name of the value
  359. * @param {object} value the value to be stored
  360. * @return {Promise}
  361. */
  362. 'string, object': function(name, value){
  363. return this.setConfVar(name, value, {});
  364. }
  365. });
  366. helpers.generateRestMethods(this, facility.tc().config().topcatUrl + "/topcat/admin/");
  367. helpers.mixinPluginMethods('admin', this);
  368. }
  369. });
  370. })();