From 04afd774aecc70dca37559fdd8b9a716829c18cd Mon Sep 17 00:00:00 2001
From: Roberto Sánchez <roberto.sanchez@curisit.net>
Date: Fri, 17 Jan 2014 12:27:16 +0000
Subject: [PATCH] #396 feature - Added LocalStorage support and http interceptor for unauthorized access

---
 securis/src/main/resources/static/js/catalogs.js |  473 ++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 279 insertions(+), 194 deletions(-)

diff --git a/securis/src/main/resources/static/js/catalogs.js b/securis/src/main/resources/static/js/catalogs.js
index 77ad975..7dc6614 100644
--- a/securis/src/main/resources/static/js/catalogs.js
+++ b/securis/src/main/resources/static/js/catalogs.js
@@ -5,209 +5,294 @@
 	 * Catalogs module
 	 */
 
-	angular.module('catalogs', ['ngResource'])
+	angular
+			.module('catalogs', [ 'ngResource' ])
 
-	.service('Catalogs', ['$rootScope', '$http', '$resource', '$q', function ($rootScope, $http, $resource, $q) {
-		var resources = {
-				application : $resource('/application/:appId', {
-					appId : '@id'
-				}),
-				user : $resource('/user/:userId', {
-					userId : '@username'
-				}),
-				organization : $resource('/organization/:orgId', {
-					orgId : '@id'
-				}),
-				licensetype : $resource('/licensetype/:licenseTypeId', {
-					licenseTypeId : '@id'
-				})
-		}
+			.service(
+					'Catalogs',
+					[
+							'$rootScope',
+							'$http',
+							'$resource',
+							'$q',
+							function($rootScope, $http, $resource, $q) {
+								var resources = {
+									application : $resource(
+											'/application/:appId', {
+												appId : '@id'
+											}),
+									user : $resource('/user/:userId', {
+										userId : '@username'
+									}),
+									organization : $resource(
+											'/organization/:orgId', {
+												orgId : '@id'
+											}),
+									licensetype : $resource(
+											'/licensetype/:licenseTypeId', {
+												licenseTypeId : '@id'
+											})
+								}
 
-	var _metadata = null;
-	var _current = null;
+								var _metadata = null;
+								var _current = null;
 
-	var _list = function() {
-		return $http.get('/js/catalogs.json').success(function(data) {
-			_metadata = data;
-		})
-	}
-	this.init = function() {
-		return _list();
-	}
-	this.getList = function() {
-		return _metadata;
-	}
-	this.getName = function(index) {
-		if (index === undefined)
-			return _current ? _current.name : '';
-		return _metadata ? _metadata[index].name : '';
-	}
-	this.getResource = function(res) {
-		if (res === undefined)
-			return _current ? resources[_current.resource] : null;
-		return _current ? resources[res] : null;		
-	}
-	this.getPk = function(catalogMetadata) {
-		if (!catalogMetadata) catalogMetadata = _current;
-		
-		for(var i = 0; i < catalogMetadata.fields.length; i++)
-			if (catalogMetadata.fields[i].pk) return catalogMetadata.fields[i].name; 
-			
-		return null;		
-	}
-	/**
-	 * Returns catalog metadata
-	 * @param index: Return current catalog if undefined, if string It find the catalog by resoource name if number it find it by position
-	 */
-	this.getMetadata = function(index) {
-		if (!_metadata) throw new Error('There is no catalog metadata info');
-		if (index === undefined)
-			return _current;
-		if (typeof index === 'string') {
-			for (var i = _metadata.length - 1; i >= 0 && _metadata[i].resource !== index; i--);
-			index = i;
-		} 
+								var _list = function() {
+									return $http.get('/js/catalogs.json')
+											.success(function(data) {
+												_metadata = data;
+											})
+								}
+								this.init = function() {
+									return _list();
+								}
+								this.getList = function() {
+									return _metadata;
+								}
+								this.getName = function(index) {
+									if (index === undefined)
+										return _current ? _current.name : '';
+									return _metadata ? _metadata[index].name
+											: '';
+								}
+								this.getResource = function(res) {
+									if (res === undefined)
+										return _current ? resources[_current.resource]
+												: null;
+									return _current ? resources[res] : null;
+								}
+								this.getPk = function(catalogMetadata) {
+									if (!catalogMetadata)
+										catalogMetadata = _current;
 
-		return _metadata[index];
-	}
-	this.setCurrent = function(index) {
-		if (!_metadata) throw new Error('There is no catalog metadata info');
-		if (index === undefined)
-			_current = null;
-		else
-			_current = _metadata[index];
-	}
-	/********************************************
-	 * Catalog fields methods                   *
-	 ********************************************/
+									for (var i = 0; i < catalogMetadata.fields.length; i++)
+										if (catalogMetadata.fields[i].pk)
+											return catalogMetadata.fields[i].name;
 
-	/**
-	 * Returns the first field in form that should get the focus. We find the first field that is not read only 
-	 */
-	this.getFFF = this.getFirstFocusableField = function() {
-		if (!_current) throw new Error('There is no current catalog selected');
-		
-		for(var i = 0; i < _current.fields.length; i++)
-			if (!_current.fields[i].readOnly) return _current.fields[i].name;
-		
-		return null;
-	}
+									return null;
+								}
+								/**
+								 * Returns catalog metadata
+								 * 
+								 * @param index:
+								 *            Return current catalog if
+								 *            undefined, if string It find the
+								 *            catalog by resoource name if
+								 *            number it find it by position
+								 */
+								this.getMetadata = function(index) {
+									if (!_metadata)
+										throw new Error(
+												'There is no catalog metadata info');
+									if (index === undefined)
+										return _current;
+									if (typeof index === 'string') {
+										for (var i = _metadata.length - 1; i >= 0
+												&& _metadata[i].resource !== index; i--)
+											;
+										index = i;
+									}
 
-	/**
-	 * Find the field by name or position 
-	 */
-	this.getField = function(key) {
-		if (!_current) throw new Error('There is no current catalog selected');
-		var index = -1;
-		if (typeof key === 'string') {
-			for (var i = _current.fields.length - 1; i >= 0 && _current.fields[i].name !== key; i--);
-			index = i;
-		} else {
-			index = key; // In this case key === field position
-		}
+									return _metadata[index];
+								}
+								this.setCurrent = function(index) {
+									if (!_metadata)
+										throw new Error(
+												'There is no catalog metadata info');
+									if (index === undefined)
+										_current = null;
+									else
+										_current = _metadata[index];
+								}
+								/***********************************************
+								 * Catalog fields methods *
+								 **********************************************/
 
-		return index === -1 ? {} : _current.fields[index];
-	}
-	
-	/********************************************
-	 * Catalog resource operations on server    *
-	 ********************************************/
+								/**
+								 * Returns the first field in form that should
+								 * get the focus. We find the first field that
+								 * is not read only
+								 */
+								this.getFFF = this.getFirstFocusableField = function() {
+									if (!_current)
+										throw new Error(
+												'There is no current catalog selected');
 
-	function _success(response) {
-		console.log('$resource')
-		console.log(response)
-	}
-	function _fail(response) {
-		console.error('Error trying to get data, HTTP error code: ' + response.status)
-	}
-	
-	
-	this.save = function(data) {
-		if (!_current) throw new Error('There is no current catalog selected');
+									for (var i = 0; i < _current.fields.length; i++)
+										if (!_current.fields[i].readOnly)
+											return _current.fields[i].name;
 
-		var resource = this.getResource();
-		return resource.save(data, _success, _fail);
-	}
-	this.remove = function(data) {
-		return this.getResource().remove({}, data, _success, _fail)
-	}
-	this.query = function() {
-		return this.getResource().query({}, _success, _fail);
-	}
-	this.refreshRef = function(refs, res, preloadedData) {
-		// We check if there is some field for the resource passed as parameter
-		var field = (function() {
-			for (var i = _current.fields.length - 1; i >= 0; i--) {
-				if (_current.fields[i].resource === res)
-					return _current.fields[i];				
-			}
-			return null;
-		})();
-		
-		// If field for that resource is not found there is nothing to refresh
-		if (!field) return;
-		var resource = this.getResource(res);
-		var data = preloadedData || resource.query({}, _success, _fail);
-		var that = this;
-		data.$promise.then(function(responseData) {
-			var pk = that.getPk(that.getMetadata(field.resource))
-			var comboData = []
-			responseData.forEach(function(row) {
-				comboData.push({
-					id: row[pk],
-					label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name
-				});
-			})
-			refs[field.name] = comboData;
-		})
-	}
-	this.loadRefs = function(refs) {
-		if (!_current) throw new Error('There is no current catalog selected');
-		var refsFields = [];
-		_current.fields.forEach(function(f) {
-			if (f.resource)
-				refsFields.push(f)
+									return null;
+								}
 
-		});
-		
-		var that = this;
-		var promises = []
-		refsFields.forEach(function(f) {
-			var resource = that.getResource(f.resource);
-			refs[f.name] = resource.query({}, _success, _fail);
-			promises.push(refs[f.name].$promise);
-		});
-		
-		console.log('promises: ' + promises.length + ' ')
-		console.log(promises)
-		$q.all(promises).then(function() {
-			
-			for (var k in refs) {
-				var field = that.getField(k);
-				var pk = that.getPk(that.getMetadata(field.resource))
-				console.log('PK field for ' + k + ' is ' + pk)
-				var comboData = []
-				refs[k].forEach(function(row) {
-					console.log('field.resource !== _current.resource: ' + field.resource +' '+ _current.resource)
-					comboData.push({
-						id: row[pk],
-						label: row.label || row.name || row.code || row.first_name + ' ' + (row.last_name || '')
-					});
-				})
-				refs[k] = comboData;
-				console.log('Ready for combo for ' + k)
-				console.log(comboData);
-			}
-			_current.fields.forEach(function(f) {
-				if (f.values)
-					refs[f.name] = f.values;
-			});
-		})
-		
-		console.log(refs);
-		return refs;
-	} 
-	
-	}])
+								/**
+								 * Find the field by name or position
+								 */
+								this.getField = function(key) {
+									if (!_current)
+										throw new Error(
+												'There is no current catalog selected');
+									var index = -1;
+									if (typeof key === 'string') {
+										for (var i = _current.fields.length - 1; i >= 0
+												&& _current.fields[i].name !== key; i--)
+											;
+										index = i;
+									} else {
+										index = key; // In this case key ===
+														// field position
+									}
+
+									return index === -1 ? {}
+											: _current.fields[index];
+								}
+
+								/***********************************************
+								 * Catalog resource operations on server *
+								 **********************************************/
+
+								function _success(response) {
+									console.log('$resource')
+									console.log(response)
+								}
+								function _fail(response) {
+									console
+											.error('Error trying to get data, HTTP error code: '
+													+ response.status)
+								}
+
+								this.save = function(data) {
+									if (!_current)
+										throw new Error(
+												'There is no current catalog selected');
+
+									var resource = this.getResource();
+									return resource.save(data, _success, _fail);
+								}
+								this.remove = function(data) {
+									return this.getResource().remove({}, data,
+											_success, _fail)
+								}
+								this.query = function() {
+									return this.getResource().query({},
+											_success, _fail);
+								}
+								this.refreshRef = function(refs, res,
+										preloadedData) {
+									// We check if there is some field for the
+									// resource passed as parameter
+									var field = (function() {
+										for (var i = _current.fields.length - 1; i >= 0; i--) {
+											if (_current.fields[i].resource === res)
+												return _current.fields[i];
+										}
+										return null;
+									})();
+
+									// If field for that resource is not found
+									// there is nothing to refresh
+									if (!field)
+										return;
+									var resource = this.getResource(res);
+									var data = preloadedData
+											|| resource.query({}, _success,
+													_fail);
+									var that = this;
+									data.$promise.then(function(responseData) {
+										var pk = that.getPk(that
+												.getMetadata(field.resource))
+										var comboData = []
+										responseData.forEach(function(row) {
+											comboData.push({
+												id : row[pk],
+												label : row.label || row.name
+														|| row.code
+														|| row.first_name + ' '
+														+ row.last_name
+											});
+										})
+										refs[field.name] = comboData;
+									})
+								}
+								this.loadRefs = function(refs) {
+									if (!_current)
+										throw new Error(
+												'There is no current catalog selected');
+									var refsFields = [];
+									_current.fields.forEach(function(f) {
+										if (f.resource)
+											refsFields.push(f)
+
+									});
+
+									var that = this;
+									var promises = []
+									refsFields.forEach(function(f) {
+										var resource = that
+												.getResource(f.resource);
+										refs[f.name] = resource.query({},
+												_success, _fail);
+										promises.push(refs[f.name].$promise);
+									});
+
+									console.log('promises: ' + promises.length
+											+ ' ')
+									console.log(promises)
+									$q
+											.all(promises)
+											.then(
+													function() {
+
+														for ( var k in refs) {
+															var field = that
+																	.getField(k);
+															var pk = that
+																	.getPk(that
+																			.getMetadata(field.resource))
+															console
+																	.log('PK field for '
+																			+ k
+																			+ ' is '
+																			+ pk)
+															var comboData = []
+															refs[k]
+																	.forEach(function(
+																			row) {
+																		console
+																				.log('field.resource !== _current.resource: '
+																						+ field.resource
+																						+ ' '
+																						+ _current.resource)
+																		comboData
+																				.push({
+																					id : row[pk],
+																					label : row.label
+																							|| row.name
+																							|| row.code
+																							|| row.first_name
+																							+ ' '
+																							+ (row.last_name || '')
+																				});
+																	})
+															refs[k] = comboData;
+															console
+																	.log('Ready for combo for '
+																			+ k)
+															console
+																	.log(comboData);
+														}
+														_current.fields
+																.forEach(function(
+																		f) {
+																	if (f.values)
+																		refs[f.name] = f.values;
+																});
+													})
+
+									console.log(refs);
+									return refs;
+								}
+
+							} ])
 
 })();
\ No newline at end of file

--
Gitblit v1.3.2