Search in sources :

Example 16 with Instance

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

the class WebEntityLinkField method initComponent.

protected void initComponent() {
    component.addClickListener(event -> {
        if (clickHandler != null) {
            clickHandler.onClick(WebEntityLinkField.this);
        } else {
            openEntityEditor();
        }
    });
    component.setCaptionFormatter((value, locale) -> {
        if (value == null) {
            return "";
        }
        if (value instanceof Instance) {
            return metadataTools.getInstanceName((Instance) value);
        }
        Datatype datatype = Datatypes.getNN(value.getClass());
        if (locale != null) {
            return datatype.format(value, locale);
        }
        return datatype.format(value);
    });
}
Also used : Instance(com.haulmont.chile.core.model.Instance) Datatype(com.haulmont.chile.core.datatypes.Datatype)

Example 17 with Instance

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

the class AbstractCollectionDatasource method getQueryParameters.

protected Map<String, Object> getQueryParameters(Map<String, Object> params) {
    final Map<String, Object> map = new HashMap<>();
    for (ParameterInfo info : queryParameters) {
        String name = info.getFlatName();
        final String path = info.getPath();
        final String[] elements = path.split("\\.");
        switch(info.getType()) {
            case DATASOURCE:
                {
                    String dsName = elements[0];
                    final Datasource datasource = dsContext.get(dsName);
                    if (datasource == null) {
                        throw new DevelopmentException("Datasource '" + dsName + "' not found in dsContext", "datasource", dsName);
                    }
                    if (datasource.getState() == State.VALID) {
                        final Entity item = datasource.getItem();
                        if (elements.length > 1) {
                            String[] valuePath = ArrayUtils.subarray(elements, 1, elements.length);
                            String propertyName = InstanceUtils.formatValuePath(valuePath);
                            Object value = InstanceUtils.getValueEx(item, propertyName);
                            map.put(name, value);
                        } else {
                            map.put(name, item);
                        }
                    } else {
                        map.put(name, null);
                    }
                    break;
                }
            case PARAM:
                {
                    Object value;
                    if (dsContext.getFrameContext() == null) {
                        value = null;
                    } else {
                        Map<String, Object> windowParams = dsContext.getFrameContext().getParams();
                        value = windowParams.get(path);
                        if (value == null && elements.length > 1) {
                            Instance instance = (Instance) windowParams.get(elements[0]);
                            if (instance != null) {
                                String[] valuePath = ArrayUtils.subarray(elements, 1, elements.length);
                                String propertyName = InstanceUtils.formatValuePath(valuePath);
                                value = InstanceUtils.getValueEx(instance, propertyName);
                            }
                        }
                    }
                    if (value instanceof String && info.isCaseInsensitive()) {
                        value = makeCaseInsensitive((String) value);
                    }
                    map.put(name, value);
                    break;
                }
            case COMPONENT:
                {
                    Object value = null;
                    if (dsContext.getFrameContext() != null) {
                        value = dsContext.getFrameContext().getValue(path);
                        if (value instanceof String && info.isCaseInsensitive()) {
                            value = makeCaseInsensitive((String) value);
                        }
                        if (java.sql.Date.class.equals(info.getJavaClass()) && value instanceof Date) {
                            value = new java.sql.Date(((Date) value).getTime());
                        }
                        if (refreshOnComponentValueChange) {
                            if (componentValueListener == null)
                                componentValueListener = new ComponentValueListener();
                            try {
                                dsContext.getFrameContext().addValueChangeListener(path, componentValueListener);
                            } catch (Exception e) {
                                Logger log = LoggerFactory.getLogger(AbstractCollectionDatasource.class);
                                log.error("Unable to add value listener: " + e);
                            }
                        }
                    }
                    map.put(name, value);
                    break;
                }
            case SESSION:
                {
                    Object value;
                    value = userSession.getAttribute(path);
                    if (value instanceof String && info.isCaseInsensitive()) {
                        value = makeCaseInsensitive((String) value);
                    }
                    map.put(name, value);
                    break;
                }
            case CUSTOM:
                {
                    Object value = params.get(info.getPath());
                    if (value == null) {
                        // a case when a query contains a parameter like :custom$city.country.id and we passed
                        // just "city" parameter to the datasource refresh() method
                        String[] pathElements = info.getPath().split("\\.");
                        if (pathElements.length > 1) {
                            Object entity = params.get(pathElements[0]);
                            if (entity instanceof Instance) {
                                value = InstanceUtils.getValueEx((Instance) entity, Arrays.copyOfRange(pathElements, 1, pathElements.length));
                            }
                        }
                    }
                    if (value instanceof String && info.isCaseInsensitive()) {
                        value = makeCaseInsensitive((String) value);
                    }
                    map.put(name, value);
                    break;
                }
            default:
                {
                    throw new UnsupportedOperationException("Unsupported parameter type: " + info.getType());
                }
        }
    }
    return map;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Entity(com.haulmont.cuba.core.entity.Entity) Instance(com.haulmont.chile.core.model.Instance) ParameterInfo(com.haulmont.cuba.core.global.filter.ParameterInfo) Logger(org.slf4j.Logger)

Example 18 with Instance

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

the class PropertyDatasourceImpl method setItem.

@Override
public void setItem(T item) {
    if (getItem() != null) {
        metadata.getTools().copy(item, getItem());
        itemsToUpdate.add(item);
    } else {
        final Instance parentItem = masterDs.getItem();
        parentItem.setValue(metaProperty.getName(), item);
    }
    setModified(true);
}
Also used : Instance(com.haulmont.chile.core.model.Instance) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance)

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