SysGroupManagementController.java
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.api.v1.impl;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.QSysGroup;
import org.gringlobal.model.QSysUser;
import org.gringlobal.model.SysGroup;
import org.gringlobal.model.SysUser;
import org.gringlobal.service.SysGroupService;
import org.gringlobal.service.filter.SysGroupFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController("groupManagementApi1")
@RequestMapping(SysGroupManagementController.API_URL)
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Tag(name = "UserAdmin")
public class SysGroupManagementController extends FilteredCRUDController<SysGroup, SysGroupService, SysGroupFilter> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv1_ADMIN_BASE + "/group";
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QSysGroup.sysGroup.groupTag.asc() };
}
@Override
protected Class<SysGroupFilter> filterType() {
return SysGroupFilter.class;
}
protected OrderSpecifier<?>[] defaultMemberSort() {
return new OrderSpecifier[] { QSysUser.sysUser.username.asc() };
}
@PostMapping(value = "/{id}/members", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "listMembers", description = "Fetch members of the group", summary = "List or filter group users")
public Page<SysUser> filterItems(@PathVariable("id") final Long groupId, @ParameterObject final Pagination page) throws SearchException {
final SysGroup sysInvGroup = crudService.get(groupId);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultMemberSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return crudService.listMembers(sysInvGroup, pageable);
}
/**
* Create a new sys group.
*
* @param groupTag the groupTag
* @return the sys group.
*/
@PostMapping(value = "/create")
public SysGroup create(@RequestParam(name = "groupTag") final String groupTag) {
return crudService.create(new SysGroup(groupTag));
}
/**
* Enable sys group.
*
* @param id the id
* @return the group
*/
@PostMapping(value = ENDPOINT_ID + "/enable")
public SysGroup enableGroup(@PathVariable("id") final long id) {
return crudService.enable(id);
}
/**
* Disable sys group.
*
* @param id the id
* @return the group
*/
@PostMapping(value = ENDPOINT_ID + "/disable")
public SysGroup disableGroup(@PathVariable("id") final long id) {
return crudService.disable(id);
}
@Override
public SysGroup remove(@PathVariable(value = "id") long id) {
return super.remove(id);
}
@Override
public FilteredPage<SysGroup, SysGroupFilter> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page, @RequestBody(required = false) SysGroupFilter filter) throws IOException, SearchException {
return super.filter(filterCode, page, filter);
}
/**
* Update sys group tag.
*
* @param id the id of sys group
* @param groupTag the tag of sys group for update
* @return the updated group
*/
@PostMapping(value = ENDPOINT_ID + "/groupTag/{groupTag}")
public SysGroup updateGroupTag(@PathVariable("id") final long id, @PathVariable final String groupTag) {
final SysGroup sysGroup = crudService.get(id);
return crudService.updateGroupTag(sysGroup, groupTag);
}
}