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: { |
| 189 | model: model.name |
| 190 | }, |
| 191 | component: 'xosCrud', |
| 192 | }; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 193 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 194 | try { |
| 195 | this.XosRuntimeStates.addState( |
| 196 | this.stateNameFromModel(model), |
| 197 | state |
| 198 | ); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 199 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 200 | // extend model |
| 201 | model.clientUrl = `${this.serviceNameFromAppName(model.app)}${clientUrl}`; |
| 202 | |
| 203 | d.resolve(model); |
| 204 | } catch (e) { |
| 205 | d.reject(e); |
| 206 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 207 | return d.promise; |
| 208 | } |
| 209 | |
| 210 | private addNavItem(model: IXosModel): ng.IPromise<IXosModel> { |
| 211 | const d = this.$q.defer(); |
| 212 | |
| 213 | const stateName = this.stateNameFromModel(model); |
| 214 | |
| 215 | const parentState: string = this.getParentStateFromModel(model); |
| 216 | |
Matteo Scandolo | c80ccbd | 2017-02-27 16:24:33 -0800 | [diff] [blame] | 217 | try { |
| 218 | this.XosNavigationService.add({ |
| 219 | label: this.ConfigHelpers.pluralize(model.name), |
| 220 | state: stateName, |
| 221 | parent: parentState |
| 222 | }); |
| 223 | d.resolve(model); |
| 224 | } catch (e) { |
| 225 | d.reject(e); |
| 226 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 227 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 228 | |
| 229 | return d.promise; |
| 230 | } |
| 231 | |
| 232 | private cacheModelEntries(model: IXosModel): ng.IPromise<IXosModel> { |
| 233 | const d = this.$q.defer(); |
| 234 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 235 | const apiUrl = this.getApiUrlFromModel(model); |
| 236 | this.XosModelStore.query(model.name, apiUrl) |
| 237 | .subscribe( |
| 238 | () => { |
Matteo Scandolo | 38e94a8 | 2017-03-02 12:17:27 -0800 | [diff] [blame] | 239 | return d.resolve(model); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 240 | }, |
| 241 | err => { |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 242 | this.AuthService.handleUnauthenticatedRequest(err); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 243 | return d.reject(err); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 244 | } |
| 245 | ); |
| 246 | |
| 247 | return d.promise; |
| 248 | } |
| 249 | |
| 250 | private getTableCfg(model: IXosModel): ng.IPromise<IXosModel> { |
| 251 | |
| 252 | const d = this.$q.defer(); |
| 253 | |
| 254 | const stateUrl = this.stateNameFromModel(model); |
| 255 | |
| 256 | model.tableCfg = this.ConfigHelpers.modelToTableCfg(model, stateUrl); |
| 257 | |
| 258 | d.resolve(model); |
| 259 | |
| 260 | return d.promise; |
| 261 | } |
| 262 | |
| 263 | private getFormCfg(model: IXosModel): ng.IPromise<IXosModel> { |
| 264 | |
| 265 | const d = this.$q.defer(); |
| 266 | |
| 267 | model.formCfg = this.ConfigHelpers.modelToFormCfg(model); |
| 268 | |
| 269 | d.resolve(model); |
| 270 | |
| 271 | return d.promise; |
| 272 | } |
| 273 | |
| 274 | private storeModel(model: IXosModel): ng.IPromise<IXosModel> { |
| 275 | |
| 276 | const d = this.$q.defer(); |
| 277 | |
| 278 | if (!_.find(this.xosModels, m => m.name === model.name)) { |
| 279 | this.xosModels.push(model); |
| 280 | } |
| 281 | |
| 282 | d.resolve(model); |
| 283 | |
| 284 | return d.promise; |
| 285 | } |
| 286 | } |