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