use of ch.interlis.ili2c.metamodel.Model in project ili2db by claeis.
the class TransferFromIli method setupTopicTagMap.
/**
* setup a mapping from a qualified model or topic name
* to the corresponding java object.
*/
private HashMap setupTopicTagMap(TransferDescription td) {
HashMap ret = new HashMap();
Iterator modeli = td.iterator();
while (modeli.hasNext()) {
Object mObj = modeli.next();
if (mObj instanceof Model) {
Model model = (Model) mObj;
ret.put(model.getScopedName(null), model);
Iterator topici = model.iterator();
while (topici.hasNext()) {
Object tObj = topici.next();
if (tObj instanceof Topic) {
Topic topic = (Topic) tObj;
ret.put(topic.getScopedName(null), topic);
}
}
}
}
return ret;
}
use of ch.interlis.ili2c.metamodel.Model 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.Model 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.Model in project ili2db by claeis.
the class ModelElementSelector method visitTopic.
private void visitTopic(HashSet<Element> visitedElements, HashSet<Model> accuScope, Topic def) {
if (visitedElements.contains(def)) {
return;
}
visitedElements.add(def);
Iterator classi = def.iterator();
while (classi.hasNext()) {
Object classo = classi.next();
if (classo instanceof Viewable) {
visitViewable(visitedElements, accuScope, (Viewable) classo);
} else if (classo instanceof Domain) {
visitDomain(visitedElements, accuScope, (Domain) classo);
}
}
// base topic
Topic base = (Topic) def.getExtending();
if (base != null) {
visitTopic(visitedElements, accuScope, base);
}
// depends on
Iterator depi = def.getDependentOn();
while (depi.hasNext()) {
Topic dep = (Topic) depi.next();
visitTopic(visitedElements, accuScope, dep);
}
// parent model
Model model = (Model) def.getContainer(Model.class);
if (!accuScope.contains(model)) {
accuScope.add(model);
}
}
use of ch.interlis.ili2c.metamodel.Model in project ili2db by claeis.
the class ModelElementSelector method getModelElements.
public List<Element> getModelElements(List<String> modelNames, TransferDescription td, boolean createItfLineTables, boolean includeEnums, Config config) {
this.td = td;
this.createItfLineTables = createItfLineTables;
this.includeEnums = includeEnums;
List<Model> models = new ArrayList<Model>();
if (modelNames == null || modelNames.isEmpty()) {
Model lastModel = td.getLastModel();
models.add(lastModel);
} else {
for (String modelName : modelNames) {
Model model = (Model) td.getElement(Model.class, modelName);
if (model == null) {
throw new IllegalArgumentException("unknown model <" + modelName + ">");
}
models.add(model);
}
}
HashSet<Element> accu = new HashSet<Element>();
HashSet<Model> accuScope = new HashSet<Model>();
for (Model model : models) {
if (config.getVer4_translation() || config.getIli1Translation() != null) {
visitModel(accu, accuScope, (Model) model.getTranslationOfOrSame());
} else {
visitModel(accu, accuScope, model);
}
}
visitStructsInScope(accu, accuScope, config);
ArrayList<Element> ret = new ArrayList<Element>(accu);
java.util.Collections.sort(ret, new java.util.Comparator<Element>() {
@Override
public int compare(Element arg0, Element arg1) {
if (arg0.getDefidx() < arg1.getDefidx()) {
return -1;
}
if (arg0.getDefidx() > arg1.getDefidx()) {
return 1;
}
return 0;
}
});
return ret;
}
Aggregations