extract prepare mapconfig and get template to their respective middlewares

This commit is contained in:
Daniel García Aubert
2017-11-01 19:02:07 +01:00
parent 8ed5df0072
commit aeb9585708

View File

@@ -57,6 +57,7 @@ MapController.prototype.register = function(app) {
userMiddleware,
this.prepareContext,
createGetPrepareConfig,
this.prepareAdapterMapConfig.bind(this),
this.createGet.bind(this),
mapErrorMiddleware({
label: 'ANONYMOUS LAYERGROUP',
@@ -69,6 +70,7 @@ MapController.prototype.register = function(app) {
userMiddleware,
this.prepareContext,
createPostPrepareConfig,
this.prepareAdapterMapConfig.bind(this),
this.createPost.bind(this),
mapErrorMiddleware({
label: 'ANONYMOUS LAYERGROUP',
@@ -81,6 +83,7 @@ MapController.prototype.register = function(app) {
userMiddleware,
this.prepareContext,
prepareJsonTemplateParams,
this.getTemplate.bind(this),
this.jsonp.bind(this),
mapErrorMiddleware({
label: 'NAMED MAP LAYERGROUP'
@@ -92,6 +95,7 @@ MapController.prototype.register = function(app) {
userMiddleware,
this.prepareContext,
prepareTemplateParams,
this.getTemplate.bind(this),
this.instantiate.bind(this),
mapErrorMiddleware({
label: 'NAMED MAP LAYERGROUP'
@@ -173,47 +177,61 @@ MapController.prototype.jsonp = function(req, res, next) {
this.instantiateTemplate(req, res, next);
};
MapController.prototype.prepareAdapterMapConfig = function (req, res, next) {
const requestMapConfig = req.body;
const { user, dbhost, dbport, dbname, dbuser, dbpassword, api_key } = res.locals;
const context = {
analysisConfiguration: {
user,
db: {
host: dbhost,
port: dbport,
dbname: dbname,
user: dbuser,
pass: dbpassword
},
batch: {
username: user,
apiKey: api_key
}
}
};
this.mapConfigAdapter.getMapConfig(user, requestMapConfig, res.locals, context, (err, requestMapConfig) => {
if (err) {
return next(err);
}
req.body = requestMapConfig;
res.locals.context = context;
next();
});
};
MapController.prototype.create = function(req, res, next) {
var self = this;
var mapConfig;
var context = {};
step(
function prepareAdapterMapConfig() {
function createLayergroup() {
const requestMapConfig = req.body;
const { context } = res.locals;
context.analysisConfiguration = {
user: res.locals.user,
db: {
host: res.locals.dbhost,
port: res.locals.dbport,
dbname: res.locals.dbname,
user: res.locals.dbuser,
pass: res.locals.dbpassword
},
batch: {
username: res.locals.user,
apiKey: res.locals.api_key
}
};
self.mapConfigAdapter.getMapConfig(res.locals.user, requestMapConfig, res.locals, context, this);
},
function createLayergroup(err, requestMapConfig) {
assert.ifError(err);
var datasource = context.datasource || Datasource.EmptyDatasource();
res.locals.mapconfig = mapConfig = new MapConfig(requestMapConfig, datasource);
const datasource = context.datasource || Datasource.EmptyDatasource();
const mapconfig = res.locals.mapconfig = new MapConfig(requestMapConfig, datasource);
self.mapBackend.createLayergroup(
mapConfig,
mapconfig,
res.locals,
new CreateLayergroupMapConfigProvider(mapConfig, res.locals.user, self.userLimitsApi, res.locals),
new CreateLayergroupMapConfigProvider(mapconfig, res.locals.user, self.userLimitsApi, res.locals),
this
);
},
function afterLayergroupCreate(err, layergroup) {
assert.ifError(err);
const { context } = res.locals;
res.locals.analysesResults = context.analysesResults;
res.locals.layergroup = layergroup;
res.locals.context = context;
@@ -242,48 +260,59 @@ MapController.prototype.create = function(req, res, next) {
);
};
MapController.prototype.getTemplate = function(req, res, next) {
const templateParams = req.body;
const { user } = res.locals;
const mapConfigProvider = res.locals.mapconfigProvider = new NamedMapMapConfigProvider(
this.templateMaps,
this.pgConnection,
this.metadataBackend,
this.userLimitsApi,
this.mapConfigAdapter,
user,
req.params.template_id,
templateParams,
res.locals.auth_token,
res.locals
);
mapConfigProvider.getMapConfig((err, mapconfig, rendererParams) => {
if (err) {
return next(err);
}
res.locals.mapconfig = mapconfig;
res.locals.rendererParams = rendererParams;
next();
});
};
MapController.prototype.instantiateTemplate = function(req, res, next) {
var self = this;
var cdbuser = res.locals.user;
var mapConfigProvider;
var mapConfig;
step(
function getTemplate() {
const templateParams = req.body;
function createLayergroup() {
const { user, mapconfig, rendererParams } = res.locals;
mapConfigProvider = new NamedMapMapConfigProvider(
self.templateMaps,
self.pgConnection,
self.metadataBackend,
self.userLimitsApi,
self.mapConfigAdapter,
cdbuser,
req.params.template_id,
templateParams,
res.locals.auth_token,
res.locals
);
mapConfigProvider.getMapConfig(this);
},
function createLayergroup(err, mapConfig_, rendererParams) {
assert.ifError(err);
res.locals.mapconfig = mapConfig = mapConfig_;
self.mapBackend.createLayergroup(
mapConfig, rendererParams,
new CreateLayergroupMapConfigProvider(mapConfig, cdbuser, self.userLimitsApi, rendererParams),
mapconfig, rendererParams,
new CreateLayergroupMapConfigProvider(mapconfig, user, self.userLimitsApi, rendererParams),
this
);
},
function afterLayergroupCreate(err, layergroup) {
assert.ifError(err);
res.locals.analysesResults = mapConfigProvider.analysesResults;
res.locals.layergroup = layergroup;
res.locals.template = mapConfigProvider.template;
res.locals.templateName = mapConfigProvider.getTemplateName();
res.locals.context = mapConfigProvider.context;
const { mapconfigProvider } = res.locals;
res.locals.analysesResults = mapconfigProvider.analysesResults;
res.locals.template = mapconfigProvider.template;
res.locals.templateName = mapconfigProvider.getTemplateName();
res.locals.context = mapconfigProvider.context;
const afterLayergroupCreate = self.afterLayergroupCreateBuilder({
useTemplateHash: true