Search in sources :

Example 21 with TypeInstance

use of org.obeonetwork.dsl.typeslibrary.TypeInstance in project InformationSystem by ObeoNetwork.

the class EntityToMLD method handleEnumType.

private void handleEnumType(Column column, Enumeration enumeration, Table table) {
    // For an enumeration
    // Column has type Texte
    TypeInstance typeInstance = TypesLibraryFactory.eINSTANCE.createTypeInstance();
    typeInstance.setNativeType(nativeTypesMap.get(ENUM_COLUMN_TYPE));
    column.setType(typeInstance);
    // Column size is the maximum length of the literals
    int maxSize = 0;
    List<String> literalValues = new ArrayList<String>();
    for (Literal literal : enumeration.getLiterals()) {
        String value = literal.getName();
        if (value != null) {
            literalValues.add("''" + value + "''");
            int length = value.length();
            if (maxSize < length) {
                maxSize = length;
            }
        }
    }
    if (maxSize > 0) {
        typeInstance.setLength(maxSize);
    }
    // A check constraint is added with all literals
    String expression = MessageFormat.format("{0} in ({1})", column.getName(), Joiner.on(",").join(literalValues));
    Constraint validityConstraint = findOrCreateConstraint(table, expression);
    validityConstraint.setName("ENUM_CONSTRAINT");
    validityConstraint.setComments(MessageFormat.format(ENUM_CONSTRAINTS_COMMENTS, column.getName()));
    addToObjectsToBeKept(validityConstraint);
    // A default value is added, it is the first literal
    if (!enumeration.getLiterals().isEmpty()) {
        column.setDefaultValue(enumeration.getLiterals().get(0).getName());
    }
}
Also used : Constraint(org.obeonetwork.dsl.database.Constraint) Literal(org.obeonetwork.dsl.environment.Literal) ArrayList(java.util.ArrayList) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance) Constraint(org.obeonetwork.dsl.database.Constraint)

Example 22 with TypeInstance

use of org.obeonetwork.dsl.typeslibrary.TypeInstance in project InformationSystem by ObeoNetwork.

the class CustomColumnPropertiesEditionComponent method updateTypeFieldsVisibility.

private void updateTypeFieldsVisibility(Column column, ColumnPropertiesEditionPart basePart) {
    if (column.getType() instanceof TypeInstance) {
        TypeInstance typeInstance = (TypeInstance) column.getType();
        if (typeInstance.getNativeType() != null) {
            NativeTypeKind nativeTypeKind = typeInstance.getNativeType().getSpec();
            boolean lengthVisible = (nativeTypeKind == NativeTypeKind.LENGTH || nativeTypeKind == NativeTypeKind.LENGTH_AND_PRECISION);
            boolean precisionVisible = (nativeTypeKind == NativeTypeKind.LENGTH_AND_PRECISION);
            boolean literalsVisible = (nativeTypeKind == NativeTypeKind.ENUM);
            basePart.updateTypeFields(lengthVisible, precisionVisible, literalsVisible);
        }
    }
}
Also used : NativeTypeKind(org.obeonetwork.dsl.typeslibrary.NativeTypeKind) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance)

Example 23 with TypeInstance

use of org.obeonetwork.dsl.typeslibrary.TypeInstance in project InformationSystem by ObeoNetwork.

the class DatabaseEditLabelServices method setType.

private void setType(Column column, String typeName, Collection<NativeTypesLibrary> typesLibraries) {
    for (NativeTypesLibrary nativeTypesLibrary : typesLibraries) {
        for (NativeType nativeType : nativeTypesLibrary.getNativeTypes()) {
            String typePattern = getNativeTypePattern(nativeType);
            String typeRegexPattern = null;
            if (nativeType.getSpec() == NativeTypeKind.ENUM) {
                typeRegexPattern = typePattern;
            } else {
                typeRegexPattern = typePattern.replace("%n", "(.*)");
                typeRegexPattern = typeRegexPattern.replace("%p", "(.*)");
            }
            if (typeRegexPattern != null) {
                Pattern ptn = null;
                try {
                    ptn = Pattern.compile(typeRegexPattern, Pattern.CASE_INSENSITIVE);
                } catch (Exception e) {
                // Do nothing, the loop will try the other types
                }
                if (ptn != null) {
                    Matcher matcher = ptn.matcher(typeName);
                    if (matcher.matches()) {
                        // Ensure there is a type instance
                        if (column.getType() == null || (column.getType() instanceof TypeInstance == false)) {
                            TypeInstance type = TypesLibraryFactory.eINSTANCE.createTypeInstance();
                            type.setNativeType(nativeType);
                            column.setType(type);
                        }
                        TypeInstance columnType = ((TypeInstance) column.getType());
                        columnType.setNativeType(nativeType);
                        switch(nativeType.getSpec()) {
                            case SIMPLE:
                                // Nothing special to do
                                return;
                            case LENGTH:
                                // Retrieve length
                                String lengthString = matcher.group(1).trim();
                                // Ensure its a string representing an int
                                int length = getIntValue(lengthString);
                                columnType.setLength(length);
                                return;
                            case LENGTH_AND_PRECISION:
                                int nPos = typePattern.indexOf("%n");
                                int pPos = typePattern.indexOf("%p");
                                String nString = null;
                                String pString = null;
                                if (nPos < pPos) {
                                    nString = matcher.group(1).trim();
                                    nString = matcher.group(2).trim();
                                } else {
                                    nString = matcher.group(2).trim();
                                    pString = matcher.group(1).trim();
                                }
                                int nValue = getIntValue(nString);
                                int pValue = getIntValue(pString);
                                columnType.setLength(nValue);
                                columnType.setPrecision(pValue);
                                break;
                            case ENUM:
                                // Retrieves values
                                columnType.getLiterals().clear();
                                String valuesString = matcher.group(1).trim();
                                for (String value : valuesString.split(",")) {
                                    columnType.getLiterals().add(value.trim());
                                }
                                return;
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NativeTypesLibrary(org.obeonetwork.dsl.typeslibrary.NativeTypesLibrary) NativeType(org.obeonetwork.dsl.typeslibrary.NativeType) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance)

Example 24 with TypeInstance

use of org.obeonetwork.dsl.typeslibrary.TypeInstance in project InformationSystem by ObeoNetwork.

the class SimpleNamedTypePropertiesEditionComponent method initPart.

/**
 * {@inheritDoc}
 *
 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#initPart(java.lang.Object, int, org.eclipse.emf.ecore.EObject,
 *      org.eclipse.emf.ecore.resource.ResourceSet)
 */
public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
        editingPart.setContext(elt, allResource);
        final SimpleNamedType simpleNamedType = (SimpleNamedType) elt;
        final SimpleNamedTypePropertiesEditionPart simpleNamedTypePart = (SimpleNamedTypePropertiesEditionPart) editingPart;
        // init values
        if (isAccessible(TypeslibraryViewsRepository.SimpleNamedType.Properties.name))
            simpleNamedTypePart.setName(EEFConverterUtil.convertToString(EcorePackage.Literals.ESTRING, simpleNamedType.getName()));
        if (isAccessible(TypeslibraryViewsRepository.SimpleNamedType.Properties.type)) {
            // init part
            typeSettings = new EObjectFlatComboSettings(simpleNamedType, TypesLibraryPackage.eINSTANCE.getSimpleNamedType_Type());
            simpleNamedTypePart.initType(typeSettings);
            // set the button mode
            simpleNamedTypePart.setTypeButtonMode(ButtonsModeEnum.BROWSE);
        }
        if (isAccessible(TypeslibraryViewsRepository.SimpleNamedType.Properties.type)) {
            simpleNamedTypePart.addFilterToType(new ViewerFilter() {

                /**
                 * {@inheritDoc}
                 *
                 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
                 */
                public boolean select(Viewer viewer, Object parentElement, Object element) {
                    return (element instanceof TypeInstance);
                }
            });
        // Start of user code for additional businessfilters for type
        // End of user code
        }
    // init values for referenced views
    // init filters for referenced views
    }
    setInitializing(false);
}
Also used : ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) SimpleNamedType(org.obeonetwork.dsl.typeslibrary.SimpleNamedType) Viewer(org.eclipse.jface.viewers.Viewer) EObject(org.eclipse.emf.ecore.EObject) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance) SimpleNamedTypePropertiesEditionPart(org.obeonetwork.dsl.typeslibrary.parts.SimpleNamedTypePropertiesEditionPart) EObjectFlatComboSettings(org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings)

Example 25 with TypeInstance

use of org.obeonetwork.dsl.typeslibrary.TypeInstance in project InformationSystem by ObeoNetwork.

the class TypeInstancePropertiesEditionComponent method initPart.

/**
 * {@inheritDoc}
 *
 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#initPart(java.lang.Object, int, org.eclipse.emf.ecore.EObject,
 *      org.eclipse.emf.ecore.resource.ResourceSet)
 */
public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
        editingPart.setContext(elt, allResource);
        final TypeInstance typeInstance = (TypeInstance) elt;
        final TypeInstancePropertiesEditionPart typeInstancePart = (TypeInstancePropertiesEditionPart) editingPart;
        // init values
        if (isAccessible(TypeslibraryViewsRepository.TypeInstance.Properties.type)) {
            typeInstancePart.initType(EEFUtils.choiceOfValues(typeInstance, TypesLibraryPackage.eINSTANCE.getTypeInstance_NativeType()), typeInstance.getNativeType());
        }
        if (isAccessible(TypeslibraryViewsRepository.TypeInstance.Properties.TypeAttributes.length))
            typeInstancePart.setLength(EEFConverterUtil.convertToString(EcorePackage.Literals.EINTEGER_OBJECT, typeInstance.getLength()));
        if (isAccessible(TypeslibraryViewsRepository.TypeInstance.Properties.TypeAttributes.precision))
            typeInstancePart.setPrecision(EEFConverterUtil.convertToString(EcorePackage.Literals.EINTEGER_OBJECT, typeInstance.getPrecision()));
        if (isAccessible(TypeslibraryViewsRepository.TypeInstance.Properties.literals))
            typeInstancePart.setLiterals(typeInstance.getLiterals());
    // init filters
    // Start of user code for additional businessfilters for type
    // End of user code
    // init values for referenced views
    // init filters for referenced views
    }
    setInitializing(false);
}
Also used : TypeInstancePropertiesEditionPart(org.obeonetwork.dsl.typeslibrary.parts.TypeInstancePropertiesEditionPart) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance)

Aggregations

TypeInstance (org.obeonetwork.dsl.typeslibrary.TypeInstance)26 Column (org.obeonetwork.dsl.database.Column)11 NativeType (org.obeonetwork.dsl.typeslibrary.NativeType)11 Test (org.junit.Test)4 AbstractTest (org.obeonetwork.database.m2doc.services.common.AbstractTest)4 EObject (org.eclipse.emf.ecore.EObject)2 Constraint (org.obeonetwork.dsl.database.Constraint)2 Type (org.obeonetwork.dsl.typeslibrary.Type)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 EList (org.eclipse.emf.common.util.EList)1 EObjectFlatComboSettings (org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings)1 Viewer (org.eclipse.jface.viewers.Viewer)1 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)1 View (org.obeonetwork.dsl.database.View)1 ColumnSpec (org.obeonetwork.dsl.database.spec.ColumnSpec)1 Attribute (org.obeonetwork.dsl.environment.Attribute)1 Literal (org.obeonetwork.dsl.environment.Literal)1