Search in sources :

Example 1 with EntityNotFoundException

use of org.summerb.easycrud.api.exceptions.EntityNotFoundException in project summerb by skarpushin.

the class EasyCrudM2mServiceImpl method removeReferencee.

@Override
public void removeReferencee(T1Id referencerId, T2Id referenceeId) throws NotAuthorizedException {
    try {
        Query q = Query.n();
        addEqQuery(ManyToManyDto.FN_SRC, referencerId, q);
        addEqQuery(ManyToManyDto.FN_DST, referenceeId, q);
        ManyToManyDto<T1Id, T2Id> pair = findOneByQuery(q);
        try {
            if (pair == null) {
                throw new GenericEntityNotFoundException(getEntityTypeMessageCode(), "" + referencerId + "<->" + referenceeId);
            }
            deleteById(pair.getId());
        } catch (EntityNotFoundException e) {
        // that's ok, we wanted it to not exist, it's not there. This
        // state
        // is acceptable
        }
    } catch (Throwable t) {
        Throwables.throwIfInstanceOf(t, NotAuthorizedException.class);
        throw new RuntimeException("Failed to remove reference from " + serviceFrom.getEntityTypeMessageCode() + " identified by " + referencerId + " to " + serviceTo.getEntityTypeMessageCode() + " identified by " + referenceeId, t);
    }
}
Also used : Query(org.summerb.easycrud.api.query.Query) GenericEntityNotFoundException(org.summerb.easycrud.api.exceptions.GenericEntityNotFoundException) GenericEntityNotFoundException(org.summerb.easycrud.api.exceptions.GenericEntityNotFoundException) EntityNotFoundException(org.summerb.easycrud.api.exceptions.EntityNotFoundException) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException)

Example 2 with EntityNotFoundException

use of org.summerb.easycrud.api.exceptions.EntityNotFoundException in project summerb by skarpushin.

the class EasyCrudRestControllerBase method resolveReferences.

protected void resolveReferences(List<String> referencesToResolve, CrudQueryResult<TId, TDto> ret, List<TDto> items) throws EntityNotFoundException, NotAuthorizedException {
    Preconditions.checkState(dataSetLoader != null, "DataSetLoader is required to resolve references");
    Preconditions.checkState(referencesRegistry != null, "referencesRegistry is required to resolve references");
    DataSet ds = new DataSet();
    DataTable<TId, TDto> table = new DataTable<>(service.getEntityTypeMessageCode());
    table.putAll(items);
    ds.getTables().put(table.getName(), table);
    List<Ref> references = referencesToResolve.stream().map(name -> referencesRegistry.getRefByName(name)).collect(Collectors.toList());
    Ref[] refsArr = (Ref[]) references.toArray(new Ref[references.size()]);
    dataSetLoader.resolveReferencedObjects(ds, refsArr);
    // now remove initial table from dataset because we don't want to
    // duplicate this. It's already populated to rows
    ds.getTables().remove(table.getName());
    // x. ret
    ret.setRefsResolved(references.stream().collect(Collectors.toMap(Ref::getName, Function.identity())));
    ret.setRefs(ds);
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) EasyCrudQueryParams(org.summerb.easycrud.mvc.model.EasyCrudQueryParams) DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) Autowired(org.springframework.beans.factory.annotation.Autowired) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) PermissionsResolverStrategy(org.summerb.easycrud.rest.permissions.PermissionsResolverStrategy) Ref(org.summerb.easycrud.api.dto.relations.Ref) ReferencesRegistry(org.summerb.easycrud.api.relations.ReferencesRegistry) Function(java.util.function.Function) InitializingBean(org.springframework.beans.factory.InitializingBean) RequestBody(org.springframework.web.bind.annotation.RequestBody) OrderBy(org.summerb.easycrud.api.query.OrderBy) PutMapping(org.springframework.web.bind.annotation.PutMapping) EntityNotFoundException(org.summerb.easycrud.api.exceptions.EntityNotFoundException) GenericFilterBean(org.springframework.web.filter.GenericFilterBean) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException) GetMapping(org.springframework.web.bind.annotation.GetMapping) FilteringParamsToQueryConverterImpl(org.summerb.easycrud.mvc.filter.FilteringParamsToQueryConverterImpl) QueryNarrowerStrategy(org.summerb.easycrud.rest.querynarrower.QueryNarrowerStrategy) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Query(org.summerb.easycrud.api.query.Query) MultipleItemsResult(org.summerb.easycrud.rest.dto.MultipleItemsResult) PostMapping(org.springframework.web.bind.annotation.PostMapping) SingleItemResult(org.summerb.easycrud.rest.dto.SingleItemResult) MediaType(org.springframework.http.MediaType) BeansException(org.springframework.beans.BeansException) HasId(org.summerb.easycrud.api.dto.HasId) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ApiIgnore(springfox.documentation.annotations.ApiIgnore) List(java.util.List) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) CollectionUtils(org.springframework.util.CollectionUtils) PathVariablesMap(org.summerb.easycrud.rest.commonpathvars.PathVariablesMap) PagerParams(org.summerb.easycrud.api.dto.PagerParams) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) CrudQueryResult(org.summerb.easycrud.rest.dto.CrudQueryResult) Preconditions(com.google.common.base.Preconditions) ApplicationContextAware(org.springframework.context.ApplicationContextAware) FilteringParamsToQueryConverter(org.summerb.easycrud.mvc.filter.FilteringParamsToQueryConverter) DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) Ref(org.summerb.easycrud.api.dto.relations.Ref) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet)

Aggregations

EntityNotFoundException (org.summerb.easycrud.api.exceptions.EntityNotFoundException)2 Query (org.summerb.easycrud.api.query.Query)2 Preconditions (com.google.common.base.Preconditions)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 BeansException (org.springframework.beans.BeansException)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ApplicationContextAware (org.springframework.context.ApplicationContextAware)1 MediaType (org.springframework.http.MediaType)1 CollectionUtils (org.springframework.util.CollectionUtils)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1 PathVariable (org.springframework.web.bind.annotation.PathVariable)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1 RequestBody (org.springframework.web.bind.annotation.RequestBody)1