Search in sources :

Example 6 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class FromIliRecordConverter method addParentRef.

private void addParentRef(Viewable parentTable, AttributeDef attr) {
    CompositionType type = (CompositionType) attr.getDomainResolvingAll();
    Table structClass = type.getComponentType();
    // if abstract struct, might have multiple tables!
    for (ViewableWrapper structWrapper : getStructWrappers(structClass)) {
        DbTableName structClassSqlName = structWrapper.getSqlTable();
        // find struct table
        DbTable dbTable = schema.findTable(structClassSqlName);
        // add ref attr
        String refAttrSqlName = ili2sqlName.mapIliAttributeDefReverse(attr, structClassSqlName.getName(), class2wrapper.get(parentTable).getSqlTablename());
        DbColId dbParentId = new DbColId();
        dbParentId.setName(refAttrSqlName);
        // values of other struct attrs will have NULL
        dbParentId.setNotNull(false);
        dbParentId.setPrimaryKey(false);
        StringBuffer cmt = new StringBuffer();
        String cmtSep = "";
        if (attr.getDocumentation() != null) {
            cmt.append(cmtSep + attr.getDocumentation());
            cmtSep = nl;
        }
        cmt.append(cmtSep + "@iliname " + attr.getContainer().getScopedName(null) + "." + attr.getName());
        cmtSep = nl;
        if (cmt.length() > 0) {
            dbParentId.setComment(cmt.toString());
        }
        if (createFk) {
            dbParentId.setReferencedTable(class2wrapper.get(parentTable).getSqlTable());
        }
        if (createFkIdx) {
            dbParentId.setIndex(true);
        }
        dbTable.addColumn(dbParentId);
    }
}
Also used : DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) DbColId(ch.ehi.sqlgen.repository.DbColId) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) DbTableName(ch.ehi.sqlgen.repository.DbTableName) DbTable(ch.ehi.sqlgen.repository.DbTable)

Example 7 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferFromIli method generatModelEles.

private void generatModelEles(java.util.List<Element> modelEles, int pass) throws Ili2dbException {
    Iterator modeli = modelEles.iterator();
    while (modeli.hasNext()) {
        Object modelo = modeli.next();
        if (modelo instanceof Model) {
            Model model = (Model) modelo;
        // generateModel(model);
        } else if (modelo instanceof Topic) {
        // generateTopic((Topic)modelo);
        } else if (modelo instanceof Domain) {
            if (pass == 2) {
                generateDomain((Domain) modelo);
                visitedElements.add((Domain) modelo);
            }
        } else if (modelo instanceof Viewable) {
            if (modelo instanceof Table && ((Table) modelo).isIli1LineAttrStruct()) {
            // skip it
            } else if ((modelo instanceof View) && !isTransferableView(modelo)) {
            // skip it
            } else {
                try {
                    ViewableWrapper wrapper = class2wrapper.get((Viewable) modelo);
                    if (wrapper != null) {
                        generateViewable(wrapper, pass);
                    }
                    if (pass == 2) {
                        visitedElements.add((Viewable) modelo);
                    }
                } catch (Ili2dbException ex) {
                    throw new Ili2dbException("mapping of " + ((Viewable) modelo).getScopedName(null) + " failed", ex);
                }
            }
        } else if (modelo instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) modelo;
            if (attr.getDomainResolvingAll() instanceof SurfaceOrAreaType) {
                generateItfLineTable(attr, pass);
            } else if (attr.getDomainResolvingAll() instanceof EnumerationType) {
                if (pass == 2) {
                    visitedEnums.add(attr);
                }
            } else {
            // skip it
            }
        } else {
        // skip it
        }
    }
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) DbTable(ch.ehi.sqlgen.repository.DbTable) Table(ch.interlis.ili2c.metamodel.Table) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) Iterator(java.util.Iterator) Model(ch.interlis.ili2c.metamodel.Model) Viewable(ch.interlis.ili2c.metamodel.Viewable) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) Domain(ch.interlis.ili2c.metamodel.Domain)

Example 8 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class FromXtfRecordConverter method getViewableWrapperOfAbstractClass.

private ViewableWrapper getViewableWrapperOfAbstractClass(Viewable abstractClass, Viewable concreteClass) {
    if (abstractClass == concreteClass) {
        return class2wrapper.get(concreteClass);
    }
    ArrayList<ViewableWrapper> rets = new ArrayList<ViewableWrapper>();
    rets.add(class2wrapper.get(concreteClass));
    Viewable superClass = (Viewable) concreteClass.getExtending();
    while (superClass != null) {
        ViewableWrapper ret2 = class2wrapper.get(superClass);
        if (ret2 != null) {
            if (!rets.contains(ret2)) {
                rets.add(0, ret2);
            }
        }
        if (superClass == abstractClass) {
            break;
        }
        superClass = (Viewable) superClass.getExtending();
    }
    for (int i = 0; i < rets.size(); i++) {
        ViewableWrapper ret = rets.get(i);
        if (i == rets.size() - 1) {
            return ret;
        }
        if (!TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(trafoConfig.getViewableConfig(ret.getViewable(), TrafoConfigNames.INHERITANCE_TRAFO))) {
            return ret;
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Viewable(ch.interlis.ili2c.metamodel.Viewable) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper)

Example 9 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class FromXtfRecordConverter method createInsertStmt.

/**
 * creates an insert statement for a given viewable.
 * @param sqlTableName table name of viewable
 * @param aclass viewable
 * @return insert statement
 */
public String createInsertStmt(boolean isUpdate, Viewable iomClass, DbTableName sqlTableName, ViewableWrapper aclass, StructWrapper structEle) {
    StringBuffer ret = new StringBuffer();
    StringBuffer values = new StringBuffer();
    // WHERE some_column=some_value;
    if (isUpdate) {
        ret.append("UPDATE ");
    } else {
        ret.append("INSERT INTO ");
    }
    ret.append(sqlTableName.getQName());
    String sep = null;
    if (isUpdate) {
        sep = " SET ";
    } else {
        sep = " (";
    }
    // add T_Id
    if (!isUpdate) {
        ret.append(sep);
        ret.append(colT_ID);
        values.append("?");
        sep = ",";
    }
    // add T_basket
    if (createBasketCol) {
        ret.append(sep);
        ret.append(DbNames.T_BASKET_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (createDatasetCol) {
        ret.append(sep);
        ret.append(DbNames.T_DATASET_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (!aclass.isSecondaryTable()) {
        // if root, add type
        if (aclass.getExtending() == null) {
            if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
                ret.append(sep);
                ret.append(DbNames.T_TYPE_COL);
                if (isUpdate) {
                    ret.append("=?");
                } else {
                    values.append(",?");
                }
                sep = ",";
            }
            // if Class
            if (!aclass.isStructure()) {
                if (!isUpdate) {
                    if (createIliTidCol || aclass.getOid() != null) {
                        ret.append(sep);
                        ret.append(DbNames.T_ILI_TID_COL);
                        values.append(",?");
                        sep = ",";
                    }
                }
            }
            // if STRUCTURE, add ref to parent
            if (aclass.isStructure()) {
                if (structEle == null) {
                // struct is extended by a class and current object is an instance of the class
                } else {
                    // current object is an instance of the structure
                    if (createGenericStructRef) {
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_ID_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_TYPE_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                        // attribute name in parent class
                        ret.append(sep);
                        ret.append(DbNames.T_PARENT_ATTR_COL);
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                    } else {
                        ret.append(sep);
                        Viewable parentViewable = getViewable(structEle.getParentSqlType());
                        ViewableWrapper parentTable = getViewableWrapperOfAbstractClass((Viewable) structEle.getParentAttr().getContainer(), parentViewable);
                        ret.append(ili2sqlName.mapIliAttributeDefReverse(structEle.getParentAttr(), sqlTableName.getName(), parentTable.getSqlTablename()));
                        if (isUpdate) {
                            ret.append("=?");
                        } else {
                            values.append(",?");
                        }
                        sep = ",";
                    }
                    // seqeunce (not null if LIST)
                    ret.append(sep);
                    ret.append(DbNames.T_SEQ_COL);
                    if (isUpdate) {
                        ret.append("=?");
                    } else {
                        values.append(",?");
                    }
                    sep = ",";
                }
            }
        }
    }
    HashSet attrs = getIomObjectAttrs(iomClass);
    Iterator iter = aclass.getAttrIterator();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (attrs.contains(attr)) {
                if (!attr.isTransient()) {
                    Type proxyType = attr.getDomain();
                    if (proxyType != null && (proxyType instanceof ObjectType)) {
                    // skip implicit particles (base-viewables) of views
                    } else {
                        sep = addAttrToInsertStmt(isUpdate, ret, values, sep, attr, sqlTableName.getName());
                    }
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                if (attrs.contains(role)) {
                    ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
                    for (ViewableWrapper targetTable : targetTables) {
                        String roleName = ili2sqlName.mapIliRoleDef(role, sqlTableName.getName(), 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);
                                ret.append(roleName);
                                if (isUpdate) {
                                    ret.append("=?");
                                } else {
                                    values.append(",?");
                                }
                                sep = ",";
                            }
                        } else {
                            // TODO if(orderPos!=0){
                            ret.append(sep);
                            ret.append(roleName);
                            if (isUpdate) {
                                ret.append("=?");
                            } else {
                                values.append(",?");
                            }
                            sep = ",";
                        }
                    }
                }
            }
        }
    }
    // stdcols
    if (createStdCols) {
        ret.append(sep);
        ret.append(DbNames.T_LAST_CHANGE_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
        if (!isUpdate) {
            ret.append(sep);
            ret.append(DbNames.T_CREATE_DATE_COL);
            values.append(",?");
            sep = ",";
        }
        ret.append(sep);
        ret.append(DbNames.T_USER_COL);
        if (isUpdate) {
            ret.append("=?");
        } else {
            values.append(",?");
        }
        sep = ",";
    }
    if (isUpdate) {
        // WHERE some_column=some_value;
        // add T_Id
        ret.append(" WHERE ");
        ret.append(colT_ID);
        ret.append("=?");
    } else {
        ret.append(") VALUES (");
        ret.append(values);
        ret.append(")");
    }
    return ret.toString();
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) NumericalType(ch.interlis.ili2c.metamodel.NumericalType) PolylineType(ch.interlis.ili2c.metamodel.PolylineType) Type(ch.interlis.ili2c.metamodel.Type) ReferenceType(ch.interlis.ili2c.metamodel.ReferenceType) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CompositionType(ch.interlis.ili2c.metamodel.CompositionType) EnumerationType(ch.interlis.ili2c.metamodel.EnumerationType) NumericType(ch.interlis.ili2c.metamodel.NumericType) BlackboxType(ch.interlis.ili2c.metamodel.BlackboxType) AreaType(ch.interlis.ili2c.metamodel.AreaType) LineType(ch.interlis.ili2c.metamodel.LineType) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) HashSet(java.util.HashSet)

Example 10 with ViewableWrapper

use of ch.ehi.ili2db.mapping.ViewableWrapper in project ili2db by claeis.

the class TransferFromXtf method readExistingSqlObjIds.

private Long readExistingSqlObjIds(boolean isItf, String bid) throws Ili2dbException {
    StringBuilder topicQName = new StringBuilder();
    Long basketSqlId = Ili2db.getBasketSqlIdFromBID(bid, conn, schema, colT_ID, topicQName);
    if (basketSqlId == null) {
        // new basket
        return null;
    }
    Topic topic = TransferToXtf.getTopicDef(td, topicQName.toString());
    if (topic == null) {
        throw new Ili2dbException("unkown topic " + topicQName.toString());
    }
    Model model = (Model) topic.getContainer();
    // for all Viewables
    Iterator iter = null;
    if (isItf) {
        ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
        iter = itftablev.iterator();
    } else {
        iter = getXtfTables(td, topic).iterator();
    }
    HashSet<String> visitedTables = new HashSet<String>();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Viewable) {
            if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
            // skip it
            } else if (!TransferToXtf.suppressViewable((Viewable) obj)) {
                Viewable aclass = (Viewable) obj;
                if (aclass.isAbstract()) {
                    throw new IllegalArgumentException("unexpected abstract viewable " + aclass.getScopedName(null));
                }
                // get sql name
                DbTableName sqlName = recConv.getSqlType(aclass);
                ViewableWrapper wrapper = recConv.getViewableWrapper(sqlName.getName());
                ViewableWrapper base = wrapper.getExtending();
                while (base != null) {
                    wrapper = base;
                    base = wrapper.getExtending();
                }
                sqlName = wrapper.getSqlTable();
                if (!visitedTables.contains(sqlName.getQName())) {
                    visitedTables.add(sqlName.getQName());
                    // if table exists?
                    if (DbUtility.tableExists(conn, sqlName)) {
                        // dump it
                        EhiLogger.logState(aclass.getScopedName(null) + " read ids...");
                        readObjectSqlIds(!wrapper.includesMultipleTypes(), sqlName, basketSqlId);
                    } else {
                        // skip it
                        EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                    }
                }
            }
        } else if (obj instanceof AttributeDef) {
            if (isItf) {
                AttributeDef attr = (AttributeDef) obj;
                // get sql name
                DbTableName sqlName = getSqlTableNameItfLineTable(attr);
                // if table exists?
                if (DbUtility.tableExists(conn, sqlName)) {
                    // dump it
                    EhiLogger.logState(attr.getContainer().getScopedName(null) + "_" + attr.getName() + " read ids...");
                    readObjectSqlIds(isItf, sqlName, basketSqlId);
                } else {
                    // skip it
                    EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
                }
            }
        }
    }
    return basketSqlId;
}
Also used : Ili2dbException(ch.ehi.ili2db.base.Ili2dbException) ArrayList(java.util.ArrayList) ViewableWrapper(ch.ehi.ili2db.mapping.ViewableWrapper) View(ch.interlis.ili2c.metamodel.View) Model(ch.interlis.ili2c.metamodel.Model) Iterator(java.util.Iterator) Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) Topic(ch.interlis.ili2c.metamodel.Topic) DbTableName(ch.ehi.sqlgen.repository.DbTableName) HashSet(java.util.HashSet)

Aggregations

ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)25 AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)12 Viewable (ch.interlis.ili2c.metamodel.Viewable)11 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)9 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)9 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)9 ArrayList (java.util.ArrayList)9 AreaType (ch.interlis.ili2c.metamodel.AreaType)8 BlackboxType (ch.interlis.ili2c.metamodel.BlackboxType)8 CoordType (ch.interlis.ili2c.metamodel.CoordType)8 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)8 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)8 ReferenceType (ch.interlis.ili2c.metamodel.ReferenceType)8 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)8 Type (ch.interlis.ili2c.metamodel.Type)8 HashSet (java.util.HashSet)8 Iterator (java.util.Iterator)8 DbTableName (ch.ehi.sqlgen.repository.DbTableName)7 IomObject (ch.interlis.iom.IomObject)7 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)5