Search in sources :

Example 16 with AttributeDef

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

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

the class TransferFromXtf method deleteExisitingObjects.

private void deleteExisitingObjects(HashMap<String, HashSet<Long>> existingObjects) {
    for (String sqlType : existingObjects.keySet()) {
        HashSet<Long> objs = existingObjects.get(sqlType);
        StringBuilder ids = new StringBuilder();
        String sep = "";
        if (objs.size() > 0) {
            for (Long sqlId : objs) {
                ids.append(sep);
                ids.append(sqlId);
                sep = ",";
            }
            Object classo = tag2class.get(ili2sqlName.mapSqlTableName(sqlType));
            if (classo instanceof Viewable) {
                Viewable aclass = (Viewable) classo;
                while (aclass != null) {
                    deleteExistingObjectsHelper(recConv.getSqlType(aclass), ids.toString());
                    aclass = (Viewable) aclass.getExtending();
                }
            } else if (classo instanceof AttributeDef) {
                deleteExistingObjectsHelper(getSqlTableNameItfLineTable((AttributeDef) classo), ids.toString());
            } else {
                throw new IllegalStateException("unexpetced sqlType <" + sqlType + ">");
            }
        }
    }
}
Also used : Viewable(ch.interlis.ili2c.metamodel.Viewable) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 18 with AttributeDef

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

the class TransferFromXtf method allReferencesKnown.

private boolean allReferencesKnown(long basketSqlId, IomObject iomObj) {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        return true;
    }
    // is it a SURFACE or AREA line table?
    if (createItfLineTables && modelele instanceof AttributeDef) {
        return true;
    }
    String tid = iomObj.getobjectoid();
    if ((tid == null || tid.length() == 0) && modelele instanceof AssociationDef) {
        tid = getAssociationId(iomObj, (AssociationDef) modelele);
    }
    if (tid != null && tid.length() > 0) {
        oidPool.createObjSqlId(Ili2cUtility.getRootViewable((Viewable) modelele).getScopedName(null), tag, tid);
    }
    FixIomObjectExtRefs extref = new FixIomObjectExtRefs(basketSqlId, tag, tid);
    allReferencesKnownHelper(iomObj, extref);
    if (!extref.needsFixing()) {
        return true;
    }
    // EhiLogger.debug("needs fixing "+iomObj.getobjectoid());
    delayedObjects.add(extref);
    objPool.put(tid, iomObj);
    return false;
}
Also used : AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Example 19 with AttributeDef

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

the class TransferFromXtf method writeItfLineTableObject.

private void writeItfLineTableObject(String datasetName, long basketSqlId, IomObject iomObj, AttributeDef attrDef) throws java.sql.SQLException, ConverterException {
    SurfaceOrAreaType type = (SurfaceOrAreaType) attrDef.getDomainResolvingAliases();
    String geomAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableGeomAttrName(attrDef);
    String refAttrName = null;
    if (type instanceof SurfaceType) {
        refAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableMainTableRef(attrDef);
    }
    Table lineAttrTable = type.getLineAttributeStructure();
    // map oid of transfer file to a sql id
    String idTag = attrDef.getContainer().getScopedName(null) + "." + attrDef.getName();
    long sqlId = oidPool.createObjSqlId(idTag, idTag, iomObj.getobjectoid());
    String sqlTableName = getSqlTableNameItfLineTable(attrDef).getQName();
    String insert = createItfLineTableInsertStmt(attrDef);
    EhiLogger.traceBackendCmd(insert);
    PreparedStatement ps = conn.prepareStatement(insert);
    try {
        int valuei = 1;
        ps.setLong(valuei, sqlId);
        valuei++;
        if (createBasketCol) {
            ps.setLong(valuei, basketSqlId);
            valuei++;
        }
        if (createDatasetCol) {
            ps.setString(valuei, datasetName);
            valuei++;
        }
        if (readIliTid) {
            // import TID from transfer file
            ps.setString(valuei, iomObj.getobjectoid());
            valuei++;
        }
        IomObject value = iomObj.getattrobj(geomAttrName, 0);
        if (value != null) {
            boolean is3D = ((CoordType) (type).getControlPointDomain().getType()).getDimensions().length == 3;
            ps.setObject(valuei, geomConv.fromIomPolyline(value, recConv.getSrsid(attrDef), is3D, recConv.getP(type)));
        } else {
            geomConv.setPolylineNull(ps, valuei);
        }
        valuei++;
        if (type instanceof SurfaceType) {
            IomObject structvalue = iomObj.getattrobj(refAttrName, 0);
            String refoid = structvalue.getobjectrefoid();
            long refsqlId = oidPool.getObjSqlId(attrDef.getContainer().getScopedName(null), refoid);
            ps.setLong(valuei, refsqlId);
            valuei++;
        }
        if (lineAttrTable != null) {
            Iterator attri = lineAttrTable.getAttributes();
            while (attri.hasNext()) {
                AttributeDef lineattr = (AttributeDef) attri.next();
                valuei = recConv.addAttrValue(iomObj, ili2sqlName.mapGeometryAsTable(attrDef), sqlId, sqlTableName, ps, valuei, lineattr, null);
            }
        }
        if (createStdCols) {
            // T_LastChange
            ps.setTimestamp(valuei, today);
            valuei++;
            // T_CreateDate
            ps.setTimestamp(valuei, today);
            valuei++;
            // T_User
            ps.setString(valuei, dbusr);
            valuei++;
        }
        ps.executeUpdate();
    } finally {
        ps.close();
    }
}
Also used : Table(ch.interlis.ili2c.metamodel.Table) IomObject(ch.interlis.iom.IomObject) SurfaceOrAreaType(ch.interlis.ili2c.metamodel.SurfaceOrAreaType) Iterator(java.util.Iterator) PreparedStatement(java.sql.PreparedStatement) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef) SurfaceType(ch.interlis.ili2c.metamodel.SurfaceType) CoordType(ch.interlis.ili2c.metamodel.CoordType)

Example 20 with AttributeDef

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

the class TransferFromXtf method allReferencesKnownHelper.

private void allReferencesKnownHelper(IomObject iomObj, FixIomObjectExtRefs extref) {
    String tag = iomObj.getobjecttag();
    // EhiLogger.debug("tag "+tag);
    Object modelele = tag2class.get(tag);
    if (modelele == null) {
        return;
    }
    // ASSERT: an ordinary class/table
    Viewable aclass = (Viewable) modelele;
    Iterator iter = aclass.getAttributesAndRoles2();
    while (iter.hasNext()) {
        ViewableTransferElement obj = (ViewableTransferElement) iter.next();
        if (obj.obj instanceof AttributeDef) {
            AttributeDef attr = (AttributeDef) obj.obj;
            if (!attr.isTransient()) {
                Type proxyType = attr.getDomain();
                if (proxyType != null && (proxyType instanceof ObjectType)) {
                // skip implicit particles (base-viewables) of views
                } else {
                    allReferencesKnownHelper(iomObj, attr, extref);
                }
            }
        }
        if (obj.obj instanceof RoleDef) {
            RoleDef role = (RoleDef) obj.obj;
            if (role.getExtending() == null) {
                String roleName = role.getName();
                // a role of an embedded association?
                if (obj.embedded) {
                    AssociationDef roleOwner = (AssociationDef) role.getContainer();
                    if (roleOwner.getDerivedFrom() == null) {
                        // not just a link?
                        IomObject structvalue = iomObj.getattrobj(roleName, 0);
                        if (roleOwner.getAttributes().hasNext() || roleOwner.getLightweightAssociations().iterator().hasNext()) {
                        // TODO handle attributes of link
                        }
                        if (structvalue != null) {
                            String refoid = structvalue.getobjectrefoid();
                            Viewable targetClass = role.getDestination();
                            if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                                extref.addFix(structvalue, targetClass);
                            }
                        }
                    }
                } else {
                    IomObject structvalue = iomObj.getattrobj(roleName, 0);
                    String refoid = structvalue.getobjectrefoid();
                    Viewable targetClass = role.getDestination();
                    if (!oidPool.containsXtfid(Ili2cUtility.getRootViewable(targetClass).getScopedName(null), refoid)) {
                        extref.addFix(structvalue, targetClass);
                    }
                }
            }
        }
    }
}
Also used : ViewableTransferElement(ch.interlis.ili2c.metamodel.ViewableTransferElement) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) 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) ObjectType(ch.interlis.ili2c.metamodel.ObjectType) CoordType(ch.interlis.ili2c.metamodel.CoordType) IomObject(ch.interlis.iom.IomObject) RoleDef(ch.interlis.ili2c.metamodel.RoleDef) Viewable(ch.interlis.ili2c.metamodel.Viewable) Iterator(java.util.Iterator) AssociationDef(ch.interlis.ili2c.metamodel.AssociationDef) IomObject(ch.interlis.iom.IomObject) AttributeDef(ch.interlis.ili2c.metamodel.AttributeDef)

Aggregations

AttributeDef (ch.interlis.ili2c.metamodel.AttributeDef)41 Iterator (java.util.Iterator)25 CompositionType (ch.interlis.ili2c.metamodel.CompositionType)22 SurfaceOrAreaType (ch.interlis.ili2c.metamodel.SurfaceOrAreaType)21 SurfaceType (ch.interlis.ili2c.metamodel.SurfaceType)21 EnumerationType (ch.interlis.ili2c.metamodel.EnumerationType)19 Type (ch.interlis.ili2c.metamodel.Type)18 Table (ch.interlis.ili2c.metamodel.Table)16 CoordType (ch.interlis.ili2c.metamodel.CoordType)15 IomObject (ch.interlis.iom.IomObject)15 Viewable (ch.interlis.ili2c.metamodel.Viewable)14 ViewableTransferElement (ch.interlis.ili2c.metamodel.ViewableTransferElement)14 PolylineType (ch.interlis.ili2c.metamodel.PolylineType)13 HashSet (java.util.HashSet)13 ViewableWrapper (ch.ehi.ili2db.mapping.ViewableWrapper)12 ObjectType (ch.interlis.ili2c.metamodel.ObjectType)12 AssociationDef (ch.interlis.ili2c.metamodel.AssociationDef)11 ReferenceType (ch.interlis.ili2c.metamodel.ReferenceType)11 RoleDef (ch.interlis.ili2c.metamodel.RoleDef)10 DbTableName (ch.ehi.sqlgen.repository.DbTableName)9