blob: 092dda8eefa2d0f36617e8c38871415ae0e4810c [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolo5d962a32017-08-01 18:16:14 -070019import {IXosModelRelation} from './crud';
20import {IXosModelStoreService} from '../../datasources/stores/model.store';
21import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
22import * as _ from 'lodash';
23import {IXosFormCfg} from '../../core/form/form';
24import {IXosTableCfg} from '../../core/table/table';
25import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers';
Matteo Scandolo04f487c2017-09-12 10:37:48 -070026import {Subscription} from 'rxjs';
Matteo Scandolo5d962a32017-08-01 18:16:14 -070027
28interface IXosCrudRelationBaseTabData {
29 model: any;
30 class?: 'full' | 'empty';
31}
32
33export interface IXosCrudRelationFormTabData extends IXosCrudRelationBaseTabData {
34 formConfig: IXosFormCfg;
35}
36
37export interface IXosCrudRelationTableTabData extends IXosCrudRelationBaseTabData {
38 tableConfig: IXosTableCfg;
39}
40
41export 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
48export 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 Scandolo04f487c2017-09-12 10:37:48 -070068 const subscription: Subscription = this.XosModelStore.get(r.model, id)
Matteo Scandolo5d962a32017-08-01 18:16:14 -070069 .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 Scandolo04f487c2017-09-12 10:37:48 -070080
81 subscription.unsubscribe();
Matteo Scandolo5d962a32017-08-01 18:16:14 -070082 },
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 Scandolo5d962a32017-08-01 18:16:14 -070090 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}