--- /dev/null
+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;
+}]);