Search in sources :

Example 1 with CategoryDto

use of org.entando.entando.aps.system.services.category.model.CategoryDto in project entando-core by entando.

the class CategoryController method getCategories.

@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RestResponse> getCategories(@RequestParam(value = "parentCode", required = false, defaultValue = "home") String parentCode) {
    logger.debug("getting category tree for parent {}", parentCode);
    List<CategoryDto> result = this.getCategoryService().getTree(parentCode);
    Map<String, String> metadata = new HashMap<>();
    metadata.put("parentCode", parentCode);
    return new ResponseEntity<>(new RestResponse(result, new ArrayList<>(), metadata), HttpStatus.OK);
}
Also used : CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) RestResponse(org.entando.entando.web.common.model.RestResponse) ArrayList(java.util.ArrayList) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CategoryDto

use of org.entando.entando.aps.system.services.category.model.CategoryDto in project entando-core by entando.

the class CategoryController method getCategory.

@RestAccessControl(permission = Permission.MANAGE_PAGES)
@RequestMapping(value = "/{categoryCode}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RestResponse> getCategory(@PathVariable String categoryCode) {
    logger.debug("getting category {}", categoryCode);
    CategoryDto category = this.getCategoryService().getCategory(categoryCode);
    return new ResponseEntity<>(new RestResponse(category, new ArrayList<>(), new HashMap<>()), HttpStatus.OK);
}
Also used : CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) RestResponse(org.entando.entando.web.common.model.RestResponse) ArrayList(java.util.ArrayList) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with CategoryDto

use of org.entando.entando.aps.system.services.category.model.CategoryDto in project entando-core by entando.

the class CategoryController method addCategory.

@RestAccessControl(permission = Permission.SUPERUSER)
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<RestResponse> addCategory(@Valid @RequestBody CategoryDto categoryRequest, BindingResult bindingResult) throws ApsSystemException {
    // field validations
    this.getCategoryValidator().validate(categoryRequest, bindingResult);
    if (bindingResult.hasErrors()) {
        throw new ValidationGenericException(bindingResult);
    }
    // business validations
    this.getCategoryValidator().validatePostReferences(categoryRequest, bindingResult);
    CategoryDto category = this.getCategoryService().addCategory(categoryRequest);
    return new ResponseEntity<>(new RestResponse(category, new ArrayList<>(), new HashMap<>()), HttpStatus.OK);
}
Also used : CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) RestResponse(org.entando.entando.web.common.model.RestResponse) ArrayList(java.util.ArrayList) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestAccessControl(org.entando.entando.web.common.annotation.RestAccessControl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with CategoryDto

use of org.entando.entando.aps.system.services.category.model.CategoryDto in project entando-core by entando.

the class CategoryService method getTree.

@Override
public List<CategoryDto> getTree(String parentCode) {
    List<CategoryDto> res = new ArrayList<>();
    Category parent = this.getCategoryManager().getCategory(parentCode);
    if (null == parent) {
        throw new RestRourceNotFoundException(CategoryValidator.ERRCODE_PARENT_CATEGORY_NOT_FOUND, "category", parentCode);
    }
    Optional.ofNullable(parent.getChildrenCodes()).ifPresent(children -> Arrays.asList(children).forEach(childCode -> {
        Category child = this.getCategoryManager().getCategory(childCode);
        CategoryDto childDto = this.getDtoBuilder().convert(child);
        childDto.setChildren(Arrays.asList(child.getChildrenCodes()));
        res.add(childDto);
    }));
    return res;
}
Also used : CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) LoggerFactory(org.slf4j.LoggerFactory) CategoryValidator(org.entando.entando.web.category.validator.CategoryValidator) Autowired(org.springframework.beans.factory.annotation.Autowired) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) DtoBuilder(org.entando.entando.aps.system.services.DtoBuilder) ArrayList(java.util.ArrayList) List(java.util.List) RestListRequest(org.entando.entando.web.common.model.RestListRequest) RestServerError(org.entando.entando.aps.system.exception.RestServerError) SearcherDaoPaginatedResult(com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult) PagedMetadata(org.entando.entando.web.common.model.PagedMetadata) CategoryUtilizer(com.agiletec.aps.system.services.category.CategoryUtilizer) IManager(com.agiletec.aps.system.common.IManager) Optional(java.util.Optional) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Category(com.agiletec.aps.system.services.category.Category) ICategoryManager(com.agiletec.aps.system.services.category.ICategoryManager) IDtoBuilder(org.entando.entando.aps.system.services.IDtoBuilder) Category(com.agiletec.aps.system.services.category.Category) ArrayList(java.util.ArrayList)

Example 5 with CategoryDto

use of org.entando.entando.aps.system.services.category.model.CategoryDto in project entando-core by entando.

the class CategoryService method addCategory.

@Override
public CategoryDto addCategory(CategoryDto categoryDto) {
    Category parentCategory = this.getCategoryManager().getCategory(categoryDto.getParentCode());
    if (null == parentCategory) {
        throw new RestRourceNotFoundException(CategoryValidator.ERRCODE_PARENT_CATEGORY_NOT_FOUND, "parent category", categoryDto.getParentCode());
    }
    Category category = this.getCategoryManager().getCategory(categoryDto.getCode());
    if (null != category) {
        BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(category, "category");
        bindingResult.reject(CategoryValidator.ERRCODE_CATEGORY_ALREADY_EXISTS, new String[] { category.getCode() }, "category.exists");
        throw new ValidationGenericException(bindingResult);
    }
    CategoryDto dto = null;
    try {
        Category categoryToAdd = new Category();
        categoryToAdd.setCode(categoryDto.getCode());
        categoryToAdd.setParentCode(categoryDto.getParentCode());
        categoryToAdd.getTitles().putAll(categoryDto.getTitles());
        this.getCategoryManager().addCategory(categoryToAdd);
        dto = this.getDtoBuilder().convert(this.getCategoryManager().getCategory(categoryDto.getCode()));
    } catch (Exception e) {
        logger.error("error adding category " + categoryDto.getCode(), e);
        throw new RestServerError("error adding category " + categoryDto.getCode(), e);
    }
    return dto;
}
Also used : RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) CategoryDto(org.entando.entando.aps.system.services.category.model.CategoryDto) Category(com.agiletec.aps.system.services.category.Category) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RestServerError(org.entando.entando.aps.system.exception.RestServerError) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException) RestRourceNotFoundException(org.entando.entando.aps.system.exception.RestRourceNotFoundException) ValidationGenericException(org.entando.entando.web.common.exceptions.ValidationGenericException)

Aggregations

CategoryDto (org.entando.entando.aps.system.services.category.model.CategoryDto)8 ArrayList (java.util.ArrayList)6 ValidationGenericException (org.entando.entando.web.common.exceptions.ValidationGenericException)6 Category (com.agiletec.aps.system.services.category.Category)4 HashMap (java.util.HashMap)4 RestRourceNotFoundException (org.entando.entando.aps.system.exception.RestRourceNotFoundException)4 RestServerError (org.entando.entando.aps.system.exception.RestServerError)4 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)4 RestResponse (org.entando.entando.web.common.model.RestResponse)4 ResponseEntity (org.springframework.http.ResponseEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 CategoryUtilizer (com.agiletec.aps.system.services.category.CategoryUtilizer)2 List (java.util.List)2 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)2 IManager (com.agiletec.aps.system.common.IManager)1 SearcherDaoPaginatedResult (com.agiletec.aps.system.common.model.dao.SearcherDaoPaginatedResult)1 ICategoryManager (com.agiletec.aps.system.services.category.ICategoryManager)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 DtoBuilder (org.entando.entando.aps.system.services.DtoBuilder)1