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