use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class PropertyDatasourceImpl method getView.
@Override
public FetchPlan getView() {
if (view == null) {
MetaClass metaMetaClass = masterDs.getMetaClass();
if (metadata.getTools().isJpaEntity(metaMetaClass) || metadata.getTools().isJpaEmbeddable(metaMetaClass)) {
FetchPlan masterView = masterDs.getView();
if (masterView == null) {
throw new DevelopmentException("No view for datasource " + masterDs.getId(), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId()));
}
FetchPlanProperty property = masterView.getProperty(metaProperty.getName());
if (property == null) {
return null;
}
if (property.getFetchPlan() == null) {
throw new DevelopmentException(String.format("Invalid view definition: %s. Property '%s' must have a view", masterView, property), ParamsMap.of("masterDs", masterDs.getId(), "propertyDs", getId(), "masterView", masterView, "property", property));
}
view = metadata.getViewRepository().getView(getMetaClass(), property.getFetchPlan().getName());
// anonymous (nameless) view
if (view == null)
view = property.getFetchPlan();
}
}
return view;
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class MultiEntityDataLoader method loadData.
@Override
public List<Map<String, Object>> loadData(ReportQuery dataSet, BandData parentBand, Map<String, Object> params) {
Map<String, Object> additionalParams = dataSet.getAdditionalParams();
String paramName = (String) additionalParams.get(DataSet.LIST_ENTITIES_PARAM_NAME);
if (StringUtils.isBlank(paramName)) {
paramName = DEFAULT_LIST_ENTITIES_PARAM_NAME;
}
boolean hasNestedCollection = paramName.contains(NESTED_COLLECTION_SEPARATOR);
String entityParameterName = StringUtils.substringBefore(paramName, NESTED_COLLECTION_SEPARATOR);
String nestedCollectionName = StringUtils.substringAfter(paramName, NESTED_COLLECTION_SEPARATOR);
FetchPlan nestedCollectionFetchPLan = null;
dataSet = ProxyWrapper.unwrap(dataSet);
Object entities = null;
if (params.containsKey(paramName)) {
entities = params.get(paramName);
} else if (hasNestedCollection && params.containsKey(entityParameterName)) {
Entity entity = (Entity) params.get(entityParameterName);
entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
if (entity != null) {
entities = EntityValues.getValueEx(entity, nestedCollectionName);
if (dataSet instanceof DataSet) {
FetchPlan entityFetchPlan = getFetchPlan(entity, (DataSet) dataSet);
if (entityFetchPlan != null && entityFetchPlan.getProperty(nestedCollectionName) != null) {
// noinspection ConstantConditions
nestedCollectionFetchPLan = entityFetchPlan.getProperty(nestedCollectionName).getFetchPlan();
}
}
}
}
if (!(entities instanceof Collection)) {
if (hasNestedCollection) {
throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter, " + "or the entity does not contain nested collection '%s'", entityParameterName, nestedCollectionName));
} else {
throw new IllegalStateException(String.format("Input parameters do not contain '%s' parameter or it has type other than collection", paramName));
}
}
Collection<Entity> entitiesList = (Collection) entities;
params.put(paramName, entitiesList);
List<Map<String, Object>> resultList = new ArrayList<>();
for (Entity entity : entitiesList) {
if (!hasNestedCollection) {
entity = reloadEntityByDataSetFetchPlan(dataSet, entity);
}
if (dataSet instanceof DataSet) {
if (hasNestedCollection) {
if (nestedCollectionFetchPLan != null) {
resultList.add(new EntityMap(entity, nestedCollectionFetchPLan, beanFactory));
} else {
resultList.add(new EntityMap(entity, beanFactory));
}
} else {
resultList.add(new EntityMap(entity, getFetchPlan(entity, (DataSet) dataSet), beanFactory));
}
} else {
resultList.add(new EntityMap(entity, beanFactory));
}
}
return resultList;
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class ImapDataProvider method saveAttachments.
public void saveAttachments(ImapMessage msg, Collection<ImapMessageAttachment> attachments) {
SaveContext saveContext = new SaveContext();
FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(ImapMessageAttachment.class, "imap-msg-attachment-full");
attachments.forEach(attachment -> {
attachment.setImapMessage(msg);
saveContext.saving(attachment, fetchPlan);
});
msg.setAttachmentsLoaded(true);
saveContext.saving(msg, fetchPlanRepository.getFetchPlan(ImapMessage.class, "imap-msg-full"));
dataManager.save(saveContext);
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class BulkEditorWindow method createView.
/**
* Creates a view, loading only necessary properties.
* Referenced entities will be loaded with a MINIMAL view.
*
* @param meta meta class
* @return View instance
*/
protected View createView(MetaClass meta) {
View view = new View(meta.getJavaClass(), false);
for (MetaProperty metaProperty : meta.getProperties()) {
if (!managedFields.containsKey(metaProperty.getName()) && !managedEmbeddedProperties.contains(metaProperty.getName())) {
continue;
}
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
view.addProperty(metaProperty.getName());
break;
case EMBEDDED:
{
// build view for embedded property
FetchPlan propView = createEmbeddedView(metaProperty.getRange().asClass(), metaProperty.getName());
view.addProperty(metaProperty.getName(), propView);
break;
}
case ASSOCIATION:
case COMPOSITION:
FetchPlan propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
// in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
// loading property lazy prevents this from happening
view.addProperty(metaProperty.getName(), propView);
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return view;
}
use of io.jmix.core.FetchPlan in project jmix by jmix-framework.
the class BulkEditorWindow method createEmbeddedView.
protected View createEmbeddedView(MetaClass meta, String fqnPrefix) {
View view = new View(meta.getJavaClass(), false);
for (MetaProperty metaProperty : meta.getProperties()) {
String fqn = fqnPrefix + "." + metaProperty.getName();
if (!managedFields.containsKey(fqn)) {
continue;
}
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
view.addProperty(metaProperty.getName());
break;
case EMBEDDED:
{
FetchPlan propView = createEmbeddedView(metaProperty.getRange().asClass(), fqn);
view.addProperty(metaProperty.getName(), propView);
break;
}
case ASSOCIATION:
case COMPOSITION:
FetchPlan propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
// in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
// loading property lazy prevents this from happening
view.addProperty(metaProperty.getName(), propView);
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return view;
}
Aggregations