webstaff: start core serials services
authorGalen Charlton <gmc@equinoxinitiative.org>
Mon, 17 Apr 2017 21:44:40 +0000 (17:44 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 30 May 2017 16:06:35 +0000 (12:06 -0400)
Routines to fetch a subscription/dist/stream tree for a bib
and produced a flattened version of it for the distribution/stream
grid.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/web/js/ui/default/staff/serials/services/core.js [new file with mode: 0644]

diff --git a/Open-ILS/web/js/ui/default/staff/serials/services/core.js b/Open-ILS/web/js/ui/default/staff/serials/services/core.js
new file mode 100644 (file)
index 0000000..f2ac3ad
--- /dev/null
@@ -0,0 +1,123 @@
+angular.module('egSerialsMod', ['egCoreMod'])
+.factory('egSerialsCoreSvc',
+       ['egCore','orderByFilter',
+function(egCore , orderByFilter) {
+    var service = {
+        bibId : null,
+        subId : null,
+        subTree : [],
+        subList : []
+    };
+
+    // fetch subscription, distributions, streams, captions,
+    // and notes associated with the indicated bib
+    service.fetch = function(bibId, contextOrg) {
+        // TODO filter by contextOrg
+        return egCore.pcrud.search('ssub', {
+                record_entry : bibId
+            }, {
+                flesh : 3,
+                flesh_fields : {
+                    'ssub'  : ['owning_lib','distributions', 'scaps', 'notes'],
+                    'sdist' : [ 'record_entry','holding_lib',
+                                'receive_call_number','receive_unit_template',
+                                'bind_call_number','bind_unit_template',
+                                'streams','notes'],
+                    'sstr'  : ['routing_list_users']
+                }
+            },
+            { atomic : true }
+        ).then(function(list) {
+            service.bibId = bibId;
+            service.subTree = list;
+            update_flat_sdist_sstr_list();
+        });
+    }
+
+    // create/update a flat version of the subscription/distribution/stream
+    // tree for feeding to the distribution and stream grid
+    function update_flat_sdist_sstr_list() {
+
+        // flatten the structure...
+        var list = [];
+        angular.forEach(service.subTree, function(ssub) {
+            var ssubHash = egCore.idl.toHash(ssub);
+            var _ssub = {
+                'id'                   : ssubHash.id,
+                'owning_lib.name'      : ssubHash.owning_lib.name,
+                'start_date'           : ssubHash.start_date,
+                'end_date'             : ssubHash.end_date,
+                'expected_date_offset' : ssubHash.expected_date_offset
+            };
+            angular.forEach(ssubHash.distributions, function(sdist) {
+                var _sdist = {};
+                angular.forEach([
+                    'id',
+                    'summary_method',
+                    'label',
+                    'display_grouping',
+                    'unit_label_prefix',
+                    'unit_label_suffix',
+                ], function(fld) {
+                    _sdist['sdist.' + fld] = sdist[fld];
+                });
+                _sdist['sdist.holding_lib.name'] = sdist.holding_lib.name;
+                _sdist['sdist.receive_call_number.label'] = 
+                    sdist.receive_call_number ? sdist.receive_call_number.label : null;
+                _sdist['sdist.receive_unit_template.name'] =
+                    sdist.receive_unit_template ? sdist.receive_unit_template.name : null;
+                _sdist['sdist.bind_call_number.label'] =
+                    sdist.bind_call_number ? sdist.bind_call_number.label : null;
+                _sdist['sdist.bind_unit_template.name'] =
+                    sdist.bind_unit_template ? sdist.bind_unit_template.name : null;
+                angular.forEach(sdist.streams, function(sstr) {
+                    var _sstr = {
+                        'sstr.id'                 : sstr.id,
+                        'sstr.routing_label'      : sstr.routing_label,
+                        'sstr.additional_routing' : ((sstr.routing_list_users.length > 0) ? true : false)
+                    };
+                    var row = {};
+                    angular.extend(row, _ssub, _sdist, _sstr);
+                    list.push(row);
+                });
+            });
+        });
+
+        // ... then sort it
+        service.subList.length = 0;
+        angular.extend(service.subList,
+            orderByFilter(list, ['owing_lib.name', 'start_date', 'end_date',
+                                 'holding_lib.name', 'sdist.id', 'sstr.id'])
+        );
+
+        // ... then remove duplication of owning library, distribution library,
+        // and distribution labels
+        var sub_lib = null;
+        var dist_lib = null;
+        var dist_label = null;
+        var index = 0;
+        angular.forEach(service.subList, function(row) {
+            row['index'] = index++;
+            if (sub_lib == row['owning_lib.name']) {
+                row['owning_lib.name'] = null;
+            } else {
+                sub_lib = row['owning_lib.name'];
+                dist_lib = row['sdist.holding_lib.name'];
+                dist_label = row['sdist.label'];
+                return;
+            }
+            if (dist_lib == row['sdist.holding_lib.name']) {
+                row['sdist.holding_lib.name'] = null;
+            } else {
+                dist_lib = row['sdist.holding_lib.name'];
+            }
+            if (dist_label == row['sdist.label']) {
+                row['sdist.label'] = null;
+            } else {
+                dist_label = row['sdist.label'];
+            }
+        });
+    }
+
+    return service;
+}]);