use of com.haulmont.chile.core.model.MetaProperty 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.chile.core.model.MetaProperty 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.chile.core.model.MetaProperty 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.MetaProperty in project cuba by cuba-platform.
the class BulkEditorWindow method createView.
/**
* Creates a view, loading only necessary properties.
* Referenced entities will be loaded with a MINIMAL view.
*
* @param meta meta class
* @return View instance
*/
protected View createView(MetaClass meta) {
@SuppressWarnings("unchecked") View view = new View(meta.getJavaClass(), false);
for (MetaProperty metaProperty : meta.getProperties()) {
if (!managedFields.containsKey(metaProperty.getName()) && !managedEmbeddedProperties.contains(metaProperty.getName())) {
continue;
}
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
view.addProperty(metaProperty.getName());
break;
case ASSOCIATION:
case COMPOSITION:
View propView;
if (!metadataTools.isEmbedded(metaProperty)) {
propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
// in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
// loading property lazy prevents this from happening
view.addProperty(metaProperty.getName(), propView, true);
} else {
// build view for embedded property
propView = createEmbeddedView(metaProperty.getRange().asClass(), metaProperty.getName());
view.addProperty(metaProperty.getName(), propView, false);
}
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return view;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class BulkEditorWindow method createEmbeddedView.
protected View createEmbeddedView(MetaClass meta, String fqnPrefix) {
@SuppressWarnings("unchecked") View view = new View(meta.getJavaClass(), false);
for (MetaProperty metaProperty : meta.getProperties()) {
String fqn = fqnPrefix + "." + metaProperty.getName();
if (!managedFields.containsKey(fqn)) {
continue;
}
switch(metaProperty.getType()) {
case DATATYPE:
case ENUM:
view.addProperty(metaProperty.getName());
break;
case ASSOCIATION:
case COMPOSITION:
View propView;
if (!metadataTools.isEmbedded(metaProperty)) {
propView = viewRepository.getView(metaProperty.getRange().asClass(), View.MINIMAL);
} else {
// build view for embedded property
propView = createEmbeddedView(metaProperty.getRange().asClass(), fqn);
}
// in some cases JPA loads extended entities as instance of base class which leads to ClassCastException
// loading property lazy prevents this from happening
view.addProperty(metaProperty.getName(), propView, true);
break;
default:
throw new IllegalStateException("unknown property type");
}
}
return view;
}
Aggregations