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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 19 | // TODO test me hard!!! |
| 20 | |
| 21 | import * as _ from 'lodash'; |
| 22 | import {IXosModeldefsService, IXosModeldef, IXosModelDefsField, IXosModelDefsRelation} from '../rest/modeldefs.rest'; |
| 23 | import {IXosTableCfg} from '../../core/table/table'; |
| 24 | import {IXosFormCfg} from '../../core/form/form'; |
| 25 | import {IXosNavigationService} from '../../core/services/navigation'; |
| 26 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
| 27 | import {IXosRuntimeStatesService, IXosState} from '../../core/services/runtime-states'; |
| 28 | import {IXosModelStoreService} from '../stores/model.store'; |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 29 | import {IXosAuthService} from '../rest/auth.rest'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 30 | |
| 31 | export interface IXosModel { |
| 32 | name: string; // the model name |
| 33 | app: string; // the service to wich it belong |
| 34 | fields: IXosModelDefsField[]; |
| 35 | relations?: IXosModelDefsRelation[]; |
| 36 | backendUrl?: string; // the api endpoint |
| 37 | clientUrl?: string; // the view url |
| 38 | tableCfg?: IXosTableCfg; |
| 39 | formCfg?: IXosFormCfg; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 40 | description: string; |
| 41 | verbose_name: string; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // Service |
| 45 | export interface IXosModelDiscovererService { |
| 46 | discover(): ng.IPromise<boolean>; |
| 47 | get(modelName: string): IXosModel; |
Matteo Scandolo | 47c53fc | 2017-03-23 14:11:32 -0700 | [diff] [blame] | 48 | getApiUrlFromModel(model: IXosModel): string; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 49 | areModelsLoaded(): boolean; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | export class XosModelDiscovererService implements IXosModelDiscovererService { |
| 53 | static $inject = [ |
| 54 | '$log', |
| 55 | '$q', |
| 56 | 'XosModelDefs', |
| 57 | 'ConfigHelpers', |
| 58 | 'XosRuntimeStates', |
| 59 | 'XosNavigationService', |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 60 | 'XosModelStore', |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 61 | 'ngProgressFactory', |
| 62 | 'AuthService' |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 63 | ]; |
| 64 | private xosModels: IXosModel[] = []; // list of augmented model definitions; |
| 65 | private xosServices: string[] = []; // list of loaded services |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 66 | private progressBar; |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 67 | private modelsLoaded: boolean = false; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 68 | |
| 69 | constructor ( |
| 70 | private $log: ng.ILogService, |
| 71 | private $q: ng.IQService, |
| 72 | private XosModelDefs: IXosModeldefsService, |
| 73 | private ConfigHelpers: IXosConfigHelpersService, |
| 74 | private XosRuntimeStates: IXosRuntimeStatesService, |
| 75 | private XosNavigationService: IXosNavigationService, |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 76 | private XosModelStore: IXosModelStoreService, |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 77 | private ngProgressFactory: any, // check for type defs |
| 78 | private AuthService: IXosAuthService |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 79 | ) { |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 80 | this.progressBar = this.ngProgressFactory.createInstance(); |
| 81 | this.progressBar.setColor('#f6a821'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 84 | public areModelsLoaded(): boolean { |
| 85 | return this.modelsLoaded; |
| 86 | } |
| 87 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 88 | public get(modelName: string): IXosModel|null { |
| 89 | return _.find(this.xosModels, m => m.name === modelName); |
| 90 | } |
| 91 | |
Matteo Scandolo | 47c53fc | 2017-03-23 14:11:32 -0700 | [diff] [blame] | 92 | public getApiUrlFromModel(model: IXosModel): string { |
| 93 | if (model.app === 'core') { |
| 94 | return `/core/${this.ConfigHelpers.pluralize(model.name.toLowerCase())}`; |
| 95 | } |
| 96 | else { |
| 97 | const serviceName = this.serviceNameFromAppName(model.app); |
| 98 | return `/${serviceName}/${this.ConfigHelpers.pluralize(model.name.toLowerCase())}`; |
| 99 | } |
| 100 | } |
| 101 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 102 | public discover() { |
| 103 | const d = this.$q.defer(); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 104 | this.progressBar.start(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 105 | this.XosModelDefs.get() |
| 106 | .then((modelsDef: IXosModeldef[]) => { |
| 107 | |
| 108 | const pArray = []; |
| 109 | _.forEach(modelsDef, (model: IXosModeldef) => { |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 110 | this.$log.debug(`[XosModelDiscovererService] Loading: ${model.name}`); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 111 | let p = this.cacheModelEntries(model) |
| 112 | .then(model => { |
| 113 | return this.addState(model); |
| 114 | }) |
| 115 | .then(model => { |
| 116 | return this.addNavItem(model); |
| 117 | }) |
| 118 | .then(model => { |
| 119 | return this.getTableCfg(model); |
| 120 | }) |
| 121 | .then(model => { |
| 122 | return this.getFormCfg(model); |
| 123 | }) |
| 124 | .then(model => { |
| 125 | return this.storeModel(model); |
| 126 | }) |
| 127 | .then(model => { |
| 128 | this.$log.debug(`[XosModelDiscovererService] Model ${model.name} stored`); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 129 | return this.$q.resolve('true'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 130 | }) |
| 131 | .catch(err => { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 132 | this.$log.error(`[XosModelDiscovererService] Model ${model.name} NOT stored`, err); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 133 | return this.$q.resolve('false'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 134 | }); |
| 135 | pArray.push(p); |
| 136 | }); |
| 137 | this.$q.all(pArray) |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 138 | .then((res) => { |
| 139 | // the Model Loader promise won't ever be reject, in case it will be resolve with value false, |
| 140 | // that's because we want to wait anyway for all the models to be loaded |
| 141 | if (res.indexOf('false') > -1) { |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 142 | return d.resolve(false); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 143 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 144 | d.resolve(true); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 145 | }) |
Matteo Scandolo | 0222938 | 2017-04-18 11:52:23 -0700 | [diff] [blame] | 146 | .catch((e) => { |
| 147 | this.$log.error(`[XosModelDiscovererService]`, e); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 148 | d.resolve(false); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 149 | }) |
| 150 | .finally(() => { |
| 151 | this.progressBar.complete(); |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 152 | this.modelsLoaded = true; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 153 | }); |
| 154 | }); |
| 155 | return d.promise; |
| 156 | } |
| 157 | |
| 158 | private serviceNameFromAppName(appName: string): string { |
| 159 | return appName.replace('services.', ''); |
| 160 | } |
| 161 | |
| 162 | private stateNameFromModel(model: IXosModel): string { |
| 163 | return `xos.${this.serviceNameFromAppName(model.app)}.${model.name.toLowerCase()}`; |
| 164 | } |
| 165 | |
| 166 | private getParentStateFromModel(model: IXosModel): string { |
| 167 | let parentState: string; |
| 168 | if (model.app === 'core') { |
| 169 | parentState = 'xos.core'; |
| 170 | } |
| 171 | else { |
| 172 | const serviceName = this.addService(model); |
| 173 | parentState = `xos.${serviceName}`; |
| 174 | } |
| 175 | return parentState; |
| 176 | } |
| 177 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 178 | // add a service state and navigation item if it is not already there |
| 179 | private addService(model: IXosModel): string { |
| 180 | const serviceName: string = this.serviceNameFromAppName(model.app); |
| 181 | if (!_.find(this.xosServices, n => n === serviceName)) { |
| 182 | const serviceState = { |
| 183 | url: serviceName, |
| 184 | parent: 'xos', |
| 185 | abstract: true, |
| 186 | template: '<div ui-view></div>' |
| 187 | }; |
| 188 | this.XosRuntimeStates.addState(`xos.${serviceName}`, serviceState); |
| 189 | |
| 190 | this.XosNavigationService.add({ |
| 191 | label: this.ConfigHelpers.toLabel(serviceName), |
| 192 | state: `xos.${serviceName}`, |
| 193 | }); |
| 194 | this.xosServices.push(serviceName); |
| 195 | } |
| 196 | return serviceName; |
| 197 | } |
| 198 | |
| 199 | private addState(model: IXosModel): ng.IPromise<IXosModel> { |
| 200 | const d = this.$q.defer(); |
| 201 | const clientUrl = `/${this.ConfigHelpers.pluralize(model.name.toLowerCase())}/:id?`; |
| 202 | const state: IXosState = { |
| 203 | parent: this.getParentStateFromModel(model), |
| 204 | url: clientUrl, |
| 205 | params: { |
| 206 | id: null |
| 207 | }, |
| 208 | data: { |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 209 | model: model.name, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 210 | }, |
| 211 | component: 'xosCrud', |
| 212 | }; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 213 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 214 | if (angular.isDefined(model.relations)) { |
| 215 | state.data.relations = model.relations; |
| 216 | } |
| 217 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 218 | try { |
| 219 | this.XosRuntimeStates.addState( |
| 220 | this.stateNameFromModel(model), |
| 221 | state |
| 222 | ); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 223 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 224 | // extend model |
| 225 | model.clientUrl = `${this.serviceNameFromAppName(model.app)}${clientUrl}`; |
| 226 | |
| 227 | d.resolve(model); |
| 228 | } catch (e) { |
| 229 | d.reject(e); |
| 230 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 231 | return d.promise; |
| 232 | } |
| 233 | |
| 234 | private addNavItem(model: IXosModel): ng.IPromise<IXosModel> { |
| 235 | const d = this.$q.defer(); |
| 236 | |
| 237 | const stateName = this.stateNameFromModel(model); |
| 238 | |
| 239 | const parentState: string = this.getParentStateFromModel(model); |
| 240 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 241 | try { |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 242 | const name = model.verbose_name ? model.verbose_name : model.name; |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 243 | this.XosNavigationService.add({ |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 244 | label: this.ConfigHelpers.pluralize(name), |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 245 | state: stateName, |
| 246 | parent: parentState |
| 247 | }); |
| 248 | d.resolve(model); |
| 249 | } catch (e) { |
| 250 | d.reject(e); |
| 251 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 252 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 253 | |
| 254 | return d.promise; |
| 255 | } |
| 256 | |
| 257 | private cacheModelEntries(model: IXosModel): ng.IPromise<IXosModel> { |
| 258 | const d = this.$q.defer(); |
| 259 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 260 | const apiUrl = this.getApiUrlFromModel(model); |
| 261 | this.XosModelStore.query(model.name, apiUrl) |
| 262 | .subscribe( |
| 263 | () => { |
Matteo Scandolo | 38e94a8 | 2017-03-02 12:17:27 -0800 | [diff] [blame] | 264 | return d.resolve(model); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 265 | }, |
| 266 | err => { |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 267 | this.AuthService.handleUnauthenticatedRequest(err); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 268 | return d.reject(err); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 269 | } |
| 270 | ); |
| 271 | |
| 272 | return d.promise; |
| 273 | } |
| 274 | |
| 275 | private getTableCfg(model: IXosModel): ng.IPromise<IXosModel> { |
| 276 | |
| 277 | const d = this.$q.defer(); |
| 278 | |
| 279 | const stateUrl = this.stateNameFromModel(model); |
| 280 | |
| 281 | model.tableCfg = this.ConfigHelpers.modelToTableCfg(model, stateUrl); |
| 282 | |
| 283 | d.resolve(model); |
| 284 | |
| 285 | return d.promise; |
| 286 | } |
| 287 | |
| 288 | private getFormCfg(model: IXosModel): ng.IPromise<IXosModel> { |
| 289 | |
| 290 | const d = this.$q.defer(); |
| 291 | |
| 292 | model.formCfg = this.ConfigHelpers.modelToFormCfg(model); |
| 293 | |
| 294 | d.resolve(model); |
| 295 | |
| 296 | return d.promise; |
| 297 | } |
| 298 | |
| 299 | private storeModel(model: IXosModel): ng.IPromise<IXosModel> { |
| 300 | |
| 301 | const d = this.$q.defer(); |
| 302 | |
| 303 | if (!_.find(this.xosModels, m => m.name === model.name)) { |
| 304 | this.xosModels.push(model); |
| 305 | } |
| 306 | |
| 307 | d.resolve(model); |
| 308 | |
| 309 | return d.promise; |
| 310 | } |
| 311 | } |