Search in sources :

Example 16 with HasId

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;
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId)

Example 17 with HasId

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());
}
Also used : HasId(org.summerb.easycrud.api.dto.HasId) DataSetLoader(org.summerb.easycrud.api.relations.DataSetLoader) Test(org.junit.Test)

Example 18 with HasId

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DtoBase(org.summerb.utils.DtoBase)

Example 19 with HasId

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();
}
Also used : Arrays(java.util.Arrays) EasyCrudWireTap(org.summerb.easycrud.api.EasyCrudWireTap) Autowired(org.springframework.beans.factory.annotation.Autowired) AutowireCapableBeanFactory(org.springframework.beans.factory.config.AutowireCapableBeanFactory) EasyCrudValidationStrategy(org.summerb.easycrud.api.EasyCrudValidationStrategy) EasyCrudTableAuthStrategy(org.summerb.easycrud.api.EasyCrudTableAuthStrategy) Function(java.util.function.Function) EasyCrudWireTapTableAuthImpl(org.summerb.easycrud.impl.wireTaps.EasyCrudWireTapTableAuthImpl) EasyCrudDao(org.summerb.easycrud.api.EasyCrudDao) DataSource(javax.sql.DataSource) EasyCrudServicePluggableImpl(org.summerb.easycrud.impl.EasyCrudServicePluggableImpl) EasyCrudScaffold(org.summerb.easycrud.scaffold.api.EasyCrudScaffold) EasyCrudWireTapValidationImpl(org.summerb.easycrud.impl.wireTaps.EasyCrudWireTapValidationImpl) EasyCrudServiceProxyFactory(org.summerb.easycrud.scaffold.api.EasyCrudServiceProxyFactory) EasyCrudDaoMySqlImpl(org.summerb.easycrud.impl.mysql.EasyCrudDaoMySqlImpl) HasId(org.summerb.easycrud.api.dto.HasId) Collectors(java.util.stream.Collectors) QueryToNativeSqlCompilerMySqlImpl(org.summerb.easycrud.impl.mysql.QueryToNativeSqlCompilerMySqlImpl) EasyCrudExceptionStrategy(org.summerb.easycrud.api.EasyCrudExceptionStrategy) List(java.util.List) EasyCrudPerRowAuthStrategy(org.summerb.easycrud.api.EasyCrudPerRowAuthStrategy) EasyCrudWireTapDelegatingImpl(org.summerb.easycrud.impl.wireTaps.EasyCrudWireTapDelegatingImpl) EasyCrudWireTapPerRowAuthImpl(org.summerb.easycrud.impl.wireTaps.EasyCrudWireTapPerRowAuthImpl) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DtoBase(org.summerb.utils.DtoBase) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) Preconditions(com.google.common.base.Preconditions) EasyCrudWireTap(org.summerb.easycrud.api.EasyCrudWireTap) EasyCrudExceptionStrategy(org.summerb.easycrud.api.EasyCrudExceptionStrategy)

Example 20 with HasId

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

HasId (org.summerb.easycrud.api.dto.HasId)20 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)9 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)5 Ref (org.summerb.easycrud.api.dto.relations.Ref)5 TestDto1 (integr.org.summerb.easycrud.TestDto1)4 LinkedList (java.util.LinkedList)4 DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)4 Query (org.summerb.easycrud.api.query.Query)4 ParameterizedType (java.lang.reflect.ParameterizedType)3 Type (java.lang.reflect.Type)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 DataTable (org.summerb.easycrud.api.dto.datapackage.DataTable)3 DataSetLoader (org.summerb.easycrud.api.relations.DataSetLoader)3 PropertyDescriptor (java.beans.PropertyDescriptor)2 PropertyAccessor (org.springframework.beans.PropertyAccessor)2