kanbanworkflowstimelinescrumrubyroadmapproject-planningproject-managementopenprojectangularissue-trackerifcgantt-chartganttbug-trackerboardsbcf
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
499 lines
16 KiB
499 lines
16 KiB
9 years ago
|
// -- copyright
|
||
11 years ago
|
// OpenProject is a project management system.
|
||
10 years ago
|
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
|
||
11 years ago
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License version 3.
|
||
|
//
|
||
|
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
||
|
// Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
|
// Copyright (C) 2010-2013 the ChiliProject Team
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License
|
||
|
// as published by the Free Software Foundation; either version 2
|
||
|
// of the License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
//
|
||
|
// See doc/COPYRIGHT.rdoc for more details.
|
||
9 years ago
|
// ++
|
||
|
|
||
|
function QueryService(Query,
|
||
|
$http,
|
||
|
PathHelper,
|
||
|
$q,
|
||
|
AVAILABLE_WORK_PACKAGE_FILTERS,
|
||
|
StatusService,
|
||
|
TypeService,
|
||
|
PriorityService,
|
||
|
UserService,
|
||
|
VersionService,
|
||
|
CategoryService,
|
||
|
RoleService,
|
||
|
GroupService,
|
||
|
ProjectService,
|
||
|
WorkPackagesTableHelper,
|
||
|
I18n,
|
||
|
queryMenuItemFactory,
|
||
|
$rootScope,
|
||
|
QUERY_MENU_ITEM_TYPE) {
|
||
11 years ago
|
|
||
|
var query;
|
||
11 years ago
|
|
||
11 years ago
|
var availableColumns = [],
|
||
|
availableUnusedColumns = [],
|
||
|
availableFilterValues = {},
|
||
11 years ago
|
availableFilters = {},
|
||
|
availableGroupedQueries;
|
||
11 years ago
|
|
||
11 years ago
|
var totalEntries;
|
||
|
|
||
11 years ago
|
var QueryService = {
|
||
11 years ago
|
initQuery: function(queryId, queryData, selectedColumns, exportFormats, afterQuerySetupCallback) {
|
||
11 years ago
|
query = new Query({
|
||
|
id: queryId,
|
||
11 years ago
|
name: queryData.name,
|
||
11 years ago
|
project_id: queryData.project_id,
|
||
|
displaySums: queryData.display_sums,
|
||
|
groupSums: queryData.group_sums,
|
||
|
sums: queryData.sums,
|
||
|
columns: selectedColumns,
|
||
11 years ago
|
groupBy: queryData.group_by,
|
||
11 years ago
|
isPublic: queryData.is_public,
|
||
11 years ago
|
exportFormats: exportFormats,
|
||
11 years ago
|
starred: queryData.starred,
|
||
|
links: queryData._links
|
||
11 years ago
|
});
|
||
10 years ago
|
query.setSortation(queryData.sort_criteria);
|
||
11 years ago
|
|
||
|
QueryService.getAvailableFilters(query.project_id)
|
||
|
.then(function(availableFilters) {
|
||
|
query.setAvailableWorkPackageFilters(availableFilters);
|
||
11 years ago
|
if (query.isDefault()) {
|
||
|
query.setDefaultFilter();
|
||
10 years ago
|
}
|
||
|
if(queryData.filters && queryData.filters.length) {
|
||
11 years ago
|
query.setFilters(queryData.filters);
|
||
|
}
|
||
11 years ago
|
|
||
|
return query;
|
||
|
})
|
||
|
.then(afterQuerySetupCallback);
|
||
|
|
||
|
return query;
|
||
|
},
|
||
11 years ago
|
|
||
10 years ago
|
updateQuery: function(values, afterUpdate) {
|
||
|
var queryData = {
|
||
|
};
|
||
|
if(!!values.display_sums) {
|
||
|
queryData.displaySums = values.display_sums;
|
||
|
}
|
||
|
if(!!values.columns) {
|
||
|
queryData.columns = values.columns;
|
||
|
}
|
||
|
if(!!values.group_by) {
|
||
|
queryData.groupBy = values.group_by;
|
||
|
}
|
||
|
if(!!values.sort_criteria) {
|
||
|
queryData.sortCriteria = values.sort_criteria;
|
||
|
}
|
||
|
query.update(queryData);
|
||
|
|
||
|
QueryService.getAvailableFilters(query.project_id)
|
||
|
.then(function(availableFilters) {
|
||
|
query.setAvailableWorkPackageFilters(availableFilters);
|
||
|
if(queryData.filters && queryData.filters.length) {
|
||
|
query.setFilters(queryData.filters);
|
||
|
}
|
||
|
|
||
|
return query;
|
||
|
})
|
||
|
.then(afterUpdate);
|
||
|
|
||
|
return query;
|
||
|
},
|
||
|
|
||
11 years ago
|
getQuery: function() {
|
||
|
return query;
|
||
|
},
|
||
|
|
||
10 years ago
|
clearQuery: function() {
|
||
|
query = null;
|
||
|
},
|
||
|
|
||
11 years ago
|
getQueryName: function() {
|
||
|
if (query && query.hasName()) {
|
||
|
return query.name;
|
||
|
}
|
||
|
},
|
||
|
|
||
11 years ago
|
setTotalEntries: function(numberOfEntries) {
|
||
|
totalEntries = numberOfEntries;
|
||
|
},
|
||
|
|
||
|
getTotalEntries: function() {
|
||
|
return totalEntries;
|
||
|
},
|
||
|
|
||
11 years ago
|
hideColumns: function(columnNames) {
|
||
11 years ago
|
WorkPackagesTableHelper.moveColumns(columnNames, this.getSelectedColumns(), availableUnusedColumns);
|
||
11 years ago
|
},
|
||
|
|
||
|
showColumns: function(columnNames) {
|
||
11 years ago
|
WorkPackagesTableHelper.moveColumns(columnNames, availableUnusedColumns, this.getSelectedColumns());
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
getAvailableGroupedQueries: function() {
|
||
|
return availableGroupedQueries;
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
// data loading
|
||
|
|
||
11 years ago
|
loadAvailableGroupedQueries: function(projectIdentifier) {
|
||
11 years ago
|
if (availableGroupedQueries) {
|
||
|
return $q.when(availableGroupedQueries);
|
||
11 years ago
|
}
|
||
|
|
||
|
return QueryService.fetchAvailableGroupedQueries(projectIdentifier);
|
||
|
},
|
||
|
|
||
|
fetchAvailableGroupedQueries: function(projectIdentifier) {
|
||
11 years ago
|
var url = projectIdentifier ? PathHelper.apiProjectGroupedQueriesPath(projectIdentifier) : PathHelper.apiGroupedQueriesPath();
|
||
|
|
||
11 years ago
|
return QueryService.doQuery(url)
|
||
|
.then(function(groupedQueriesResults) {
|
||
11 years ago
|
availableGroupedQueries = groupedQueriesResults;
|
||
|
return availableGroupedQueries;
|
||
11 years ago
|
});
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
loadAvailableUnusedColumns: function(projectIdentifier) {
|
||
|
return QueryService.loadAvailableColumns(projectIdentifier)
|
||
10 years ago
|
.then(function(availableColumns) {
|
||
|
availableUnusedColumns = QueryService.selectUnusedColumns(availableColumns);
|
||
11 years ago
|
return availableUnusedColumns;
|
||
11 years ago
|
});
|
||
|
},
|
||
|
|
||
10 years ago
|
selectUnusedColumns: function(columns) {
|
||
10 years ago
|
return WorkPackagesTableHelper.getColumnDifference(
|
||
|
columns, QueryService.getSelectedColumns());
|
||
10 years ago
|
},
|
||
|
|
||
11 years ago
|
loadAvailableColumns: function(projectIdentifier) {
|
||
11 years ago
|
// TODO: Once we have a single page app we need to differentiate between different project columns
|
||
|
if(availableColumns.length) {
|
||
|
return $q.when(availableColumns);
|
||
|
}
|
||
|
|
||
11 years ago
|
var url = projectIdentifier ? PathHelper.apiProjectAvailableColumnsPath(projectIdentifier) : PathHelper.apiAvailableColumnsPath();
|
||
11 years ago
|
|
||
11 years ago
|
return QueryService.doGet(url, function(response){
|
||
|
availableColumns = response.data.available_columns;
|
||
|
return availableColumns;
|
||
11 years ago
|
});
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
getGroupBy: function() {
|
||
|
return query.groupBy;
|
||
|
},
|
||
|
|
||
|
setGroupBy: function(groupBy) {
|
||
|
query.setGroupBy(groupBy);
|
||
10 years ago
|
query.dirty = true;
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
getSelectedColumns: function() {
|
||
11 years ago
|
return this.getQuery().getSelectedColumns();
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
setSelectedColumns: function(selectedColumnNames) {
|
||
10 years ago
|
query.dirty = true;
|
||
11 years ago
|
var currentColumns = this.getSelectedColumns();
|
||
|
|
||
11 years ago
|
this.hideColumns(currentColumns.map(function(column) { return column.name; }));
|
||
|
this.showColumns(selectedColumnNames);
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
updateSortElements: function(sortation) {
|
||
|
return query.updateSortElements(sortation);
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
getSortation: function() {
|
||
|
return query.getSortation();
|
||
|
},
|
||
|
|
||
11 years ago
|
getAvailableFilters: function(projectIdentifier){
|
||
11 years ago
|
// TODO once this is becoming more single-page-app-like keep the available filters of the query model in sync when the project identifier is changed on the scope but the page isn't reloaded
|
||
11 years ago
|
var identifier = 'global';
|
||
|
var getFilters = QueryService.getCustomFieldFilters;
|
||
|
var getFiltersArgs = [];
|
||
|
if(projectIdentifier){
|
||
|
identifier = projectIdentifier;
|
||
|
getFilters = QueryService.getProjectCustomFieldFilters;
|
||
|
getFiltersArgs.push(identifier);
|
||
|
}
|
||
|
if(availableFilters[identifier]){
|
||
|
return $q.when(availableFilters[identifier]);
|
||
|
} else {
|
||
|
return getFilters.apply(this, getFiltersArgs)
|
||
|
.then(function(data){
|
||
|
return QueryService.storeAvailableFilters(identifier, angular.extend(AVAILABLE_WORK_PACKAGE_FILTERS, data.custom_field_filters));
|
||
|
});
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
getProjectCustomFieldFilters: function(projectIdentifier) {
|
||
11 years ago
|
return QueryService.doQuery(PathHelper.apiProjectCustomFieldsPath(projectIdentifier));
|
||
|
},
|
||
|
|
||
11 years ago
|
getCustomFieldFilters: function() {
|
||
|
return QueryService.doQuery(PathHelper.apiCustomFieldsPath());
|
||
|
},
|
||
|
|
||
11 years ago
|
getAvailableFilterValues: function(filterName, projectIdentifier) {
|
||
11 years ago
|
return QueryService.getAvailableFilters(projectIdentifier)
|
||
11 years ago
|
.then(function(filters){
|
||
|
var filter = filters[filterName];
|
||
11 years ago
|
var modelName = filter.modelName;
|
||
11 years ago
|
|
||
11 years ago
|
if(filter.values) {
|
||
11 years ago
|
// Note: We have filter values already because it is a custom field and the server gives the possible values.
|
||
11 years ago
|
var values = filter.values.map(function(value){
|
||
11 years ago
|
if(Array.isArray(value)){
|
||
|
return { id: value[1], name: value[0] };
|
||
|
} else {
|
||
|
return { id: value, name: value };
|
||
|
}
|
||
11 years ago
|
});
|
||
11 years ago
|
return $q.when(QueryService.storeAvailableFilterValues(modelName, values));
|
||
|
}
|
||
|
|
||
|
if(availableFilterValues[modelName]) {
|
||
|
return $q.when(availableFilterValues[modelName]);
|
||
|
} else {
|
||
|
var retrieveAvailableValues;
|
||
|
|
||
|
switch(modelName) {
|
||
|
case 'status':
|
||
|
retrieveAvailableValues = StatusService.getStatuses(projectIdentifier);
|
||
|
break;
|
||
|
case 'type':
|
||
|
retrieveAvailableValues = TypeService.getTypes(projectIdentifier);
|
||
|
break;
|
||
|
case 'priority':
|
||
|
retrieveAvailableValues = PriorityService.getPriorities(projectIdentifier);
|
||
|
break;
|
||
|
case 'user':
|
||
|
retrieveAvailableValues = UserService.getUsers(projectIdentifier);
|
||
|
break;
|
||
|
case 'version':
|
||
10 years ago
|
retrieveAvailableValues = VersionService.getVersions(projectIdentifier);
|
||
11 years ago
|
break;
|
||
10 years ago
|
case 'category':
|
||
|
retrieveAvailableValues = CategoryService.getCategories(projectIdentifier);
|
||
|
break;
|
||
11 years ago
|
case 'role':
|
||
|
retrieveAvailableValues = RoleService.getRoles();
|
||
|
break;
|
||
|
case 'group':
|
||
|
retrieveAvailableValues = GroupService.getGroups();
|
||
|
break;
|
||
|
case 'project':
|
||
|
retrieveAvailableValues = ProjectService.getProjects();
|
||
|
break;
|
||
|
case 'sub_project':
|
||
|
retrieveAvailableValues = ProjectService.getSubProjects(projectIdentifier);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return retrieveAvailableValues.then(function(values) {
|
||
|
return QueryService.storeAvailableFilterValues(modelName, values);
|
||
|
});
|
||
|
}
|
||
11 years ago
|
});
|
||
11 years ago
|
|
||
11 years ago
|
},
|
||
|
|
||
|
storeAvailableFilterValues: function(modelName, values) {
|
||
|
availableFilterValues[modelName] = values;
|
||
|
return values;
|
||
|
},
|
||
|
|
||
11 years ago
|
storeAvailableFilters: function(projectIdentifier, filters){
|
||
|
availableFilters[projectIdentifier] = filters;
|
||
|
return availableFilters[projectIdentifier];
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
// synchronization
|
||
|
|
||
11 years ago
|
saveQuery: function() {
|
||
11 years ago
|
var url = query.project_id ? PathHelper.apiProjectQueryPath(query.project_id, query.id) : PathHelper.apiQueryPath(query.id);
|
||
|
|
||
10 years ago
|
return QueryService.doQuery(url, query.toUpdateParams(), 'PUT', function(response) {
|
||
10 years ago
|
query.dirty = false;
|
||
11 years ago
|
QueryService.fetchAvailableGroupedQueries(query.project_id);
|
||
10 years ago
|
|
||
11 years ago
|
return angular.extend(response.data, { status: { text: I18n.t('js.notice_successful_update') }} );
|
||
11 years ago
|
});
|
||
|
},
|
||
|
|
||
11 years ago
|
saveQueryAs: function(name) {
|
||
|
query.setName(name);
|
||
11 years ago
|
var url = query.project_id ? PathHelper.apiProjectQueriesPath(query.project_id) : PathHelper.apiQueriesPath();
|
||
|
|
||
11 years ago
|
return QueryService.doQuery(url, query.toParams(), 'POST', function(response){
|
||
11 years ago
|
query.save(response.data.query);
|
||
11 years ago
|
QueryService.fetchAvailableGroupedQueries(query.project_id);
|
||
|
|
||
10 years ago
|
// The starred-state does not get saved via the API. So we manually
|
||
|
// set it, if the old query was starred.
|
||
|
if (query.starred) {
|
||
|
QueryService.starQuery();
|
||
|
}
|
||
|
|
||
11 years ago
|
return angular.extend(response.data, { status: { text: I18n.t('js.notice_successful_create') }} );
|
||
11 years ago
|
});
|
||
11 years ago
|
},
|
||
|
|
||
11 years ago
|
deleteQuery: function() {
|
||
10 years ago
|
var url;
|
||
|
if(_.isNull(query.project_id)) {
|
||
|
url = PathHelper.apiQueryPath(query.id);
|
||
|
} else {
|
||
|
url = PathHelper.apiProjectQueryPath(query.project_id, query.id);
|
||
|
}
|
||
11 years ago
|
return QueryService.doQuery(url, query.toUpdateParams(), 'DELETE', function(response){
|
||
11 years ago
|
QueryService.fetchAvailableGroupedQueries(query.project_id);
|
||
|
|
||
10 years ago
|
$rootScope.$broadcast('openproject.layout.removeMenuItem', {
|
||
|
itemType: QUERY_MENU_ITEM_TYPE,
|
||
|
objectId: query.id
|
||
|
});
|
||
11 years ago
|
return angular.extend(response.data, { status: { text: I18n.t('js.notice_successful_delete') }} );
|
||
|
});
|
||
|
},
|
||
|
|
||
10 years ago
|
getQueryPath: function(query) {
|
||
|
if (query.project_id) {
|
||
9 years ago
|
return PathHelper.projectWorkPackagesPath(query.project_id) + '?query_id=' + query.id;
|
||
10 years ago
|
} else {
|
||
9 years ago
|
return PathHelper.workPackagesPath() + '?query_id=' + query.id;
|
||
10 years ago
|
}
|
||
|
},
|
||
|
|
||
|
addOrRemoveMenuItem: function(query) {
|
||
|
if (!query) return;
|
||
|
if(query.starred) {
|
||
10 years ago
|
queryMenuItemFactory
|
||
|
.generateMenuItem(query.name, QueryService.getQueryPath(query), query.id)
|
||
|
.then(function() {
|
||
|
$rootScope.$broadcast('openproject.layout.activateMenuItem', {
|
||
|
itemType: QUERY_MENU_ITEM_TYPE,
|
||
|
objectId: query.id
|
||
|
});
|
||
|
});
|
||
|
|
||
10 years ago
|
} else {
|
||
|
$rootScope.$broadcast('openproject.layout.removeMenuItem', {
|
||
|
itemType: QUERY_MENU_ITEM_TYPE,
|
||
|
objectId: query.id
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
|
||
|
toggleQueryStarred: function(query) {
|
||
11 years ago
|
if(query.starred) {
|
||
|
return QueryService.unstarQuery();
|
||
|
} else {
|
||
|
return QueryService.starQuery();
|
||
|
}
|
||
|
},
|
||
|
|
||
|
starQuery: function() {
|
||
|
var url = PathHelper.apiQueryStarPath(query.id);
|
||
|
var theQuery = query;
|
||
|
|
||
10 years ago
|
var success = function(response){
|
||
11 years ago
|
theQuery.star();
|
||
10 years ago
|
QueryService.addOrRemoveMenuItem(theQuery);
|
||
11 years ago
|
return response.data;
|
||
10 years ago
|
};
|
||
|
|
||
|
var failure = function(response){
|
||
|
var msg = undefined;
|
||
|
|
||
|
if(response.data.errors) {
|
||
|
msg = response.data.errors.join(", ");
|
||
|
}
|
||
|
|
||
|
return QueryService.failure(msg)(response);
|
||
|
};
|
||
|
|
||
|
return QueryService.doPatch(url, success, failure);
|
||
11 years ago
|
},
|
||
|
|
||
|
unstarQuery: function() {
|
||
|
var url = PathHelper.apiQueryUnstarPath(query.id);
|
||
|
var theQuery = query;
|
||
|
|
||
|
return QueryService.doPatch(url, function(response){
|
||
|
theQuery.unstar();
|
||
10 years ago
|
QueryService.addOrRemoveMenuItem(theQuery);
|
||
11 years ago
|
return response.data;
|
||
|
});
|
||
|
},
|
||
|
|
||
11 years ago
|
updateHighlightName: function() {
|
||
|
// TODO Implement an API endpoint for updating the names or add an appropriate endpoint that returns query names for all highlighted queries
|
||
|
return this.unstarQuery().then(this.starQuery);
|
||
|
},
|
||
|
|
||
11 years ago
|
doGet: function(url, success, failure) {
|
||
|
return QueryService.doQuery(url, null, 'GET', success, failure);
|
||
|
},
|
||
|
|
||
11 years ago
|
doPatch: function(url, success, failure) {
|
||
|
return QueryService.doQuery(url, null, 'PATCH', success, failure);
|
||
|
},
|
||
|
|
||
11 years ago
|
doQuery: function(url, params, method, success, failure) {
|
||
11 years ago
|
method = method || 'GET';
|
||
11 years ago
|
success = success || function(response){
|
||
11 years ago
|
return response.data;
|
||
|
};
|
||
10 years ago
|
failure = failure || QueryService.failure();
|
||
11 years ago
|
|
||
11 years ago
|
return $http({
|
||
11 years ago
|
method: method,
|
||
11 years ago
|
url: url,
|
||
11 years ago
|
params: params,
|
||
11 years ago
|
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
|
||
11 years ago
|
}).then(success, failure);
|
||
10 years ago
|
},
|
||
|
|
||
|
failure: function(msg){
|
||
|
msg = msg || I18n.t('js.notice_bad_request');
|
||
|
return function(response){
|
||
|
return angular.extend(response, { status: { text: msg, isError: true }} );
|
||
10 years ago
|
};
|
||
11 years ago
|
}
|
||
|
};
|
||
|
|
||
|
return QueryService;
|
||
9 years ago
|
}
|
||
|
|
||
|
angular
|
||
|
.module('openproject.services')
|
||
|
.factory('QueryService', QueryService);
|