use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EntityCombinedScreen method initBrowseCreateAction.
/**
* Adds a CreateAction that removes selection in table, sets a newly created item to editDs
* and enables controls for record editing.
*/
protected void initBrowseCreateAction() {
ListComponent table = getTable();
table.addAction(new CreateAction(table) {
@SuppressWarnings("unchecked")
@Override
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
initNewItem(newItem);
table.setSelected(Collections.emptyList());
getFieldGroup().getDatasource().setItem(newItem);
refreshOptionsForLookupFields();
enableEditControls(true);
}
});
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class WebEntityLinkField method afterCommitOpenedEntity.
@Override
protected void afterCommitOpenedEntity(Object item) {
if (getCollectionDatasourceFromOwner() == null) {
super.afterCommitOpenedEntity(item);
return;
}
MetaProperty metaProperty = getMetaPropertyForEditedValue();
if (metaProperty != null && metaProperty.getRange().isClass()) {
if (getValueSource() != null) {
boolean ownerDsModified = false;
boolean nonModifiedInTable = false;
DatasourceImplementation ownerDs = null;
if (getCollectionDatasourceFromOwner() != null) {
ownerDs = ((DatasourceImplementation) getCollectionDatasourceFromOwner());
nonModifiedInTable = !ownerDs.getItemsToUpdate().contains(((EntityValueSource) getValueSource()).getItem());
ownerDsModified = ownerDs.isModified();
}
// noinspection unchecked
setValueSilently((V) item);
// remove from items to update if it was not modified before setValue
if (ownerDs != null) {
if (nonModifiedInTable) {
ownerDs.getItemsToUpdate().remove(getDatasource().getItem());
}
ownerDs.setModified(ownerDsModified);
}
} else {
// noinspection unchecked
setValue((V) item);
}
// if we edit property with non Entity type and set ListComponent owner
} else if (owner != null) {
if (getCollectionDatasourceFromOwner() != null) {
// noinspection unchecked
getCollectionDatasourceFromOwner().updateItem((Entity) item);
}
if (owner instanceof Focusable) {
// focus owner
((Focusable) owner).focus();
}
// if we edit property with non Entity type
} else {
// noinspection unchecked
setValueSilently((V) item);
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class RestParseUtils method toObject.
@Nullable
public Object toObject(Type type, @Nullable String value, @Nullable String modelVersion) throws ParseException {
if (value == null)
return null;
Class clazz;
Class argumentTypeClass = null;
if (type instanceof Class) {
clazz = (Class) type;
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
if (actualTypeArguments.length > 0) {
if (actualTypeArguments[0] instanceof Class) {
argumentTypeClass = (Class) actualTypeArguments[0];
} else if (actualTypeArguments[0] instanceof ParameterizedType) {
argumentTypeClass = (Class) ((ParameterizedType) actualTypeArguments[0]).getRawType();
}
}
clazz = (Class) parameterizedType.getRawType();
} else {
throw new RuntimeException("Cannot handle the method argument with type " + type.getTypeName());
}
if (String.class == clazz)
return value;
if (Integer.class == clazz || Integer.TYPE == clazz || Byte.class == clazz || Byte.TYPE == clazz || Short.class == clazz || Short.TYPE == clazz)
return datatypeRegistry.get(Integer.class).parse(value);
if (Date.class == clazz) {
try {
return datatypeRegistry.get(Date.class).parse(value);
} catch (DateTimeParseException | ParseException e) {
try {
return datatypeRegistry.get(java.sql.Date.class).parse(value);
} catch (ParseException e1) {
return datatypeRegistry.get(Time.class).parse(value);
}
}
}
if (LocalDate.class == clazz) {
return datatypeRegistry.get(LocalDate.class).parse(value);
}
if (LocalDateTime.class == clazz) {
return datatypeRegistry.get(LocalDateTime.class).parse(value);
}
if (LocalTime.class == clazz) {
return datatypeRegistry.get(LocalTime.class).parse(value);
}
if (OffsetDateTime.class == clazz) {
return datatypeRegistry.get(OffsetDateTime.class).parse(value);
}
if (OffsetTime.class == clazz) {
return datatypeRegistry.get(OffsetTime.class).parse(value);
}
if (Time.class == clazz) {
LocalTime result = datatypeRegistry.get(LocalTime.class).parse(value);
if (result == null) {
return null;
}
return Time.valueOf(result);
}
if (BigDecimal.class == clazz)
return datatypeRegistry.get(BigDecimal.class).parse(value);
if (Boolean.class == clazz || Boolean.TYPE == clazz)
return datatypeRegistry.get(Boolean.class).parse(value);
if (Long.class == clazz || Long.TYPE == clazz)
return datatypeRegistry.get(Long.class).parse(value);
if (Double.class == clazz || Double.TYPE == clazz || Float.class == clazz || Float.TYPE == clazz)
return datatypeRegistry.get(Double.class).parse(value);
if (UUID.class == clazz)
return UUID.fromString(value);
if (Entity.class.isAssignableFrom(clazz)) {
return entitySerializationAPI.entityFromJson(value, metadata.getClass(clazz));
}
if (Collection.class.isAssignableFrom(clazz)) {
// if type argument for the collection is defined and is not entity, then do the basic deserialization
if (argumentTypeClass != null) {
if (!Entity.class.isAssignableFrom(argumentTypeClass)) {
return deserialize(value, type);
}
}
// if type argument for the collection is defined and is entity or if there is no type argument then try to
// deserialize entities collection
MetaClass metaClass = null;
if (argumentTypeClass != null) {
metaClass = metadata.getClass(argumentTypeClass);
String entityName = restControllerUtils.transformEntityNameIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION);
value = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION, value);
}
return entitySerializationAPI.entitiesCollectionFromJson(value, metaClass);
}
return deserialize(value, clazz);
}
use of io.jmix.core.Entity 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.Entity in project jmix by jmix-framework.
the class BulkEditorWindow method createNestedEmbeddedDatasources.
protected void createNestedEmbeddedDatasources(Datasource masterDs, MetaClass metaClass, String fqnPrefix) {
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (MetaProperty.Type.ASSOCIATION == metaProperty.getType() || MetaProperty.Type.COMPOSITION == metaProperty.getType() || MetaProperty.Type.EMBEDDED == metaProperty.getType()) {
String fqn = metaProperty.getName();
if (StringUtils.isNotEmpty(fqnPrefix)) {
fqn = fqnPrefix + "." + fqn;
}
if (managedEmbeddedProperties.contains(fqn) && metadataTools.isEmbedded(metaProperty)) {
MetaClass propertyMetaClass = metaProperty.getRange().asClass();
@SuppressWarnings("unchecked") NestedDatasource<Entity> propertyDs = new EmbeddedDatasourceImpl();
propertyDs.setup(fqn + "Ds", masterDs, metaProperty.getName());
propertyDs.setAllowCommit(false);
createNestedEmbeddedDatasources(propertyDs, propertyMetaClass, fqn);
datasources.put(fqn, propertyDs);
dsContext.register(propertyDs);
}
}
}
}
Aggregations