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);
}
}
Aggregations