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();
}
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 + ">");
}
}
}
}
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;
}
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();
}
}
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);
}
}
}
}
}
}
Aggregations