Search in sources :

Example 96 with MetaProperty

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

the class ConditionDescriptorsTreeBuilder method addMultiplePropertyDescriptors.

protected void addMultiplePropertyDescriptors(String includeRe, String excludeRe, List<AbstractConditionDescriptor> descriptors, Filter filter) {
    List<String> includedProps = new ArrayList<>();
    Pattern inclPattern = Pattern.compile(includeRe.replace(" ", ""));
    MetaClass metaClass = filter.getDatasource().getMetaClass();
    for (MetaProperty property : metaClass.getProperties()) {
        if (!isPropertyAllowed(metaClass, property)) {
            continue;
        }
        if (inclPattern.matcher(property.getName()).matches()) {
            includedProps.add(property.getName());
        }
    }
    Pattern exclPattern = null;
    if (!StringUtils.isBlank(excludeRe)) {
        exclPattern = Pattern.compile(excludeRe.replace(" ", ""));
    }
    for (String prop : includedProps) {
        if (exclPattern == null || !exclPattern.matcher(prop).matches()) {
            AbstractConditionDescriptor conditionDescriptor = new PropertyConditionDescriptor(prop, null, filter.getFrame().getMessagesPack(), filterComponentName, filter.getDatasource());
            descriptors.add(conditionDescriptor);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 97 with MetaProperty

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

the class CollectionDsHelper method createProperties.

public static List<MetaPropertyPath> createProperties(View view, MetaClass metaClass) {
    List<MetaPropertyPath> properties = new ArrayList<>();
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    if (view != null && metadataTools.isPersistent(metaClass)) {
        for (ViewProperty property : view.getProperties()) {
            final String name = property.getName();
            final MetaProperty metaProperty = metaClass.getProperty(name);
            if (metaProperty == null) {
                String message = String.format("Unable to find property %s for entity %s", name, metaClass.getName());
                throw new DevelopmentException(message);
            }
            if (!metadataTools.isPersistent(metaProperty)) {
                String message = String.format("Specified transient property %s in view for datasource with persistent entity %s", name, metaClass.getName());
                LoggerFactory.getLogger(CollectionDsHelper.class).warn(message);
                continue;
            }
            final Range range = metaProperty.getRange();
            if (range == null) {
                continue;
            }
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
        // add all non-persistent properties
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            if (metadataTools.isNotPersistent(metaProperty)) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    } else {
        if (view != null) {
            LoggerFactory.getLogger(CollectionDsHelper.class).warn("Specified view {} for datasource with not persistent entity {}", view.getName(), metaClass.getName());
        }
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            final Range range = metaProperty.getRange();
            if (range == null)
                continue;
            final Range.Cardinality cardinality = range.getCardinality();
            if (!cardinality.isMany()) {
                properties.add(new MetaPropertyPath(metaClass, metaProperty));
            }
        }
    }
    return properties;
}
Also used : MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ArrayList(java.util.ArrayList) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Range(com.haulmont.chile.core.model.Range)

Example 98 with MetaProperty

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

the class DsContextImpl method repairReferences.

@SuppressWarnings("unchecked")
protected void repairReferences(Entity entity, Entity contextEntity) {
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    MetaClass contextEntityMetaClass = metadata.getClassNN(contextEntity.getClass());
    for (MetaProperty property : metaClass.getProperties()) {
        if (!property.getRange().isClass() || !property.getRange().asClass().equals(contextEntityMetaClass) || !PersistenceHelper.isLoaded(entity, property.getName()))
            continue;
        Object value = entity.getValue(property.getName());
        if (value != null) {
            if (property.getRange().getCardinality().isMany()) {
                Collection collection = (Collection) value;
                for (Object item : new ArrayList(collection)) {
                    if (contextEntity.equals(item) && contextEntity != item) {
                        if (collection instanceof List) {
                            List list = (List) collection;
                            list.set(list.indexOf(item), contextEntity);
                        } else {
                            collection.remove(item);
                            collection.add(contextEntity);
                        }
                    }
                }
            } else {
                if (contextEntity.equals(value) && contextEntity != value) {
                    entity.setValue(property.getName(), contextEntity);
                }
            }
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 99 with MetaProperty

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

the class DsContextImpl method replaceMasterCopies.

// Replace the reference to master entity with actual entity containing in the master datasource,
// because in case of nested property datasources there may be references to cloned master entities.
protected void replaceMasterCopies(Entity entity, NestedDatasource datasource) {
    Datasource masterDs = datasource.getMaster();
    MetaProperty metaProperty = datasource.getProperty();
    if (masterDs != null && metaProperty != null) {
        MetaProperty inverseProp = metaProperty.getInverse();
        if (inverseProp != null && !inverseProp.getRange().getCardinality().isMany()) {
            MetaClass metaClass = metadata.getExtendedEntities().getEffectiveMetaClass(inverseProp.getDomain());
            if (metaClass.equals(datasource.getMetaClass()) && (PersistenceHelper.isLoaded(entity, inverseProp.getName()) && // replace master only if it's already set
            entity.getValue(inverseProp.getName()) != null)) {
                Object masterItem = null;
                if (masterDs instanceof CollectionDatasource) {
                    Entity value = entity.getValue(inverseProp.getName());
                    if (value != null) {
                        Object id = value.getId();
                        // noinspection unchecked
                        masterItem = ((CollectionDatasource) masterDs).getItem(id);
                    }
                } else {
                    masterItem = masterDs.getItem();
                }
                if (masterItem != null) {
                    // CAUTION need to rework this mechanism in case of two or more nested collection datasources
                    ((AbstractInstance) entity).setValue(inverseProp.getName(), masterItem, false);
                }
            }
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 100 with MetaProperty

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

the class PropertyDatasourceImpl method commit.

@SuppressWarnings("unchecked")
@Override
public void commit() {
    if (!allowCommit) {
        return;
    }
    if (getCommitMode() == CommitMode.PARENT) {
        if (parentDs == null) {
            throw new IllegalStateException("parentDs is null while commitMode=PARENT");
        }
        if (parentDs instanceof CollectionDatasource) {
            CollectionDatasource parentCollectionDs = (CollectionDatasource) parentDs;
            for (Entity item : itemsToCreate) {
                parentCollectionDs.addItem(item);
            }
            for (Entity item : itemsToUpdate) {
                parentCollectionDs.modifyItem(item);
            }
            for (Entity item : itemsToDelete) {
                parentCollectionDs.removeItem(item);
            }
            // after repeated edit of new items the parent datasource can contain items-to-create which are deleted
            // in this datasource, so we need to delete them
            Collection<Entity> parentItemsToCreate = ((DatasourceImplementation) parentCollectionDs).getItemsToCreate();
            for (Entity createdItem : new ArrayList<>(parentItemsToCreate)) {
                if (!this.itemsToCreate.contains(createdItem)) {
                    MetaProperty inverseProp = metaProperty.getInverse();
                    // delete only if they have the same master item
                    if (inverseProp != null && PersistenceHelper.isLoaded(createdItem, inverseProp.getName()) && Objects.equals(createdItem.getValue(inverseProp.getName()), masterDs.getItem())) {
                        parentCollectionDs.removeItem(createdItem);
                    }
                }
            }
        } else {
            Entity item = null;
            if (!itemsToCreate.isEmpty()) {
                item = itemsToCreate.iterator().next();
            } else if (!itemsToUpdate.isEmpty()) {
                item = itemsToUpdate.iterator().next();
            } else if (!itemsToDelete.isEmpty()) {
                item = itemsToDelete.iterator().next();
            }
            if (item != null) {
                parentDs.setItem(item);
            }
        }
        clearCommitLists();
        modified = false;
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) 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