Search in sources :

Example 6 with EasyCrudService

use of org.summerb.approaches.jdbccrud.api.EasyCrudService in project summerb by skarpushin.

the class DataSetLoaderImpl method loadObjectsByIds.

@Override
public List<HasId> loadObjectsByIds(Set<Object> ids, String entityTypeName) throws EntityNotFoundException, NotAuthorizedException {
    Preconditions.checkArgument(!CollectionUtils.isEmpty(ids));
    EasyCrudService service = easyCrudServiceResolver.resolveByEntityType(entityTypeName);
    Object firstId = ids.iterator().next();
    List<HasId> ret = new ArrayList<>(ids.size());
    if (ids.size() == 1) {
        HasId loaded = loadOne(firstId, service);
        ret.add(loaded);
    } else if (firstId instanceof Long || firstId instanceof String) {
        Query query;
        if (firstId instanceof String) {
            query = Query.n().in(HasId.FN_ID, ids.toArray(new String[0]));
        } else {
            query = Query.n().in(HasId.FN_ID, ids.toArray(new Long[0]));
        }
        List loaded = loadMultipleByQuery(query, service);
        assertFound(loaded.size() == ids.size(), entityTypeName, "One of: " + Arrays.toString(ids.toArray()));
        ret.addAll(loaded);
    } else {
        List<HasId> loaded = loadOneByOne(ids, service);
        ret.addAll(loaded);
    }
    return ret;
}
Also used : HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService) Query(org.summerb.approaches.jdbccrud.api.query.Query) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) PaginatedList(org.summerb.approaches.jdbccrud.api.dto.PaginatedList) List(java.util.List)

Example 7 with EasyCrudService

use of org.summerb.approaches.jdbccrud.api.EasyCrudService in project summerb by skarpushin.

the class EasyCrudServiceResolverSpringImpl method discoverServicesByClass.

private Map<Class<?>, EasyCrudService> discoverServicesByClass() {
    Preconditions.checkState(applicationContext != null, "applicationContext expected to be injected");
    Map<String, EasyCrudService> foundBeans = applicationContext.getBeansOfType(EasyCrudService.class);
    Map<Class<?>, EasyCrudService> ret = new HashMap<>();
    for (Entry<String, EasyCrudService> entry : foundBeans.entrySet()) {
        EasyCrudService service = entry.getValue();
        EasyCrudService wasOverwritten = ret.put(service.getDtoClass(), service);
        if (wasOverwritten != null) {
            log.warn("Ambigious EasyCrudService for same dtoClass 1st " + wasOverwritten + " and 2nd " + service + " named " + entry.getKey());
        }
    }
    return ret;
}
Also used : EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService) HashMap(java.util.HashMap)

Example 8 with EasyCrudService

use of org.summerb.approaches.jdbccrud.api.EasyCrudService in project summerb by skarpushin.

the class EasyCrudServiceResolverSpringImpl method resolveByDtoClass.

@Override
public EasyCrudService resolveByDtoClass(Class<?> entityClass) {
    getServicesMap();
    EasyCrudService ret = servicesMapByClass.get(entityClass);
    Preconditions.checkArgument(ret != null, "Serivce for that entity (by class) wasn't found: " + entityClass);
    return ret;
}
Also used : EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService)

Example 9 with EasyCrudService

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

Example 10 with EasyCrudService

use of org.summerb.approaches.jdbccrud.api.EasyCrudService in project summerb by skarpushin.

the class DataSetLoaderImpl method loadOneToManyReferences.

private EntityTypeToObjectsMap loadOneToManyReferences(Map<Ref, Set<Object>> refToReferencersIds) throws NotAuthorizedException {
    Multimap<String, Entry<Ref, Set<Object>>> targetEntityToRef = HashMultimap.create();
    for (Entry<Ref, Set<Object>> entry : refToReferencersIds.entrySet()) {
        targetEntityToRef.put(entry.getKey().getToEntity(), entry);
    }
    EntityTypeToObjectsMap ret = new EntityTypeToObjectsMap();
    for (String entityTypeCode : targetEntityToRef.keySet()) {
        Collection<Entry<Ref, Set<Object>>> entries = targetEntityToRef.get(entityTypeCode);
        List<Query> queries = new ArrayList<>(entries.size());
        for (Entry<Ref, Set<Object>> entry : entries) {
            Set<Object> ids = entry.getValue();
            Ref ref = entry.getKey();
            Object firstId = ids.iterator().next();
            if (firstId instanceof Long) {
                queries.add(Query.n().in(ref.getToField(), ids.toArray(new Long[0])));
            } else if (firstId instanceof String) {
                queries.add(Query.n().in(ref.getToField(), ids.toArray(new String[0])));
            } else {
                throw new IllegalStateException("such id type is not supported: " + firstId.getClass());
            }
        }
        Query q = queries.size() == 1 ? queries.get(0) : Query.n().or(queries.toArray(new Query[0]));
        EasyCrudService service = easyCrudServiceResolver.resolveByEntityType(entityTypeCode);
        PaginatedList<HasId> results = service.query(PagerParams.ALL, q);
        ret.put(entityTypeCode, new ArrayList<>(results.getItems()));
    }
    return ret;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet) Query(org.summerb.approaches.jdbccrud.api.query.Query) ArrayList(java.util.ArrayList) HasId(org.summerb.approaches.jdbccrud.api.dto.HasId) Entry(java.util.Map.Entry) Ref(org.summerb.approaches.jdbccrud.api.dto.relations.Ref) EasyCrudService(org.summerb.approaches.jdbccrud.api.EasyCrudService)

Aggregations

EasyCrudService (org.summerb.approaches.jdbccrud.api.EasyCrudService)16 Test (org.junit.Test)8 HasId (org.summerb.approaches.jdbccrud.api.dto.HasId)7 DataSetLoaderImpl (org.summerb.approaches.jdbccrud.impl.relations.DataSetLoaderImpl)7 TestDto1 (integr.org.summerb.jdbccrud.TestDto1)5 PaginatedList (org.summerb.approaches.jdbccrud.api.dto.PaginatedList)5 Query (org.summerb.approaches.jdbccrud.api.query.Query)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 PagerParams (org.summerb.approaches.jdbccrud.api.dto.PagerParams)3 DataSet (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataSet)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Ref (org.summerb.approaches.jdbccrud.api.dto.relations.Ref)2 TestDto2 (integr.org.summerb.jdbccrud.TestDto2)1 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1 DataTable (org.summerb.approaches.jdbccrud.api.dto.datapackage.DataTable)1