Search in sources :

Example 1 with ViewableTransferElement

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

the class ToXtfRecordConverter method createQueryStmt.

/**
 * creates sql query statement for a class.
 * @param aclass type of objects to build query for
 * @param wrapper not null, if building query for struct values
 * @return SQL-Query statement
 */
public String createQueryStmt(Viewable aclass1, Long basketSqlId, StructWrapper structWrapper) {
    ViewableWrapper aclass = class2wrapper.get(aclass1);
    ViewableWrapper rootWrapper = aclass.getWrappers().get(0);
    StringBuffer ret = new StringBuffer();
    ret.append("SELECT r0." + colT_ID);
    if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
        ret.append(", r0." + DbNames.T_TYPE_COL);
    }
    if (!aclass.isStructure()) {
        if (createIliTidCol || aclass.getOid() != null) {
            ret.append(", r0." + DbNames.T_ILI_TID_COL);
        }
    }
    if (structWrapper != null) {
        if (createGenericStructRef) {
            ret.append(", r0." + DbNames.T_PARENT_ID_COL);
            ret.append(", r0." + DbNames.T_PARENT_TYPE_COL);
            ret.append(", r0." + DbNames.T_PARENT_ATTR_COL);
        } else {
            ret.append(", r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()));
        }
        ret.append(", r0." + DbNames.T_SEQ_COL);
    }
    String sep = ",";
    int tableAliasIdx = 0;
    HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
    for (ViewableWrapper table : aclass.getWrappers()) {
        String tableAlias = "r" + tableAliasIdx;
        String sqlTableName = table.getSqlTablename();
        Iterator iter = table.getAttrIterator();
        while (iter.hasNext()) {
            ViewableTransferElement obj = (ViewableTransferElement) iter.next();
            if (obj.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) obj.obj;
                AttributeDef baseAttr = attr;
                while (true) {
                    AttributeDef baseAttr1 = (AttributeDef) baseAttr.getExtending();
                    if (baseAttr1 == null) {
                        break;
                    }
                    baseAttr = baseAttr1;
                }
                if (!visitedAttrs.contains(baseAttr)) {
                    visitedAttrs.add(baseAttr);
                    if (!baseAttr.isTransient()) {
                        Type proxyType = baseAttr.getDomain();
                        if (proxyType != null && (proxyType instanceof ObjectType)) {
                        // skip implicit particles (base-viewables) of views
                        } else {
                            sep = addAttrToQueryStmt(ret, sep, tableAlias, baseAttr, sqlTableName);
                        }
                    }
                }
            }
            if (obj.obj instanceof RoleDef) {
                RoleDef role = (RoleDef) obj.obj;
                if (role.getExtending() == null) {
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    for (ViewableWrapper targetTable : targetTables) {
                        String roleSqlName = ili2sqlName.mapIliRoleDef(role, sqlTableName, targetTable.getSqlTablename(), targetTables.size() > 1);
                        // a role of an embedded association?
                        if (obj.embedded) {
                            AssociationDef roleOwner = (AssociationDef) role.getContainer();
                            if (roleOwner.getDerivedFrom() == null) {
                                // TODO if(orderPos!=0){
                                ret.append(sep);
                                sep = ",";
                                ret.append(makeColumnRef(tableAlias, roleSqlName));
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            ret.append(sep);
                            sep = ",";
                            ret.append(makeColumnRef(tableAlias, roleSqlName));
                        }
                    }
                }
            }
        }
        // next table alias
        tableAliasIdx++;
    }
    // stdcols
    if (createStdCols) {
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_LAST_CHANGE_COL);
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_CREATE_DATE_COL);
        ret.append(sep);
        sep = ",";
        ret.append("r0." + DbNames.T_USER_COL);
    }
    ret.append(" FROM ");
    ArrayList<ViewableWrapper> tablev = new ArrayList<ViewableWrapper>(10);
    tablev.addAll(aclass.getWrappers());
    sep = "";
    int tablec = tablev.size();
    if (isMsAccess) {
        for (int i = 0; i < tablec; i++) {
            ret.append("(");
        }
    }
    for (int i = 0; i < tablec; i++) {
        ret.append(sep);
        ret.append(tablev.get(i).getSqlTableQName());
        ret.append(" r" + Integer.toString(i));
        if (i > 0) {
            ret.append(" ON r0." + colT_ID + "=r" + Integer.toString(i) + "." + colT_ID);
        }
        if (isMsAccess) {
            ret.append(")");
        }
        sep = " LEFT JOIN ";
    }
    sep = " WHERE";
    if (createTypeDiscriminator || rootWrapper.includesMultipleTypes()) {
        ret.append(sep + " r0." + DbNames.T_TYPE_COL + "='" + getSqlType(aclass1).getName() + "'");
        sep = " AND";
    }
    if (structWrapper != null) {
        if (createGenericStructRef) {
            ret.append(sep + " r0." + DbNames.T_PARENT_ID_COL + "=? AND r0." + DbNames.T_PARENT_ATTR_COL + "=?");
        } else {
            ret.append(sep + " r0." + ili2sqlName.mapIliAttributeDefReverse(structWrapper.getParentAttr(), getSqlType(aclass.getViewable()).getName(), getSqlType(structWrapper.getParentTable().getViewable()).getName()) + "=?");
        }
        sep = " AND";
    }
    if (basketSqlId != null) {
        ret.append(sep + " r0." + DbNames.T_BASKET_COL + "=?");
    }
    if (structWrapper != null) {
        ret.append(" ORDER BY r0." + DbNames.T_SEQ_COL + " ASC");
    }
    return ret.toString();
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 2 with ViewableTransferElement

use of ch.interlis.ili2c.metamodel.ViewableTransferElement 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 3 with ViewableTransferElement

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

the class TransferToXtf method genClassHelper.

private void genClassHelper(Viewable aclass) {
    boolean doStruct = false;
    if (aclass instanceof Table) {
        doStruct = !((Table) aclass).isIdentifiable();
    }
    if (doStruct) {
        expgen.println("private void add" + aclass.getName() + "(String parentTid,String parentAttrSql,IomObject parent,String parentAttrIli)");
    } else {
        expgen.println("private void add" + aclass.getName() + "(String subset)");
        String addany = "if(iliClassName.equals(\"" + aclass.getScopedName(null) + "\")){add" + aclass.getName() + "(select);}";
        addanyLines.add(addany);
    }
    expgen.println("{");
    expgen.indent();
    expgen.println("String tabName=\"" + createQueryStmtFromClause(aclass) + "\";");
    expgen.println("String stmt=\"" + recConv.createQueryStmt(aclass, null, null) + "\";");
    if (!doStruct) {
        expgen.println("if(subset!=null){");
        expgen.println("stmt=stmt+\" AND \"+subset;");
        expgen.println("}");
    }
    expgen.println("EhiLogger.traceBackendCmd(stmt);");
    expgen.println("java.sql.PreparedStatement dbstmt = null;");
    expgen.println("try{");
    expgen.indent();
    // expgen.println("dbstmt = conn.createStatement();");
    // expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery(stmt);");
    expgen.println("dbstmt = conn.prepareStatement(stmt);");
    expgen.println("dbstmt.clearParameters();");
    if (doStruct) {
        expgen.println("dbstmt.setString(1,parentTid);");
        expgen.println("dbstmt.setString(2,parentAttrSql);");
    }
    expgen.println("java.sql.ResultSet rs=dbstmt.executeQuery();");
    expgen.println("while(rs.next()){");
    expgen.indent();
    expgen.println("String tid=DbUtility.getString(rs,\"T_Id\",false,tabName);");
    expgen.println("String recInfo=tabName+\" \"+tid;");
    expgen.println("IomObject iomObj;");
    if (!doStruct) {
        expgen.println("iomObj=newObject(\"" + aclass.getScopedName(null) + "\",mapId(\"" + recConv.getSqlType(aclass) + "\",tid));");
    } else {
        expgen.println("iomObj=(IomObject)parent.addattrobj(parentAttrIli,\"" + aclass.getScopedName(null) + "\");");
    }
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attr.getExtending() == null) {
                String attrName = attr.getName();
                String sqlAttrName = ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null);
                Type type = attr.getDomain();
                if ((type instanceof TypeAlias) && Ili2cUtility.isBoolean(td, type)) {
                    expgen.println("Boolean prop_" + attrName + "=Db2Xtf.getBoolean(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
                } else {
                    type = attr.getDomainResolvingAliases();
                    if (type instanceof CompositionType) {
                        // enque iomObj as parent
                        // enqueueStructAttr(new StructWrapper(tid,attr,iomObj));
                        CompositionType ct = (CompositionType) type;
                        expgen.println("add" + ct.getComponentType().getName() + "(tid,\"" + ili2sqlName.mapIliAttributeDef(attr, recConv.getSqlType(aclass).getName(), null) + "\",iomObj,\"" + attr.getName() + "\");");
                    } else if (type instanceof PolylineType) {
                    } else if (type instanceof SurfaceOrAreaType) {
                    } else if (type instanceof CoordType) {
                    } else if (type instanceof EnumerationType) {
                        String enumName = null;
                        if (attr.getDomain() instanceof TypeAlias) {
                            Domain domainDef = ((TypeAlias) attr.getDomain()).getAliasing();
                            enumName = domainDef.getScopedName(null);
                        } else {
                            enumName = aclass.getScopedName(null) + "->" + attrName;
                            enumTypes.add(enumName);
                        }
                        expgen.println("String prop_" + attrName + "=Db2Xtf.getEnum(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,getEnumMapper(\"" + enumName + "\"),iomObj,\"" + attrName + "\");");
                    } else {
                        expgen.println("String prop_" + attrName + "=Db2Xtf.getString(rs,\"" + sqlAttrName + "\"," + (type.isMandatoryConsideringAliases() ? "false" : "true") + ",recInfo,iomObj,\"" + attrName + "\");");
                    }
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                String roleName = role.getName();
                String sqlRoleName = ili2sqlName.mapIliRoleDef(role, recConv.getSqlType(aclass).getName(), recConv.getSqlType(role.getDestination()).getName());
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // TODO if(orderPos!=0){
                        expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "true" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"" + roleOwner.getScopedName(null) + "\");");
                    }
                } else {
                    // TODO if(orderPos!=0){
                    expgen.println("String prop_" + roleName + "=Db2Xtf.getRef(rs,\"" + sqlRoleName + "\"," + "false" + ",recInfo,this,\"" + recConv.getSqlType(role.getDestination()) + "\",iomObj,\"" + roleName + "\",\"REF\");");
                }
            }
        }
    }
    // add referenced and referencing objects if it is not a struct
    if (!doStruct && aclass instanceof AbstractClassDef) {
        AbstractClassDef aclass2 = (AbstractClassDef) aclass;
        Iterator associ = aclass2.getTargetForRoles();
        while (associ.hasNext()) {
            RoleDef roleThis = (RoleDef) associ.next();
            if (roleThis.getKind() == RoleDef.Kind.eAGGREGATE || roleThis.getKind() == RoleDef.Kind.eCOMPOSITE) {
                RoleDef oppEnd = roleThis.getOppEnd();
                if (roleThis.isAssociationEmbedded()) {
                    expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"T_Id\",prop_" + oppEnd.getName() + ");");
                } else if (oppEnd.isAssociationEmbedded()) {
                    expgen.println("addPendingObject(\"" + oppEnd.getDestination().getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",tid);");
                } else {
                    expgen.println("addPendingObject(\"" + ((AssociationDef) (oppEnd.getContainer())).getScopedName(null) + "\",\"" + ili2sqlName.mapIliRoleDef(roleThis, null, null) + "\",prop_" + roleThis.getName() + ");");
                }
            }
        }
    }
    // writeObject if it is not a struct
    if (!doStruct) {
        expgen.println("writeObject(iomObj);");
    }
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}catch(java.sql.SQLException ex){");
    expgen.indent();
    expgen.println("EhiLogger.logError(\"failed to query \"+tabName,ex);");
    expgen.unindent();
    expgen.println("}finally{");
    expgen.indent();
    expgen.println("if(dbstmt!=null){");
    expgen.indent();
    expgen.println("try{");
    expgen.indent();
    expgen.println("dbstmt.close();");
    expgen.unindent();
    expgen.println("}catch(java.sql.SQLException ex){");
    expgen.indent();
    expgen.println("EhiLogger.logError(\"failed to close query of \"+tabName,ex);");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
    expgen.unindent();
    expgen.println("}");
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) TypeAlias(ch.interlis.ili2c.metamodel.TypeAlias) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) AbstractClassDef(ch.interlis.ili2c.metamodel.AbstractClassDef) Domain(ch.interlis.ili2c.metamodel.Domain) CoordType(ch.interlis.ili2c.metamodel.CoordType)

Example 4 with ViewableTransferElement

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

the class Viewable2TableMapper method addProps.

private void addProps(ViewableWrapper viewable, List<ViewableTransferElement> attrv, Iterator<ViewableTransferElement> iter) {
    boolean hasGeometry = false;
    // only one geometry column per table?
    if (singleGeom) {
        for (ViewableTransferElement attrE : attrv) {
            if (attrE.obj instanceof AttributeDef) {
                AttributeDef attr = (AttributeDef) attrE.obj;
                ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
                if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO)))) || (Ili2cUtility.isMultiPointAttr(getTransferDescription(attr), attr) && (coalesceMultiPoint || TrafoConfigNames.MULTIPOINT_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTIPOINT_TRAFO))))) {
                    hasGeometry = true;
                    break;
                }
            }
        }
    }
    while (iter.hasNext()) {
        ViewableTransferElement obj = iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            String sqlname = trafoConfig.getAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE);
            // attribute configured to be in a secondary table?
            if (sqlname != null) {
                // add attribute to given secondary table
                ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
                if (attrWrapper == null) {
                    attrWrapper = viewable.createSecondaryTable(sqlname);
                }
                List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
                attrProps.add(obj);
                attrWrapper.setAttrv(attrProps);
            } else {
                // only one geometry column per table?
                if (singleGeom) {
                    ch.interlis.ili2c.metamodel.Type type = attr.getDomainResolvingAliases();
                    if (type instanceof ch.interlis.ili2c.metamodel.CoordType || type instanceof ch.interlis.ili2c.metamodel.LineType || (Ili2cUtility.isMultiSurfaceAttr(getTransferDescription(attr), attr) && (coalesceMultiSurface || TrafoConfigNames.MULTISURFACE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTISURFACE_TRAFO)))) || (Ili2cUtility.isMultiLineAttr(getTransferDescription(attr), attr) && (coalesceMultiLine || TrafoConfigNames.MULTILINE_TRAFO_COALESCE.equals(trafoConfig.getAttrConfig(attr, TrafoConfigNames.MULTILINE_TRAFO))))) {
                        if (createItfLineTables && type instanceof ch.interlis.ili2c.metamodel.SurfaceOrAreaType) {
                        // ignore it; will be created by legacy code
                        } else {
                            // table already has a geometry column?
                            if (hasGeometry) {
                                // create a new secondary table
                                sqlname = nameMapping.mapGeometryAsTable(attr);
                                ViewableWrapper attrWrapper = viewable.getSecondaryTable(sqlname);
                                if (attrWrapper == null) {
                                    attrWrapper = viewable.createSecondaryTable(sqlname);
                                }
                                // add attribute to new secondary table
                                List<ViewableTransferElement> attrProps = new java.util.ArrayList<ViewableTransferElement>();
                                attrProps.add(obj);
                                attrWrapper.setAttrv(attrProps);
                                trafoConfig.setAttrConfig(attr, TrafoConfigNames.SECONDARY_TABLE, sqlname);
                            } else {
                                // table has not yet a geometry column
                                // add it
                                hasGeometry = true;
                                attrv.add(obj);
                            }
                        }
                    } else {
                        // not a Geom type
                        attrv.add(obj);
                    }
                } else {
                    attrv.add(obj);
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            AssociationDef roleOwner = (AssociationDef) role.getContainer();
            // not an embedded role and roledef not defined in a lightweight association?
            if (!obj.embedded && !roleOwner.isLightweight()) {
                attrv.add(obj);
            }
            // a role of an embedded association?
            if (obj.embedded) {
                if (roleOwner.getDerivedFrom() == null) {
                    attrv.add(obj);
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 5 with ViewableTransferElement

use of ch.interlis.ili2c.metamodel.ViewableTransferElement 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)

Aggregations

ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)17 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)14 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)13 Type (ch.interlis.ili2c.metamodel.Type)13 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)11 CoordType (ch.interlis.ili2c.metamodel.CoordType)10 RoleDef (ch.interlis.ili2c.metamodel.RoleDef)10 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)9 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)9 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)9 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)9 Iterator (java.util.Iterator)9 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)8 ReferenceType (ch.interlis.ili2c.metamodel.ReferenceType)7 AreaType (ch.interlis.ili2c.metamodel.AreaType)6 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)6 HashSet (java.util.HashSet)6 NumericType (ch.interlis.ili2c.metamodel.NumericType)5 Table (ch.interlis.ili2c.metamodel.Table)5 Viewable (ch.interlis.ili2c.metamodel.Viewable)5