/**
* Copyright© 2016, Oracle and/or its affiliates. All rights reserved.
*/
/**
* Class that provides cloud-based storage capabilities. Callers should use
* MobileBackend's [Storage()]{@link MobileBackend#Storage} property.
* @constructor
* @global
*/
function Storage(backend, utils, platform, logger) {
var _backend = backend;
var storage = this;
/**
* Callback invoked after successfully fetching a StorageCollection.
* @callback Storage~getCollectionSuccessCallback
* @param storageCollection {StorageCollection} The downloaded StorageCollection instance.
* @deprecated Use promises instead
*/
/**
* Callback invoked on error.
* @callback Storage~errorCallback
* @param statusCode {Number} Any HTTP status code returned from the server, if available.
* @param message {String} The HTTP payload from the server, if available, or an error message.
* @deprecated Use promises instead
*/
/**
* Returns a StorageCollection with the given name from the service associated with the user. Subsequent accesses to StorageObjects in the
* StorageCollection will only return StorageObjects owned by the user.
* @param name {String} The name of the StorageCollection.
* @example name: "JSCollection"
* @param userId {String} Optional, the ID of the user retrieved from the UI.
* @example userId: "e8671189-585d-478e-b437-005b7632b8803"
* @param [userIsolated] {Boolean} - indicate if collection is in isolated mode, used in combination with lazyLoad and userId.
* This parameter is not required in case lazyLoad is not provided.
* @param [lazyLoad] {Boolean} - indicate not to load collection metadata
* @param [successCallback] {Storage~getCollectionSuccessCallback} Callback invoked on success (deprecated use promises instead).
* @param [errorCallback] {Storage~errorCallback} Callback invoked on error (deprecated use promises instead).
* @return {Promise.<StorageCollection|NetworkResponse>}
*/
this.getCollection = function(name, userId, userIsolated, lazyLoad, successCallback, errorCallback) {
// TODO: remove when callbacks are removed
if(typeof userIsolated === 'function'){
errorCallback = lazyLoad;
successCallback = userIsolated;
lazyLoad = false;
userIsolated = undefined;
}
var collection = new StorageCollection(name, utils.validateConfiguration(userId), userIsolated, _backend, utils, logger, platform);
if(lazyLoad){
return Promise.resolve(collection).then(invokeServiceSuccess, invokeServiceError);
} else {
return collection.loadMetadata().then(invokeServiceSuccess, invokeServiceError);
}
function invokeServiceSuccess(collection) {
if(successCallback) {
successCallback(collection);
}
return collection;
}
function invokeServiceError(response) {
if(errorCallback) {
errorCallback(response.statusCode, response.data);
} else {
return Promise.reject(response);
}
}
};
}