(function () { 'use strict'; //provides functionality that interacts with server side RESTful endpoints var serviceId = 'myFormsDataContext'; angular.module('myforms').factory(serviceId, ['$q', '$http', 'myFormsConfig', 'DSCacheFactory', datacontext]); function datacontext($q, $http, config, DSCacheFactory) { var currentForm; var currentActivityEntries; var userFormTemplatesCache = 'userFormTemplatesCache'; var userActivityFormTemplatesCache = 'userActivityFormTemplatesCache'; var adminActivityFormTemplatesCache = 'adminActivityFormTemplatesCache'; createUserFormTemplatesCache(); var service = { getForms: getForms, getForm: getForm, createForm: createForm, editForm: editForm, lockForm: lockForm, unlockForm: unlockForm, deleteForm: deleteForm, getAvailableTemplates: getAvailableTemplates, getAvailableActivityTemplates: getAvailableActivityTemplates, getAvailableTemplatesToCreateForms: getAvailableTemplatesToCreateForms, getAvailableTemplatesToViewEntries: getAvailableTemplatesToViewEntries, getPublicFormTemplates: getPublicFormTemplates, getFormTemplate: getFormTemplate, getCurrentForm: getCurrentForm, setCurrentForm: setCurrentForm, setCurrentActivityEntries: setCurrentActivityEntries, getCurrentActivityEntries: getCurrentActivityEntries, clearAvailableTemplatesCache: clearAvailableTemplatesCache, getAllAvailableTemplates: getAllAvailableTemplates, getNewScoringBand: getNewScoringBand, getActivityEntries: getActivityEntries }; return service; function createUserFormTemplatesCache() { var cache = DSCacheFactory(userFormTemplatesCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 30000, deleteOnExpire: 'aggressive' }); return cache; } function createUserActivityFormTemplatesCache() { var cache = DSCacheFactory(userActivityFormTemplatesCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 30000, deleteOnExpire: 'aggressive' }); return cache; } function createAdminActivityFormTemplatesCache() { var cache = DSCacheFactory(adminActivityFormTemplatesCache, { storageMode: 'localStorage' }); cache.setOptions({ maxAge: 30000, deleteOnExpire: 'aggressive' }); return cache; } function getCurrentForm() { return currentForm; } function setCurrentForm(form) { currentForm = form; } function getCurrentActivityEntries() { return currentActivityEntries; } function setCurrentActivityEntries(entries) { currentActivityEntries = entries; } function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if ( !angular.isObject(response.data) || !response.data.message ) { return ($q.reject("An unknown error occurred.")); } // Otherwise, use expected error message. return ($q.reject(response.data.message)); } // I transform the successful response, unwrapping the application data // from the API response payload. function handleSuccess(response) { return (response.data); } //get all my forms function getForms() { var request = $http({ method: "get", url: config.formRemoteServiceUrl + '?cache=' + Date.now() }); return (request.then(function (response) { return response.data; }, handleError)); } //get a specific form function getForm(formId) { var request = $http({ method: "get", url: config.formRemoteServiceUrl + formId + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } //create a new form function createForm(formModel) { var request = $http({ method: "post", url: config.formRemoteServiceUrl, data: formModel }); clearAvailableTemplatesCache(); return (request.then(handleSuccess, handleError)); } //edit an existing form function editForm(formId, formModel) { var request = $http({ method: "put", url: config.formRemoteServiceUrl + formId, data: formModel }); clearAvailableTemplatesCache(); return (request.then(handleSuccess, handleError)); } //lock a form function lockForm(formId) { var request = $http({ method: "get", url: config.formRemoteServiceUrl + formId + "/lock" }); return (request.then(handleSuccess, handleError)); } //unlock a form function unlockForm(formId) { var request = $http({ method: "get", url: config.formRemoteServiceUrl + formId + "/unlock" }); return (request.then(handleSuccess, handleError)); } //delete a form function deleteForm(formId) { var request = $http({ method: "delete", url: config.formRemoteServiceUrl + formId }); return (request.then(handleSuccess, handleError)); } //clear the local cache for available form templates function clearAvailableTemplatesCache() { var cache = DSCacheFactory.get(userFormTemplatesCache); if (cache) cache.removeAll(); } //get available template for a set of organisation ids function getAvailableTemplates(externalId) { var cache = DSCacheFactory.get(userFormTemplatesCache); if (cache) { var cacheTemplates = cache.get('mytemplates'); if (cacheTemplates) { var deferred = $q.defer(); deferred.resolve(cacheTemplates); return deferred.promise; } } else cache = createUserFormTemplatesCache(); var request = $http({ method: "get", url: config.formTemplateRemoteServiceUrl + externalId + '/allavailable' + '?cache=' + Date.now() }); return (request.then(function (response) { cache.put('mytemplates', response.data); return response.data; }, handleError)); } function getAvailableActivityTemplates() { var request = $http({ method: "get", url: config.formTemplateRemoteServiceUrl + '/activities/allavailable' + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } function getAvailableTemplatesToCreateForms() { var request = $http({ method: "get", url: config.formTemplateManagementRemoteServiceUrl + '/activities/allavailabletocreate' + '?cache=' + Date.now() }); return (request.then(function (response) { return response.data; }, handleError)); } function getAvailableTemplatesToViewEntries() { var request = $http({ method: "get", url: config.formTemplateManagementRemoteServiceUrl + '/activities/allavailabletoviewentries' + '?cache=' + Date.now() }); return (request.then(function (response) { return response.data; }, handleError)); } function getActivityEntries(activityFormId) { var request = $http({ method: "get", url: config.formRemoteServiceUrl + 'activities/' + activityFormId + '/entries' + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } function getAllAvailableTemplates() { var cache = DSCacheFactory.get(userFormTemplatesCache); if (cache) { var cacheTemplates = cache.get('mytemplates'); if (cacheTemplates) { var deferred = $q.defer(); deferred.resolve(cacheTemplates); return deferred.promise; } } else cache = createUserFormTemplatesCache(); var request = $http({ method: "get", url: config.formTemplateRemoteServiceUrl + '/allavailable' + '?cache=' + Date.now() }); return (request.then(function (response) { cache.put('mytemplates', response.data); return response.data; }, handleError)); } //get all publically available form templates function getPublicFormTemplates() { var request = $http({ method: "get", url: config.formTemplateRemoteServiceUrl + 'public' + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } //get a specific form template function getFormTemplate(formTemplateId) { var request = $http({ method: "get", url: config.formTemplateRemoteServiceUrl + formTemplateId + '?cache=' + Date.now() }); return (request.then(handleSuccess, handleError)); } //get a new scoring band based on the max range and existing scoring bands function getNewScoringBand(scoring, max) { var request = $http({ method: "post", data: { bands: scoring, maxRange: max, }, url: config.formTemplateManagementRemoteServiceUrl + "scoringbands" }); return (request.then(handleSuccess, handleError)); } } })();