Search in sources :

Example 31 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class RelatedEntitiesServiceBean method getRelatedIds.

@SuppressWarnings("unchecked")
@Override
public List<Object> getRelatedIds(List<Object> parentIds, String parentMetaClass, String relationProperty) {
    checkNotNullArgument(parentIds, "parents argument is null");
    checkNotNullArgument(parentMetaClass, "parentMetaClass argument is null");
    checkNotNullArgument(relationProperty, "relationProperty argument is null");
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClassNN(parentMetaClass));
    Class parentClass = metaClass.getJavaClass();
    MetaProperty metaProperty = metaClass.getPropertyNN(relationProperty);
    // return empty list only after all argument checks
    if (parentIds.isEmpty()) {
        return Collections.emptyList();
    }
    MetaClass propertyMetaClass = extendedEntities.getEffectiveMetaClass(metaProperty.getRange().asClass());
    Class propertyClass = propertyMetaClass.getJavaClass();
    List<Object> relatedIds = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String parentPrimaryKey = metadata.getTools().getPrimaryKeyName(metaClass);
        String queryString = "select x from " + parentMetaClass + " x where x." + parentPrimaryKey + " in :ids";
        Query query = em.createQuery(queryString);
        String relatedPrimaryKey = metadata.getTools().getPrimaryKeyName(propertyMetaClass);
        View view = new View(parentClass);
        view.addProperty(relationProperty, new View(propertyClass).addProperty(relatedPrimaryKey));
        query.setView(view);
        query.setParameter("ids", parentIds);
        List<Entity> resultList = query.getResultList();
        for (Entity e : resultList) {
            Object value = e.getValue(relationProperty);
            if (value instanceof Entity) {
                relatedIds.add(((Entity) value).getId());
            } else if (value instanceof Collection) {
                for (Object collectionItem : (Collection) value) {
                    relatedIds.add(((Entity) collectionItem).getId());
                }
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return relatedIds;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) View(com.haulmont.cuba.core.global.View) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 32 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class EntityInspectorBrowse method createEntityImportView.

protected EntityImportView createEntityImportView(MetaClass metaClass) {
    @SuppressWarnings("unchecked") Class<? extends Entity> javaClass = metaClass.getJavaClass();
    EntityImportView entityImportView = new EntityImportView(javaClass);
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        if (!metadata.getTools().isPersistent(metaProperty))
            continue;
        switch(metaProperty.getType()) {
            case DATATYPE:
            case ENUM:
                entityImportView.addLocalProperty(metaProperty.getName());
                break;
            case ASSOCIATION:
            case COMPOSITION:
                Range.Cardinality cardinality = metaProperty.getRange().getCardinality();
                switch(cardinality) {
                    case MANY_TO_ONE:
                        entityImportView.addManyToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
                        break;
                    case ONE_TO_ONE:
                        entityImportView.addOneToOneProperty(metaProperty.getName(), ReferenceImportBehaviour.IGNORE_MISSING);
                        break;
                }
                break;
            default:
                throw new IllegalStateException("unknown property type");
        }
    }
    return entityImportView;
}
Also used : EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 33 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class EntityInspectorBrowse method createEntitiesTable.

protected void createEntitiesTable(MetaClass meta) {
    if (entitiesTable != null)
        tableBox.remove(entitiesTable);
    if (filter != null) {
        filterBox.remove(filter);
    }
    entitiesTable = componentsFactory.createComponent(Table.class);
    entitiesTable.setFrame(frame);
    if (companion != null) {
        companion.setHorizontalScrollEnabled(entitiesTable, true);
    }
    ClientType clientType = AppConfig.getClientType();
    if (clientType == ClientType.WEB) {
        textSelection.setVisible(true);
        textSelection.addValueChangeListener(e -> changeTableTextSelectionEnabled());
    }
    final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
    Formatter dateTimeFormatter = value -> {
        if (value == null) {
            return StringUtils.EMPTY;
        }
        return dateTimeFormat.format(value);
    };
    // collect properties in order to add non-system columns first
    List<Table.Column> nonSystemPropertyColumns = new ArrayList<>(10);
    List<Table.Column> systemPropertyColumns = new ArrayList<>(10);
    for (MetaProperty metaProperty : meta.getProperties()) {
        // don't show embedded, transient & multiple referred entities
        if (isEmbedded(metaProperty) || metadata.getTools().isNotPersistent(metaProperty))
            continue;
        Range range = metaProperty.getRange();
        if (range.getCardinality().isMany())
            continue;
        Table.Column column = new Table.Column(meta.getPropertyPath(metaProperty.getName()));
        if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
            column.setFormatter(dateTimeFormatter);
        }
        if (metaProperty.getJavaType().equals(String.class)) {
            column.setMaxTextLength(MAX_TEXT_LENGTH);
        }
        if (!metadata.getTools().isSystem(metaProperty)) {
            column.setCaption(getPropertyCaption(meta, metaProperty));
            nonSystemPropertyColumns.add(column);
        } else {
            column.setCaption(metaProperty.getName());
            systemPropertyColumns.add(column);
        }
    }
    for (Table.Column column : nonSystemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    for (Table.Column column : systemPropertyColumns) {
        entitiesTable.addColumn(column);
    }
    if (entitiesDs != null) {
        ((DsContextImplementation) getDsContext()).unregister(entitiesDs);
    }
    entitiesDs = DsBuilder.create(getDsContext()).setId("entitiesDs").setMetaClass(meta).setView(createView(meta)).buildCollectionDatasource();
    entitiesDs.setLoadDynamicAttributes(true);
    entitiesDs.setSoftDeletion(!removedRecords.isChecked());
    entitiesDs.setQuery(String.format("select e from %s e", meta.getName()));
    entitiesTable.setDatasource(entitiesDs);
    tableBox.add(entitiesTable);
    entitiesTable.setSizeFull();
    createButtonsPanel(entitiesTable);
    RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
    rowsCount.setDatasource(entitiesDs);
    entitiesTable.setRowsCount(rowsCount);
    entitiesTable.setEnterPressAction(entitiesTable.getAction("edit"));
    entitiesTable.setItemClickAction(entitiesTable.getAction("edit"));
    entitiesTable.setMultiSelect(true);
    createFilter();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) ReferenceImportBehaviour(com.haulmont.cuba.core.app.importexport.ReferenceImportBehaviour) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) LoggerFactory(org.slf4j.LoggerFactory) ParamsMap(com.haulmont.bali.util.ParamsMap) CubaIcon(com.haulmont.cuba.gui.icons.CubaIcon) SimpleDateFormat(java.text.SimpleDateFormat) Icons(com.haulmont.cuba.gui.icons.Icons) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) JSON(com.haulmont.cuba.gui.export.ExportFormat.JSON) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Files(com.google.common.io.Files) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) ExportFormat(com.haulmont.cuba.gui.export.ExportFormat) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) com.haulmont.cuba.gui.components.actions(com.haulmont.cuba.gui.components.actions) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Logger(org.slf4j.Logger) Range(com.haulmont.chile.core.model.Range) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) EntityImportExportService(com.haulmont.cuba.core.app.importexport.EntityImportExportService) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) ZIP(com.haulmont.cuba.gui.export.ExportFormat.ZIP) IOUtils(org.apache.commons.io.IOUtils) EntityOp(com.haulmont.cuba.security.entity.EntityOp) ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) AppConfig(com.haulmont.cuba.gui.AppConfig) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) ClientConfig(com.haulmont.cuba.client.ClientConfig) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Session(com.haulmont.chile.core.model.Session) Entity(com.haulmont.cuba.core.entity.Entity) InputStream(java.io.InputStream) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Formatter(com.haulmont.cuba.gui.components.Formatter) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) SimpleDateFormat(java.text.SimpleDateFormat)

Example 34 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class EntityInspectorBrowse method createView.

protected View createView(MetaClass meta) {
    // noinspection unchecked
    View view = new View(meta.getJavaClass(), false);
    for (MetaProperty metaProperty : meta.getProperties()) {
        switch(metaProperty.getType()) {
            case DATATYPE:
            case ENUM:
                view.addProperty(metaProperty.getName());
                break;
            case ASSOCIATION:
            case COMPOSITION:
                if (!metaProperty.getRange().getCardinality().isMany()) {
                    View minimal = metadata.getViewRepository().getView(metaProperty.getRange().asClass(), View.MINIMAL);
                    View propView = new View(minimal, metaProperty.getName() + "Ds", false);
                    view.addProperty(metaProperty.getName(), propView);
                }
                break;
            default:
                throw new IllegalStateException("unknown property type");
        }
    }
    return view;
}
Also used : MetaProperty(com.haulmont.chile.core.model.MetaProperty) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView)

Example 35 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class EntityInspectorEditor method createNestedEmbeddedDatasources.

protected void createNestedEmbeddedDatasources(MetaClass metaClass, String fqnPrefix, Datasource masterDs) {
    for (MetaProperty metaProperty : metaClass.getProperties()) {
        if (MetaProperty.Type.ASSOCIATION == metaProperty.getType() || MetaProperty.Type.COMPOSITION == metaProperty.getType()) {
            if (isEmbedded(metaProperty)) {
                String fqn = fqnPrefix + "." + metaProperty.getName();
                MetaClass propertyMetaClass = metaProperty.getRange().asClass();
                NestedDatasource propertyDs = new EmbeddedDatasourceImpl();
                propertyDs.setup(fqn + "Ds", masterDs, metaProperty.getName());
                createNestedEmbeddedDatasources(propertyMetaClass, fqn, propertyDs);
                datasources.put(fqn, propertyDs);
                dsContext.register(propertyDs);
            }
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) 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