use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class TransferToXtf method doBasket.
private boolean doBasket(String filename, IoxWriter iomFile, Topic topic, Long basketSqlId, String basketXtfId) throws IoxException {
Model model = (Model) topic.getContainer();
boolean referrs = false;
StartBasketEvent iomBasket = null;
delayedObjects = new ArrayList<FixIomObjectRefs>();
// for all Viewables
Iterator iter = null;
if (iomFile instanceof ItfWriter) {
ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
iter = itftablev.iterator();
} else {
iter = topic.getViewables().iterator();
}
while (iter.hasNext()) {
Object obj = iter.next();
if (obj instanceof Viewable) {
if ((obj instanceof View) && !TransferFromIli.isTransferableView(obj)) {
// skip it
} else if (!suppressViewable((Viewable) obj)) {
Viewable aclass = (Viewable) obj;
if (languageFilter != null) {
aclass = (Viewable) aclass.getTranslationOfOrSame();
}
ViewableWrapper wrapper = class2wrapper.get(aclass);
// get sql name
DbTableName sqlName = recConv.getSqlType(wrapper.getViewable());
// if table exists?
if (DbUtility.tableExists(conn, sqlName)) {
// dump it
EhiLogger.logState(aclass.getScopedName(null) + "...");
if (iomBasket == null) {
iomBasket = new StartBasketEvent(topic.getScopedName(null), basketXtfId);
if (languageFilter != null) {
iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
}
if (exportBaseModelFilter != null) {
iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
}
if (validator != null)
validator.validate(iomBasket);
iomFile.write(iomBasket);
}
dumpObject(iomFile, aclass, basketSqlId);
} else {
// skip it
EhiLogger.traceUnusualState(aclass.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
}
}
} else if (obj instanceof AttributeDef) {
if (iomFile instanceof ItfWriter) {
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() + "...");
if (iomBasket == null) {
iomBasket = new StartBasketEvent(topic.getScopedName(null), topic.getScopedName(null));
if (languageFilter != null) {
iomBasket = (StartBasketEvent) languageFilter.filter(iomBasket);
}
if (exportBaseModelFilter != null) {
iomBasket = (StartBasketEvent) exportBaseModelFilter.filter(iomBasket);
}
if (validator != null)
validator.validate(iomBasket);
iomFile.write(iomBasket);
}
dumpItfTableObject(iomFile, attr, basketSqlId);
} else {
// skip it
EhiLogger.traceUnusualState(attr.getScopedName(null) + "...skipped; no table " + sqlName + " in db");
}
}
}
}
if (iomBasket != null) {
// fix forward references
for (FixIomObjectRefs fixref : delayedObjects) {
boolean skipObj = false;
for (IomObject ref : fixref.getRefs()) {
long sqlid = fixref.getTargetSqlid(ref);
if (sqlidPool.containsSqlid(sqlid)) {
// fix it
ref.setobjectrefoid(sqlidPool.getXtfid(sqlid));
} else {
// object in another basket
Viewable aclass = fixref.getTargetClass(ref);
// read object
String tid = readObjectTid(aclass, sqlid);
if (tid == null) {
EhiLogger.logError("unknown referenced object " + aclass.getScopedName(null) + " sqlid " + fixref.getTargetSqlid(ref) + " referenced from " + fixref.getRoot().getobjecttag() + " TID " + fixref.getRoot().getobjectoid());
referrs = true;
skipObj = true;
} else {
// fix reference
ref.setobjectrefoid(tid);
}
}
}
if (!skipObj) {
ObjectEvent objEvent = new ObjectEvent(fixref.getRoot());
if (languageFilter != null) {
objEvent = (ObjectEvent) languageFilter.filter(objEvent);
}
if (exportBaseModelFilter != null) {
objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
}
if (objEvent != null) {
if (validator != null)
validator.validate(objEvent);
iomFile.write(objEvent);
}
}
}
EndBasketEvent endBasket = new EndBasketEvent();
if (languageFilter != null) {
endBasket = (EndBasketEvent) languageFilter.filter(endBasket);
}
if (exportBaseModelFilter != null) {
endBasket = (EndBasketEvent) exportBaseModelFilter.filter(endBasket);
}
if (validator != null)
validator.validate(endBasket);
iomFile.write(endBasket);
saveObjStat(iomBasket.getBid(), basketSqlId, filename, iomBasket.getType());
}
return referrs;
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class TransferToXtf method dumpItfTableObject.
private void dumpItfTableObject(IoxWriter out, AttributeDef attr, Long basketSqlId) {
String stmt = createItfLineTableQueryStmt(attr, basketSqlId, geomConv);
String sqlTabName = ili2sqlName.mapGeometryAsTable(attr);
EhiLogger.traceBackendCmd(stmt);
SurfaceOrAreaType type = (SurfaceOrAreaType) attr.getDomainResolvingAliases();
String geomAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableGeomAttrName(attr);
String refAttrName = null;
if (type instanceof SurfaceType) {
refAttrName = ch.interlis.iom_j.itf.ModelUtilities.getHelperTableMainTableRef(attr);
}
java.sql.PreparedStatement dbstmt = null;
try {
dbstmt = conn.prepareStatement(stmt);
dbstmt.clearParameters();
int paramIdx = 1;
if (basketSqlId != null) {
dbstmt.setLong(paramIdx++, basketSqlId);
}
java.sql.ResultSet rs = dbstmt.executeQuery();
while (rs.next()) {
int valuei = 1;
long sqlid = rs.getLong(valuei);
valuei++;
String sqlIliTid = null;
if (writeIliTid) {
sqlIliTid = rs.getString(valuei);
valuei++;
} else {
sqlIliTid = Long.toString(sqlid);
}
Viewable aclass = (Viewable) attr.getContainer();
Iom_jObject iomObj;
iomObj = new Iom_jObject(aclass.getScopedName(null) + "_" + attr.getName(), sqlIliTid);
// geomAttr
Object geomobj = rs.getObject(valuei);
valuei++;
if (!rs.wasNull()) {
try {
boolean is3D = false;
IomObject polyline = geomConv.toIomPolyline(geomobj, ili2sqlName.getSqlColNameItfLineTableGeomAttr(attr, sqlTabName), is3D);
iomObj.addattrobj(geomAttrName, polyline);
} catch (ConverterException ex) {
EhiLogger.logError("Object " + sqlid + ": failed to convert polyline", ex);
}
}
// is of type SURFACE?
if (type instanceof SurfaceType) {
// -> mainTable
IomObject ref = iomObj.addattrobj(refAttrName, "REF");
long refSqlId = rs.getLong(valuei);
if (sqlidPool.containsSqlid(refSqlId)) {
String refTid = sqlidPool.getXtfid(refSqlId);
ref.setobjectrefoid(refTid);
} else {
EhiLogger.logError("unknown referenced object " + attr.getContainer().getScopedName(null) + " sqlid " + refSqlId + " referenced from " + sqlTabName + " " + colT_ID + " " + sqlid);
}
valuei++;
}
Table lineAttrTable = type.getLineAttributeStructure();
if (lineAttrTable != null) {
Iterator attri = lineAttrTable.getAttributes();
while (attri.hasNext()) {
AttributeDef lineattr = (AttributeDef) attri.next();
valuei = recConv.addAttrValue(rs, valuei, sqlid, iomObj, lineattr, null, class2wrapper.get(lineAttrTable), null);
}
}
if (out != null) {
// write object
ObjectEvent objEvent = new ObjectEvent(iomObj);
if (languageFilter != null) {
objEvent = (ObjectEvent) languageFilter.filter(objEvent);
}
if (exportBaseModelFilter != null) {
objEvent = (ObjectEvent) exportBaseModelFilter.filter(objEvent);
}
if (validator != null)
validator.validate(objEvent);
out.write(objEvent);
}
}
} catch (java.sql.SQLException ex) {
EhiLogger.logError("failed to query " + attr.getScopedName(null), ex);
} catch (ch.interlis.iox.IoxException ex) {
EhiLogger.logError("failed to write " + attr.getScopedName(null), ex);
} finally {
if (dbstmt != null) {
try {
dbstmt.close();
} catch (java.sql.SQLException ex) {
EhiLogger.logError("failed to close query of " + attr.getScopedName(null), ex);
}
}
}
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class ToXtfRecordConverter method convertRecord.
public Iom_jObject convertRecord(java.sql.ResultSet rs, Viewable aclass1, FixIomObjectRefs fixref, StructWrapper structWrapper, HashMap structelev, ArrayList<StructWrapper> structQueue, long sqlid) throws SQLException {
ViewableWrapper aclass = class2wrapper.get(aclass1);
Iom_jObject iomObj;
int valuei = 1;
valuei++;
if (createTypeDiscriminator || aclass.includesMultipleTypes()) {
// String t_type=rs.getString(valuei);
valuei++;
}
String sqlIliTid = null;
if (structWrapper == null) {
if (!aclass.isStructure()) {
if (createIliTidCol || aclass.getOid() != null) {
sqlIliTid = rs.getString(valuei);
sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
valuei++;
} else {
sqlIliTid = Long.toString(sqlid);
sqlid2xtfid.putSqlid2Xtfid(sqlid, sqlIliTid);
}
}
}
if (structWrapper == null) {
if (!aclass.isStructure()) {
iomObj = new Iom_jObject(aclass1.getScopedName(null), sqlIliTid);
} else {
iomObj = new Iom_jObject(aclass1.getScopedName(null), null);
}
iomObj.setattrvalue(ItfWriter2.INTERNAL_T_ID, Long.toString(sqlid));
fixref.setRoot(iomObj);
} else {
iomObj = (Iom_jObject) structelev.get(Long.toString(sqlid));
if (createGenericStructRef) {
valuei += 4;
} else {
valuei += 2;
}
}
HashSet<AttributeDef> visitedAttrs = new HashSet<AttributeDef>();
for (ViewableWrapper table : aclass.getWrappers()) {
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 {
valuei = addAttrValue(rs, valuei, sqlid, iomObj, baseAttr, structQueue, table, fixref);
}
}
}
}
if (obj.obj instanceof RoleDef) {
RoleDef role = (RoleDef) obj.obj;
if (role.getExtending() == null) {
String roleName = role.getName();
ArrayList<ViewableWrapper> targetTables = getTargetTables(role.getDestination());
boolean refAlreadyDefined = false;
for (ViewableWrapper targetTable : targetTables) {
String sqlRoleName = ili2sqlName.mapIliRoleDef(role, getSqlType(table.getViewable()).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){
long value = rs.getLong(valuei);
valuei++;
if (!rs.wasNull()) {
if (refAlreadyDefined) {
EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
} else {
IomObject ref = iomObj.addattrobj(roleName, roleOwner.getScopedName(null));
mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
refAlreadyDefined = true;
}
}
}
} else {
// TODO if(orderPos!=0){
long value = rs.getLong(valuei);
valuei++;
if (!rs.wasNull()) {
if (refAlreadyDefined) {
EhiLogger.logAdaption("Table " + table.getSqlTablename() + "(id " + sqlid + ") more than one value for role " + roleName + "; value of " + sqlRoleName + " ignored");
} else {
IomObject ref = iomObj.addattrobj(roleName, "REF");
mapSqlid2Xtfid(fixref, value, ref, role.getDestination());
refAlreadyDefined = true;
}
}
}
}
}
}
}
}
return iomObj;
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class ReduceToBaseModel method translateAttrValue.
private void translateAttrValue(IomObject iomObj, AttributeDef srcAttr) {
String srcAttrName = srcAttr.getName();
int attrc = iomObj.getattrvaluecount(srcAttrName);
if (attrc == 0) {
return;
}
boolean isCompType = srcAttr.getDomain() instanceof CompositionType ? true : false;
boolean isEnumType = srcAttr.getDomainResolvingAliases() instanceof EnumerationType ? true : false;
EnumerationType srcEnumType = null;
if (isEnumType) {
srcEnumType = (EnumerationType) srcAttr.getDomainResolvingAliases();
}
AttributeDef destAttr = (AttributeDef) ((ViewableTransferElement) srctag2destElement.get(srcAttr.getScopedName())).obj;
for (int attri = 0; attri < attrc; attri++) {
String attrValue = iomObj.getattrprim(srcAttrName, attri);
if (attrValue != null) {
if (isEnumType) {
attrValue = translateEnumValue((String) attrValue, srcEnumType, (EnumerationType) destAttr.getDomainResolvingAliases());
iomObj.setattrvalue(srcAttrName, (String) attrValue);
}
} else {
IomObject structValue = iomObj.getattrobj(srcAttrName, attri);
if (isCompType) {
translateObject(structValue);
}
}
}
}
use of ch.interlis.ili2c.metamodel.AttributeDef in project ili2db by claeis.
the class ReduceToBaseModel method translateObject.
private void translateObject(IomObject iomObj) {
Element modelElement = (Element) tag2class.get(iomObj.getobjecttag());
Element destModelEle = getTranslatedElement(modelElement);
if (destModelEle == modelElement) {
// no translation required
return;
}
Viewable aclass = (Viewable) modelElement;
Viewable destClass = (Viewable) destModelEle;
String destName = destClass.getScopedName();
iomObj.setobjecttag(destName);
// handle attrs
Iterator iter = aclass.getAttributesAndRoles2();
while (iter.hasNext()) {
ViewableTransferElement srcProp = (ViewableTransferElement) iter.next();
ViewableTransferElement destProp = (ViewableTransferElement) srctag2destElement.get(((Element) srcProp.obj).getScopedName());
if (destProp == null) {
iomObj.setattrundefined(((Element) srcProp.obj).getName());
} else {
if (srcProp.obj instanceof AttributeDef) {
AttributeDef attr = (AttributeDef) srcProp.obj;
if (!attr.isTransient()) {
Type proxyType = attr.getDomain();
if (proxyType != null && (proxyType instanceof ObjectType)) {
// skip implicit particles (base-viewables) of views
} else {
translateAttrValue(iomObj, attr);
}
}
}
}
}
}
Aggregations