Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1737 Query panel search fails if an id is not defined #1792

Merged
merged 6 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docma-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"web/client/epics/globeswitcher.js",
"web/client/epics/maptype.js",
"web/client/epics/search.js",
"web/client/epics/wfsquery.js",

"web/client/utils/index.jsdoc",
"web/client/utils/PluginsUtils.js",
Expand Down
38 changes: 12 additions & 26 deletions web/client/actions/wfsquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const QUERY_CREATE = 'QUERY_CREATE';
const QUERY_RESULT = 'QUERY_RESULT';
const QUERY_ERROR = 'QUERY_ERROR';
const RESET_QUERY = 'RESET_QUERY';
const QUERY = 'QUERY';

const axios = require('../libs/ajax');
const {toggleControl, setControlProperty} = require('./controls');
const FilterUtils = require('../utils/FilterUtils');
const {reset} = require('./queryform');

function featureTypeSelected(url, typeName) {
Expand Down Expand Up @@ -103,30 +103,12 @@ function createQuery(searchUrl, filterObj) {
};
}

function query(searchUrl, filterObj) {
createQuery(searchUrl, filterObj);
let data;
if (typeof filterObj === 'string') {
data = filterObj;
} else {
data = filterObj.filterType === "OGC" ?
FilterUtils.toOGCFilter(filterObj.featureTypeName, filterObj, filterObj.ogcVersion, filterObj.sortOptions, filterObj.hits) :
FilterUtils.toCQLFilter(filterObj);
}
return (dispatch, getState) => {
let state = getState();
if (state.controls && state.controls.queryPanel && state.controls.drawer && state.controls.drawer.enabled && state.query && state.query.open) {
dispatch(setControlProperty('drawer', 'enabled', false));
dispatch(setControlProperty('drawer', 'disabled', true));
}
return axios.post(searchUrl + '?service=WFS&&outputFormat=json', data, {
timeout: 60000,
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}
}).then((response) => {
dispatch(querySearchResponse(response.data, searchUrl, filterObj));
}).catch((e) => {
dispatch(queryError(e));
});
function query(searchUrl, filterObj, retry) {
return {
type: QUERY,
searchUrl,
filterObj,
retry
};
}

Expand Down Expand Up @@ -175,14 +157,18 @@ module.exports = {
QUERY_RESULT,
QUERY_ERROR,
RESET_QUERY,
QUERY,
featureTypeSelected,
featureTypeLoaded,
featureTypeError,
featureError,
loadFeature,
createQuery,
query,
featureClose,
resetQuery,
toggleQueryPanel,
closeResponse
closeResponse,
queryError,
querySearchResponse
};
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const DockedFeatureGrid = React.createClass({
};
this.featureLoaded = params;
this.sortModel = params && params.sortModel;
this.props.onQuery(this.props.searchUrl, filterObj, this.props.params);
this.props.onQuery(this.props.searchUrl, filterObj);

}
},
Expand Down
115 changes: 113 additions & 2 deletions web/client/epics/wfsquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
const Rx = require('rxjs');
const axios = require('../libs/ajax');
const {changeSpatialAttribute} = require('../actions/queryform');
const {FEATURE_TYPE_SELECTED, featureTypeLoaded, featureTypeError} = require('../actions/wfsquery');
const {FEATURE_TYPE_SELECTED, QUERY, featureTypeLoaded, featureTypeError, createQuery, querySearchResponse, queryError, featureClose} = require('../actions/wfsquery');
const FilterUtils = require('../utils/FilterUtils');
const assign = require('object-assign');
const {isString} = require('lodash');
const {TOGGLE_CONTROL, setControlProperty} = require('../actions/controls');

const types = {
'xsd:string': 'string',
'xsd:dateTime': 'date',
'xsd:number': 'number',
'xsd:int': 'number'
};

const fieldConfig = {};

const extractInfo = (data) => {
return {
geometry: data.featureTypes[0].properties
Expand Down Expand Up @@ -51,6 +57,62 @@ const extractInfo = (data) => {
};
};

const getWFSFilterData = (filterObj) => {
let data;
if (typeof filterObj === 'string') {
data = filterObj;
} else {
data = filterObj.filterType === "OGC" ?
FilterUtils.toOGCFilter(filterObj.featureTypeName, filterObj, filterObj.ogcVersion, filterObj.sortOptions, filterObj.hits) :
FilterUtils.toCQLFilter(filterObj);
}
return data;
};

const getWFSFeature = (searchUrl, filterObj) => {
const data = getWFSFilterData(filterObj);
return Rx.Observable.defer( () =>
axios.post(searchUrl + '?service=WFS&outputFormat=json', data, {
timeout: 60000,
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}
}));
};

const getWFSResponseException = (response, code) => {
const {unmarshaller} = require('../utils/ogc/WFS');
const json = isString(response.data) ? unmarshaller.unmarshalString(response.data) : null;
return json && json.value && json.value.exception && json.value.exception[0] && json.value.exception[0].TYPE_NAME === 'OWS_1_0_0.ExceptionType' && json.value.exception[0].exceptionCode === code;
};

const getFirstAttribute = (state)=> {
return state.query && state.query.featureTypes && state.query.featureTypes[state.query.typeName] && state.query.featureTypes[state.query.typeName].attributes && state.query.featureTypes[state.query.typeName].attributes[0] && state.query.featureTypes[state.query.typeName].attributes[0].attribute || null;
};

const getDefaultSortOptions = (attribute) => {
return attribute ? { sortBy: attribute, sortOrder: 'A'} : {};
};

const retryWithForcedSortOptions = (action, store) => {
const sortOptions = getDefaultSortOptions(getFirstAttribute(store.getState()));
return getWFSFeature(action.searchUrl, assign(action.filterObj, {
sortOptions
}))
.map((newResponse) => {
const newError = getWFSResponseException(newResponse, 'NoApplicableCode');
return !newError ? querySearchResponse(newResponse.data, action.searchUrl, action.filterObj) : queryError('No sortable request');
})
.catch((e) => {
return Rx.Observable.of(queryError(e));
});
};

/**
* Gets the WFS feature type attributes and geometry when the feature has been selected
* @param {Observable} action$ manages `FEATURE_TYPE_SELECTED`
* @memberof epics.wfsquery
* @return {Observable}
*/

const featureTypeSelectedEpic = action$ =>
action$.ofType(FEATURE_TYPE_SELECTED).switchMap(action => {
return Rx.Observable.defer( () =>
Expand All @@ -72,6 +134,55 @@ const featureTypeSelectedEpic = action$ =>
.catch(e => Rx.Observable.of(featureTypeError(action.typeName, e.message)));
});

/**
* Sends a WFS query, returns a response or handles request error
* in particular the NoApplicableCode WFS error with a forced sort option on the first attribute
* @param {Observable} action$ manages `QUERY`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

external:Observable see other epics.

* @memberof epics.wfsquery
* @return {Observable}
*/

const wfsQueryEpic = (action$, store) =>
action$.ofType(QUERY).switchMap(action => {

return Rx.Observable.merge(
Rx.Observable.of(createQuery(action.searchUrl, action.filterObj)),
Rx.Observable.of(setControlProperty('drawer', 'enabled', false)),
getWFSFeature(action.searchUrl, action.filterObj)
.switchMap((response) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent

// try to guess if it was a missing id error and try to search again with forced sortOptions
const error = getWFSResponseException(response, 'NoApplicableCode');
if (error) {
return retryWithForcedSortOptions(action, store);
}
return Rx.Observable.of(querySearchResponse(response.data, action.searchUrl, action.filterObj));
})
.catch((e) => {
return Rx.Observable.of(queryError(e));
})
);
});

/**
* Closes the feature grid when the drawer menu button has been toggled
* @param {Observable} action$ manages `TOGGLE_CONTROL`
* @memberof epics.wfsquery
* @return {Observable}
*/

const closeFeatureEpic = action$ =>
action$.ofType(TOGGLE_CONTROL).switchMap(action => {
return action.control && action.control === 'drawer' ? Rx.Observable.of(featureClose()) : Rx.Observable.empty();
});

/**
* Epics for WFS query requests
* @name epics.wfsquery
* @type {Object}
*/

module.exports = {
featureTypeSelectedEpic
featureTypeSelectedEpic,
wfsQueryEpic,
closeFeatureEpic
};
4 changes: 2 additions & 2 deletions web/client/plugins/QueryPanel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const LayersUtils = require('../utils/LayersUtils');
// include application component
const QueryBuilder = require('../components/data/query/QueryBuilder');

const {featureTypeSelectedEpic} = require('../epics/wfsquery');
const {featureTypeSelectedEpic, wfsQueryEpic, closeFeatureEpic} = require('../epics/wfsquery');

const {bindActionCreators} = require('redux');
const {
Expand Down Expand Up @@ -237,5 +237,5 @@ module.exports = {
queryform: require('../reducers/queryform'),
query: require('../reducers/query')
},
epics: {featureTypeSelectedEpic}
epics: {featureTypeSelectedEpic, wfsQueryEpic, closeFeatureEpic}
};