use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class LockEditor method init.
@Override
public void init(Map<String, Object> params) {
Map<String, Object> options = new TreeMap<>();
for (MetaClass metaClass : metadata.getTools().getAllPersistentMetaClasses()) {
if (metadata.getExtendedEntities().getExtendedClass(metaClass) == null) {
MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalMetaClass(metaClass);
String originalName = originalMetaClass == null ? metaClass.getName() : originalMetaClass.getName();
options.put(messages.getTools().getEntityCaption(metaClass) + " (" + metaClass.getName() + ")", originalName);
}
}
entityNameLookupField = componentsFactory.createComponent(LookupField.class);
entityNameLookupField.setDatasource(lockDescriptorDs, "name");
entityNameLookupField.setOptionsMap(options);
entityNameLookupField.setCaption(messages.getMessage(LockDescriptor.class, "LockDescriptor.name"));
fieldGroup.getFieldNN("entity").setComponent(entityNameLookupField);
if (((LockDescriptor) WindowParams.ITEM.getEntity(params)).getName() != null) {
nameTypeOptionsGroup.setVisible(false);
entityNameLookupField.setVisible(false);
nameField.setEditable(false);
timeoutSecField.requestFocus();
} else {
nameTypeOptionsGroup.setOptionsEnum(LockDescriptorNameType.class);
nameTypeOptionsGroup.addValueChangeListener(e -> {
if (LockDescriptorNameType.ENTITY.equals(e.getValue())) {
nameField.setVisible(false);
entityNameLookupField.setVisible(true);
entityNameLookupField.requestFocus();
} else {
nameField.setVisible(true);
nameField.requestFocus();
entityNameLookupField.setVisible(false);
}
});
nameTypeOptionsGroup.setValue(LockDescriptorNameType.ENTITY);
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class EntityRestore method buildLayout.
protected void buildLayout() {
Object value = entities.getValue();
if (value != null) {
MetaClass metaClass = (MetaClass) value;
MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
if (deleteTsMetaProperty != null) {
if (entitiesTable != null) {
tablePanel.remove(entitiesTable);
}
if (filter != null) {
tablePanel.remove(filter);
}
ComponentsFactory componentsFactory = AppConfig.getFactory();
entitiesTable = componentsFactory.createComponent(Table.class);
entitiesTable.setFrame(frame);
restoreButton = componentsFactory.createComponent(Button.class);
restoreButton.setId("restore");
restoreButton.setCaption(getMessage("entityRestore.restore"));
ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
buttonsPanel.add(restoreButton);
entitiesTable.setButtonsPanel(buttonsPanel);
RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
entitiesTable.setRowsCount(rowsCount);
final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
Formatter dateTimeFormatter = propertyValue -> {
if (propertyValue == null) {
return StringUtils.EMPTY;
}
return dateTimeFormat.format(propertyValue);
};
// collect properties in order to add non-system columns first
LinkedList<Table.Column> nonSystemPropertyColumns = new LinkedList<>();
LinkedList<Table.Column> systemPropertyColumns = new LinkedList<>();
List<MetaProperty> metaProperties = new ArrayList<>();
for (MetaProperty metaProperty : metaClass.getProperties()) {
// don't show embedded & multiple referred entities
Range range = metaProperty.getRange();
if (isEmbedded(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
continue;
}
metaProperties.add(metaProperty);
Table.Column column = new Table.Column(metaClass.getPropertyPath(metaProperty.getName()));
if (!metadataTools.isSystem(metaProperty)) {
column.setCaption(getPropertyCaption(metaClass, metaProperty));
nonSystemPropertyColumns.add(column);
} else {
column.setCaption(metaProperty.getName());
systemPropertyColumns.add(column);
}
if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
column.setFormatter(dateTimeFormatter);
}
}
for (Table.Column column : nonSystemPropertyColumns) {
entitiesTable.addColumn(column);
}
for (Table.Column column : systemPropertyColumns) {
entitiesTable.addColumn(column);
}
DsContext dsContext = getDsContext();
if (entitiesDs != null) {
((DsContextImplementation) dsContext).unregister(entitiesDs);
}
entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
entitiesDs.setSoftDeletion(false);
entitiesDs.refresh();
entitiesTable.setDatasource(entitiesDs);
String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
filter = componentsFactory.createComponent(Filter.class);
filter.setId(filterId);
filter.setFrame(getFrame());
StringBuilder sb = new StringBuilder("");
for (MetaProperty property : metaClass.getProperties()) {
AnnotatedElement annotatedElement = property.getAnnotatedElement();
if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
sb.append(property.getName()).append("|");
}
}
Element filterElement = Dom4j.readDocument(String.format("<filter id=\"%s\">\n" + " <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
String excludedProperties = sb.toString();
if (StringUtils.isNotEmpty(excludedProperties)) {
Element properties = filterElement.element("properties");
properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
}
filter.setXmlDescriptor(filterElement);
filter.setUseMaxResults(true);
filter.setDatasource(entitiesDs);
entitiesTable.setSizeFull();
entitiesTable.setMultiSelect(true);
Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withHandler(event -> showRestoreDialog());
entitiesTable.addAction(restoreAction);
restoreButton.setAction(restoreAction);
tablePanel.add(filter);
tablePanel.add(entitiesTable);
tablePanel.expand(entitiesTable, "100%", "100%");
entitiesTable.refresh();
((FilterImplementation) filter).loadFiltersAndApplyDefault();
}
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class ConstraintLocalizationEdit method initEntityNameField.
protected void initEntityNameField() {
String entityName = getItem().getEntityName();
MetaClass metaClass = metadata.getClass(entityName);
if (metaClass != null) {
MessageTools messageTools = messages.getTools();
this.entityName.setValue(messageTools.getEntityCaption(metaClass) + " (" + metaClass.getName() + ")");
} else {
this.entityName.setValue(entityName);
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class GroupBrowser method init.
@Override
public void init(final Map<String, Object> params) {
CreateAction createAction = new CreateAction(groupsTree);
createAction.setAfterCommitHandler(entity -> {
groupsTree.expandTree();
});
groupsTree.addAction(createAction);
createAction.setCaption(getMessage("action.create"));
createAction.setOpenType(OpenType.DIALOG);
EditAction groupEditAction = new EditAction(groupsTree);
groupEditAction.setAfterCommitHandler(entity -> {
groupsTree.expandTree();
});
groupEditAction.setOpenType(OpenType.DIALOG);
groupsTree.addAction(groupEditAction);
groupCreateButton.addAction(createAction);
groupCreateButton.addAction(groupCopyAction);
userCreateAction = new GroupPropertyCreateAction(usersTable);
userCreateAction.setAfterCommitHandler(entity -> {
usersTable.getDatasource().refresh();
});
groupsTree.addAction(new RemoveAction(groupsTree) {
@Override
protected boolean isApplicable() {
if (target != null && target.getDatasource() != null && target.getSingleSelected() != null) {
@SuppressWarnings("unchecked") HierarchicalDatasource<Group, UUID> ds = (HierarchicalDatasource<Group, UUID>) target.getDatasource();
UUID selectedItemId = (UUID) target.getSingleSelected().getId();
return ds.getChildren(selectedItemId).isEmpty();
}
return false;
}
});
usersTable.addAction(userCreateAction);
RemoveAction removeAction = new UserRemoveAction(usersTable, userManagementService);
usersTable.addAction(removeAction);
Action moveToGroupAction = new ItemTrackingAction("moveToGroup").withIcon("icons/move.png").withCaption(getMessage("moveToGroup")).withHandler(event -> {
Set<User> selected = usersTable.getSelected();
if (!selected.isEmpty()) {
Map<String, Object> lookupParams = ParamsMap.of("exclude", groupsTree.getSelected().iterator().next(), "excludeChildren", false);
Lookup lookupWindow = openLookup(Group.class, items -> {
if (items.size() == 1) {
Group group = (Group) items.iterator().next();
List<UUID> usersForModify = new ArrayList<>();
for (User user : selected) {
usersForModify.add(user.getId());
}
userManagementService.moveUsersToGroup(usersForModify, group.getId());
if (selected.size() == 1) {
User user = selected.iterator().next();
showNotification(formatMessage("userMovedToGroup", user.getLogin(), group.getName()));
} else {
showNotification(formatMessage("usersMovedToGroup", group.getName()));
}
usersTable.getDatasource().refresh();
}
}, OpenType.DIALOG, lookupParams);
lookupWindow.addCloseListener(actionId -> {
usersTable.requestFocus();
});
}
});
MetaClass userMetaClass = metadata.getSession().getClass(User.class);
moveToGroupAction.setEnabled(security.isEntityOpPermitted(userMetaClass, EntityOp.UPDATE));
usersTable.addAction(moveToGroupAction);
tabsheet.addListener(newTab -> {
if ("constraintsTab".equals(newTab.getName())) {
initConstraintsTab();
} else if ("attributesTab".equals(newTab.getName())) {
initAttributesTab();
}
});
final boolean hasPermissionsToCreateGroup = security.isEntityOpPermitted(metadata.getSession().getClass(Group.class), EntityOp.CREATE);
// enable actions if group is selected
groupsDs.addItemChangeListener(e -> {
if (userCreateAction != null) {
userCreateAction.setEnabled(e.getItem() != null);
}
if (attributeCreateAction != null) {
attributeCreateAction.setEnabled(e.getItem() != null);
}
if (constraintCreateAction != null) {
constraintCreateAction.setEnabled(e.getItem() != null);
}
groupCopyAction.setEnabled(hasPermissionsToCreateGroup && e.getItem() != null);
CollectionDatasource ds = usersTable.getDatasource();
if (ds instanceof CollectionDatasource.SupportsPaging) {
((CollectionDatasource.SupportsPaging) ds).setFirstResult(0);
}
});
groupsDs.refresh();
groupsTree.expandTree();
final Collection<UUID> itemIds = groupsDs.getRootItemIds();
if (!itemIds.isEmpty()) {
groupsTree.setSelected(groupsDs.getItem(itemIds.iterator().next()));
}
groupCreateButton.setEnabled(hasPermissionsToCreateGroup);
groupCopyAction.setEnabled(hasPermissionsToCreateGroup);
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 import 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), createGroupsImportView());
} else {
importedEntities = entityImportExportService.importEntitiesFromZIP(fileBytes, createGroupsImportView());
}
long importedGroupsCount = importedEntities.stream().filter(entity -> entity instanceof Group).count();
showNotification(importedGroupsCount + " groups imported", NotificationType.HUMANIZED);
groupsDs.refresh();
} catch (Exception e) {
showNotification(formatMessage("importError", e.getMessage()), NotificationType.ERROR);
}
});
Companion companion = getCompanion();
if (companion != null) {
companion.initDragAndDrop(usersTable, groupsTree, (event) -> {
if (event.getUsers().size() == 1) {
if (moveSelectedUsersToGroup(event)) {
showNotification(formatMessage("userMovedToGroup", event.getUsers().get(0).getLogin(), event.getGroup().getName()));
}
} else {
showOptionDialog(messages.getMainMessage("dialogs.Confirmation"), formatMessage("dialogs.moveToGroup.message", event.getGroup().getName(), event.getUsers().size()), MessageType.CONFIRMATION, new Action[] { new DialogAction(Type.OK).withHandler(dialogEvent -> {
if (moveSelectedUsersToGroup(event)) {
showNotification(formatMessage("usersMovedToGroup", event.getGroup().getName()));
}
}), new DialogAction(Type.CANCEL, Action.Status.PRIMARY) });
}
});
}
}
use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.
the class GroupBrowser method initConstraintsTab.
protected void initConstraintsTab() {
if (constraintsTabInitialized) {
return;
}
@SuppressWarnings("unchecked") Table<Constraint> constraintsTable = (Table) getComponentNN("constraintsTable");
constraintCreateAction = new GroupPropertyCreateAction(constraintsTable);
constraintsTable.addAction(constraintCreateAction);
Action activateAction = new ItemTrackingAction("activate").withHandler(event -> {
Constraint constraint = constraintsTable.getSingleSelected();
if (constraint != null) {
constraint.setIsActive(!Boolean.TRUE.equals(constraint.getIsActive()));
constraintsDs.commit();
constraintsDs.refresh();
}
});
constraintsTable.addAction(activateAction);
constraintsTable.addAction(new ConstraintLocalizationEditAction(constraintsTable));
constraintsDs.addItemChangeListener(e -> {
if (e.getItem() != null) {
activateAction.setCaption(Boolean.TRUE.equals(e.getItem().getIsActive()) ? getMessage("deactivate") : getMessage("activate"));
}
});
constraintsTable.addGeneratedColumn("entityName", constraint -> {
if (StringUtils.isEmpty(constraint.getEntityName())) {
return componentsFactory.createComponent(Label.class);
}
MetaClass metaClass = metadata.getClassNN(constraint.getEntityName());
MetaClass effectiveMetaClass = metadata.getExtendedEntities().getEffectiveMetaClass(metaClass);
Label label = componentsFactory.createComponent(Label.class);
label.setValue(effectiveMetaClass.getName());
return label;
});
constraintsTabInitialized = true;
constraintsTable.refresh();
}
Aggregations