use of ch.interlis.ili2c.metamodel.View in project ili2db by claeis.
the class TransferFromIli method isTransferableView.
public static boolean isTransferableView(Object modelo) {
if (!(modelo instanceof View)) {
return false;
}
View view = (View) modelo;
Topic parent = (Topic) view.getContainer();
if (!parent.isViewTopic()) {
return false;
}
if (view.isTransient()) {
return false;
}
return true;
}
use of ch.interlis.ili2c.metamodel.View 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
}
}
}
use of ch.interlis.ili2c.metamodel.View 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;
}
use of ch.interlis.ili2c.metamodel.View in project ili2db by claeis.
the class TransferFromXtf method deleteObjectsOfBasket.
private void deleteObjectsOfBasket(long basketSqlId, String topicQName) throws Ili2dbException {
boolean isItf = false;
Topic topic = TransferToXtf.getTopicDef(td, topicQName);
if (topic == null) {
throw new Ili2dbException("unkown topic " + topicQName.toString());
}
Model model = (Model) topic.getContainer();
// for all Viewables
// see also export
Iterator iter = null;
if (isItf) {
ArrayList itftablev = ModelUtilities.getItfTables(td, model.getName(), topic.getName());
iter = itftablev.iterator();
} else {
// get transferable viewables of topic
iter = topic.getViewables().iterator();
}
HashSet<ViewableWrapper> visitedTables = new HashSet<ViewableWrapper>();
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 aclass1 = (Viewable) obj;
ViewableWrapper wrapper = class2wrapper.get(aclass1);
while (wrapper != null) {
{
if (!visitedTables.contains(wrapper)) {
visitedTables.add(wrapper);
// if table exists?
// get sql name
DbTableName sqlName = wrapper.getSqlTable();
if (DbUtility.tableExists(conn, sqlName)) {
// delete it
dropRecords(sqlName, basketSqlId);
} else {
// skip it; no table
}
}
}
for (ViewableWrapper secondary : wrapper.getSecondaryTables()) {
if (!visitedTables.contains(secondary)) {
visitedTables.add(secondary);
// if table exists?
// get sql name
DbTableName sqlName = secondary.getSqlTable();
if (DbUtility.tableExists(conn, sqlName)) {
// delete it
dropRecords(sqlName, basketSqlId);
} else {
// skip it; no table
}
}
}
wrapper = wrapper.getExtending();
}
}
} 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)) {
dropRecords(sqlName, basketSqlId);
} else {
// skip it; no table
}
}
}
}
dropExistingStructEles(topicQName, basketSqlId);
String sqlName = DbNames.IMPORTS_BASKETS_TAB;
if (schema != null) {
sqlName = schema + "." + sqlName;
}
java.sql.PreparedStatement getstmt = null;
try {
String stmt = "DELETE FROM " + sqlName + " WHERE " + DbNames.IMPORTS_BASKETS_TAB_BASKET_COL + "= ?";
EhiLogger.traceBackendCmd(stmt);
getstmt = conn.prepareStatement(stmt);
getstmt.setLong(1, basketSqlId);
getstmt.executeUpdate();
} catch (java.sql.SQLException ex) {
throw new Ili2dbException("failed to delete from " + sqlName, ex);
} finally {
if (getstmt != null) {
try {
getstmt.close();
getstmt = null;
} catch (java.sql.SQLException ex) {
EhiLogger.logError(ex);
}
}
}
}
use of ch.interlis.ili2c.metamodel.View 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;
}
Aggregations