Search in sources :

Example 1 with DataTable

use of org.summerb.easycrud.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());
        @SuppressWarnings("deprecation") 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.easycrud.api.dto.datapackage.DataTable) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) RefToReferencedObjectsIdsMap(org.summerb.easycrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) HasId(org.summerb.easycrud.api.dto.HasId) Ref(org.summerb.easycrud.api.dto.relations.Ref) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) RowIdToBackReferencesMap(org.summerb.easycrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) Map(java.util.Map) RefToReferencedObjectsIdsMap(org.summerb.easycrud.api.dto.datapackage.DataTable.RefToReferencedObjectsIdsMap) RowIdToBackReferencesMap(org.summerb.easycrud.api.dto.datapackage.DataTable.RowIdToBackReferencesMap) HashSet(java.util.HashSet)

Example 2 with DataTable

use of org.summerb.easycrud.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.easycrud.api.dto.datapackage.DataTable) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with DataTable

use of org.summerb.easycrud.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.easycrud.api.dto.datapackage.DataTable) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) PropertyAccessor(org.springframework.beans.PropertyAccessor) HashMap(java.util.HashMap) HasId(org.summerb.easycrud.api.dto.HasId) Ref(org.summerb.easycrud.api.dto.relations.Ref) HashSet(java.util.HashSet)

Example 4 with DataTable

use of org.summerb.easycrud.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.easycrud.api.dto.HasId) DataTable(org.summerb.easycrud.api.dto.datapackage.DataTable) EasyCrudService(org.summerb.easycrud.api.EasyCrudService)

Example 5 with DataTable

use of org.summerb.easycrud.api.dto.datapackage.DataTable 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

DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)5 HasId (org.summerb.easycrud.api.dto.HasId)4 List (java.util.List)3 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)3 DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)3 Ref (org.summerb.easycrud.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.easycrud.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