Search in sources :

Example 36 with MetaProperty

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);
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 37 with MetaProperty

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;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 38 with MetaProperty

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();
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Dom4j(com.haulmont.bali.util.Dom4j) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) EntityRestoreService(com.haulmont.cuba.core.app.EntityRestoreService) SimpleDateFormat(java.text.SimpleDateFormat) EnableRestore(com.haulmont.cuba.core.entity.annotation.EnableRestore) MetaClass(com.haulmont.chile.core.model.MetaClass) Inject(javax.inject.Inject) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) GroupDatasource(com.haulmont.cuba.gui.data.GroupDatasource) ViewRepository(com.haulmont.cuba.core.global.ViewRepository) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Range(com.haulmont.chile.core.model.Range) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Status(com.haulmont.cuba.gui.components.Action.Status) Type(com.haulmont.cuba.gui.components.DialogAction.Type) View(com.haulmont.cuba.core.global.View) AppConfig(com.haulmont.cuba.gui.AppConfig) Element(org.dom4j.Element) Entity(com.haulmont.cuba.core.entity.Entity) MessageTools(com.haulmont.cuba.core.global.MessageTools) AnnotatedElement(java.lang.reflect.AnnotatedElement) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Formatter(com.haulmont.cuba.gui.components.Formatter) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaClass(com.haulmont.chile.core.model.MetaClass) SimpleDateFormat(java.text.SimpleDateFormat)

Example 39 with MetaProperty

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;
}
Also used : MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 40 with MetaProperty

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;
}
Also used : MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)157 MetaClass (com.haulmont.chile.core.model.MetaClass)102 Entity (com.haulmont.cuba.core.entity.Entity)44 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)26 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Range (com.haulmont.chile.core.model.Range)13 Nullable (javax.annotation.Nullable)11 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)9 java.util (java.util)9 Element (org.dom4j.Element)9 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)8 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Query (com.haulmont.cuba.core.Query)5 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)5 Collection (java.util.Collection)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)4 Logger (org.slf4j.Logger)4