4 * This provides access to the organizational unit tree and
5 * caches it in browser session storage.
8 * get() - retrieve OU based on ID or aou object
9 * list() - retrieve flattened list of OUs
10 * tree() - retrieve OU as tree
11 * root() - get aou object representing root of the OU tree
12 * ancestors() - get ancestors of supplied OU
13 * descendants() - get descendants of supplied OU
15 * TODO more to document
18 angular.module('egCoreMod')
21 ['$q','egEnv','egAuth','egNet','$injector',
22 function($q, egEnv, egAuth, egNet , $injector) {
26 // org unit settings cache.
27 // This allows the caller to avoid local caches
28 service.cachedSettings = {};
30 service.get = function(node_or_id) {
31 if (typeof node_or_id == 'object')
33 return egEnv.aou.map[node_or_id];
36 service.list = function() {
37 return egEnv.aou.list;
40 service.tree = function() {
41 return egEnv.aou.tree;
45 service.root = function() {
46 return egEnv.aou.list[0];
49 // list of org_unit objects or IDs for ancestors + me
50 service.ancestors = function(node_or_id, as_id) {
51 var node = service.get(node_or_id);
54 while( (node = service.get(node.parent_ou())))
57 return nodes.map(function(n){return n.id()});
61 // tests that a node can have users
62 service.CanHaveUsers = function(node_or_id) {
66 .can_have_users() == 't';
69 // tests that a node can have volumes
70 service.CanHaveVolumes = function(node_or_id) {
74 .can_have_vols() == 't';
77 // list of org_unit objects or IDs for me + descendants
78 service.descendants = function(node_or_id, as_id) {
79 var node = service.get(node_or_id);
84 angular.forEach(n.children(), descend);
88 return nodes.map(function(n){return n.id()});
92 // list of org_unit objects or IDs for ancestors + me + descendants
93 service.fullPath = function(node_or_id, as_id) {
94 var list = service.ancestors(node_or_id).concat(
95 service.descendants(node_or_id).slice(1));
97 return list.map(function(n){return n.id()});
101 var egLovefield = null;
102 // returns a promise, resolved with a hash of setting name =>
103 // setting value for the selected org unit. Org unit defaults to
104 // auth workstation org unit.
105 service.settings = function(names, ou_id) {
107 egLovefield = $injector.get('egLovefield');
111 if (!angular.isArray(names)) names = [names];
114 return egLovefield.getSettingsCache(names)
115 .then(function(settings) {
117 angular.forEach(settings, function (s) {
118 hash[s.name] = s.value;
120 return $q.when(hash);
125 if (!egAuth.user()) return $q.when();
127 var deferred = $q.defer();
128 ou_id = ou_id || egAuth.user().ws_ou();
129 var here = (ou_id == egAuth.user().ws_ou());
133 // only cache org settings retrieved for the current
134 // workstation org unit.
136 angular.forEach(names, function(name) {
137 if (!angular.isDefined(service.cachedSettings[name]))
141 // only retrieve uncached values
143 if (names.length == 0)
144 return $q.when(service.cachedSettings);
149 'open-ils.actor.ou_setting.ancestor_default.batch',
150 ou_id, names, egAuth.token()
151 ).then(function(blob) {
153 angular.forEach(blob, function(val, key) {
154 // val is either null or a structure containing the value
155 settings[key] = val ? val.value : null;
156 if (here) service.cachedSettings[key] = settings[key];
159 return egLovefield.setSettingsCache(settings).then(function() {
160 // resolve with cached settings if 'here', since 'settings'
161 // will only contain settings we had to retrieve
162 deferred.resolve(here ? service.cachedSettings : settings);
165 return deferred.promise;