Search in sources :

Example 1 with Element

use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.

the class MetaAttrUtility method addMetaAttrsFromDb.

/**
 * Read meta-attributes from the db and add them to the ili2c metamodel.
 * @param td
 * @param conn
 * @param schema
 * @throws Ili2dbException
 */
public static void addMetaAttrsFromDb(TransferDescription td, Connection conn, String schema) throws Ili2dbException {
    String sqlName = DbNames.META_ATTRIBUTES_TAB;
    if (schema != null) {
        sqlName = schema + "." + sqlName;
    }
    try {
        String stmt = "SELECT " + DbNames.META_ATTRIBUTES_TAB_ILIELEMENT_COL + ", " + DbNames.META_ATTRIBUTES_TAB_ATTRNAME_COL + ", " + DbNames.META_ATTRIBUTES_TAB_ATTRVALUE_COL + " " + "FROM " + sqlName;
        EhiLogger.traceBackendCmd(stmt);
        Statement dbstmt = conn.createStatement();
        ResultSet rs = dbstmt.executeQuery(stmt);
        while (rs.next()) {
            String ilielement = rs.getString(DbNames.META_ATTRIBUTES_TAB_ILIELEMENT_COL);
            String attrname = rs.getString(DbNames.META_ATTRIBUTES_TAB_ATTRNAME_COL);
            String attrvalue = rs.getString(DbNames.META_ATTRIBUTES_TAB_ATTRVALUE_COL);
            // Add meta-attr to the Element
            Element element = td.getElement(ilielement);
            // known element?
            if (element != null) {
                // meta-attr not yet set/defined?
                if (element.getMetaValue(attrname) == null) {
                    // set it to the read value
                    element.setMetaValue(attrname, attrvalue);
                }
            }
        }
    } catch (java.sql.SQLException ex) {
        throw new Ili2dbException("failed to read meta-attributes table", ex);
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Element(ch.interlis.ili2c.metamodel.Element) ResultSet(java.sql.ResultSet)

Example 2 with Element

use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.

the class ReduceToBaseModel method setupTopicTranslation.

private void setupTopicTranslation(Topic srcTopic, Topic baseTopic) {
    srctag2destElement.put(srcTopic.getScopedName(), baseTopic);
    if (srcTopic == baseTopic) {
        // no translation required
        return;
    }
    // for each class of srcTopic, find base in baseTopic
    while (srcTopic != null) {
        if (srcTopic.isExtending(baseTopic) && srcTopic != baseTopic) {
            Iterator<Element> eleIt = srcTopic.iterator();
            while (eleIt.hasNext()) {
                Element srcEle = eleIt.next();
                if (srcEle instanceof AbstractClassDef) {
                    AbstractClassDef baseEle = (AbstractClassDef) srcEle;
                    while (baseEle != null) {
                        if (baseEle.getContainer() == baseTopic) {
                            break;
                        }
                        baseEle = (AbstractClassDef) baseEle.getExtending();
                    }
                    if (baseEle != null) {
                        srctag2destElement.put(srcEle.getScopedName(), baseEle);
                        if (srcEle != baseEle) {
                            setupClassTranslation((AbstractClassDef) srcEle, baseEle);
                        } else {
                        // no object translation required
                        // setupClassTranslation((AbstractClassDef) srcEle,baseEle);
                        }
                    } else {
                        // delete object from export
                        srctag2destElement.put(srcEle.getScopedName(), null);
                    }
                }
            }
        } else {
            // keep elements, as they are (no translation required)
            Iterator<Element> eleIt = srcTopic.iterator();
            while (eleIt.hasNext()) {
                Element srcEle = eleIt.next();
                if (srcEle instanceof AbstractClassDef) {
                    AbstractClassDef baseEle = (AbstractClassDef) srcEle;
                    srctag2destElement.put(srcEle.getScopedName(), baseEle);
                // no object translation required
                // setupClassTranslation((AbstractClassDef) srcEle,baseEle);
                }
            }
        }
        srcTopic = (Topic) srcTopic.getExtending();
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) Element(ch.interlis.ili2c.metamodel.Element) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef)

Example 3 with Element

use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.

the class ReduceToBaseModel method setupClassTranslation.

private void setupClassTranslation(AbstractClassDef srcEle, AbstractClassDef baseEle) {
    HashMap<String, ViewableTransferElement> baseAttrs = new HashMap<String, ViewableTransferElement>();
    Iterator<ViewableTransferElement> basePropIt = baseEle.getAttributesAndRoles2();
    while (basePropIt.hasNext()) {
        ViewableTransferElement baseProp = basePropIt.next();
        String basePropName = ((Element) baseProp.obj).getName();
        baseAttrs.put(basePropName, baseProp);
    }
    Iterator<ViewableTransferElement> srcPropIt = srcEle.getAttributesAndRoles2();
    while (srcPropIt.hasNext()) {
        ViewableTransferElement srcProp = srcPropIt.next();
        String srcPropName = ((Element) srcProp.obj).getName();
        if (baseAttrs.containsKey(srcPropName)) {
            // attribute exists in base class
            srctag2destElement.put(((Element) srcProp.obj).getScopedName(), baseAttrs.get(srcPropName));
        } else {
            // attribute doesn't exist in base class
            // remove it from export
            srctag2destElement.put(((Element) srcProp.obj).getScopedName(), null);
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) HashMap(java.util.HashMap) ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) Element(ch.interlis.ili2c.metamodel.Element)

Example 4 with Element

use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.

the class Viewable2TableMapper method doit.

private Viewable2TableMapping doit(List<Element> eles) {
    // 
    if (Config.INHERITANCE_TRAFO_SMART1.equals(config.getInheritanceTrafo())) {
        doSmart1(eles);
    } else if (Config.INHERITANCE_TRAFO_SMART2.equals(config.getInheritanceTrafo())) {
        doSmart2(eles);
    } else {
        doSmartOff(eles);
    }
    // 
    // create ViewableWrappers for all NewClass or NewAndSubClass tagged viewables
    // 
    Viewable2TableMapping ret = new Viewable2TableMapping();
    for (Element ele : eles) {
        if (!(ele instanceof Viewable)) {
        // not a Viewable; skip it
        } else {
            // a Viewable
            Viewable aclass = (Viewable) ele;
            String inheritanceStrategy = trafoConfig.getViewableConfig(aclass, TrafoConfigNames.INHERITANCE_TRAFO);
            if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(inheritanceStrategy) || TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(inheritanceStrategy)) {
                String sqlTablename = nameMapping.mapIliClassDef(aclass);
                ViewableWrapper wrapper = new ViewableWrapper(sqlSchemaname, sqlTablename, aclass);
                List<ViewableTransferElement> props = new java.util.ArrayList<ViewableTransferElement>();
                // defined attrs
                {
                    addProps(wrapper, props, aclass.getDefinedAttributesAndRoles2());
                }
                // defined attrs of bases with subclass or newAndSubclass strategy
                {
                    Viewable base = (Viewable) aclass.getExtending();
                    while (base != null) {
                        String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
                        if (!TrafoConfigNames.INHERITANCE_TRAFO_SUBCLASS.equals(baseInheritanceStrategy) && !TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(baseInheritanceStrategy)) {
                            break;
                        }
                        addProps(wrapper, props, base.getDefinedAttributesAndRoles2());
                        base = (Viewable) base.getExtending();
                    }
                }
                // add attrs of extensions with superclass strategy while visiting extensions
                wrapper.setAttrv(props);
                // link to base ViewableWrapper
                {
                    Viewable base = (Viewable) aclass.getExtending();
                    while (base != null) {
                        String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
                        if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(baseInheritanceStrategy)) {
                            // but NOT INHERITANCE_TRAFO_NEWANDSUBCLASS!
                            break;
                        }
                        base = (Viewable) base.getExtending();
                    }
                    if (base != null) {
                        ViewableWrapper baseWrapper = ret.get(base);
                        if (baseWrapper != wrapper) {
                            wrapper.setExtending(baseWrapper);
                        }
                    }
                }
                // includes more than one type
                if (wrapper.getExtending() != null) {
                    // base contains type typediscriminator
                    wrapper.setMultipleTypes(false);
                } else {
                    // if a concrete base
                    if (hasAnyConcreteBaseWithSubClass(trafoConfig, aclass)) {
                        wrapper.setMultipleTypes(true);
                    } else if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(inheritanceStrategy) && hasAnyConreteExtension(aclass)) {
                        // newClass and any concrete extensions
                        wrapper.setMultipleTypes(true);
                    } else if (TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(inheritanceStrategy) && hasAnyConreteExtensionWithoutNewClass(trafoConfig, aclass)) {
                        // newAndSubClass and any concrete extensions without newClass or newAndSubClass
                        wrapper.setMultipleTypes(true);
                    } else {
                        wrapper.setMultipleTypes(false);
                    }
                    aclass.getDirectExtensions();
                }
                ret.add(aclass, wrapper);
            } else if (TrafoConfigNames.INHERITANCE_TRAFO_SUPERCLASS.equals(inheritanceStrategy)) {
                // add props of extensions with superclass strategy to base-class
                // find base
                Viewable base = (Viewable) aclass.getExtending();
                while (base != null) {
                    String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
                    if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(baseInheritanceStrategy) || TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(baseInheritanceStrategy)) {
                        break;
                    }
                    base = (Viewable) base.getExtending();
                }
                ViewableWrapper wrapper = ret.get(base);
                List<ViewableTransferElement> props = wrapper.getAttrv();
                // add props of extension
                addProps(wrapper, props, aclass.getDefinedAttributesAndRoles2());
                wrapper.setAttrv(props);
                ret.add(aclass, wrapper);
            } else if (TrafoConfigNames.INHERITANCE_TRAFO_SUBCLASS.equals(inheritanceStrategy)) {
            // skip it; props already added when visiting subclass
            } else {
                throw new IllegalStateException("unexpected inheritance config <" + inheritanceStrategy + ">");
            }
        }
    }
    return ret;
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) Element(ch.interlis.ili2c.metamodel.Element) Viewable(ch.interlis.ili2c.metamodel.Viewable) List(java.util.List)

Example 5 with Element

use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.

the class ModelElementSelector method getModelElements.

public List<Element> getModelElements(List<String> modelNames, TransferDescription td, boolean createItfLineTables, boolean includeEnums, Config config) {
    this.td = td;
    this.createItfLineTables = createItfLineTables;
    this.includeEnums = includeEnums;
    List<Model> models = new ArrayList<Model>();
    if (modelNames == null || modelNames.isEmpty()) {
        Model lastModel = td.getLastModel();
        models.add(lastModel);
    } else {
        for (String modelName : modelNames) {
            Model model = (Model) td.getElement(Model.class, modelName);
            if (model == null) {
                throw new IllegalArgumentException("unknown model <" + modelName + ">");
            }
            models.add(model);
        }
    }
    HashSet<Element> accu = new HashSet<Element>();
    HashSet<Model> accuScope = new HashSet<Model>();
    for (Model model : models) {
        if (config.getVer4_translation() || config.getIli1Translation() != null) {
            visitModel(accu, accuScope, (Model) model.getTranslationOfOrSame());
        } else {
            visitModel(accu, accuScope, model);
        }
    }
    visitStructsInScope(accu, accuScope, config);
    ArrayList<Element> ret = new ArrayList<Element>(accu);
    java.util.Collections.sort(ret, new java.util.Comparator<Element>() {

        @Override
        public int compare(Element arg0, Element arg1) {
            if (arg0.getDefidx() < arg1.getDefidx()) {
                return -1;
            }
            if (arg0.getDefidx() > arg1.getDefidx()) {
                return 1;
            }
            return 0;
        }
    });
    return ret;
}
Also used : Element(ch.interlis.ili2c.metamodel.Element) ArrayList(java.util.ArrayList) Model(ch.interlis.ili2c.metamodel.Model) HashSet(java.util.HashSet)

Aggregations

Element (ch.interlis.ili2c.metamodel.Element)14 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)7 Viewable (ch.interlis.ili2c.metamodel.Viewable)5 ArrayList (java.util.ArrayList)4 StdLogEvent (ch.ehi.basics.logging.StdLogEvent)3 ConverterException (ch.ehi.ili2db.converter.ConverterException)3 SqlColumnConverter (ch.ehi.ili2db.converter.SqlColumnConverter)3 CustomMapping (ch.ehi.ili2db.fromili.CustomMapping)3 ModelElementSelector (ch.ehi.ili2db.fromili.ModelElementSelector)3 NameMapping (ch.ehi.ili2db.mapping.NameMapping)3 TrafoConfig (ch.ehi.ili2db.mapping.TrafoConfig)3 Viewable2TableMapping (ch.ehi.ili2db.mapping.Viewable2TableMapping)3 DbSchema (ch.ehi.sqlgen.repository.DbSchema)3 DbTableName (ch.ehi.sqlgen.repository.DbTableName)3 Configuration (ch.interlis.ili2c.config.Configuration)3 Ili2cMetaAttrs (ch.interlis.ili2c.metamodel.Ili2cMetaAttrs)3 TransferDescription (ch.interlis.ili2c.metamodel.TransferDescription)3 IoxException (ch.interlis.iox.IoxException)3 FileLogger (ch.interlis.iox_j.logging.FileLogger)3 StdLogger (ch.interlis.iox_j.logging.StdLogger)3