Search in sources :

Example 11 with NativeType

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

the class ColumnServicesTest method testColumnLengthSIMPLE.

@Test
public void testColumnLengthSIMPLE() {
    Column col = (Column) EcoreUtil.create(DatabasePackage.Literals.COLUMN);
    TypeInstance type = (TypeInstance) EcoreUtil.create(TypesLibraryPackage.Literals.TYPE_INSTANCE);
    NativeType nType = (NativeType) EcoreUtil.create(TypesLibraryPackage.Literals.NATIVE_TYPE);
    col.setType(type);
    type.setNativeType(nType);
    nType.setSpec(NativeTypeKind.SIMPLE);
    assertEquals("", new ColumnServices().typeLength(col));
}
Also used : Column(org.obeonetwork.dsl.database.Column) NativeType(org.obeonetwork.dsl.typeslibrary.NativeType) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance) Test(org.junit.Test) AbstractTest(org.obeonetwork.database.m2doc.services.common.AbstractTest)

Example 12 with NativeType

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

the class ColumnServicesTest method testColumnLengthLENGTHANDPRECISION.

@Test
public void testColumnLengthLENGTHANDPRECISION() {
    Column col = (Column) EcoreUtil.create(DatabasePackage.Literals.COLUMN);
    TypeInstance type = (TypeInstance) EcoreUtil.create(TypesLibraryPackage.Literals.TYPE_INSTANCE);
    NativeType nType = (NativeType) EcoreUtil.create(TypesLibraryPackage.Literals.NATIVE_TYPE);
    col.setType(type);
    type.setNativeType(nType);
    nType.setSpec(NativeTypeKind.LENGTH_AND_PRECISION);
    type.setLength(255);
    type.setPrecision(8);
    assertEquals("255,8", new ColumnServices().typeLength(col));
}
Also used : Column(org.obeonetwork.dsl.database.Column) NativeType(org.obeonetwork.dsl.typeslibrary.NativeType) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance) Test(org.junit.Test) AbstractTest(org.obeonetwork.database.m2doc.services.common.AbstractTest)

Example 13 with NativeType

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

the class EntityToMLD method loadTypesLibrary.

private Map<String, NativeType> loadTypesLibrary() {
    Map<String, NativeType> types = new HashMap<String, NativeType>();
    TypesLibrary typesLibrary = TypesLibraryUtil.getLogicalTypesLibrary(getResourceSet());
    for (Iterator<EObject> it = typesLibrary.eAllContents(); it.hasNext(); ) {
        EObject object = it.next();
        if (object instanceof NativeType) {
            NativeType type = (NativeType) object;
            types.put(type.getName(), type);
        }
    }
    return types;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EObject(org.eclipse.emf.ecore.EObject) NativeType(org.obeonetwork.dsl.typeslibrary.NativeType) TypesLibrary(org.obeonetwork.dsl.typeslibrary.TypesLibrary)

Example 14 with NativeType

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

the class EntityToMLD method resolveType.

private TypeInstance resolveType(DataType type, String physicalSize) {
    TypeInstance typeInstance = TypesLibraryFactory.eINSTANCE.createTypeInstance();
    if (type != null && type.getName() != null) {
        if (getTypeProperties().containsKey(type.getName())) {
            String logicalTypeName = getTypeProperties().getProperty(type.getName());
            NativeType logicalType = nativeTypesMap.get(logicalTypeName);
            if (logicalType != null) {
                typeInstance.setNativeType(logicalType);
                if (physicalSize != null && physicalSize.length() > 0) {
                    if (physicalSize.contains(",")) {
                        int length = Integer.parseInt(physicalSize.substring(0, physicalSize.indexOf(",")));
                        int precision = Integer.parseInt(physicalSize.substring(physicalSize.indexOf(",") + 1));
                        typeInstance.setLength(length);
                        typeInstance.setPrecision(precision);
                    } else {
                        typeInstance.setLength(Integer.parseInt(physicalSize));
                    }
                }
            }
        }
    }
    return typeInstance;
}
Also used : NativeType(org.obeonetwork.dsl.typeslibrary.NativeType) TypeInstance(org.obeonetwork.dsl.typeslibrary.TypeInstance) Constraint(org.obeonetwork.dsl.database.Constraint)

Example 15 with NativeType

use of org.obeonetwork.dsl.typeslibrary.NativeType 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)

Aggregations

NativeType (org.obeonetwork.dsl.typeslibrary.NativeType)19 TypeInstance (org.obeonetwork.dsl.typeslibrary.TypeInstance)11 EObject (org.eclipse.emf.ecore.EObject)5 Column (org.obeonetwork.dsl.database.Column)5 Test (org.junit.Test)4 AbstractTest (org.obeonetwork.database.m2doc.services.common.AbstractTest)4 NativeTypesLibrary (org.obeonetwork.dsl.typeslibrary.NativeTypesLibrary)3 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)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 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1 EReferencePropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EReferencePropertiesEditionContext)1 PropertiesEditingPolicy (org.eclipse.emf.eef.runtime.policies.PropertiesEditingPolicy)1 CreateEditingPolicy (org.eclipse.emf.eef.runtime.policies.impl.CreateEditingPolicy)1 PropertiesEditingProvider (org.eclipse.emf.eef.runtime.providers.PropertiesEditingProvider)1