Search in sources :

Example 11 with Instance

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

the class WebLookupField method initNullEntity.

protected void initNullEntity() {
    // noinspection IncorrectCreateEntity
    nullEntity = new BaseUuidEntity() {

        @Override
        public String getInstanceName() {
            if (nullOption instanceof Instance) {
                return InstanceUtils.getInstanceName((Instance) nullOption);
            }
            if (nullOption == null) {
                return "";
            } else {
                return nullOption.toString();
            }
        }

        // Used for captionProperty of null entity
        @Override
        public <T> T getValue(String s) {
            return (T) getInstanceName();
        }
    };
    component.setNullSelectionItemId(nullEntity);
}
Also used : Instance(com.haulmont.chile.core.model.Instance) BaseUuidEntity(com.haulmont.cuba.core.entity.BaseUuidEntity)

Example 12 with Instance

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

the class EntityFetcher method fetch.

@SuppressWarnings("unchecked")
protected void fetch(Entity entity, View view, Map<Instance, Set<View>> visited, boolean optimizeForDetached) {
    Set<View> views = visited.get(entity);
    if (views == null) {
        views = new HashSet<>();
        visited.put(entity, views);
    } else if (views.contains(view)) {
        return;
    }
    views.add(view);
    if (log.isTraceEnabled())
        log.trace("Fetching instance " + entity);
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    for (ViewProperty property : view.getProperties()) {
        MetaProperty metaProperty = metaClass.getPropertyNN(property.getName());
        if (!metaProperty.getRange().isClass() && !metadata.getTools().isLazyFetchedLocalAttribute(metaProperty) || metadata.getTools().isNotPersistent(metaClass, metaProperty))
            continue;
        if (log.isTraceEnabled())
            log.trace("Fetching property " + property.getName());
        Object value = entity.getValue(property.getName());
        View propertyView = property.getView();
        if (value != null && propertyView != null) {
            if (value instanceof Collection) {
                for (Object item : new ArrayList(((Collection) value))) {
                    if (item instanceof Entity) {
                        Entity e = (Entity) item;
                        if (entityStates.isDetached(e)) {
                            fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
                                if (value instanceof List) {
                                    List list = (List) value;
                                    list.set(list.indexOf(e), managed);
                                } else {
                                    Collection collection = (Collection) value;
                                    collection.remove(e);
                                    collection.add(managed);
                                }
                            });
                        } else {
                            fetch((Entity) item, propertyView, visited, optimizeForDetached);
                        }
                    }
                }
            } else if (value instanceof Entity) {
                Entity e = (Entity) value;
                if (!metaProperty.isReadOnly() && entityStates.isDetached(e) && !(e instanceof EmbeddableEntity)) {
                    fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
                        entity.setValue(property.getName(), managed);
                    });
                } else {
                    fetch(e, propertyView, visited, optimizeForDetached);
                }
            }
        }
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) EntityManager(com.haulmont.cuba.core.EntityManager) Persistence(com.haulmont.cuba.core.Persistence) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Instance(com.haulmont.chile.core.model.Instance) Transaction(com.haulmont.cuba.core.Transaction) Entity(com.haulmont.cuba.core.entity.Entity) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) Entity(com.haulmont.cuba.core.entity.Entity) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 13 with Instance

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

the class AppliedFilter method formatParamValue.

protected String formatParamValue(Param param) {
    Object value = param.getValue();
    if (value == null)
        return "";
    if (param.isDateInterval()) {
        DateIntervalValue dateIntervalValue = AppBeans.getPrototype(DateIntervalValue.NAME, (String) value);
        return dateIntervalValue.getLocalizedValue();
    }
    if (value instanceof Instance)
        return ((Instance) value).getInstanceName();
    if (value instanceof Enum)
        return messages.getMessage((Enum) value);
    if (value instanceof ArrayList) {
        ArrayList<String> names = new ArrayList<>();
        ArrayList list = ((ArrayList) value);
        for (Object obj : list) {
            if (obj instanceof Instance)
                names.add(((Instance) obj).getInstanceName());
            else {
                names.add(FilterConditionUtils.formatParamValue(param, obj));
            }
        }
        return names.toString();
    }
    return FilterConditionUtils.formatParamValue(param, value);
}
Also used : Instance(com.haulmont.chile.core.model.Instance) ArrayList(java.util.ArrayList) DateIntervalValue(com.haulmont.cuba.gui.components.filter.dateinterval.DateIntervalValue)

Example 14 with Instance

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

the class DataContextImpl method mergeState.

protected void mergeState(Entity srcEntity, Entity dstEntity, Set<Entity> mergedSet, boolean isRoot, MergeOptions options) {
    EntityStates entityStates = getEntityStates();
    boolean srcNew = entityStates.isNew(srcEntity);
    boolean dstNew = entityStates.isNew(dstEntity);
    mergeSystemState(srcEntity, dstEntity, isRoot, options);
    MetaClass metaClass = getMetadata().getClassNN(srcEntity.getClass());
    MetaProperty primaryKeyProperty = getMetadataTools().getPrimaryKeyProperty(metaClass);
    for (MetaProperty property : metaClass.getProperties()) {
        String propertyName = property.getName();
        if (// local
        !property.getRange().isClass() && // not PK
        property != primaryKeyProperty && // loaded src
        (srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
            // loaded dst
            Object value = srcEntity.getValue(propertyName);
            // ignore null values in non-root source entities
            if (!isRoot && !options.isFresh() && value == null) {
                continue;
            }
            setPropertyValue(dstEntity, property, value);
        }
    }
    for (MetaProperty property : metaClass.getProperties()) {
        String propertyName = property.getName();
        if (// refs and collections
        property.getRange().isClass() && // not PK
        property != primaryKeyProperty && // loaded src
        (srcNew || entityStates.isLoaded(srcEntity, propertyName)) && (dstNew || entityStates.isLoaded(dstEntity, propertyName))) {
            // loaded dst
            Object value = srcEntity.getValue(propertyName);
            // ignore null values in non-root source entities
            if (!isRoot && !options.isFresh() && value == null) {
                continue;
            }
            if (value == null) {
                setPropertyValue(dstEntity, property, null);
                continue;
            }
            if (value instanceof Collection) {
                if (value instanceof List) {
                    mergeList((List) value, dstEntity, property, isRoot, options, mergedSet);
                } else if (value instanceof Set) {
                    mergeSet((Set) value, dstEntity, property, isRoot, options, mergedSet);
                } else {
                    throw new UnsupportedOperationException("Unsupported collection type: " + value.getClass().getName());
                }
            } else {
                Entity srcRef = (Entity) value;
                if (!mergedSet.contains(srcRef)) {
                    Entity managedRef = internalMerge(srcRef, mergedSet, false, options);
                    setPropertyValue(dstEntity, property, managedRef, false);
                    if (getMetadataTools().isEmbedded(property)) {
                        EmbeddedPropertyChangeListener listener = new EmbeddedPropertyChangeListener(dstEntity);
                        managedRef.addPropertyChangeListener(listener);
                        embeddedPropertyListeners.computeIfAbsent(dstEntity, e -> new HashMap<>()).put(propertyName, listener);
                    }
                } else {
                    Entity managedRef = find(srcRef.getClass(), srcRef.getId());
                    if (managedRef != null) {
                        setPropertyValue(dstEntity, property, managedRef, false);
                    } else {
                        // should never happen
                        log.debug("Instance was merged but managed instance is null: {}", srcRef);
                    }
                }
            }
        }
    }
}
Also used : java.util(java.util) DataContext(com.haulmont.cuba.gui.model.DataContext) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Subscription(com.haulmont.bali.events.Subscription) Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance) com.haulmont.cuba.core.entity(com.haulmont.cuba.core.entity) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) Nullable(javax.annotation.Nullable) FetchGroupUtils(com.haulmont.cuba.core.sys.persistence.FetchGroupUtils) Logger(org.slf4j.Logger) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) InvocationTargetException(java.lang.reflect.InvocationTargetException) MergeOptions(com.haulmont.cuba.gui.model.MergeOptions) Consumer(java.util.function.Consumer) FetchGroup(org.eclipse.persistence.queries.FetchGroup) FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) EventHub(com.haulmont.bali.events.EventHub) EntityReferencesNormalizer(com.haulmont.cuba.core.sys.EntityReferencesNormalizer) AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 15 with Instance

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

the class ListEditorHelper method getValueCaption.

@Nullable
public static String getValueCaption(@Nullable Object v, ListEditor.ItemType itemType, @Nullable TimeZone timeZone) {
    if (v == null)
        return null;
    switch(itemType) {
        case ENTITY:
            if (v instanceof Instance)
                return ((Instance) v).getInstanceName();
            else
                return v.toString();
        case STRING:
            return (String) v;
        case DATE:
            return Datatypes.getNN(java.sql.Date.class).format(v);
        case DATETIME:
            UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
            UserSession userSession = userSessionSource.getUserSession();
            if (timeZone != null) {
                return ((TimeZoneAwareDatatype) Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
            } else {
                return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
            }
        case INTEGER:
            return Datatypes.getNN(Integer.class).format(v);
        case LONG:
            return Datatypes.getNN(Long.class).format(v);
        case BIGDECIMAL:
            return Datatypes.getNN(BigDecimal.class).format(v);
        case DOUBLE:
            return Datatypes.getNN(Double.class).format(v);
        case ENUM:
            return AppBeans.get(Messages.class).getMessage((Enum) v);
        case UUID:
            return Datatypes.getNN(java.util.UUID.class).format(v);
        default:
            throw new IllegalStateException("Unknown item type");
    }
}
Also used : UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) TimeZoneAwareDatatype(com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype) Messages(com.haulmont.cuba.core.global.Messages) Instance(com.haulmont.chile.core.model.Instance) Date(java.util.Date) BigDecimal(java.math.BigDecimal) UserSession(com.haulmont.cuba.security.global.UserSession) Nullable(javax.annotation.Nullable)

Aggregations

Instance (com.haulmont.chile.core.model.Instance)18 MetaClass (com.haulmont.chile.core.model.MetaClass)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)4 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)4 Entity (com.haulmont.cuba.core.entity.Entity)4 Table (com.haulmont.cuba.gui.components.Table)3 Nullable (javax.annotation.Nullable)3 Logger (org.slf4j.Logger)3 Datatype (com.haulmont.chile.core.datatypes.Datatype)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 Messages (com.haulmont.cuba.core.global.Messages)2 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 Consumer (java.util.function.Consumer)2 LoggerFactory (org.slf4j.LoggerFactory)2 Sets (com.google.common.collect.Sets)1 EventHub (com.haulmont.bali.events.EventHub)1 Subscription (com.haulmont.bali.events.Subscription)1