use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class EntityInspectorBrowse method createButtonsPanel.
protected void createButtonsPanel(Table table) {
ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
createButton = componentsFactory.createComponent(Button.class);
createButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "create"));
CreateAction createAction = new CreateAction();
table.addAction(createAction);
createButton.setAction(createAction);
createButton.setIcon(icons.get(CubaIcon.CREATE_ACTION));
createButton.setFrame(frame);
editButton = componentsFactory.createComponent(Button.class);
editButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "edit"));
EditAction editAction = new EditAction();
table.addAction(editAction);
editButton.setAction(editAction);
editButton.setIcon(icons.get(CubaIcon.EDIT_ACTION));
editButton.setFrame(frame);
removeButton = componentsFactory.createComponent(Button.class);
removeButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "remove"));
RemoveAction removeAction = new RemoveAction(entitiesTable);
removeAction.setAfterRemoveHandler(removedItems -> entitiesDs.refresh());
table.addAction(removeAction);
removeButton.setAction(removeAction);
removeButton.setIcon(icons.get(CubaIcon.REMOVE_ACTION));
removeButton.setFrame(frame);
excelButton = componentsFactory.createComponent(Button.class);
excelButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "excel"));
excelButton.setAction(new ExcelAction(entitiesTable));
excelButton.setIcon(icons.get(CubaIcon.EXCEL_ACTION));
excelButton.setFrame(frame);
refreshButton = componentsFactory.createComponent(Button.class);
refreshButton.setCaption(messages.getMessage(EntityInspectorBrowse.class, "refresh"));
refreshButton.setAction(new RefreshAction(entitiesTable));
refreshButton.setIcon(icons.get(CubaIcon.REFRESH_ACTION));
refreshButton.setFrame(frame);
exportPopupButton = componentsFactory.createComponent(PopupButton.class);
exportPopupButton.setIcon(icons.get(CubaIcon.DOWNLOAD));
exportPopupButton.addAction(new ExportAction("exportJSON", JSON));
exportPopupButton.addAction(new ExportAction("exportZIP", ZIP));
importUpload = componentsFactory.createComponent(FileUploadField.class);
importUpload.setUploadButtonIcon(icons.get(CubaIcon.UPLOAD));
importUpload.setUploadButtonCaption(getMessage("import"));
importUpload.addFileUploadSucceedListener(event -> {
File file = fileUploadingAPI.getFile(importUpload.getFileId());
if (file == null) {
String errorMsg = String.format("Entities import upload error. File with id %s not found", importUpload.getFileId());
throw new RuntimeException(errorMsg);
}
byte[] fileBytes;
try (InputStream is = new FileInputStream(file)) {
fileBytes = IOUtils.toByteArray(is);
} catch (IOException e) {
throw new RuntimeException("Unable to upload file", e);
}
try {
fileUploadingAPI.deleteFile(importUpload.getFileId());
} catch (FileStorageException e) {
log.error("Unable to delete temp file", e);
}
try {
Collection<Entity> importedEntities;
if ("json".equals(Files.getFileExtension(importUpload.getFileName()))) {
importedEntities = entityImportExportService.importEntitiesFromJSON(new String(fileBytes), createEntityImportView(selectedMeta));
} else {
importedEntities = entityImportExportService.importEntitiesFromZIP(fileBytes, createEntityImportView(selectedMeta));
}
showNotification(importedEntities.size() + " entities imported", NotificationType.HUMANIZED);
} catch (Exception e) {
showNotification(getMessage("importFailed"), e.getMessage(), NotificationType.ERROR);
log.error("Entities import error", e);
}
entitiesDs.refresh();
});
buttonsPanel.add(createButton);
buttonsPanel.add(editButton);
buttonsPanel.add(removeButton);
buttonsPanel.add(excelButton);
buttonsPanel.add(refreshButton);
buttonsPanel.add(exportPopupButton);
buttonsPanel.add(importUpload);
table.setButtonsPanel(buttonsPanel);
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class EntityInspectorEditor method createEmbeddedFields.
/**
* Recursively instantiates the embedded properties.
* E.g. embedded properties of the embedded property will also be instantiated.
*
* @param metaClass meta class of the entity
* @param item entity instance
*/
protected void createEmbeddedFields(MetaClass metaClass, Entity item) {
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (isEmbedded(metaProperty)) {
MetaClass embeddedMetaClass = metaProperty.getRange().asClass();
Entity embedded = item.getValue(metaProperty.getName());
if (embedded == null) {
embedded = metadata.create(embeddedMetaClass);
item.setValue(metaProperty.getName(), embedded);
}
createEmbeddedFields(embeddedMetaClass, embedded);
}
}
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class EntityInspectorEditor method createAddHandler.
@SuppressWarnings("unchecked")
protected Lookup.Handler createAddHandler(final MetaProperty metaProperty, final CollectionDatasource propertyDs) {
Lookup.Handler result = new Lookup.Handler() {
@Override
public void handleLookup(Collection items) {
for (Object item : items) {
Entity entity = (Entity) item;
if (!propertyDs.getItems().contains(entity)) {
MetaProperty inverseProperty = metaProperty.getInverse();
if (inverseProperty != null) {
if (!inverseProperty.getRange().getCardinality().isMany()) {
// set currently editing item to the child's parent property
entity.setValue(inverseProperty.getName(), datasource.getItem());
propertyDs.addItem(entity);
} else {
Collection properties = entity.getValue(inverseProperty.getName());
if (properties != null) {
properties.add(datasource.getItem());
propertyDs.addItem(entity);
}
}
}
}
propertyDs.addItem(entity);
}
}
};
propertyDs.refresh();
return result;
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class EntityLogBrowser method init.
@Override
public void init(Map<String, Object> params) {
WindowParams.DISABLE_AUTO_REFRESH.set(params, true);
usersDs.refresh();
loggedEntityDs.refresh();
Companion companion = getCompanion();
if (companion != null) {
companion.enableTextSelection(entityLogTable);
companion.enableTextSelection(entityLogAttrTable);
}
systemAttrsList = Arrays.asList("createTs", "createdBy", "updateTs", "updatedBy", "deleteTs", "deletedBy", "version", "id");
Map<String, Object> changeTypeMap = new LinkedHashMap<>();
changeTypeMap.put(messages.getMessage(getClass(), "createField"), "C");
changeTypeMap.put(messages.getMessage(getClass(), "modifyField"), "M");
changeTypeMap.put(messages.getMessage(getClass(), "deleteField"), "D");
changeTypeMap.put(messages.getMessage(getClass(), "restoreField"), "R");
entityMetaClassesMap = getEntityMetaClasses();
entityNameField.setOptionsMap(entityMetaClassesMap);
changeTypeField.setOptionsMap(changeTypeMap);
filterEntityNameField.setOptionsMap(entityMetaClassesMap);
Action clearAction = instancePicker.getAction("clear");
instancePicker.removeAction(clearAction);
instancePicker.removeAction("lookup");
addAction(new SaveAction());
addAction(new CancelAction());
Label label1 = factory.createComponent(Label.class);
label1.setValue(messages.getMessage(getClass(), "show"));
label1.setAlignment(Alignment.MIDDLE_LEFT);
Label label2 = factory.createComponent(Label.class);
label2.setValue(messages.getMessage(getClass(), "rows"));
label2.setAlignment(Alignment.MIDDLE_LEFT);
ButtonsPanel panel = entityLogTable.getButtonsPanel();
showRowField = factory.createComponent(TextField.class);
showRowField.setWidth(themeConstants.get("cuba.gui.EntityLogBrowser.showRowField.width"));
showRowField.setValue(String.valueOf(DEFAULT_SHOW_ROWS));
panel.add(label1);
panel.add(showRowField);
panel.add(label2);
disableControls();
setDateFieldTime();
instancePicker.setEnabled(false);
PickerField.LookupAction lookupAction = new PickerField.LookupAction(instancePicker) {
@Override
public void actionPerform(Component component) {
final MetaClass metaClass = pickerField.getMetaClass();
if (pickerField.isEditable()) {
String currentWindowAlias = lookupScreen;
if (currentWindowAlias == null) {
if (metaClass == null) {
throw new IllegalStateException("Please specify metaclass or property for PickerField");
}
currentWindowAlias = windowConfig.getLookupScreenId(metaClass);
}
Window lookupWindow = (Window) pickerField.getFrame();
Lookup.Handler lookupWindowHandler = items -> {
if (!items.isEmpty()) {
Object item = items.iterator().next();
pickerField.setValue(item);
afterSelect(items);
}
};
if (config.hasWindow(currentWindowAlias)) {
lookupWindow.openLookup(currentWindowAlias, lookupWindowHandler, lookupScreenOpenType, lookupScreenParams != null ? lookupScreenParams : Collections.emptyMap());
} else {
lookupWindow.openLookup(EntityInspectorBrowse.SCREEN_NAME, lookupWindowHandler, WindowManager.OpenType.THIS_TAB, ParamsMap.of("entity", metaClass.getName()));
}
lookupWindow.addCloseListener(actionId -> pickerField.requestFocus());
}
}
};
instancePicker.addAction(lookupAction);
instancePicker.addAction(clearAction);
entityNameField.addValueChangeListener(e -> {
if (entityNameField.isEditable())
fillAttributes((String) e.getValue(), null, true);
});
loggedEntityDs.addItemChangeListener(e -> {
if (e.getItem() != null) {
fillAttributes(e.getItem().getName(), e.getItem(), false);
checkAllCheckboxes();
} else {
setSelectAllCheckBox(false);
clearAttributes();
}
});
filterEntityNameField.addValueChangeListener(e -> {
if (e.getValue() != null) {
instancePicker.setEnabled(true);
MetaClass metaClass = metadata.getSession().getClassNN(e.getValue().toString());
instancePicker.setMetaClass(metaClass);
} else {
instancePicker.setEnabled(false);
}
instancePicker.setValue(null);
});
selectAllCheckBox.addValueChangeListener(e -> enableAllCheckBoxes((boolean) e.getValue()));
entityLogTable.addGeneratedColumn("entityId", entity -> {
if (entity.getObjectEntityId() != null) {
return new Table.PlainTextCell(entity.getObjectEntityId().toString());
}
return null;
}, Table.PlainTextCell.class);
}
use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.
the class FileDownloadHelper method initGeneratedColumn.
/**
* Initializes a table column for downloading files.
*
* @param table table displaying some entity
* @param fileProperty property of the entity which is a reference to {@link FileDescriptor}
*/
public static void initGeneratedColumn(final Table table, final String fileProperty) {
final ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.NAME);
final ExportDisplay exportDisplay = AppBeans.get(ExportDisplay.NAME);
table.addGeneratedColumn(fileProperty + ".name", new Table.ColumnGenerator() {
@Override
public Component generateCell(final Entity entity) {
final FileDescriptor fd = entity.getValueEx(fileProperty);
if (fd == null) {
return componentsFactory.createComponent(Label.class);
}
if (PersistenceHelper.isNew(fd)) {
Label label = componentsFactory.createComponent(Label.class);
label.setValue(fd.getName());
return label;
} else {
Button button = componentsFactory.createComponent(Button.class);
button.setStyleName("link");
button.setAction(new AbstractAction("download") {
@Override
public void actionPerform(Component component) {
exportDisplay.show(fd);
}
@Override
public String getCaption() {
return fd.getName();
}
});
return button;
}
}
});
}
Aggregations