(function() { 'use strict'; var HTTP_ERRORS = { 401: "Unathorized action", 418: "Application error", 403: "Forbidden action", 500: "Server error", 404: "Element not found" } var app = angular.module('securis'); app.service('Packs', ['$L','$resource', '$http', 'toaster', function($L, $resource, $http, toaster) { var PACK_STATUS = { CREATED: 'CR', ACTIVE: 'AC', ONHOLD: 'OH', EXPIRED: 'EX', CANCELLED: 'CA' } var PACK_STATUSES = { 'CR': $L.get('Created'), 'AC': $L.get('Active'), 'OH': $L.get('On Hold'), 'EX': $L.get('Expired'), 'CA': $L.get('Cancelled') }; /** * These transitions could be get from server, class Pack.Status, but we * copy them for simplicity, this info won't change easily */ var PACK_ACTIONS_BY_STATUS = { edit: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD, PACK_STATUS.ACTIVE], activate: [PACK_STATUS.CREATED, PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD], putonhold: [PACK_STATUS.ACTIVE], cancel: [PACK_STATUS.EXPIRED, PACK_STATUS.ONHOLD, PACK_STATUS.ACTIVE], 'delete': [PACK_STATUS.CREATED, PACK_STATUS.CANCELLED] } var packResource = $resource('pack/:packId/:action', { packId : '@id', action : '@action' }, { activate: { method: "POST", params: {action: "activate"} }, putonhold: { method: "POST", params: {action: "putonhold"} }, cancel: { method: "POST", params: {action: "cancel"} } } ); this.getStatusColor = function(status) { var COLORS_BY_STATUS = { 'CR': '#808080', 'AC': '#329e5a', 'OH': '#9047c7', 'EX': '#ea7824', 'CA': '#a21717' }; return COLORS_BY_STATUS[status]; }, this.getStatusName = function(status) { return PACK_STATUSES[status]; } this.savePackData = function(pack, isNew, _onsuccess) { var _success = function() { _onsuccess(); toaster.pop('success', 'Packs', $L.get("Pack '{0}' {1} successfully", pack.code, isNew ? $L.get("created") : $L.get("updated"))); } var _error = function(error) { console.log(error); toaster.pop('error', 'Packs', $L.get("Error {0} pack '{1}'. Reason: {2}", isNew ? $L.get("creating") : $L.get("updating"), pack.code, $L.get(error.headers('X-SECURIS-ERROR-MSG')))); } packResource.save(pack, _success, _error); } this.isActionAvailable = function(action, pack) { var validStatuses = PACK_ACTIONS_BY_STATUS[action]; return pack && validStatuses && validStatuses.indexOf(pack.status) !== -1; } var _createSuccessCallback = function(actionName, message, _innerCallback) { return function() { _innerCallback && _innerCallback(); toaster.pop('success', actionName, message); } } var _createErrorCallback = function(pack, actionName, _innerCallback) { return function(error) { console.log(error); _innerCallback && _innerCallback(); toaster.pop('error', actionName, $L.get("Error on action '{0}', pack '{1}'. Reason: {2}", actionName, pack.code, $L.get(error.headers('X-SECURIS-ERROR-MSG')))); } } this.getPacksList = function(_onsuccess, _onerror) { var query = packResource.query(); query.$promise.then(_onsuccess, _onerror); return query; } this.activate = function(pack, _onsuccess, _onerror) { console.log('Activation on pack: ' + pack.id); var _success = _createSuccessCallback($L.get('Activation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("activated")), _onsuccess); var _error = _createErrorCallback(pack, $L.get('Activation'), _onerror); packResource.activate({id: pack.id}, _success, _error); } this.putonhold = function(pack, _onsuccess, _onerror) { console.log('Put on hold on pack: ' + pack.id); var _success = _createSuccessCallback($L.get('Put on hold'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("put on hold")), _onsuccess); var _error = _createErrorCallback(pack, $L.get('Put on hold'), _onerror); packResource.putonhold({id: pack.id}, _success, _error); } this.nextliccode = function(packId, _onsuccess, _onerror) { console.log('Get next code: ' + packId); var _error = function(data, status, headers, config) { console.log(headers); toaster.pop('error', $L.get('Getting next code suffix'), $L.get("Error getting license code, pack ID: '{0}'. Reason: {1}", packId, $L.get(headers('X-SECURIS-ERROR-MSG')))); } $http.get("/securis/pack/"+packId+"/next_license_code").success(_onsuccess).error(_error); } this.cancel = function(pack, extra_data, _onsuccess, _onerror) { console.log('Cancellation on pack: ' + pack.id); var _success = _createSuccessCallback($L.get('Cancellation'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("cancelled")), _onsuccess); var _error = _createErrorCallback(pack, $L.get('Cancellation'), _onerror); var params = angular.extend({id: pack.id}, extra_data); packResource.cancel(params, _success, _error); } this.delete = function(pack, _onsuccess, _onerror) { console.log('Delete on pack: ' + pack.id); var _success = _createSuccessCallback($L.get('Deletion'), $L.get("Pack '{0}' {1} successfully", pack.code, $L.get("deleted")), _onsuccess); var _error = _createErrorCallback(pack, $L.get('Deletion'), _onerror); packResource.delete({packId: pack.id}, _success, _error); } }]); app.service('Licenses', ['$L', '$resource', 'toaster', 'Packs', function($L, $resource, toaster, Packs) { var LIC_STATUS = { CREATED: 'CR', ACTIVE: 'AC', REQUESTED: 'RE', PREACTIVE: 'PA', EXPIRED: 'EX', BLOCKED: 'BL', CANCELLED: 'CA' } var LIC_STATUSES = { 'CR': $L.get('Created'), 'AC': $L.get('Active'), 'PA': $L.get('Pre-active'), 'RE': $L.get('Requested'), 'EX': $L.get('Expired'), 'BL': $L.get('Blocked'), 'CA': $L.get('Cancelled') }; /** * These transitions could be get from server, class License.Status, but * we copy them for simplicity, this info won't change easily */ var LIC_ACTIONS_BY_STATUS = { edit: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE, LIC_STATUS.CANCELLED, LIC_STATUS.BLOCKED], add_request: [LIC_STATUS.CREATED], activate: [LIC_STATUS.CREATED, LIC_STATUS.REQUESTED, LIC_STATUS.PREACTIVE], send: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], download: [LIC_STATUS.ACTIVE, LIC_STATUS.PREACTIVE], block: [LIC_STATUS.CANCELLED], unblock: [LIC_STATUS.BLOCKED], cancel: [LIC_STATUS.REQUESTED, LIC_STATUS.EXPIRED, LIC_STATUS.PREACTIVE, LIC_STATUS.ACTIVE], 'delete': [LIC_STATUS.CREATED, LIC_STATUS.CANCELLED, LIC_STATUS.BLOCKED] } var licenseResource = $resource('license/:licenseId/:action', { licenseId : '@id', action : '@action' }, { activate: { method: "POST", params: {action: "activate"} }, cancel: { method: "POST", params: {action: "cancel"} }, // Download a file cannot be done form AJAX, We should do it // manually, using $http download: { method: "GET", params: {action: "download"} }, block: { method: "POST", params: {action: "block"} }, sendEmail: { method: "POST", params: {action: "send"} }, unblock: { method: "POST", params: {action: "unblock"} } }); this.isActionAvailable = function(action, lic) { var validStatuses = LIC_ACTIONS_BY_STATUS[action]; return lic && validStatuses && validStatuses.indexOf(lic.status) !== -1; } this.getStatusColor = function(status) { var COLORS_BY_STATUS = { 'CR': '#808080', 'AC': '#329e5a', 'RE': '#2981d4', 'EX': '#ea7824', 'BL': '#ff0000', 'CA': '#a21717' }; return COLORS_BY_STATUS[status]; }, this.getStatusName = function(status) { return LIC_STATUSES[status]; } this.saveLicenseData = function(license, isNew, _onsuccess) { var _success = function() { _onsuccess(); toaster.pop('success', 'Licenses', $L.get("License '{0}' {1} successfully", license.code, isNew ? $L.get("created") : $L.get("updated"))); } var _error = function(error) { console.log(error); toaster.pop('error', 'Licenses', $L.get("Error {0} license '{1}'. Reason: {2}", isNew ? $L.get("creating") : $L.get("updating"), license.code, $L.get(error.headers('X-SECURIS-ERROR-MSG'))), 5000); if (error.headers('X-SECURIS-ERROR-CODE') === '1301') { Packs.nextliccode(license.pack_id, function(data) { if (license.code !== data) { // Only if the new code is different we can think about an erro related with License CODE license.code = data; toaster.pop('info', 'Licenses', $L.get("New license code, {0}, has been generated, please try again", license.code), 5000); } }); } } licenseResource.save(license, _success, _error); } var _createSuccessCallback = function(actionName, message, _innerCallback) { return function() { _innerCallback && _innerCallback(); toaster.pop('success', actionName, message); } } var _createErrorCallback = function(license, actionName, _innerCallback) { return function(error) { console.log(error); _innerCallback && _innerCallback(); toaster.pop('error', actionName, $L.get("Error on action '{0}', license '{1}'. Reason: {2}", actionName, license.code, $L.get(error.headers('X-SECURIS-ERROR-MSG')))); } } this.getLicensesList = function(pack, _onsuccess, _onerror) { var query = licenseResource.query({packId: pack.id}); query.$promise.then(_onsuccess, _onerror); return query; } this.activate = function(license, _onsuccess, _onerror) { console.log('Activation on license: ' + license.id); var _success = _createSuccessCallback($L.get('Activation'), $L.get("License '{0}' {1} successfully", license.code, $L.get("activated")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Activation'), _onerror); licenseResource.activate({id: license.id}, _success, _error); } this.block = function(license, _onsuccess, _onerror) { console.log('Block on license: ' + license.id); var _success = _createSuccessCallback($L.get('Block'), $L.get("License '{0}' {1} successfully", license.code, $L.get("blocked")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Block'), _onerror); licenseResource.block({id: license.id}, _success, _error); } this.unblock = function(license, _onsuccess, _onerror) { console.log('Unblock on license: ' + license.id); var _success = _createSuccessCallback($L.get('Unblock'), $L.get("License '{0}' {1} successfully", license.code, $L.get("unblocked")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Unblock'), _onerror); licenseResource.unblock({id: license.id}, _success, _error); } this.send = function(license, _onsuccess, _onerror) { console.log('Sending email: ' + license.id); var _success = _createSuccessCallback($L.get('Send email'), $L.get("License '{0}' was sent by email ({1}) successfully", license.code, license.email), _onsuccess); var _error = _createErrorCallback(license, $L.get('Send email'), _onerror); licenseResource.sendEmail({id: license.id}, _success, _error); } this.download = function(license, _onsuccess, _onerror) { console.log('Download license: ' + license.id); var _success = _createSuccessCallback($L.get('Download'), $L.get("License '{0}' {1} successfully", license.code, $L.get("downloaded")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Download license file'), _onerror); // window.open(downloadPath, '_blank', ''); var _success2 = function(data, headers) { // console.log(headers.get("Content-Disposition")); // attachment; filename="license.lic" var filename = JSON.parse(headers('Content-Disposition').match(/".*"$/g)[0]); data.$promise.then(function(content) { saveAs( new Blob([ JSON.stringify(content, null, 2) ], { type : 'application/octet-stream' }), filename); }); _success(); }; licenseResource.download({licenseId: license.id}, _success2, _error); } this.cancel = function(license, extra_data, _onsuccess, _onerror) { console.log('Cancellation on license: ' + license.id); var _success = _createSuccessCallback($L.get('Cancellation'), $L.get("License '{0}' {1} successfully", license.code, $L.get("cancelled")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Cancellation'), _onerror); var params = angular.extend({id: license.id}, extra_data); licenseResource.cancel(params, _success, _error); } this.delete = function(license, _onsuccess, _onerror) { console.log('Delete on license: ' + license.id); var _success = _createSuccessCallback($L.get('Deletion'), $L.get("License '{0}' {1} successfully", license.code, $L.get("deleted")), _onsuccess); var _error = _createErrorCallback(license, $L.get('Deletion'), _onerror); licenseResource.delete({licenseId: license.id}, _success, _error); } }]); app.directive('fileLoader', function($timeout, $parse) { return { restrict : 'A', // only activate on element attribute require : '', link : function(scope, element, attrs) { console.log('scope.license: ' + scope.$parent.license); var setter = $parse(attrs.fileLoader).assign; element.bind('change', function(evt) { if (!window.FileReader) { // Browser is not // compatible BootstrapDialog.alert($L.get("Open your .req file with a text editor and copy&paste the content in the form text field?")); return; } console.log('File selected'); // console.log('scope.license: ' + // scope.$parent.license); var field = $parse(attrs.fileLoader); // console.log('field: ' + field); var fileList = evt.target.files; if (fileList != null && fileList[0]) { var reader = new FileReader(); reader.onerror = function(data) { setter(scope.$parent, 'ERROR'); scope.$apply(); } reader.onload = function(data) { setter(scope.$parent, reader.result); scope.$apply(); } reader.readAsText(fileList[0]); element.val(''); } else { setter(scope.$parent, ''); scope.$apply(); } }); } }; }); app.controller('PackAndLicensesCtrl', [ '$scope', '$http', 'toaster', '$store', '$L', function($scope, $http, toaster, $store, $L) { $store.set('location', '/licenses'); $scope.maxLengthErrorMsg = function(displayname, fieldMaxlength) { return $L.get("{0} length is too long (max: {1}).", $L.get(displayname), fieldMaxlength); } $scope.mandatoryFieldErrorMsg = function(displayname) { return $L.get("'{0}' is required.", $L.get(displayname)); } $scope.field1ShouldBeGreaterThanField2 = function(field1, field2) { return $L.get("{0} should be greater than {1}", $L.get(field1), $L.get(field2)); } $scope.ellipsis = function(txt, len) { if (!txt || txt.length <= len) return txt; return txt.substring(0, len) + '...'; } $scope.currentPack = $store.get('currentPack'); if ($scope.currentPack) { $scope.currentPack._selected = true; setTimeout(function() { $scope.$broadcast('pack_changed', $scope.currentPack); }, 0); } }]); app.controller('PacksCtrl', [ '$scope', '$http', '$resource', 'toaster', 'Catalogs', 'Packs', '$store', '$L', function($scope, $http, $resource, toaster, Catalogs, Packs, $store, $L) { $scope.Packs = Packs; $scope.mandatory = { code: true, num_licenses: true, init_valid_date: true, end_valid_date: true, status: true, organization_id: true, license_type_id: true } $scope.maxlength = { code: 50, comments: 1024 } $scope.refs = {}; Catalogs.init().then(function() { var refFields = [{resource: 'organization', name: 'organization_id'},{resource: 'licensetype', name: 'license_type_id'}]; Catalogs.loadRefs(function(refs) { $scope.refs = refs; $scope._extendPackListing($scope.packs); }, refFields); }); // Used to create the form with the // appropriate data $scope.isNew = undefined; // Selected pack from listing // pack is the edited pack, in creation // contains the data for // the new pack $scope.pack = null; $scope.packs = Packs.getPacksList(function(list) { $scope._extendPackListing(list); }); /** * Added calculated fields like org_code and lic_type_code to pack listing */ $scope._extendPackListing = function(listing) { angular.forEach(listing, function(elem) { elem.organization_code = $scope.getOrganizationCode(elem.organization_id); elem.license_type_code = $scope.getLicenseTypeCode(elem.license_type_id); }); } $scope.save = function() { Packs.savePackData($scope.pack, $scope.isNew, function() { if (!$scope.isNew) { $scope.showForm = false; } else { $scope.newPack(); } $scope.packs = Packs.getPacksList(function(list) { $scope._extendPackListing(list); }); }); } /** * Execute an action over the pack, * activation, onhold, cancellation */ $scope.execute = function(action, pack) { var _execute = function(extra_data) { if (extra_data) { Packs[action](pack || $scope.pack, extra_data, function() { if (!$scope.isNew) $scope.showForm = false; $scope.packs = Packs.getPacksList(function(list) { $scope._extendPackListing(list); }); }); } else { Packs[action](pack || $scope.pack, function() { if (!$scope.isNew) $scope.showForm = false; $scope.packs = Packs.getPacksList(function(list) { $scope._extendPackListing(list); }); }); } } if (action === 'delete') { BootstrapDialog.confirm($L.get("The pack '{0}' will be deleted, are you sure ?", pack.code), function(answer) { if (answer) { _execute(); } }); } else { if (action === 'cancel') { BootstrapDialog.show({ title: $L.get("Pack cancellation"), type: BootstrapDialog.TYPE_DANGER, message: function(dialog) { var $content = $('
'); var $message = $('
'); $message.append($('