use of io.jmix.core.Entity in project jmix by jmix-framework.
the class BulkEditorWindow method createDataComponents.
@SuppressWarnings("unchecked")
protected void createDataComponents() {
if (managedFields.isEmpty()) {
infoLabel.setValue(messages.getMessage("io.jmix.ui.app.bulk/bulk.noEditableProperties"));
applyButton.setVisible(false);
return;
}
List<ManagedField> editFields = new ArrayList<>(managedFields.values());
// sort fields
Comparator comparator;
if (fieldSorter != null) {
Map<MetaProperty, Integer> sorted = fieldSorter.apply(editFields.stream().map(ManagedField::getMetaProperty).collect(Collectors.toList()));
comparator = Comparator.<ManagedField>comparingInt(item -> sorted.get(item.getMetaProperty()));
} else {
comparator = Comparator.comparing(ManagedField::getLocalizedName);
}
editFields.sort(comparator);
CssLayout fieldsLayout = uiComponents.create(CssLayout.NAME);
fieldsLayout.setStyleName("jmix-bulk-editor-fields-layout");
fieldsLayout.setWidthFull();
fieldsLayout.setHeightFull();
int fromField;
int toField = 0;
int addedColumns = 0;
for (int col = 0; col < columnsMode.getColumnsCount(); col++) {
fromField = toField;
toField += getFieldsCountForColumn(editFields.size() - toField, columnsMode.getColumnsCount() - col);
DeviceInfo deviceInfo = deviceInfoProvider.getDeviceInfo();
VBoxLayout column = uiComponents.create(VBoxLayout.NAME);
column.setStyleName("jmix-bulk-editor-column");
column.setWidth(Component.AUTO_SIZE);
for (int fieldIndex = fromField; fieldIndex < toField; fieldIndex++) {
ManagedField field = editFields.get(fieldIndex);
CssLayout row = uiComponents.create(CssLayout.NAME);
row.setStyleName("jmix-bulk-editor-row");
row.setWidth("100%");
Label<String> label = uiComponents.create(Label.NAME);
label.setValue(field.getLocalizedName());
label.setStyleName("jmix-bulk-editor-label");
row.add(label);
Datasource<Entity> fieldDs = datasource;
// so we can check field domain
if (metadataTools.isJpaEmbeddable(field.getMetaProperty().getDomain())) {
fieldDs = datasources.get(field.getParentFqn());
}
BulkEditorFieldFactory fieldFactory = getFieldFactory();
Field<?> editField = fieldFactory.createField(fieldDs, field.getMetaProperty());
if (editField != null) {
editField.setFrame(getFrame());
editField.setStyleName("jmix-bulk-editor-field");
if (isPickerFieldWrapperNeeded(editField, deviceInfo)) {
CssLayout wrapper = uiComponents.create(CssLayout.NAME);
wrapper.setStyleName("jmix-bulk-editor-picker-field-wrapper");
wrapper.add(editField);
row.add(wrapper);
} else {
row.add(editField);
}
boolean required = editField.isRequired();
if (!required) {
Button clearButton = uiComponents.create(Button.class);
clearButton.setIconFromSet(JmixIcon.TRASH);
clearButton.setCaption("");
clearButton.setDescription(messages.getMessage("io.jmix.ui.app.bulk/bulk.clearAttribute"));
clearButton.addClickListener(e -> {
editField.setEnabled(!editField.isEnabled());
if (!editField.isEnabled()) {
if (editField instanceof ListEditor) {
((Field) editField).setValue(Collections.EMPTY_LIST);
} else {
editField.setValue(null);
}
e.getSource().setIconFromSet(JmixIcon.EDIT);
e.getSource().setDescription(messages.getMessage("io.jmix.ui.app.bulk/bulk.editAttribute"));
} else {
e.getSource().setIconFromSet(JmixIcon.TRASH);
e.getSource().setDescription(messages.getMessage("io.jmix.ui.app.bulk/bulk.clearAttribute"));
}
});
row.add(clearButton);
} else {
// hidden component for correctly showing layout
Button spacerButton = uiComponents.create(Button.class);
spacerButton.setIconFromSet(JmixIcon.TRASH);
spacerButton.setStyleName("jmix-bulk-editor-spacer");
row.add(spacerButton);
}
// disable bean validator
// noinspection RedundantCast
editField.getValidators().stream().filter(v -> v instanceof AbstractBeanValidator).findFirst().ifPresent(((Field) editField)::removeValidator);
// disable required
editField.setRequired(false);
if (editField instanceof ListEditor) {
((Field) editField).setValue(Collections.EMPTY_LIST);
} else {
editField.setValue(null);
}
if (fieldValidators != null) {
Consumer validator = fieldValidators.get(field.getFqn());
if (validator != null) {
editField.addValidator(validator);
}
}
column.add(row);
dataFields.put(field.getFqn(), editField);
} else {
column.add(uiComponents.create(Label.class));
}
}
fieldsLayout.add(column);
// if there is no fields remain
if (editFields.size() - toField == 0) {
addedColumns = col + 1;
break;
}
}
fieldsLayout.addStyleName(COLUMN_COUNT_STYLENAME + addedColumns);
fieldsScrollBox.add(fieldsLayout);
dataFields.values().stream().filter(f -> f instanceof Focusable).findFirst().ifPresent(f -> ((Focusable) f).focus());
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EntityManagerImpl method reload.
@SuppressWarnings("unchecked")
@Nullable
@Override
public <T extends Entity> T reload(T entity, String... fetchPlanNames) {
Preconditions.checkNotNullArgument(entity, "entity is null");
Entity resultEntity = find(entity.getClass(), EntityValues.getId(entity), fetchPlanNames);
return (T) resultEntity;
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class UpdateDetachedTest method testDataManager.
@Test
public void testDataManager() throws Exception {
Permission p;
LoadContext<Permission> ctx = new LoadContext<>(Permission.class);
ctx.setId(permissionId);
ctx.setView(new View(Permission.class).addProperty("target").addProperty("role", new View(Role.class).addProperty("name")));
p = dataManager.load(ctx);
assertNotNull(p);
p.setTarget("newTarget");
CommitContext commitCtx = new CommitContext(Collections.singleton(p));
Set<Entity> entities = dataManager.commit(commitCtx);
Permission result = null;
for (Entity entity : entities) {
if (entity.equals(p))
result = (Permission) entity;
}
result = testSupport.reserialize(result);
assertTrue(entityStates.isDetached(result));
assertNotNull(result.getRole());
assertTrue(entityStates.isDetached(result.getRole()));
assertTrue(entityStates.isLoaded(result, "role"));
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class QueryResultTest method testThirdQuery.
@Test
public void testThirdQuery() throws SQLException {
LoadContext context;
List<Entity> entities;
context = new LoadContext(User.class).setFetchPlan(FetchPlan.LOCAL);
LoadContext.Query query1 = context.setQueryString("select u from test$User u where u.email like :email").setParameter("email", "%aaa.com");
entities = dataManager.loadList(context);
assertEquals(20, entities.size());
context = new LoadContext(User.class).setFetchPlan(FetchPlan.LOCAL);
LoadContext.Query query2 = context.setQueryString("select u from test$User u where u.name like :name").setParameter("name", "A-%");
context.getPrevQueries().add(query1);
context.setQueryKey(111);
entities = dataManager.loadList(context);
assertEquals(10, entities.size());
context = new LoadContext(User.class).setFetchPlan(FetchPlan.LOCAL);
context.setQueryString("select u from test$User u where u.firstName like :firstName").setParameter("firstName", "C-%");
context.getPrevQueries().add(query1);
context.getPrevQueries().add(query2);
context.setQueryKey(111);
entities = dataManager.loadList(context);
assertEquals(5, entities.size());
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class EntityMutationDataFetcher method populateAndCheckComposition.
/**
* Walk through entity graph and check that all compositions have correct relations. If composition inverse value is
* null - update it to correct value. If composition inverse value doesn't match parent - throw exception.
*
* @param entity entity to be checked
* @param visited a set of entities that already be checked in graph
*/
protected void populateAndCheckComposition(Object entity, Set<Object> visited) {
if (visited.contains(entity)) {
return;
}
MetaClass metaClass = metadata.getClass(entity);
visited.add(entity);
metaClass.getProperties().stream().filter(metaProperty -> MetaProperty.Type.COMPOSITION.equals(metaProperty.getType())).filter(metaProperty -> !visited.contains(EntityValues.getValue(entity, metaProperty.getName()))).filter(metaProperty -> EntityValues.getValue(entity, metaProperty.getName()) != null).forEach(metaProperty -> {
// value from COMPOSITION metaProperty
Object childEntity = EntityValues.getValue(entity, metaProperty.getName());
Stream<?> childEntityStream = (childEntity instanceof Iterable<?>) ? StreamSupport.stream(((Iterable<?>) childEntity).spliterator(), false) : Stream.of(childEntity);
childEntityStream.forEach(childElement -> {
// have to check a linking for retrieved composition with a parent it can be different
assureCompositionInverseLink(entity, metaClass, metaProperty, childElement);
// digging deeper for child element to find any compositions inside
populateAndCheckComposition(childElement, visited);
});
});
}
Aggregations