Search in sources :

Example 1 with DataTable

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable in project summerb by skarpushin.

the class DataSetLoaderImpl method enumerateOutgoingReferences.

/**
 * @param scanForReferences
 *            dataSet to scan for Many2one & One2one referenced objects
 * @param checkForExistence
 *            data set that contains already loaded objects so that we can
 *            skip loading these objects again
 * @param references
 *            references to use
 * @return map entityTypeCode to list of ids of these entities to be loaded
 */
private Map<String, Set<Object>> enumerateOutgoingReferences(DataSet scanForReferences, DataSet checkForExistence, Ref[] references) {
    Map<String, Set<Object>> ret = new HashMap<>();
    for (DataTable table : scanForReferences.getTables().values()) {
        List<Ref> outgoingRefs = enumOutgoingRefsToTableOrNull(references, table.getName());
        if (outgoingRefs == null) {
            continue;
        }
        for (Object rowObj : table.getRows().values()) {
            HasId row = (HasId) rowObj;
            PropertyAccessor propertyAccessor = PropertyAccessorFactory.forBeanPropertyAccess(row);
            for (Ref ref : outgoingRefs) {
                Object referencedId = null;
                try {
                    referencedId = propertyAccessor.getPropertyValue(ref.getFromField());
                } catch (Throwable t) {
                    throw new RuntimeException("Failed to read property " + ref.getFromField() + " from " + row, t);
                }
                if (referencedId == null) {
                    continue;
                }
                if (checkForExistence.get(ref.getToEntity()).find(referencedId) != null) {
                    // that one is already loaded, skip
                    continue;
                }
                Set<Object> referencedIds = ret.get(ref.getToEntity());
                if (referencedIds == null) {
                    ret.put(ref.getToEntity(), referencedIds = new HashSet<>());
                }
                referencedIds.add(referencedId);
            }
        }
    }
    return ret;
}
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) PropertyAccessor(org.springframework.beans.PropertyAccessor) HashMap(java.util.HashMap) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) HashSet(java.util.HashSet)

Example 2 with DataTable

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable 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 3 with DataTable

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable 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 4 with DataTable

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable in project summerb by skarpushin.

the class DataSetUpdaterOnEntityChangedEventImpl method updateDataSet.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void updateDataSet(DataSet dataSet, EntityChangedEvent<?> e) {
    if (!e.isTypeOf(HasId.class)) {
        return;
    }
    EasyCrudService service = easyCrudServiceResolver.resolveByDtoClass(e.getValue().getClass());
    if (!dataSet.getTables().containsKey(service.getEntityTypeMessageCode())) {
        return;
    }
    DataTable table = dataSet.getTables().get(service.getEntityTypeMessageCode());
    HasId dto = (HasId) e.getValue();
    if (e.getChangeType() == ChangeType.REMOVED) {
        table.getRows().remove(dto.getId());
    } else {
        table.put(dto);
    }
}
Also used : HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService)

Example 5 with DataTable

use of org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable in project summerb by skarpushin.

the class DataSetLoaderImpl method addAllObjects.

private void addAllObjects(EntityTypeToObjectsMap loadedObjects, DataSet dataSet) {
    for (Entry<String, List<HasId>> entry : loadedObjects.entrySet()) {
        DataTable table = dataSet.get(entry.getKey());
        table.putAll(entry.getValue());
    }
}
Also used : DataTable(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List)

Aggregations

DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)5 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)4 List (java.util.List)3 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)3 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)3 Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 EasyCrudService (org.summerb.approaches.jdbccrud.api.EasyCrudService)2 Preconditions (com.google.common.base.Preconditions)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 BeansException (org.springframework.beans.BeansException)1 PropertyAccessor (org.springframework.beans.PropertyAccessor)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1