Search in sources :

Example 1 with EasyCrudService

use of org.summerb.easycrud.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.easycrud.api.dto.datapackage.DataSet) Query(org.summerb.easycrud.api.query.Query) ArrayList(java.util.ArrayList) HasId(org.summerb.easycrud.api.dto.HasId) Entry(java.util.Map.Entry) Ref(org.summerb.easycrud.api.dto.relations.Ref) EasyCrudService(org.summerb.easycrud.api.EasyCrudService)

Example 2 with EasyCrudService

use of org.summerb.easycrud.api.EasyCrudService in project summerb by skarpushin.

the class DataSetLoaderImpl method loadManyToManyReferences.

private ManyToManyRefToReferenceesMap loadManyToManyReferences(Map<Ref, Set<Object>> manyToManyReferences) {
    // NOTE: Queries to same types of referencees are not grouped. Should we
    // impl this like we did for one2many?
    ManyToManyRefToReferenceesMap ret = new ManyToManyRefToReferenceesMap();
    for (Entry<Ref, Set<Object>> refToReferencersEntry : manyToManyReferences.entrySet()) {
        EasyCrudService m2mServiceTmp = easyCrudServiceResolver.resolveByEntityType(refToReferencersEntry.getKey().getM2mEntity());
        Preconditions.checkState(m2mServiceTmp instanceof EasyCrudM2mService);
        EasyCrudM2mService m2mService = (EasyCrudM2mService) m2mServiceTmp;
        Map<Object, List<HasId>> referenceeMap = m2mService.findReferenceeByReferencers(refToReferencersEntry.getValue());
        ret.put(refToReferencersEntry.getKey(), referenceeMap);
    }
    return ret;
}
Also used : Ref(org.summerb.easycrud.api.dto.relations.Ref) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) HashSet(java.util.HashSet) Set(java.util.Set) DataSet(org.summerb.easycrud.api.dto.datapackage.DataSet) EasyCrudM2mService(org.summerb.easycrud.api.relations.EasyCrudM2mService) PaginatedList(org.summerb.easycrud.api.dto.PaginatedList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with EasyCrudService

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

Example 4 with EasyCrudService

use of org.summerb.easycrud.api.EasyCrudService in project summerb by skarpushin.

the class EasyCrudServiceResolverSpringImpl method discoverServices.

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

Example 5 with EasyCrudService

use of org.summerb.easycrud.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.easycrud.api.EasyCrudService) HashMap(java.util.HashMap)

Aggregations

EasyCrudService (org.summerb.easycrud.api.EasyCrudService)17 Test (org.junit.Test)8 HasId (org.summerb.easycrud.api.dto.HasId)8 TestDto1 (integr.org.summerb.easycrud.TestDto1)5 PaginatedList (org.summerb.easycrud.api.dto.PaginatedList)5 Query (org.summerb.easycrud.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.easycrud.api.dto.PagerParams)3 DataSet (org.summerb.easycrud.api.dto.datapackage.DataSet)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Ref (org.summerb.easycrud.api.dto.relations.Ref)2 TestDto2 (integr.org.summerb.easycrud.TestDto2)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1