use of io.jmix.core.Entity in project jmix by jmix-framework.
the class ExcelExporter method formatValueCell.
protected void formatValueCell(Cell cell, @Nullable Object cellValue, @Nullable MetaPropertyPath metaPropertyPath, int sizersIndex, int notificationRequired, int level, @Nullable Integer groupChildCount) {
if (cellValue == null) {
if (metaPropertyPath != null && metaPropertyPath.getRange().isDatatype()) {
Class javaClass = metaPropertyPath.getRange().asDatatype().getJavaClass();
if (Boolean.class.equals(javaClass)) {
cellValue = false;
}
} else {
return;
}
}
String childCountValue = "";
if (groupChildCount != null) {
childCountValue = " (" + groupChildCount + ")";
}
if (cellValue instanceof Number) {
Number n = (Number) cellValue;
Datatype datatype = null;
if (metaPropertyPath != null) {
Range range = metaPropertyPath.getMetaProperty().getRange();
if (range.isDatatype()) {
datatype = range.asDatatype();
}
}
datatype = datatype == null ? datatypeRegistry.get(n.getClass()) : datatype;
String str;
// and we should skip it
if (sizersIndex == 0 && level > 0) {
str = createSpaceString(level) + datatype.format(n);
cell.setCellValue(str);
} else {
try {
str = datatype.format(n);
Number result = (Number) datatype.parse(str);
if (result != null) {
if (n instanceof Integer || n instanceof Long || n instanceof Byte || n instanceof Short) {
cell.setCellValue(result.longValue());
cell.setCellStyle(integerFormatCellStyle);
} else {
cell.setCellValue(result.doubleValue());
cell.setCellStyle(doubleFormatCellStyle);
}
}
} catch (ParseException e) {
throw new RuntimeException("Unable to parse numeric value", e);
}
cell.setCellType(CellType.NUMERIC);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Date) {
Class javaClass = null;
if (metaPropertyPath != null) {
MetaProperty metaProperty = metaPropertyPath.getMetaProperty();
if (metaProperty.getRange().isDatatype()) {
javaClass = metaProperty.getRange().asDatatype().getJavaClass();
}
}
Date date = (Date) cellValue;
cell.setCellValue(date);
if (Objects.equals(java.sql.Time.class, javaClass)) {
cell.setCellStyle(timeFormatCellStyle);
} else if (Objects.equals(java.sql.Date.class, javaClass)) {
cell.setCellStyle(dateFormatCellStyle);
} else {
cell.setCellStyle(dateTimeFormatCellStyle);
}
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
String str = datatypeRegistry.get(Date.class).format(date);
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Boolean) {
String str = "";
if (sizersIndex == 0) {
str += createSpaceString(level);
}
str += ((Boolean) cellValue) ? getMessage("excelExporter.true") : getMessage("excelExporter.false");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Enum) {
String message = (sizersIndex == 0 ? createSpaceString(level) : "") + messages.getMessage((Enum) cellValue);
cell.setCellValue(message + childCountValue);
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(message, stdFont);
}
} else if (cellValue instanceof Entity) {
Object entityVal = cellValue;
String instanceName = metadataTools.getInstanceName(entityVal);
String str = sizersIndex == 0 ? createSpaceString(level) + instanceName : instanceName;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof Collection) {
String str = "";
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else if (cellValue instanceof byte[]) {
String str = messages.getMessage("excelExporter.bytes");
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
} else {
String strValue = cellValue == null ? "" : cellValue.toString();
String str = sizersIndex == 0 ? createSpaceString(level) + strValue : strValue;
str = str + childCountValue;
cell.setCellValue(createStringCellValue(str));
if (sizers[sizersIndex].isNotificationRequired(notificationRequired)) {
sizers[sizersIndex].notifyCellValue(str, stdFont);
}
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class Param method createEntityLookup.
protected Component createEntityLookup(FilterDataContext filterDataContext, ValueProperty valueProperty) {
MetaClass metaClass = metadata.getSession().getClassNN(javaClass);
LookupType type = null;
if (property != null && property.getRange().isClass()) {
type = (LookupType) metadata.getTools().getMetaAnnotationAttributes(property.getAnnotations(), Lookup.class).get("type");
}
PersistenceManagerClient persistenceManager = (PersistenceManagerClient) applicationContext.getBean(PersistenceManagerClient.NAME);
boolean useLookupScreen = type != null ? type == LookupType.SCREEN : persistenceManager.useLookupScreen(metaClass.getName());
if (useLookupScreen) {
if (inExpr) {
ListEditor listEditor = uiComponents.create(ListEditor.class);
listEditor.setItemType(ListEditor.ItemType.ENTITY);
listEditor.setEntityName(metaClass.getName());
initListEditor(listEditor, valueProperty);
return listEditor;
} else {
PickerField<Entity> picker = uiComponents.create(PickerField.NAME);
picker.setMetaClass(metaClass);
picker.setWidth(theme.get("cuba.gui.filter.Param.textComponent.width"));
picker.addAction(actions.create(LookupAction.ID));
picker.addAction(actions.create(EntityClearAction.ID));
picker.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));
picker.setValue((Entity) _getValue(valueProperty));
return picker;
}
} else {
if (inExpr) {
CollectionLoader<Entity> loader = createEntityOptionsLoader(metaClass);
CollectionContainer<Entity> container = dataComponents.createCollectionContainer(metaClass.getJavaClass());
loader.setContainer(container);
ListEditor listEditor = uiComponents.create(ListEditor.class);
listEditor.setItemType(ListEditor.ItemType.ENTITY);
listEditor.setEntityName(metaClass.getName());
// noinspection unchecked
listEditor.setOptions(new ContainerOptions<>(container));
// noinspection unchecked
initListEditor(listEditor, valueProperty);
// noinspection unchecked
Consumer<CollectionContainer.CollectionChangeEvent<?>> listener = e -> listEditor.setValue(null);
if (filterDataContext != null) {
filterDataContext.registerCollectionLoader(this, loader);
filterDataContext.registerContainerCollectionChangeListener(this, container, listener);
}
return listEditor;
} else {
CollectionLoader<Entity> loader = createEntityOptionsLoader(metaClass);
CollectionContainer<Entity> container = dataComponents.createCollectionContainer(metaClass.getJavaClass());
loader.setContainer(container);
LookupPickerField<Entity> lookup = uiComponents.create(LookupPickerField.NAME);
lookup.setWidth(theme.get("cuba.gui.filter.Param.textComponent.width"));
lookup.addAction(actions.create(EntityClearAction.ID));
lookup.setOptions(new ContainerOptions<>(container));
Consumer<CollectionContainer.CollectionChangeEvent<?>> listener = e -> lookup.setValue(null);
lookup.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));
lookup.setValue((Entity) _getValue(valueProperty));
if (filterDataContext != null) {
filterDataContext.registerCollectionLoader(this, loader);
filterDataContext.registerContainerCollectionChangeListener(this, container, listener);
}
return lookup;
}
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EditAction method internalOpenEditor.
protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) {
LegacyFrame frameOwner = (LegacyFrame) target.getFrame().getFrameOwner();
AbstractEditor window = frameOwner.openEditor(getWindowId(), existingItem, getOpenType(), params, parentDs);
if (editorCloseListener == null) {
window.addCloseListener(actionId -> {
// move focus to owner
if (target instanceof Component.Focusable) {
((Component.Focusable) target).focus();
}
if (Window.COMMIT_ACTION_ID.equals(actionId)) {
Entity editedItem = window.getItem();
if (editedItem != null) {
if (parentDs == null) {
editedItem = (Entity) AppBeans.get(GuiActionSupport.class).reloadEntityIfNeeded(editedItem, datasource);
// noinspection unchecked
datasource.updateItem(editedItem);
}
afterCommit(editedItem);
if (afterCommitHandler != null) {
afterCommitHandler.handle(editedItem);
}
}
}
afterWindowClosed(window);
if (afterWindowClosedHandler != null) {
afterWindowClosedHandler.handle(window, actionId);
}
});
} else {
window.addCloseListener(editorCloseListener);
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class ExcludeAction method doRemove.
@SuppressWarnings("unchecked")
@Override
protected void doRemove(Set<Entity> selected, boolean autocommit) {
CollectionDatasource ds = target.getDatasource();
if (ds instanceof NestedDatasource) {
// Clear reference to master entity
Datasource masterDs = ((NestedDatasource) ds).getMaster();
MetaProperty metaProperty = ((NestedDatasource) ds).getProperty();
if (masterDs != null && metaProperty != null) {
MetaProperty inverseProp = metaProperty.getInverse();
if (inverseProp != null) {
ExtendedEntities extendedEntities = metadata.getExtendedEntities();
Class inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
Class dsClass = extendedEntities.getEffectiveClass(ds.getMetaClass());
if (inversePropClass.isAssignableFrom(dsClass)) {
for (Entity item : selected) {
EntityValues.setValue(item, inverseProp.getName(), null);
}
}
}
}
}
for (Entity item : selected) {
ds.modifyItem(item);
ds.excludeItem(item);
}
if (autocommit && (ds.getCommitMode() != Datasource.CommitMode.PARENT)) {
try {
ds.commit();
} catch (RuntimeException e) {
ds.refresh();
throw e;
}
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class AddConditionWindow method select.
public void select() {
Set<Entity> selectedItems = tree.getSelected();
if (selectedItems.isEmpty()) {
showNotification(messages.getMessage("filter.addCondition.selectCondition"), NotificationType.WARNING);
return;
} else {
for (Entity item : selectedItems) {
if (item instanceof HeaderConditionDescriptor) {
showNotification(messages.getMessage("filter.addCondition.youSelectedGroup"), NotificationType.WARNING);
return;
} else if (isEmbeddedProperty((AbstractConditionDescriptor) item)) {
showNotification(messages.getMessage("filter.addCondition.youSelectedEmbedded"), NotificationType.WARNING);
return;
}
}
}
close(COMMIT_ACTION_ID);
}
Aggregations