EntryStore.js

  1. import Auth from './Auth.js';
  2. import Cache from './Cache.js';
  3. import factory from './factory.js';
  4. import PrototypeEntry from './PrototypeEntry.js';
  5. import Resource from './Resource.js';
  6. import Rest from './Rest.js';
  7. import SolrQuery from './SolrQuery.js';
  8. import types from './types.js';
  9. import User from './User.js';
  10. import { isBrowser } from './utils.js';
  11. /**
  12. * EntryStore is the main class that is used to connect to a running server-side EntryStore
  13. * repository.
  14. * @exports store/EntryStore
  15. */
  16. export default class EntryStore {
  17. /**
  18. * @param {String=} baseURI - URL to the EntryStore repository we should communicate with,
  19. * may be left out and
  20. * guessed if run in a browser environment (appends "/" to the window.location.origin)
  21. * @param {Object=} credentials - same as provided in the {@link EntryStore#auth auth}
  22. * method.
  23. */
  24. constructor(baseURI, credentials) {
  25. if (isBrowser() && baseURI == null) {
  26. this._baseURI = `${window.location.origin}/`;
  27. } else {
  28. this._baseURI = baseURI;
  29. if (this._baseURI[this._baseURI.length - 1] !== '/') {
  30. this._baseURI = `${this._baseURI}/`;
  31. }
  32. }
  33. this._cache = new Cache();
  34. this._auth = new Auth(this);
  35. if (credentials) {
  36. this._auth.login(...credentials);
  37. }
  38. this._contexts = {};
  39. this._rest = new Rest();
  40. this._requestCache = false;
  41. }
  42. /**
  43. * Provides a listener that will be called for every asynchronous call being made.
  44. * The handler is invoked with the promise from the asynchronous call
  45. * and a callType parameter indicating which asynchronous call that has been made.
  46. *
  47. * The callType parameter can take the following values:
  48. * - getEntry - an entry is retrieved (EntryStore.getEntry)
  49. * - createEntry - an entry is created (EntryStore.createEntry)
  50. * - createGroupAndContext - a group and context pair is created
  51. * (EntryStore.createGroupAndContext)
  52. * - loadViaProxy - data is requested via repository proxy (EntryStore.loadViaProxy)
  53. * - commitMetadata - changes to metadata is pushed (Entry.commitMetadata)
  54. * - commitCachedExternalMetadata - changes to cached external metadata is pushed
  55. * (Entry.commitCachedExternalMetadata)
  56. * - getResource - the entry's resource has been requested (Entry.getResource)
  57. * - getLinkedEntry - a linked entry is requested (Entry.getLinkedEntry)
  58. * - delEntry - an entry is deleted (Entry.del)
  59. * - refresh - an entry is refreshed (Entry.refresh)
  60. * - setContextName - the name of a context is changed (Context.setName)
  61. * - getUserInfo - the user information is requested (auth.getUserInfo)
  62. * - getUserEntry - the user entry is requested (auth.getUserEntry)
  63. * - login - logging in (auth.login)
  64. * - logout - logging out (auth.logout)
  65. * - commitEntryInfo - pushing changes in entry information (EntryInfo.commit)
  66. * - getFile - the contents of a file resource is requested (File.get*)
  67. * - putFile - the contents of a file is pushed (File.put*)
  68. * - commitGraph - a graph resource is pushed (Graph.commit)
  69. * - commitString - a string resource is pushed (String.commit)
  70. * - setGroupName - a new name of a group is pushed (Group.setName)
  71. * - setUserName - a new name of a user is pushed (User.setName)
  72. * - setUserDisabled - a new disabled state of a user is pushed (User.setDisabled)
  73. * - setUserLanguage - a new preferred language of the user is pushed (User.setLanguage)
  74. * - setUserPassword - a new password for the user is pushed (User.setPassword)
  75. * - setUserHomeContext - a new home context for the user is pushed (User.setHomeContext)
  76. * - setUserCustomProperties - new custom properties for the user (User.setCustomProperties)
  77. * - loadListEntries - members of a list are requested (List.getEntries)
  78. * - setList - the list members are changed via a list
  79. * - addToList - See List.addEntry
  80. * - removeFromList - See List.removeEntry
  81. * .removeEntry)
  82. * - search - a search is being performed (SearchList.getEntries)
  83. * - execute - a pipeline is executed (Pipeline.execute)
  84. *
  85. * @param {Promise.<string>} listener
  86. */
  87. addAsyncListener(listener) {
  88. if (this.asyncListeners) {
  89. this.asyncListeners.push(listener);
  90. } else {
  91. this.asyncListeners = [listener];
  92. }
  93. }
  94. /**
  95. * Removes a previously added listener for asynchronous calls.
  96. * @param {string} listener
  97. */
  98. removeAsyncListener(listener) {
  99. if (this.asyncListeners) {
  100. this.asyncListeners.splice(this.asyncListeners.indexOf(listener), 1);
  101. }
  102. }
  103. /**
  104. *
  105. * @param {Promise} promise
  106. * @param {string} context
  107. * @return {Promise}
  108. */
  109. handleAsync(promise, context) {
  110. if (this.asyncListeners) {
  111. for (let i = 0; i < this.asyncListeners.length; i++) {
  112. this.asyncListeners[i](promise, context);
  113. }
  114. }
  115. return promise;
  116. }
  117. /**
  118. * @returns {Auth} where functionality related to authorization are located,
  119. * including a listener infrastructure.
  120. */
  121. getAuth() {
  122. return this._auth;
  123. }
  124. /**
  125. * Yields information about who currently is authenticated against the EntryStore repository.
  126. * @returns {Promise.<EntryInfo>} - upon success an object containing attributes "user" being
  127. * the username, "id" of the user entry, and "homecontext" being the entry-id of the
  128. * home context is provided.
  129. * @see {@link EntryStore#auth auth}
  130. * @see {@link EntryStore#logout logout}
  131. * @deprecated use corresponding method on auth object instead.
  132. */
  133. getUserInfo() {
  134. return this._auth.getUserInfo();
  135. }
  136. /**
  137. * @returns {Promise.<Entry>} on success the entry for the currently signed in user is provided.
  138. * @deprecated use corresponding method on auth object instead.
  139. */
  140. getUserEntry() {
  141. return this._auth.getUserEntry();
  142. }
  143. /**
  144. * Authenticate using credentials containing a user, a password and an optional maxAge given
  145. * in seconds.
  146. *
  147. * @param {{user, password, maxAge}} credentials as a parameter object
  148. * @deprecated use corresponding method on auth object instead.
  149. */
  150. auth(credentials) {
  151. if (credentials == null) {
  152. return this._auth.logout();
  153. }
  154. return this._auth.login(credentials.user, credentials.password, credentials.maxAge);
  155. }
  156. /**
  157. * Logout the currently authorized user.
  158. * @returns {Promise}
  159. * @deprecated use corresponding method on auth object instead.
  160. */
  161. logout() {
  162. return this._auth.logout();
  163. }
  164. /**
  165. * Fetches an entry given an entryURI. If the entry is already loaded and available in the
  166. * cache it will be returned directly, otherwise it will be loaded from the repository.
  167. * If the entry is already loaded but marked as in need of a refresh it will be refreshed
  168. * first.
  169. *
  170. * The optional load parameters are provided in a single parameter object with six possible
  171. * attributes. Below we outline these attributes, the first two (forceLoad and direct) applies
  172. * to all kind of entries while the following three (limit, offset and sort) only applies if
  173. * the entry is a list:
  174. *
  175. * forceLoad - ignores if the entry is already in cache and fetches from the repository
  176. * loadResource - makes sure that entry.getResource(true) will not return null
  177. * (does not work in combination with direct).
  178. * direct - returns the entry from the cache directly rather than returning a promise,
  179. * if the entry is not in the cache an undefined value will be returned.
  180. * limit - only a limited number of children are loaded, -1 means no limit, 0, undefined
  181. * or if the attribute is not provided means that the default limit of 20 is used.
  182. * offset - only children from offest and forward is returned, must be positive.
  183. * sort - information on how to sort the children:
  184. * * if sort is not provided at all or an empty object is provided the members of the
  185. * list will not be sorted, instead the list's natural order will be used
  186. * * if sort is given as null the defaults will be used ({sortBy: "title", prio: "List"}).
  187. * * if sort is given as a non emtpy object the following attributes are considered:
  188. * ** sortBy - the attribute instructs which metadata field to sort the children by,
  189. * i.e., title, created, modified, or size.
  190. * ** lang - if sort is title and the title is provided in several languages a
  191. * prioritized language can be given.
  192. * ** prio - allows specific graphtypes to be prioritized
  193. * (e.g. show up in the top of the list).
  194. * ** descending - if true the children are shown in descending order.
  195. *
  196. *
  197. * **Note** - in the case where the entry is a list it is possible to change the limit,
  198. * offset and sort later by calling the corresponding methods on the {@link List}
  199. * resource, e.g. {@link List#setSort}. However, setting the values already in this
  200. * method call has as a consequence that one less request to the repository is made as you
  201. * will get members (in the right amount and order) in the same request as you get metadata
  202. * and other information.
  203. *
  204. * A request of a list entry can look like:
  205. *
  206. * var euri = entrystore.getEntryURI("1", "1");
  207. * entrystore.getEntry(euri, {
  208. * forceLoad: true,
  209. * limit: 10,
  210. * offset: 20,
  211. * sort: {
  212. * sortBy: "modified",
  213. * prio: types.GT_LIST
  214. * }
  215. * });
  216. *
  217. * The optional params here says that we force a load from the repository, that we want the
  218. * results to be paginated with a limit of 10 entries per page and that we want page 3.
  219. * We also indicate that we want the list to be sorted by latest modification date and that
  220. * if there are member entries that are lists they should be sorted to the top.
  221. *
  222. * @param {string} entryURI - the entryURI for the entry to retrieve.
  223. * @param {{forceLoad, direct, loadResource, limit, offset, sort, asyncContext}} optionalLoadParams - parameters for how to load an entry.
  224. * @return {Promise.<Entry> | Entry | undefined} - by default a promise is returned,
  225. * if the direct parameter is specified the entry is returned directly or undefined if the
  226. * entry is not in cache.
  227. * @see {@link EntryStore#getEntryURI getEntryURI} for help to construct entry URIs.
  228. * @see {@link Context#getEntryById} for loading entries relative to a context.
  229. */
  230. getEntry(entryURI, optionalLoadParams = {}) {
  231. const forceLoad = optionalLoadParams ? optionalLoadParams.forceLoad === true : false;
  232. const e = this._cache.get(entryURI);
  233. let asyncContext = 'getEntry';
  234. if (optionalLoadParams != null) {
  235. if (optionalLoadParams.asyncContext) {
  236. asyncContext = optionalLoadParams.asyncContext;
  237. }
  238. if (optionalLoadParams.direct === true) {
  239. return e;
  240. }
  241. }
  242. const checkResourceLoaded = (entry) => {
  243. if (optionalLoadParams != null && optionalLoadParams.loadResource
  244. && entry.getResource() == null) {
  245. return entry.getResource().then(() => entry);
  246. }
  247. return entry;
  248. };
  249. if (e && !forceLoad) {
  250. if ((e.isList() || e.isGroup()) && optionalLoadParams != null) {
  251. const list = e.getResource(true); // Direct access works for lists and groups.
  252. list.setLimit(optionalLoadParams.limit);
  253. list.setSort(optionalLoadParams.sort);
  254. }
  255. // Will only refresh if needed, a promise is returned in any case
  256. return this.handleAsync(e.refresh().then(checkResourceLoaded), asyncContext);
  257. }
  258. const entryPromise = this._cache.getPromise(entryURI);
  259. if (entryPromise) {
  260. return entryPromise;
  261. }
  262. const self = this;
  263. const entryLoadURI = factory.getEntryLoadURI(entryURI, optionalLoadParams);
  264. const loadEntryPromise = this.handleAsync(this._rest.get(entryLoadURI).then((data) => {
  265. // The entry, will always be there.
  266. const entry = factory.updateOrCreate(entryURI, data, self);
  267. return checkResourceLoaded(entry);
  268. }, (err) => {
  269. throw new Error(`Failed fetching entry. ${err}`);
  270. }), asyncContext).finally(() => {
  271. this._cache.removePromise(entryURI);
  272. });
  273. this._cache.addPromise(entryURI, loadEntryPromise);
  274. return loadEntryPromise;
  275. }
  276. /**
  277. * Retrieves entries from a list. One way to see it is that this is a convenience method
  278. * that retrieves a list entry, its member entries and returns those in an array.
  279. *
  280. * @param {string} entryURI - URI of the list entry to load entries from.
  281. * @param {Object} sort - same sort object as provided in the optionalLoadParams to
  282. * {@see EntryStore#getEntry getEntry} method.
  283. * @param {Object} limit - same limit as provided in the optionalLoadParams to
  284. * {@see EntryStore#getEntry getEntry} method.
  285. * @param {integer} page - unless limit is set to -1 (no pagination) we need to specify which
  286. * page to load, first page is 0.
  287. * @returns {Promise.<Entry[]>} upon success the promise returns an array of entries.
  288. */
  289. getListEntries(entryURI, sort, limit, page) {
  290. return new Promise((resolve, reject) => {
  291. const op = {};
  292. if (sort != null) {
  293. op.sort = sort;
  294. }
  295. if (limit % 1 === 0) {
  296. op.limit = limit;
  297. }
  298. if (page % 1 === 0) {
  299. if (limit % 1 === 0) {
  300. op.offset = limit * page;
  301. } else {
  302. op.offset = factory.getDefaultLimit() * page;
  303. }
  304. }
  305. this.getEntryStore().getEntry(entryURI, op)
  306. .then((entry) => {
  307. const list = entry.getResource(true);
  308. list.getEntries(page).then(resolve, reject);
  309. }, reject);
  310. });
  311. }
  312. /**
  313. * Retrieves a Context instance via its id. Note that this method returns directly without
  314. * checking with the EntryStore repository that the context exists. Hence successive
  315. * operations via this context instance may fail if the context does not exist in the
  316. * EntryStore
  317. * repository.
  318. *
  319. * Note that in EntryStore everything is connected to entries. Hence a context is nothing else
  320. * than a special kind of resource maintained by an entry. This entry provides metadata about
  321. * the context as well as the default ownership and access control that applies to all entries
  322. * inside of this context.
  323. *
  324. * To get a hold of the contexts own entry use the {@link Resource#getEntry}
  325. * method on the context (inherited from the generic {@link Resource} class.
  326. *
  327. * Advanced: Entrys corresponding to contexts are stored in the special _contexts
  328. * context which, since it is a context, contains its own entry.
  329. *
  330. * @param {string} contextId - identifier for the context (not necessarily the same as the
  331. * alias/name for the context)
  332. * @return {Context}
  333. */
  334. getContextById(contextId) {
  335. return factory.getContext(this, `${this._baseURI}_contexts/entry/${contextId}`);
  336. }
  337. /**
  338. * Retrieves a Context instance via its entry's URI.
  339. *
  340. * @param {String} contextEntryURI - URI to the context's entry, e.g. base/_contexts/entry/1.
  341. * @returns {Context}
  342. * @see {@link EntryStore#getContextById getContextById}
  343. */
  344. getContext(contextEntryURI) {
  345. return factory.getContext(this, contextEntryURI);
  346. }
  347. /**
  348. * Retrieves a paginated list of all contexts in the EntryStore repository.
  349. * @return {List} - the list contains entries which have contexts as resources.
  350. */
  351. getContextList() {
  352. return this.newSolrQuery().graphType(types.GT_CONTEXT).list();
  353. }
  354. /**
  355. * Retrieves a paginated list of all users and groups in the EntryStore repository
  356. * @return {List} the list contains entries that have principals as resources.
  357. * @todo May include folders and other entries as well...
  358. */
  359. getPrincipalList() {
  360. return this.newSolrQuery().graphType([types.GT_USER, types.GT_GROUP]).list();
  361. }
  362. /**
  363. * Creates a new entry according to information in the provided {@link PrototypeEntry}.
  364. * The information specifies the type of entry, which context it should reside in,
  365. * initial metadata etc. This method is seldom called explicitly, instead it is called
  366. * indirectly via the {@link PrototypeEntry#commit} method. E.g.:
  367. *
  368. * context.newEntry().commit().then(function(newlyCreatedEntry) {...}
  369. *
  370. * @param {PrototypeEntry} prototypeEntry - information about the entry to create.
  371. * @return {Promise}
  372. * @see PrototypeEntry#commit
  373. * @see EntryStore#newContext
  374. * @see EntryStore#newUser
  375. * @see EntryStore#newGroup
  376. * @see Context#newEntry
  377. * @see Context#newLink
  378. * @see Context#newLinkRef
  379. * @see Context#newRef
  380. * @see Context#newList
  381. * @see Context#newGraph
  382. * @see Context#newString
  383. */
  384. async createEntry(prototypeEntry) {
  385. const postURI = factory.getEntryCreateURI(prototypeEntry, prototypeEntry.getParentList());
  386. const postParams = factory.getEntryCreatePostData(prototypeEntry);
  387. let entryURI;
  388. try {
  389. entryURI = await this.handleAsync(this._rest.create(postURI, postParams), 'createEntry');
  390. } catch (err) {
  391. return Promise.reject(err);
  392. }
  393. // var euri = factory.getURIFromCreated(data, prototypeEntry.getContext());
  394. const parentList = prototypeEntry.getParentList();
  395. if (parentList != null) {
  396. const res = parentList.getResource(true);
  397. if (res != null && res.needRefresh) {
  398. parentList.getResource(true).needRefresh();
  399. }
  400. }
  401. return this.getEntry(entryURI);
  402. }
  403. /**
  404. * Provides a PrototypeEntry for creating a new context.
  405. * @param {string=} contextName - optional name for the context, can be changed later,
  406. * must be unique in the _principals context
  407. * @param {string=} id - optional requested identifier (entryId) for the context,
  408. * cannot be changed later, must be unique in the _principals context
  409. * @returns {PrototypeEntry}
  410. */
  411. newContext(contextName, id) {
  412. const _contexts = factory.getContext(this, `${this._baseURI}_contexts/entry/_contexts`);
  413. const prototypeEntry = new PrototypeEntry(_contexts, id).setGraphType(types.GT_CONTEXT);
  414. if (contextName != null) {
  415. const ei = prototypeEntry.getEntryInfo();
  416. const resource = new Resource(ei.getEntryURI(), ei.getResourceURI(), this);
  417. resource._update({ name: contextName });
  418. prototypeEntry._resource = resource;
  419. }
  420. return prototypeEntry;
  421. }
  422. /**
  423. *
  424. * @param name
  425. * @return {Promise.<Entry>}
  426. * @async
  427. */
  428. async createGroupAndContext(name) {
  429. let uri = `${this._baseURI}_principals/groups`;
  430. if (name != null) {
  431. uri += `?name=${encodeURIComponent(name)}`;
  432. }
  433. const location = await this.handleAsync(this._rest.create(uri), 'createGroupAndContext');
  434. return this.getEntry(location);
  435. }
  436. /**
  437. * Provides a PrototypeEntry for creating a new user.
  438. * @param {string=} username - the name the user will use to authenticate himself
  439. * @param {string=} password - the password the user will use to authenticate himself
  440. * @param {string=} homeContext - a specific context the user will consider his own home
  441. * @param {string=} id - requested identifier for the user
  442. * @returns {PrototypeEntry}
  443. */
  444. newUser(username, password, homeContext, id) {
  445. const _principals = factory.getContext(this, `${this._baseURI}_contexts/entry/_principals`);
  446. const prototypeEntry = new PrototypeEntry(_principals, id).setGraphType(types.GT_USER);
  447. const entryInfo = prototypeEntry.getEntryInfo();
  448. const data = {};
  449. if (username != null) {
  450. data.name = username;
  451. }
  452. if (password != null) {
  453. data.password = password;
  454. }
  455. if (homeContext != null) {
  456. data.homecontext = homeContext;
  457. }
  458. prototypeEntry._resource = new User(entryInfo.getEntryURI(), entryInfo.getResourceURI(), this, data);
  459. return prototypeEntry;
  460. }
  461. /**
  462. * @param {string=} groupName - optional name for the group, can be changed later,
  463. * must be unique in the _principals context
  464. * @param {string=} id - optional requested identifier (entryId) for the group,
  465. * cannot be changed later, must be unique in the _principals context
  466. * @returns {PrototypeEntry}
  467. */
  468. newGroup(groupName, id) {
  469. const _principals = factory.getContext(this, `${this._baseURI}_contexts/entry/_principals`);
  470. const prototypeEntry = new PrototypeEntry(_principals, id).setGraphType(types.GT_GROUP);
  471. if (groupName != null) {
  472. const ei = prototypeEntry.getEntryInfo();
  473. const resource = new Resource(ei.getEntryURI(), ei.getResourceURI(), this);
  474. resource._update({ name: groupName });
  475. prototypeEntry._resource = resource;
  476. }
  477. return prototypeEntry;
  478. }
  479. /**
  480. * Move an entry from one list to another.
  481. *
  482. * @param {Entry} entry - entry to move
  483. * @param {Entry} fromList - source list where the entry is currently residing.
  484. * @param {Entry} toList - destination list where the entry is supposed to end up.
  485. * @returns {Promise}
  486. */
  487. moveEntry(entry, fromList, toList) {
  488. const uri = factory.getMoveURI(entry, fromList, toList, this._baseURI);
  489. return this.handleAsync(this.getREST().post(uri, ''), 'moveEntry');
  490. }
  491. /**
  492. * Loads data via the EntryStore repository's own proxy.
  493. *
  494. * @param {string} uri indicates the resource to load.
  495. * @param {string} formatHint indicates that you want data back in the format specified
  496. * (e.g. by specifiying a suitable accept header).
  497. * @returns {Promise}
  498. */
  499. loadViaProxy(uri, formatHint) {
  500. const url = factory.getProxyURI(this._baseURI, uri);
  501. return this.handleAsync(this.getREST().get(url, formatHint, true), 'loadViaProxy');
  502. }
  503. /**
  504. * Performing searches against an EntryStore repository is achieved by creating a
  505. * {@link SearchList} which is similar to a regular {@link List}.
  506. * From this list it is possible to get paginated results in form of matching entries.
  507. * For example:
  508. *
  509. * var personType = "http://xmlns.com/foaf/0.1/Person";
  510. * var searchList = entrystore.newSolrQuery().rdfType(personType).list();
  511. * searchList.setLimit(20).getEntries().then(function(results) {...});
  512. *
  513. * @returns {SolrQuery}
  514. */
  515. newSolrQuery() {
  516. return new SolrQuery(this);
  517. }
  518. /**
  519. * @deprecated use {@link #newSolrQuery} instead.
  520. */
  521. createSearchList(query) {
  522. return factory.createSearchList(this, query);
  523. }
  524. /**
  525. * Constructs an metadata URI from the id for the context and the specific entry.
  526. * @param {string} contextId - an identifier for the context the entry belongs to
  527. * @param {string} entryId - an identifier for the entry
  528. * @returns {String} - an entry URI
  529. */
  530. getMetadataURI(contextId, entryId) {
  531. return factory.getMetadataURI(this, contextId, entryId);
  532. }
  533. /**
  534. * Constructs an entry URI from the id for the context and the specific entry.
  535. * @param {string} contextId - an identifier for the context the entry belongs to
  536. * @param {string} entryId - an identifier for the entry
  537. * @returns {String} - an entry URI
  538. */
  539. getEntryURI(contextId, entryId) {
  540. return factory.getEntryURI(this, contextId, entryId);
  541. }
  542. /**
  543. * Constructs an entry URI from a normal repository URI, e.g. any URI from which is possible
  544. * to deduce a contextId and an entryId. Equivalent to calling:
  545. * es.getEntryURI(es.getContextId(uri), es.getEntryId(uri))
  546. *
  547. * @param {string} uri - a URI for the entry, can be a entryURI (obviously), resourceURI
  548. * (if local), metadataURI, or relationsURI.
  549. * @returns {String} - an entry URI
  550. */
  551. getEntryURIFromURI(uri) {
  552. return factory.getEntryURIFromURI(this, uri);
  553. }
  554. /**
  555. * Constructs an entry resource URI (local URI, not a link obviously) from the id for the
  556. * context and the specific entry.
  557. *
  558. * @param {string} contextId - an identifier for the context the resource belongs to
  559. * @param {string} entryId - an identifier for the entry the resource belongs to
  560. * @returns {String} a resource URI
  561. */
  562. getResourceURI(contextId, entryId) {
  563. return factory.getResourceURI(this, contextId, entryId);
  564. }
  565. /**
  566. * The base URI of the EntryStore repository we have connected to.
  567. *
  568. * @returns {String}
  569. */
  570. getBaseURI() {
  571. return this._baseURI;
  572. }
  573. /**
  574. * The entry id of this entry, resource or metadata uri.
  575. *
  576. * @param {string} uri
  577. * @returns {string}
  578. */
  579. getEntryId(uri) {
  580. return factory.getEntryId(uri, this.getBaseURI());
  581. }
  582. /**
  583. * The context id of this entry, resource or metadata uri.
  584. *
  585. * @param {string} uri
  586. * @returns {string}
  587. */
  588. getContextId(uri) {
  589. return factory.getContextId(uri, this.getBaseURI());
  590. }
  591. /**
  592. * To get status resource
  593. *
  594. * @returns {Promise}
  595. */
  596. getStatus() {
  597. const uri = `${this._baseURI}management/status?extended`;
  598. return this.handleAsync(this.getREST().get(uri));
  599. }
  600. /**
  601. * The cache where all entries are cached after loading.
  602. *
  603. * @returns {Cache}
  604. */
  605. getCache() {
  606. return this._cache;
  607. }
  608. /**
  609. * The loading mechanism are performed via REST calls, this REST module can be
  610. * used for doing manual lookups outside of the scope of this API.
  611. *
  612. * @returns {Rest}
  613. */
  614. getREST() {
  615. return this._rest;
  616. }
  617. /**
  618. * Requests to EntryStore instances may be cached to increase performance, by setting the requestCachePrevention to
  619. * true that cache is circumvented by appending unique parameters to the URI.
  620. * By default the requestCachePrevention is disabled.
  621. *
  622. * @param {boolean} prevent pass true to enable the prevention.
  623. */
  624. setRequestCachePrevention(prevent) {
  625. this._requestCache = prevent;
  626. }
  627. /**
  628. * Weather the cache of requests made to EntryStore instances are circumvented by appending a random parameter.
  629. * @return {boolean}
  630. */
  631. getRequestCachePrevention() {
  632. return this._requestCache;
  633. }
  634. //= =============Non-public methods==============
  635. /**
  636. * @returns {Object}
  637. */
  638. getCachedContextsIdx() {
  639. return this._contexts;
  640. }
  641. /**
  642. * Provides information about version of EntryStore repository, the javascript API,
  643. * status of services etc.
  644. * @todo Needs support from EntryStore REST API
  645. * @todo Document promise
  646. */
  647. static info() {
  648. const packageJSON = require('../package.json');
  649. return { version: packageJSON.version };
  650. }
  651. };