Context.js

  1. import GraphResource from './Graph.js';
  2. import Pipeline from './Pipeline.js';
  3. import PrototypeEntry from './PrototypeEntry.js';
  4. import Resource from './Resource.js';
  5. import StringResource from './String.js';
  6. import types from './types.js';
  7. /**
  8. * Methods for interacting with the EntryStore repository scoped to a specific context.
  9. *
  10. * @exports store/Context
  11. */
  12. export default class Context extends Resource {
  13. /**
  14. * @param {string} entryURI - URI to an entry where this resource is contained.
  15. * @param {string} resourceURI - URI to the resource.
  16. * @param {EntryStore} entryStore - the API's repository instance.
  17. */
  18. // eslint-disable-next-line no-useless-constructor
  19. constructor(entryURI, resourceURI, entryStore) {
  20. super(entryURI, resourceURI, entryStore);
  21. }
  22. /**
  23. * Retrieves a list of entries in the context.
  24. *
  25. * @param {Object} sort - same sort object as provided in the optionalLoadParams to
  26. * @link EntryStore#getEntry getEntry method.
  27. * @param {Object} limit - same limit as provided in the optionalLoadParams to
  28. * {@see EntryStore#getEntry getEntry} method.
  29. * @param {integer} page - unless limit is set to -1 (no pagination) we need to specify
  30. * which page to load, first page is 0.
  31. * @returns {Promise.<Entry[]>} upon success the promise returns an array of entries.
  32. * @see EntryStore#getListEntries
  33. */
  34. listEntries(sort, limit, page) {
  35. return this.getEntryStore().getListEntries(`${this._resourceURI}/entry/_all`, sort, limit, page);
  36. }
  37. /**
  38. * Convenience method, to retrieve an entry from this context.
  39. *
  40. * @param {string} entryId
  41. * @param {object} optionalLoadParams same parameter as in {@see EntryStore#getEntry}
  42. * @returns {Promise.<Entry>}
  43. * @see EntryStore#getEntry
  44. */
  45. getEntryById(entryId, optionalLoadParams = {}) {
  46. return this.getEntryStore().getEntry(this.getEntryURIbyId(entryId), optionalLoadParams);
  47. }
  48. /**
  49. * Expands the given entry id into a full URI.
  50. *
  51. * @param {string} entryId
  52. * @returns {string} the URI for an entry in this context with the given id.
  53. */
  54. getEntryURIbyId(entryId) {
  55. return this.getEntryStore().getEntryURI(this.getId(), entryId);
  56. }
  57. /**
  58. * Factory method to create a PrototypeEntry that has the current context as container.
  59. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create it
  60. * (returns a promise).
  61. *
  62. * @param {string=} id - id for the entry, fails after commit if an entry exists already with
  63. * this id.
  64. * @returns {PrototypeEntry}
  65. */
  66. newEntry(id) {
  67. return new PrototypeEntry(this, id);
  68. }
  69. /**
  70. * Factory method to create a PrototypeEntry that corresponds to a local named resource that
  71. * has the current context as container.
  72. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create it
  73. * (returns a promise).
  74. *
  75. * @param {string=} id - id for the entry, fails after commit if an entry exists already
  76. * with this id.
  77. * @returns {PrototypeEntry}
  78. */
  79. newNamedEntry(id) {
  80. return new PrototypeEntry(this, id).setResourceType(types.RT_NAMEDRESOURCE);
  81. }
  82. /**
  83. * Factory method to create a PrototypeEntry that corresponds to a link that has the
  84. * current context as container.
  85. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create
  86. * it (returns a promise).
  87. *
  88. * @param {string} link - the URI for the resource we are making a link to, mandatory.
  89. * @param {string=} id - id for the entry, fails after commit if an entry exists already
  90. * with this id.
  91. * @returns {PrototypeEntry}
  92. */
  93. newLink(link, id) {
  94. return new PrototypeEntry(this, id).setResourceURI(link).setEntryType(types.ET_LINK);
  95. }
  96. /**
  97. * Factory method to create a PrototypeEntry that is a linkref that has the current context
  98. * as container. Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to
  99. * actually create it (returns a promise).
  100. *
  101. * @param {string} link - is the URI for the resource we are making a link to, mandatory.
  102. * @param {string} metadataLink - is the URI for the metadata are referring to, mandatory.
  103. * @param {string=} id - id for the entry, fails after commit if an entry exists already
  104. * with this id.
  105. * @returns {PrototypeEntry}
  106. */
  107. newLinkRef(link, metadataLink, id) {
  108. return new PrototypeEntry(this, id)
  109. .setResourceURI(link)
  110. .setExternalMetadataURI(metadataLink)
  111. .setEntryType(types.ET_LINKREF);
  112. }
  113. /**
  114. * Factory method to create a PrototypeEntry that is a reference and has the current
  115. * context as container. Call {@link PrototypeEntry#commit commit} on the
  116. * PrototypeEntry to actually create it (returns a promise).
  117. * The only difference to the newLinkRef method is that the EntryType is Reference instead
  118. * of LinkReference which implies that there is no local metadata.
  119. *
  120. * @param {string} link - the URI for the resource we are making a link to, mandatory.
  121. * @param {string} metadataLink - the URI for the metadata are referring to, mandatory.
  122. * @param {string=} id for the entry, fails after commit if an entry exists already with
  123. * this id.
  124. * @returns {PrototypeEntry}
  125. */
  126. newRef(link, metadataLink, id) {
  127. return new PrototypeEntry(this, id)
  128. .setResourceURI(link)
  129. .setExternalMetadataURI(metadataLink)
  130. .setEntryType(types.ET_REF);
  131. }
  132. /**
  133. * Factory method to create a PrototypeEntry whose resource is a {@link List List)
  134. * and has the current context as container.
  135. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create
  136. * it (returns a promise).
  137. *
  138. * @param {string} id an optional id for the entry, fails on commit if an entry exists already
  139. * with this id.
  140. * @returns {PrototypeEntry}
  141. */
  142. newList(id) {
  143. return new PrototypeEntry(this, id).setGraphType(types.GT_LIST);
  144. }
  145. /**
  146. * Factory method to create a PrototypeEntry whose resource is a {@link Graph Graph}
  147. * and has the current context as container.
  148. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create it
  149. * (returns a promise).
  150. *
  151. * @param {rdfjson/Graph|{}} graph - graph to store as a resource.
  152. * @param {string=} id - id for the entry, fails upon commit if an entry exists already
  153. * with this id.
  154. * @returns {PrototypeEntry}
  155. */
  156. newGraph(graph = {}, id) {
  157. const prototypeEntry = new PrototypeEntry(this, id).setGraphType(types.GT_GRAPH);
  158. const entryInfo = prototypeEntry.getEntryInfo();
  159. prototypeEntry._resource = new GraphResource(entryInfo.getEntryURI(), entryInfo.getResourceURI(),
  160. this.getEntryStore(), graph);
  161. return prototypeEntry;
  162. }
  163. /**
  164. * Factory method to create a PrototypeEntry whose resource is a {@link String String}
  165. * that has the current context as container.
  166. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create
  167. * it (returns a promise).
  168. *
  169. * @param {string=} str an optional string for the String Resource.
  170. * @param {String} id an optional id for the entry, fails upon commit if an entry exists
  171. * already with this id.
  172. * @returns {PrototypeEntry}
  173. */
  174. newString(str, id) {
  175. const prototypeEntry = new PrototypeEntry(this, id).setGraphType(types.GT_STRING);
  176. const entryInfo = prototypeEntry.getEntryInfo();
  177. prototypeEntry._resource = new StringResource(entryInfo.getEntryURI(),
  178. entryInfo.getResourceURI(), this.getEntryStore(), str);
  179. return prototypeEntry;
  180. }
  181. /**
  182. * Factory method to create a PrototypeEntry whose resource is a
  183. * {@link Pipeline pipeline} that has the current context as container.
  184. * Call {@link PrototypeEntry#commit commit} on the PrototypeEntry to actually create it
  185. * (returns a promise).
  186. *
  187. * @param {String} id an optional id for the entry, fails upon commit if an entry exists
  188. * already with this id.
  189. * @returns {PrototypeEntry}
  190. */
  191. newPipeline(id) {
  192. const prototypeEntry = new PrototypeEntry(this, id).setGraphType(types.GT_PIPELINE);
  193. const entryInfo = prototypeEntry.getEntryInfo();
  194. prototypeEntry._resource = new Pipeline(entryInfo.getEntryURI(), entryInfo.getResourceURI(), this.getEntryStore(), {});
  195. return prototypeEntry;
  196. }
  197. /**
  198. * The name for this context.
  199. *
  200. * @returns {string}
  201. */
  202. getName() {
  203. return this._name;
  204. }
  205. /**
  206. * Change of context name, succeeds if name is not in use already by another context.
  207. * @param {string} name
  208. * @returns {Promise.<string>}
  209. */
  210. async setName(name) {
  211. const oldName = this._name;
  212. this._name = name;
  213. try {
  214. const promise = this.getEntryStore().getREST().put(`${this.getEntryURI()}/name`, JSON.stringify({ name }));
  215. this.getEntryStore().handleAsync(promise, 'setContextName');
  216. await promise;
  217. const entry = this.getEntry(true);
  218. if (entry) {
  219. entry.getEntryInfo()._name = name;
  220. }
  221. return name;
  222. } catch (e) {
  223. this._name = oldName;
  224. throw e;
  225. }
  226. }
  227. /**
  228. * Finds the user or group that has this context as homecontext if any.
  229. *
  230. * @returns {Promise.<Entry>} if succeeds if context a homecontext of some user or group.
  231. * @async
  232. */
  233. async getHomeContextOf() {
  234. const contextEntry = await this.getEntry();
  235. const es = contextEntry.getEntryStore();
  236. const groupResourceArr = contextEntry.getReferrers('store:homeContext');
  237. if (groupResourceArr.length > 0) {
  238. return es.getEntry(es.getEntryURIFromURI(groupResourceArr[0]));
  239. }
  240. throw new Error('No user or group that has this context as home context');
  241. }
  242. /**
  243. *
  244. * @param data
  245. * @private
  246. */
  247. _update(data) {
  248. this._name = data.alias || data.name; // TODO, change to only name after clean-up
  249. }
  250. };