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