Search in sources :

Example 6 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class CustomConditionFrame method requestHint.

protected List<Suggestion> requestHint(SourceCodeEditor sender, String text, int senderCursorPosition) {
    String joinStr = joinField.getValue();
    String whereStr = whereField.getValue();
    CollectionDatasource ds = (CollectionDatasource) condition.getDatasource();
    // CAUTION: the magic entity name!  The length is three character to match "{E}" length in query
    String entityAlias = "a39";
    int queryPosition = -1;
    String queryStart = "select " + entityAlias + " from " + ds.getMetaClass().getName() + " " + entityAlias + " ";
    StringBuilder queryBuilder = new StringBuilder(queryStart);
    if (StringUtils.isNotEmpty(joinStr)) {
        if (sender == joinField) {
            queryPosition = queryBuilder.length() + senderCursorPosition - 1;
        }
        if (!StringUtils.containsIgnoreCase(joinStr, "join") && !StringUtils.contains(joinStr, ",")) {
            queryBuilder.append("join ").append(joinStr);
            queryPosition += "join ".length();
        } else {
            queryBuilder.append(joinStr);
        }
    }
    if (StringUtils.isNotEmpty(whereStr)) {
        if (sender == whereField) {
            queryPosition = queryBuilder.length() + WHERE.length() + senderCursorPosition - 1;
        }
        queryBuilder.append(WHERE).append(whereStr);
    }
    String query = queryBuilder.toString();
    query = query.replace("{E}", entityAlias);
    return JpqlSuggestionFactory.requestHint(query, queryPosition, sender.getAutoCompleteSupport(), senderCursorPosition);
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource)

Example 7 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class AbstractOptionsBaseLoader method loadDatasource.

@Override
protected void loadDatasource(DatasourceComponent component, Element element) {
    String multiselect = element.attributeValue("multiselect");
    if (StringUtils.isNotEmpty(multiselect)) {
        ((OptionsField) component).setMultiSelect(Boolean.parseBoolean(multiselect));
    }
    String datasource = element.attributeValue("optionsDatasource");
    if (!StringUtils.isEmpty(datasource)) {
        Datasource ds = context.getDsContext().get(datasource);
        ((T) component).setOptionsDatasource((CollectionDatasource) ds);
    }
    super.loadDatasource(component, element);
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) OptionsField(com.haulmont.cuba.gui.components.OptionsField)

Example 8 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class CalendarLoader method loadDatasource.

protected void loadDatasource(Calendar component, Element element) {
    final String datasource = element.attributeValue("datasource");
    if (!StringUtils.isEmpty(datasource)) {
        CollectionDatasource ds = (CollectionDatasource) context.getDsContext().get(datasource);
        if (ds == null) {
            throw new GuiDevelopmentException(String.format("Datasource '%s' is not defined", datasource), getContext().getFullFrameId(), "Component ID", component.getId());
        }
        component.setDatasource(ds);
        if (component.getEventProvider() instanceof EntityCalendarEventProvider) {
            EntityCalendarEventProvider eventProvider = (EntityCalendarEventProvider) component.getEventProvider();
            String startDateProperty = element.attributeValue("startDateProperty");
            if (StringUtils.isNotEmpty(startDateProperty)) {
                eventProvider.setStartDateProperty(startDateProperty);
            }
            String endDateProperty = element.attributeValue("endDateProperty");
            if (StringUtils.isNotEmpty(endDateProperty)) {
                eventProvider.setEndDateProperty(endDateProperty);
            }
            String captionProperty = element.attributeValue("captionProperty");
            if (StringUtils.isNotEmpty(captionProperty)) {
                eventProvider.setCaptionProperty(captionProperty);
            }
            String descriptionProperty = element.attributeValue("descriptionProperty");
            if (StringUtils.isNotEmpty(descriptionProperty)) {
                eventProvider.setDescriptionProperty(descriptionProperty);
            }
            String styleNameProperty = element.attributeValue("stylenameProperty");
            if (StringUtils.isNotEmpty(styleNameProperty)) {
                eventProvider.setStyleNameProperty(styleNameProperty);
            }
            String allDayProperty = element.attributeValue("isAllDayProperty");
            if (StringUtils.isNotEmpty(allDayProperty)) {
                eventProvider.setAllDayProperty(allDayProperty);
            }
        }
    }
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) EntityCalendarEventProvider(com.haulmont.cuba.gui.components.calendar.EntityCalendarEventProvider)

Example 9 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class FieldGroupLoader method loadField.

protected FieldGroup.FieldConfig loadField(Element element, Datasource ds, String columnWidth) {
    String id = element.attributeValue("id");
    String property = element.attributeValue("property");
    if (Strings.isNullOrEmpty(id) && Strings.isNullOrEmpty(property)) {
        throw new GuiDevelopmentException(String.format("id/property is not defined for field of FieldGroup '%s'. " + "Set id or property attribute.", resultComponent.getId()), context.getFullFrameId());
    }
    if (Strings.isNullOrEmpty(property)) {
        property = id;
    } else if (Strings.isNullOrEmpty(id)) {
        id = property;
    }
    Datasource targetDs = ds;
    Datasource datasource = loadDatasource(element);
    if (datasource != null) {
        targetDs = datasource;
    }
    CollectionDatasource optionsDs = null;
    String optDsName = element.attributeValue("optionsDatasource");
    if (StringUtils.isNotBlank(optDsName)) {
        DsContext dsContext = getContext().getFrame().getDsContext();
        optionsDs = findDatasourceRecursively(dsContext, optDsName);
        if (optionsDs == null) {
            throw new GuiDevelopmentException(String.format("Options datasource %s not found for field %s", optDsName, id), context.getFullFrameId());
        }
    }
    boolean customField = false;
    String custom = element.attributeValue("custom");
    if (StringUtils.isNotEmpty(custom)) {
        customField = Boolean.parseBoolean(custom);
    }
    if (StringUtils.isNotEmpty(element.attributeValue("generator"))) {
        customField = true;
    }
    List<Element> elements = Dom4j.elements(element);
    List<Element> customElements = elements.stream().filter(e -> !("formatter".equals(e.getName()) || "validator".equals(e.getName()))).collect(Collectors.toList());
    if (!customElements.isEmpty()) {
        if (customElements.size() > 1) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s element cannot contains two or more custom field definitions", id), context.getCurrentFrameId());
        }
        if (customField) {
            throw new GuiDevelopmentException(String.format("FieldGroup field %s cannot use both custom/generator attribute and inline component definition", id), context.getCurrentFrameId());
        }
        customField = true;
    }
    if (!customField && targetDs == null) {
        throw new GuiDevelopmentException(String.format("Datasource is not defined for FieldGroup field '%s'. " + "Only custom fields can have no datasource.", property), context.getFullFrameId());
    }
    FieldGroup.FieldConfig field = resultComponent.createField(id);
    if (property != null) {
        field.setProperty(property);
    }
    if (datasource != null) {
        field.setDatasource(datasource);
    }
    if (optionsDs != null) {
        field.setOptionsDatasource(optionsDs);
    }
    String stylename = element.attributeValue("stylename");
    if (StringUtils.isNotEmpty(stylename)) {
        field.setStyleName(stylename);
    }
    MetaPropertyPath metaPropertyPath = null;
    if (targetDs != null && property != null) {
        MetaClass metaClass = targetDs.getMetaClass();
        metaPropertyPath = metadataTools.resolveMetaPropertyPath(targetDs.getMetaClass(), property);
        if (metaPropertyPath == null) {
            if (!customField) {
                throw new GuiDevelopmentException(String.format("Property '%s' is not found in entity '%s'", property, metaClass.getName()), context.getFullFrameId());
            }
        }
    }
    String propertyName = metaPropertyPath != null ? metaPropertyPath.getMetaProperty().getName() : null;
    if (metaPropertyPath != null && DynamicAttributesUtils.isDynamicAttribute(metaPropertyPath.getMetaProperty())) {
        CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
        field.setCaption(categoryAttribute != null ? categoryAttribute.getLocaleName() : propertyName);
    } else {
        loadCaption(field, element);
        if (field.getCaption() == null) {
            field.setCaption(getDefaultCaption(field, targetDs));
        }
    }
    loadDescription(field, element);
    loadContextHelp(field, element);
    field.setXmlDescriptor(element);
    com.haulmont.cuba.gui.components.Formatter formatter = loadFormatter(element);
    if (formatter != null) {
        field.setFormatter(formatter);
    }
    String defaultWidth = element.attributeValue("width");
    if (StringUtils.isEmpty(defaultWidth)) {
        defaultWidth = columnWidth;
    }
    loadWidth(field, defaultWidth);
    if (customField) {
        field.setCustom(true);
    }
    String required = element.attributeValue("required");
    if (StringUtils.isNotEmpty(required)) {
        field.setRequired(Boolean.parseBoolean(required));
    }
    String requiredMsg = element.attributeValue("requiredMessage");
    if (requiredMsg != null) {
        requiredMsg = loadResourceString(requiredMsg);
        field.setRequiredMessage(requiredMsg);
    }
    String tabIndex = element.attributeValue("tabIndex");
    if (StringUtils.isNotEmpty(tabIndex)) {
        field.setTabIndex(Integer.parseInt(tabIndex));
    }
    loadInputPrompt(field, element);
    if (customElements.size() == 1) {
        // load nested component defined as inline
        Element customFieldElement = customElements.get(0);
        LayoutLoader loader = new LayoutLoader(context, factory, layoutLoaderConfig);
        loader.setLocale(getLocale());
        loader.setMessagesPack(getMessagesPack());
        ComponentLoader childComponentLoader = loader.createComponent(customFieldElement);
        childComponentLoader.loadComponent();
        Component customComponent = childComponentLoader.getResultComponent();
        String inlineAttachMode = element.attributeValue("inlineAttachMode");
        if (StringUtils.isNotEmpty(inlineAttachMode)) {
            field.setComponent(customComponent, FieldGroup.FieldAttachMode.valueOf(inlineAttachMode));
        } else {
            field.setComponent(customComponent);
        }
    }
    return field;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Iterables(com.google.common.collect.Iterables) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Datasource(com.haulmont.cuba.gui.data.Datasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) Dom4j(com.haulmont.bali.util.Dom4j) DynamicAttributeCustomFieldGenerator(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributeCustomFieldGenerator) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) DynamicAttributesUtils(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils) AppBeans(com.haulmont.cuba.core.global.AppBeans) MetaClass(com.haulmont.chile.core.model.MetaClass) BooleanUtils(org.apache.commons.lang.BooleanUtils) Strings(com.google.common.base.Strings) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) Nullable(javax.annotation.Nullable) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DeclarativeFieldGenerator(com.haulmont.cuba.gui.xml.DeclarativeFieldGenerator) CustomFieldGenerator(com.haulmont.cuba.gui.components.FieldGroup.CustomFieldGenerator) Collectors(java.util.stream.Collectors) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) EntityOp(com.haulmont.cuba.security.entity.EntityOp) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) FieldCaptionAlignment(com.haulmont.cuba.gui.components.FieldGroup.FieldCaptionAlignment) Element(org.dom4j.Element) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MessageTools(com.haulmont.cuba.core.global.MessageTools) LayoutLoader(com.haulmont.cuba.gui.xml.layout.LayoutLoader) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) DsContext(com.haulmont.cuba.gui.data.DsContext) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) ComponentLoader(com.haulmont.cuba.gui.xml.layout.ComponentLoader) CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) MetaClass(com.haulmont.chile.core.model.MetaClass) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Example 10 with CollectionDatasource

use of com.haulmont.cuba.gui.data.CollectionDatasource in project cuba by cuba-platform.

the class TokenListLoader method loadLookup.

protected void loadLookup(TokenList component, Element element) {
    Element lookupElement = element.element("lookup");
    if (lookupElement == null) {
        throw new GuiDevelopmentException("'tokenList' must contain 'lookup' element", context.getFullFrameId(), "TokenList ID", element.attributeValue("id"));
    }
    String optionsDatasource = lookupElement.attributeValue("optionsDatasource");
    if (!StringUtils.isEmpty(optionsDatasource)) {
        CollectionDatasource ds = (CollectionDatasource) context.getDsContext().get(optionsDatasource);
        component.setOptionsDatasource(ds);
    }
    String optionsCaptionProperty = lookupElement.attributeValue("captionProperty");
    if (!StringUtils.isEmpty(optionsCaptionProperty)) {
        component.setOptionsCaptionMode(CaptionMode.PROPERTY);
        component.setOptionsCaptionProperty(optionsCaptionProperty);
    }
    String lookup = lookupElement.attributeValue("lookup");
    if (StringUtils.isNotEmpty(lookup)) {
        component.setLookup(Boolean.parseBoolean(lookup));
        if (component.isLookup()) {
            String lookupScreen = lookupElement.attributeValue("lookupScreen");
            if (StringUtils.isNotEmpty(lookupScreen)) {
                component.setLookupScreen(lookupScreen);
            }
            String openType = lookupElement.attributeValue("openType");
            if (StringUtils.isNotEmpty(openType)) {
                component.setLookupOpenMode(OpenType.valueOf(openType));
            }
        }
    }
    String multiSelect = lookupElement.attributeValue("multiselect");
    if (StringUtils.isNotEmpty(multiSelect)) {
        component.setMultiSelect(Boolean.parseBoolean(multiSelect));
    }
    String inputPrompt = lookupElement.attributeValue("inputPrompt");
    if (StringUtils.isNotEmpty(inputPrompt)) {
        component.setLookupInputPrompt(loadResourceString(inputPrompt));
    }
    loadFilterMode(component, lookupElement);
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) GuiDevelopmentException(com.haulmont.cuba.gui.GuiDevelopmentException)

Aggregations

CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)67 Datasource (com.haulmont.cuba.gui.data.Datasource)39 UUID (java.util.UUID)24 Group (com.haulmont.cuba.security.entity.Group)23 User (com.haulmont.cuba.security.entity.User)23 ArrayList (java.util.ArrayList)23 Ignore (org.junit.Ignore)23 Test (org.junit.Test)23 Component (com.haulmont.cuba.gui.components.Component)20 List (java.util.List)19 Assert.assertEquals (org.junit.Assert.assertEquals)19 Assert.assertTrue (org.junit.Assert.assertTrue)19 Entity (com.haulmont.cuba.core.entity.Entity)15 MetaClass (com.haulmont.chile.core.model.MetaClass)10 MetaProperty (com.haulmont.chile.core.model.MetaProperty)10 LookupField (com.haulmont.cuba.gui.components.LookupField)8 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)7 LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)7 Element (org.dom4j.Element)7 Assert.assertNotNull (org.junit.Assert.assertNotNull)7