use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.
the class DataSetLoaderImpl method loadOne.
private HasId loadOne(Object id, EasyCrudService<Object, HasId> service) throws NotAuthorizedException, GenericEntityNotFoundException {
HasId ret = service.findById(id);
String entityTypeName = service.getEntityTypeMessageCode();
assertFound(ret != null, entityTypeName, id);
return ret;
}
use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.
the class DataSetLoaderTest method testLoadObjectsByIds_ExpectTwoObjsOfSameTypeLoadedOk.
@Test
public void testLoadObjectsByIds_ExpectTwoObjsOfSameTypeLoadedOk() throws EntityNotFoundException, NotAuthorizedException, FieldValidationException {
DataSetLoader loader = buildLoaderCase1();
TestDto2 d2i1 = new TestDto2();
d2i1.setEnv("d2i1");
d2i1.setLinkToFullDonwload("asdad");
d2i1 = testDto2Service.create(d2i1);
TestDto2 d2i2 = new TestDto2();
d2i2.setEnv("d2i2");
d2i2.setLinkToFullDonwload("asdad");
d2i2 = testDto2Service.create(d2i2);
List<HasId> result = loader.loadObjectsByIds(ids(d2i1, d2i2), testDto2Service.getEntityTypeMessageCode());
assertNotNull(result);
assertEquals(2, result.size());
}
use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.
the class EasyCrudScaffoldImpl method discoverDtoClassFromServiceInterface.
@SuppressWarnings("unchecked")
protected <TId, TDto extends HasId<TId>, TService extends EasyCrudService<TId, TDto>> Class<TDto> discoverDtoClassFromServiceInterface(Class<TService> serviceInterface) {
Preconditions.checkArgument(EasyCrudService.class.isAssignableFrom(serviceInterface), "Service interface is supposed to be a subclass of EasyCrudService");
ParameterizedType easyCrudServiceType = null;
for (int i = 0; i < serviceInterface.getGenericInterfaces().length; i++) {
Type candidate = serviceInterface.getGenericInterfaces()[i];
if (!(candidate instanceof ParameterizedType)) {
continue;
}
ParameterizedType candidatePt = (ParameterizedType) candidate;
if (EasyCrudService.class.equals(candidatePt.getRawType())) {
easyCrudServiceType = candidatePt;
break;
}
}
Preconditions.checkState(easyCrudServiceType != null, "Wasn't able to located parent interface EasyCrudService");
Type ret = easyCrudServiceType.getActualTypeArguments()[1];
Preconditions.checkArgument(DtoBase.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl DtoBase interface");
Preconditions.checkArgument(HasId.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl HasId interface");
return (Class<TDto>) ret;
}
use of org.summerb.easycrud.api.dto.HasId in project summerb by skarpushin.
the class EasyCrudScaffoldImpl method initService.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected <TId, TDto extends HasId<TId>> void initService(EasyCrudServicePluggableImpl<TId, TDto, EasyCrudDao<TId, TDto>> service, Class<TDto> dtoClass, String messageCode, String tableName, EasyCrudDao<TId, TDto> dao, Object... injections) throws Exception {
service.setDtoClass(dtoClass);
service.setDao(dao);
service.setEntityTypeMessageCode(messageCode);
beanFactory.autowireBean(service);
if (injections == null || injections.length == 0) {
service.afterPropertiesSet();
return;
}
// NOTE: Ok. This thing around EasyCrudExceptionStrategy tells me this code is
// begging to be refactored. Screamign about OCP... I'll do it later.
List<EasyCrudWireTap<TId, TDto>> wireTaps = Arrays.stream(injections).filter(x -> !(x instanceof EasyCrudExceptionStrategy)).map(injectionToWireTapMapper(dtoClass, messageCode, tableName)).collect(Collectors.toList());
if (wireTaps.size() > 0) {
service.setWireTap(new EasyCrudWireTapDelegatingImpl<>(wireTaps));
}
EasyCrudExceptionStrategy exceptionStrategy = Arrays.stream(injections).filter(x -> x instanceof EasyCrudExceptionStrategy).map(x -> (EasyCrudExceptionStrategy) x).findFirst().orElse(null);
if (exceptionStrategy != null) {
service.setGenericExceptionStrategy(exceptionStrategy);
}
// x.
service.afterPropertiesSet();
}
use of org.summerb.easycrud.api.dto.HasId 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