use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class CrossDataStoreReferenceLoader method loadBatch.
private void loadBatch(CrossDataStoreProperty crossDataStoreProperty, List<Entity> entities) {
List<Object> idList = entities.stream().map(e -> e.getValue(crossDataStoreProperty.relatedPropertyName)).filter(Objects::nonNull).distinct().collect(Collectors.toList());
if (idList.isEmpty())
return;
MetaClass cdsrMetaClass = crossDataStoreProperty.property.getRange().asClass();
LoadContext<Entity> loadContext = new LoadContext<>(cdsrMetaClass);
MetaProperty primaryKeyProperty = metadataTools.getPrimaryKeyProperty(cdsrMetaClass);
if (primaryKeyProperty == null || !primaryKeyProperty.getRange().isClass()) {
String queryString = String.format("select e from %s e where e.%s in :idList", cdsrMetaClass, crossDataStoreProperty.primaryKeyName);
loadContext.setQuery(LoadContext.createQuery(queryString).setParameter("idList", idList));
} else {
// composite key entity
StringBuilder sb = new StringBuilder("select e from ");
sb.append(cdsrMetaClass).append(" e where ");
MetaClass idMetaClass = primaryKeyProperty.getRange().asClass();
for (Iterator<MetaProperty> it = idMetaClass.getProperties().iterator(); it.hasNext(); ) {
MetaProperty property = it.next();
sb.append("e.").append(crossDataStoreProperty.primaryKeyName).append(".").append(property.getName());
sb.append(" in :list_").append(property.getName());
if (it.hasNext())
sb.append(" and ");
}
LoadContext.Query query = LoadContext.createQuery(sb.toString());
for (MetaProperty property : idMetaClass.getProperties()) {
List<Object> propList = idList.stream().map(o -> ((Entity) o).getValue(property.getName())).collect(Collectors.toList());
query.setParameter("list_" + property.getName(), propList);
}
loadContext.setQuery(query);
}
loadContext.setView(crossDataStoreProperty.viewProperty.getView());
List<Entity> loadedEntities = dataManager.loadList(loadContext);
for (Entity entity : entities) {
Object relatedPropertyValue = entity.getValue(crossDataStoreProperty.relatedPropertyName);
loadedEntities.stream().filter(e -> {
Object id = e.getId() instanceof IdProxy ? ((IdProxy) e.getId()).getNN() : e.getId();
return id.equals(relatedPropertyValue);
}).findAny().ifPresent(e -> entity.setValue(crossDataStoreProperty.property.getName(), e));
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataManagerBean method writeCrossDataStoreReferences.
protected boolean writeCrossDataStoreReferences(Entity entity, Collection<Entity> allEntities) {
if (Stores.getAdditional().isEmpty())
return false;
boolean repeatRequired = false;
MetaClass metaClass = metadata.getClassNN(entity.getClass());
for (MetaProperty property : metaClass.getProperties()) {
if (property.getRange().isClass() && !property.getRange().getCardinality().isMany()) {
MetaClass propertyMetaClass = property.getRange().asClass();
if (!Objects.equals(metadataTools.getStoreName(propertyMetaClass), metadataTools.getStoreName(metaClass))) {
List<String> relatedProperties = metadataTools.getRelatedProperties(property);
if (relatedProperties.size() == 0) {
continue;
}
if (relatedProperties.size() > 1) {
log.warn("More than 1 related property is defined for attribute {}, skip handling different data store", property);
continue;
}
String relatedPropertyName = relatedProperties.get(0);
if (PersistenceHelper.isLoaded(entity, relatedPropertyName)) {
Entity refEntity = entity.getValue(property.getName());
if (refEntity == null) {
entity.setValue(relatedPropertyName, null);
} else {
Object refEntityId = refEntity.getId();
if (refEntityId instanceof IdProxy) {
Object realId = ((IdProxy) refEntityId).get();
if (realId == null) {
if (allEntities.stream().anyMatch(e -> e.getId().equals(refEntityId))) {
repeatRequired = true;
} else {
log.warn("No entity with ID={} in the context, skip handling different data store", refEntityId);
}
} else {
entity.setValue(relatedPropertyName, realId);
}
} else if (refEntityId instanceof EmbeddableEntity) {
MetaProperty relatedProperty = metaClass.getPropertyNN(relatedPropertyName);
if (!relatedProperty.getRange().isClass()) {
log.warn("PK of entity referenced by {} is a EmbeddableEntity, but related property {} is not", property, relatedProperty);
} else {
entity.setValue(relatedPropertyName, metadataTools.copy((Entity) refEntityId));
}
} else {
entity.setValue(relatedPropertyName, refEntityId);
}
}
}
}
}
}
return repeatRequired;
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataManagerBean method load.
@Nullable
@Override
public <E extends Entity> E load(LoadContext<E> context) {
MetaClass metaClass = metadata.getClassNN(context.getMetaClass());
String storeName = metadata.getTools().getStoreName(metaClass);
if (storeName == null) {
log.debug("Data store for {} is not defined, returning null", metaClass);
return null;
}
DataStore storage = storeFactory.get(storeName);
E entity = storage.load(context);
if (entity != null)
readCrossDataStoreReferences(Collections.singletonList(entity), context.getView(), metaClass);
return entity;
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataServiceQueryBuilder method restrictByPreviousResults.
public void restrictByPreviousResults(UUID sessionId, int queryKey) {
QueryTransformer transformer = QueryTransformerFactory.createTransformer(queryString);
MetaClass metaClass = metadata.getClassNN(entityName);
MetaProperty primaryKey = metadata.getTools().getPrimaryKeyProperty(metaClass);
if (primaryKey == null)
throw new IllegalStateException(String.format("Entity %s has no primary key", entityName));
Class type = primaryKey.getJavaType();
String entityIdField;
if (UUID.class.equals(type)) {
entityIdField = "entityId";
} else if (Long.class.equals(type)) {
entityIdField = "longEntityId";
} else if (Integer.class.equals(type)) {
entityIdField = "intEntityId";
} else if (String.class.equals(type)) {
entityIdField = "stringEntityId";
} else {
throw new IllegalStateException(String.format("Unsupported primary key type: %s for %s", type.getSimpleName(), entityName));
}
transformer.addJoinAndWhere(", sys$QueryResult _qr", String.format("_qr.%s = {E}.%s and _qr.sessionId = :_qr_sessionId and _qr.queryKey = %s", entityIdField, primaryKey.getName(), queryKey));
queryString = transformer.getResult();
this.queryParams.put("_qr_sessionId", sessionId);
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class DataServiceQueryBuilder method init.
public void init(String queryString, Map<String, Object> queryParams, String[] noConversionParams, Object id, String entityName) {
this.entityName = entityName;
if (!StringUtils.isBlank(queryString)) {
this.queryString = queryString;
this.queryParams = queryParams;
this.noConversionParams = noConversionParams;
} else {
MetaClass metaClass = metadata.getClassNN(entityName);
String pkName = metadata.getTools().getPrimaryKeyName(metaClass);
if (pkName == null)
throw new IllegalStateException(String.format("Entity %s has no primary key", entityName));
this.queryString = "select e from " + entityName + " e where e." + pkName + " = :entityId";
this.queryParams = new HashMap<>();
this.queryParams.put("entityId", id);
}
}
Aggregations