Search in sources :

Example 6 with Ref

use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.

the class DataSetLoaderImpl method populateBackReferencesMany2Many.

private void populateBackReferencesMany2Many(ManyToManyRefToReferenceesMap manyToManyRefs, DataSet dataSet) {
    for (Entry<Ref, Map<Object, List<HasId>>> refToReferenceeListPair : manyToManyRefs.entrySet()) {
        DataTable sourceTable = dataSet.get(refToReferenceeListPair.getKey().getFromEntity());
        RowIdToBackReferencesMap backRefs = sourceTable.getBackRefs();
        for (Entry<Object, List<HasId>> referencerToReferencesListPair : refToReferenceeListPair.getValue().entrySet()) {
            Object referencerId = referencerToReferencesListPair.getKey();
            RefToReferencedObjectsIdsMap refToObjsMap = backRefs.get(referencerId);
            if (refToObjsMap == null) {
                backRefs.put(referencerId, refToObjsMap = new RefToReferencedObjectsIdsMap());
            }
            String refName = refToReferenceeListPair.getKey().getName();
            Set<Object> referenceeIdsList = refToObjsMap.get(refName);
            if (referenceeIdsList == null) {
                refToObjsMap.put(refName, referenceeIdsList = new HashSet<>());
            }
            Set referenceeIds = EasyCrudDtoUtils.enumerateIds(referencerToReferencesListPair.getValue());
            referenceeIdsList.addAll(referenceeIds);
        }
    }
}
Also used : DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) RefToReferencedObjectsIdsMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List) HashMap(java.util.HashMap) RowIdToBackReferencesMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) Map(java.util.Map) RefToReferencedObjectsIdsMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) RowIdToBackReferencesMap(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) HashSet(java.util.HashSet)

Example 7 with Ref

use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.

the class EasyCrudRestControllerBase method resolveReferences.

private 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) PermissionsResolverStrategy(org.summerb.approaches.jdbccrud.rest.permissions.PermissionsResolverStrategy) Arrays(java.util.Arrays) RequestParam(org.springframework.web.bind.annotation.RequestParam) SingleItemResult(org.summerb.approaches.jdbccrud.rest.dto.SingleItemResult) RestExceptionTranslator(org.summerb.approaches.springmvc.security.implsrest.RestExceptionTranslator) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Autowired(org.springframework.beans.factory.annotation.Autowired) DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) Function(java.util.function.Function) InitializingBean(org.springframework.beans.factory.InitializingBean) PagerParams(org.summerb.approaches.jdbccrud.api.dto.PagerParams) RequestBody(org.springframework.web.bind.annotation.RequestBody) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) PutMapping(org.springframework.web.bind.annotation.PutMapping) GenericFilterBean(org.springframework.web.filter.GenericFilterBean) DataSetLoader(org.summerb.approaches.jdbccrud.api.relations.DataSetLoader) GetMapping(org.springframework.web.bind.annotation.GetMapping) OrderBy(org.summerb.approaches.jdbccrud.api.query.OrderBy) MultipleItemsResult(org.summerb.approaches.jdbccrud.rest.dto.MultipleItemsResult) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ReferencesRegistry(org.summerb.approaches.jdbccrud.api.relations.ReferencesRegistry) PostMapping(org.springframework.web.bind.annotation.PostMapping) PathVariablesMap(org.summerb.approaches.jdbccrud.rest.commonpathvars.PathVariablesMap) MediaType(org.springframework.http.MediaType) FilteringParamsToQueryConverterImpl(org.summerb.approaches.jdbccrud.mvc.filter.FilteringParamsToQueryConverterImpl) BeansException(org.springframework.beans.BeansException) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) QueryNarrowerStrategy(org.summerb.approaches.jdbccrud.rest.querynarrower.QueryNarrowerStrategy) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) Query(org.summerb.approaches.jdbccrud.api.query.Query) FilteringParamsToQueryConverter(org.summerb.approaches.jdbccrud.mvc.filter.FilteringParamsToQueryConverter) EasyCrudQueryParams(org.summerb.approaches.jdbccrud.mvc.model.EasyCrudQueryParams) CrudQueryResult(org.summerb.approaches.jdbccrud.rest.dto.CrudQueryResult) NotAuthorizedException(org.summerb.approaches.security.api.exceptions.NotAuthorizedException) ApiIgnore(springfox.documentation.annotations.ApiIgnore) List(java.util.List) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) CollectionUtils(org.springframework.util.CollectionUtils) EntityNotFoundException(org.summerb.approaches.jdbccrud.api.exceptions.EntityNotFoundException) Preconditions(com.google.common.base.Preconditions) ApplicationContextAware(org.springframework.context.ApplicationContextAware) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService) DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)

Example 8 with Ref

use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.

the class ReferencesRegistryPreconfiguredAbstract method initialize.

private void initialize() {
    Multimap<String, Ref> refsBySourceNameLocal = discoverRefsBySources();
    Preconditions.checkState(refsBySourceNameLocal != null);
    Map<String, Ref> refsByNameLocal = new HashMap<>();
    for (Ref ref : refsBySourceNameLocal.values()) {
        Ref previous = refsByNameLocal.put(ref.getName(), ref);
        if (previous != null) {
            throw new RuntimeException("Duplicate reference name not allowed " + ref.getName());
        }
    }
    Multimap<String, Ref> refsByAliasLocal = discoverAliases();
    Preconditions.checkState(refsByAliasLocal != null);
    refsBySourceName = refsBySourceNameLocal;
    refsByName = refsByNameLocal;
    refsByAlias = refsByAliasLocal;
}
Also used : Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashMap(java.util.HashMap)

Example 9 with Ref

use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.

the class DataSetLoaderImpl method loadOneToManyReferences.

private EntityTypeToObjectsMap loadOneToManyReferences(Map<Ref, Set<Object>> refToReferencersIds) throws NotAuthorizedException {
    Multimap<String, Entry<Ref, Set<Object>>> targetEntityToRef = HashMultimap.create();
    for (Entry<Ref, Set<Object>> entry : refToReferencersIds.entrySet()) {
        targetEntityToRef.put(entry.getKey().getToEntity(), entry);
    }
    EntityTypeToObjectsMap ret = new EntityTypeToObjectsMap();
    for (String entityTypeCode : targetEntityToRef.keySet()) {
        Collection<Entry<Ref, Set<Object>>> entries = targetEntityToRef.get(entityTypeCode);
        List<Query> queries = new ArrayList<>(entries.size());
        for (Entry<Ref, Set<Object>> entry : entries) {
            Set<Object> ids = entry.getValue();
            Ref ref = entry.getKey();
            Object firstId = ids.iterator().next();
            if (firstId instanceof Long) {
                queries.add(Query.n().in(ref.getToField(), ids.toArray(new Long[0])));
            } else if (firstId instanceof String) {
                queries.add(Query.n().in(ref.getToField(), ids.toArray(new String[0])));
            } else {
                throw new IllegalStateException("such id type is not supported: " + firstId.getClass());
            }
        }
        Query q = queries.size() == 1 ? queries.get(0) : Query.n().or(queries.toArray(new Query[0]));
        EasyCrudService service = easyCrudServiceResolver.resolveByEntityType(entityTypeCode);
        PaginatedList<HasId> results = service.query(PagerParams.ALL, q);
        ret.put(entityTypeCode, new ArrayList<>(results.getItems()));
    }
    return ret;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) Query(org.summerb.approaches.jdbccrud.api.query.Query) ArrayList(java.util.ArrayList) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Entry(java.util.Map.Entry) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService)

Example 10 with Ref

use of org.summerb.approaches.jdbccrud.api.dto.relations.Ref in project summerb by skarpushin.

the class DataSetLoaderImpl method resolveReferencedObjects.

@Override
public void resolveReferencedObjects(DataSet dataSet, Ref... references) throws EntityNotFoundException, NotAuthorizedException {
    Preconditions.checkArgument(dataSet != null);
    Preconditions.checkArgument(references != null);
    DataSet nextDataSet = dataSet;
    while (nextDataSet != null) {
        DataSet curDataSet = nextDataSet;
        nextDataSet = null;
        DataSet newDataSet = new DataSet();
        Map<String, Set<Object>> idsToLoad = enumerateOutgoingReferences(curDataSet, dataSet, references);
        if (!idsToLoad.isEmpty()) {
            EntityTypeToObjectsMap loadObjects = loadObjectsByIds(idsToLoad);
            addAllObjects(loadObjects, dataSet);
            addAllObjects(loadObjects, newDataSet);
        }
        // Handle one2many
        Map<Ref, Set<Object>> oneToManyReferencesToLoad = enumerateOneToManyReferences(curDataSet, references);
        if (!oneToManyReferencesToLoad.isEmpty()) {
            EntityTypeToObjectsMap loadedOneToManyRefs = loadOneToManyReferences(oneToManyReferencesToLoad);
            addAllObjects(loadedOneToManyRefs, dataSet);
            addAllObjects(loadedOneToManyRefs, newDataSet);
            populateBackReferencesOne2Many(loadedOneToManyRefs, oneToManyReferencesToLoad, dataSet);
        }
        // Handle many2many
        Map<Ref, Set<Object>> manyToManyRefToReferencersIds = enumerateManyToManyReferences(curDataSet, references);
        if (!manyToManyRefToReferencersIds.isEmpty()) {
            ManyToManyRefToReferenceesMap loadedManyToManyRefs = loadManyToManyReferences(manyToManyRefToReferencersIds);
            addAllObjects(loadedManyToManyRefs, dataSet);
            addAllObjects(loadedManyToManyRefs, newDataSet);
            populateBackReferencesMany2Many(loadedManyToManyRefs, dataSet);
        }
        // next iteration
        if (!newDataSet.isEmpty()) {
            nextDataSet = newDataSet;
        }
    }
}
Also used : Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)

Aggregations

Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)11 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)8 HashSet (java.util.HashSet)7 Set (java.util.Set)7 HashMap (java.util.HashMap)5 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)4 LinkedList (java.util.LinkedList)3 EasyCrudService (org.summerb.approaches.jdbccrud.api.EasyCrudService)3 DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)3 PropertyAccessor (org.springframework.beans.PropertyAccessor)2 RefToReferencedObjectsIdsMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap)2 RowIdToBackReferencesMap (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap)2 Query (org.summerb.approaches.jdbccrud.api.query.Query)2 Preconditions (com.google.common.base.Preconditions)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1