Search in sources :

Example 16 with DataSet

use of org.summerb.easycrud.api.dto.datapackage.DataSet in project summerb by skarpushin.

the class DomLoaderImpl method load.

/*
	 * (non-Javadoc)
	 * 
	 * @see ru.skarpushin.smarthome.devicesgate.services.envconfig.impl.DomLoader#
	 * load(java.lang.Class, java.util.Set,
	 * org.summerb.easycrud.api.dto.relations.Ref)
	 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <TId, TRowClass extends HasId<TId>, TDomClass extends TRowClass> List<TDomClass> load(Class<TDomClass> rootDomClass, Set<TId> ids, Ref... refsToResolve) {
    try {
        Preconditions.checkArgument(rootDomClass != null, "rootDomClass must not be empty");
        Preconditions.checkArgument(ids != null && ids.size() > 0, "List of ids must not be empty");
        // Resolve root row entity name (MessageCode)
        String dtoMessageCode = resolveDtoMessageCodeFromDomClass(rootDomClass);
        // Use DataSetLoader to load data
        List<HasId> loadedRoots = dataSetLoader.loadObjectsByIds((Set<Object>) ids, dtoMessageCode);
        DataSet ds = new DataSet();
        ds.get(dtoMessageCode).putAll(loadedRoots);
        boolean haveRefsToLoad = refsToResolve != null && refsToResolve.length > 0;
        if (haveRefsToLoad) {
            dataSetLoader.resolveReferencedObjects(ds, refsToResolve);
        }
        Map<String, Ref> refs = !haveRefsToLoad ? Collections.emptyMap() : Arrays.stream(refsToResolve).collect(Collectors.toMap(x -> x.getName(), x -> x));
        // Construct root Dom objects
        Collection<TRowClass> rootRows = ds.get(dtoMessageCode).getRows().values();
        // map of already converted entities
        Map<EntityAndId, HasId> cache = new HashMap<>();
        List<TDomClass> ret = rootRows.stream().map(mapDtoToDom(dtoMessageCode, rootDomClass, refs, ds, cache)).collect(Collectors.toList());
        return ret;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to loadAndConvert DOM " + rootDomClass + " identified by " + ids, t);
    }
}
Also used : DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) HashMap(java.util.HashMap) HasId(org.summerb.easycrud.api.dto.HasId) Ref(org.summerb.easycrud.api.dto.relations.Ref)

Aggregations

DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)16 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Test (org.junit.Test)9 DataSetLoader (org.summerb.easycrud.api.relations.DataSetLoader)9 Ref (org.summerb.easycrud.api.dto.relations.Ref)7 HasId (org.summerb.easycrud.api.dto.HasId)4 DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)3 List (java.util.List)2 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)2 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)2 Preconditions (com.google.common.base.Preconditions)1 TestDto1 (integr.org.summerb.easycrud.TestDto1)1 TestDto2 (integr.org.summerb.easycrud.TestDto2)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Function (java.util.function.Function)1