Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 19 | import {IXosModelRelation} from './crud'; |
| 20 | import {IXosModelStoreService} from '../../datasources/stores/model.store'; |
| 21 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
| 22 | import * as _ from 'lodash'; |
| 23 | import {IXosFormCfg} from '../../core/form/form'; |
| 24 | import {IXosTableCfg} from '../../core/table/table'; |
| 25 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 26 | import {Subscription} from 'rxjs'; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 27 | |
| 28 | interface IXosCrudRelationBaseTabData { |
| 29 | model: any; |
| 30 | class?: 'full' | 'empty'; |
| 31 | } |
| 32 | |
| 33 | export interface IXosCrudRelationFormTabData extends IXosCrudRelationBaseTabData { |
| 34 | formConfig: IXosFormCfg; |
| 35 | } |
| 36 | |
| 37 | export interface IXosCrudRelationTableTabData extends IXosCrudRelationBaseTabData { |
| 38 | tableConfig: IXosTableCfg; |
| 39 | } |
| 40 | |
| 41 | export interface IXosCrudRelationService { |
| 42 | getModel(r: IXosModelRelation, id: string | number): Promise<IXosCrudRelationFormTabData>; |
| 43 | getModels(r: IXosModelRelation, source_id: string | number): Promise<IXosCrudRelationTableTabData>; |
| 44 | existsRelatedItem(r: IXosModelRelation, item: any): boolean; |
| 45 | getHumanReadableOnField(r: IXosModelRelation, baseModel: string): string; |
| 46 | } |
| 47 | |
| 48 | export class XosCrudRelationService implements IXosCrudRelationService { |
| 49 | |
| 50 | static $inject = [ |
| 51 | '$log', |
| 52 | '$q', |
| 53 | 'XosModelStore', |
| 54 | 'XosModelDiscoverer', |
| 55 | 'ConfigHelpers' |
| 56 | ]; |
| 57 | |
| 58 | constructor ( |
| 59 | private $log: ng.ILogService, |
| 60 | private $q: ng.IQService, |
| 61 | private XosModelStore: IXosModelStoreService, |
| 62 | private XosModelDiscovererService: IXosModelDiscovererService, |
| 63 | private ConfigHelpers: IXosConfigHelpersService |
| 64 | ) {} |
| 65 | |
| 66 | public getModel (r: IXosModelRelation, id: string | number): Promise<IXosCrudRelationFormTabData> { |
| 67 | const d = this.$q.defer(); |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 68 | const subscription: Subscription = this.XosModelStore.get(r.model, id) |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 69 | .subscribe( |
| 70 | item => { |
| 71 | this.$log.debug(`[XosCrud] Loaded manytoone relation with ${r.model} on ${r.on_field}`, item); |
| 72 | |
| 73 | const data: IXosCrudRelationFormTabData = { |
| 74 | model: item, |
| 75 | formConfig: this.XosModelDiscovererService.get(r.model).formCfg, |
| 76 | class: angular ? 'full' : 'empty' |
| 77 | }; |
| 78 | |
| 79 | d.resolve(data); |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 80 | |
| 81 | subscription.unsubscribe(); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 82 | }, |
| 83 | err => d.reject |
| 84 | ); |
| 85 | return d.promise; |
| 86 | }; |
| 87 | |
| 88 | public getModels(r: IXosModelRelation, source_id: string | number): Promise<IXosCrudRelationTableTabData> { |
| 89 | const d = this.$q.defer(); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 90 | this.XosModelStore.query(r.model) |
| 91 | .subscribe( |
| 92 | items => { |
| 93 | this.$log.debug(`[XosCrud] Loaded onetomany relation with ${r.model} on ${r.on_field}`, items); |
| 94 | // building the filter parameters |
| 95 | const match = {}; |
| 96 | match[`${r.on_field.toLowerCase()}_id`] = source_id; |
| 97 | const filtered = _.filter(items, match); |
| 98 | // removing search bar from table |
| 99 | const tableCfg = this.XosModelDiscovererService.get(r.model).tableCfg; |
| 100 | tableCfg.filter = null; |
| 101 | |
| 102 | const data: IXosCrudRelationTableTabData = { |
| 103 | model: filtered, |
| 104 | tableConfig: tableCfg, |
| 105 | class: filtered.length > 0 ? 'full' : 'empty' |
| 106 | }; |
| 107 | |
| 108 | d.resolve(data); |
| 109 | }, |
| 110 | err => d.reject |
| 111 | ); |
| 112 | |
| 113 | return d.promise; |
| 114 | } |
| 115 | |
| 116 | public existsRelatedItem(r: IXosModelRelation, item: any): boolean { |
| 117 | return item && angular.isDefined(item[`${r.on_field.toLowerCase()}_id`]); |
| 118 | } |
| 119 | |
| 120 | public getHumanReadableOnField(r: IXosModelRelation, baseModel: string): string { |
| 121 | if (r.on_field.toLowerCase() === baseModel.toLowerCase()) { |
| 122 | return ''; |
| 123 | } |
| 124 | if (r.model.toLowerCase() === r.on_field.toLowerCase()) { |
| 125 | return ''; |
| 126 | } |
| 127 | return `[${this.ConfigHelpers.toLabel(r.on_field, false)}]`; |
| 128 | } |
| 129 | } |