use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.
the class MetaAttrUtility method addMetaAttrsFromDb.
/**
* Read meta-attributes from the db and add them to the ili2c metamodel.
* @param td
* @param conn
* @param schema
* @throws Ili2dbException
*/
public static void addMetaAttrsFromDb(TransferDescription td, Connection conn, String schema) throws Ili2dbException {
String sqlName = DbNames.META_ATTRIBUTES_TAB;
if (schema != null) {
sqlName = schema + "." + sqlName;
}
try {
String stmt = "SELECT " + DbNames.META_ATTRIBUTES_TAB_ILIELEMENT_COL + ", " + DbNames.META_ATTRIBUTES_TAB_ATTRNAME_COL + ", " + DbNames.META_ATTRIBUTES_TAB_ATTRVALUE_COL + " " + "FROM " + sqlName;
EhiLogger.traceBackendCmd(stmt);
Statement dbstmt = conn.createStatement();
ResultSet rs = dbstmt.executeQuery(stmt);
while (rs.next()) {
String ilielement = rs.getString(DbNames.META_ATTRIBUTES_TAB_ILIELEMENT_COL);
String attrname = rs.getString(DbNames.META_ATTRIBUTES_TAB_ATTRNAME_COL);
String attrvalue = rs.getString(DbNames.META_ATTRIBUTES_TAB_ATTRVALUE_COL);
// Add meta-attr to the Element
Element element = td.getElement(ilielement);
// known element?
if (element != null) {
// meta-attr not yet set/defined?
if (element.getMetaValue(attrname) == null) {
// set it to the read value
element.setMetaValue(attrname, attrvalue);
}
}
}
} catch (java.sql.SQLException ex) {
throw new Ili2dbException("failed to read meta-attributes table", ex);
}
}
use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.
the class ReduceToBaseModel method setupTopicTranslation.
private void setupTopicTranslation(Topic srcTopic, Topic baseTopic) {
srctag2destElement.put(srcTopic.getScopedName(), baseTopic);
if (srcTopic == baseTopic) {
// no translation required
return;
}
// for each class of srcTopic, find base in baseTopic
while (srcTopic != null) {
if (srcTopic.isExtending(baseTopic) && srcTopic != baseTopic) {
Iterator<Element> eleIt = srcTopic.iterator();
while (eleIt.hasNext()) {
Element srcEle = eleIt.next();
if (srcEle instanceof AbstractClassDef) {
AbstractClassDef baseEle = (AbstractClassDef) srcEle;
while (baseEle != null) {
if (baseEle.getContainer() == baseTopic) {
break;
}
baseEle = (AbstractClassDef) baseEle.getExtending();
}
if (baseEle != null) {
srctag2destElement.put(srcEle.getScopedName(), baseEle);
if (srcEle != baseEle) {
setupClassTranslation((AbstractClassDef) srcEle, baseEle);
} else {
// no object translation required
// setupClassTranslation((AbstractClassDef) srcEle,baseEle);
}
} else {
// delete object from export
srctag2destElement.put(srcEle.getScopedName(), null);
}
}
}
} else {
// keep elements, as they are (no translation required)
Iterator<Element> eleIt = srcTopic.iterator();
while (eleIt.hasNext()) {
Element srcEle = eleIt.next();
if (srcEle instanceof AbstractClassDef) {
AbstractClassDef baseEle = (AbstractClassDef) srcEle;
srctag2destElement.put(srcEle.getScopedName(), baseEle);
// no object translation required
// setupClassTranslation((AbstractClassDef) srcEle,baseEle);
}
}
}
srcTopic = (Topic) srcTopic.getExtending();
}
}
use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.
the class ReduceToBaseModel method setupClassTranslation.
private void setupClassTranslation(AbstractClassDef srcEle, AbstractClassDef baseEle) {
HashMap<String, ViewableTransferElement> baseAttrs = new HashMap<String, ViewableTransferElement>();
Iterator<ViewableTransferElement> basePropIt = baseEle.getAttributesAndRoles2();
while (basePropIt.hasNext()) {
ViewableTransferElement baseProp = basePropIt.next();
String basePropName = ((Element) baseProp.obj).getName();
baseAttrs.put(basePropName, baseProp);
}
Iterator<ViewableTransferElement> srcPropIt = srcEle.getAttributesAndRoles2();
while (srcPropIt.hasNext()) {
ViewableTransferElement srcProp = srcPropIt.next();
String srcPropName = ((Element) srcProp.obj).getName();
if (baseAttrs.containsKey(srcPropName)) {
// attribute exists in base class
srctag2destElement.put(((Element) srcProp.obj).getScopedName(), baseAttrs.get(srcPropName));
} else {
// attribute doesn't exist in base class
// remove it from export
srctag2destElement.put(((Element) srcProp.obj).getScopedName(), null);
}
}
}
use of ch.interlis.ili2c.metamodel.Element in project ili2db by claeis.
the class Viewable2TableMapper method doit.
private Viewable2TableMapping doit(List<Element> eles) {
//
if (Config.INHERITANCE_TRAFO_SMART1.equals(config.getInheritanceTrafo())) {
doSmart1(eles);
} else if (Config.INHERITANCE_TRAFO_SMART2.equals(config.getInheritanceTrafo())) {
doSmart2(eles);
} else {
doSmartOff(eles);
}
//
// create ViewableWrappers for all NewClass or NewAndSubClass tagged viewables
//
Viewable2TableMapping ret = new Viewable2TableMapping();
for (Element ele : eles) {
if (!(ele instanceof Viewable)) {
// not a Viewable; skip it
} else {
// a Viewable
Viewable aclass = (Viewable) ele;
String inheritanceStrategy = trafoConfig.getViewableConfig(aclass, TrafoConfigNames.INHERITANCE_TRAFO);
if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(inheritanceStrategy) || TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(inheritanceStrategy)) {
String sqlTablename = nameMapping.mapIliClassDef(aclass);
ViewableWrapper wrapper = new ViewableWrapper(sqlSchemaname, sqlTablename, aclass);
List<ViewableTransferElement> props = new java.util.ArrayList<ViewableTransferElement>();
// defined attrs
{
addProps(wrapper, props, aclass.getDefinedAttributesAndRoles2());
}
// defined attrs of bases with subclass or newAndSubclass strategy
{
Viewable base = (Viewable) aclass.getExtending();
while (base != null) {
String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
if (!TrafoConfigNames.INHERITANCE_TRAFO_SUBCLASS.equals(baseInheritanceStrategy) && !TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(baseInheritanceStrategy)) {
break;
}
addProps(wrapper, props, base.getDefinedAttributesAndRoles2());
base = (Viewable) base.getExtending();
}
}
// add attrs of extensions with superclass strategy while visiting extensions
wrapper.setAttrv(props);
// link to base ViewableWrapper
{
Viewable base = (Viewable) aclass.getExtending();
while (base != null) {
String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(baseInheritanceStrategy)) {
// but NOT INHERITANCE_TRAFO_NEWANDSUBCLASS!
break;
}
base = (Viewable) base.getExtending();
}
if (base != null) {
ViewableWrapper baseWrapper = ret.get(base);
if (baseWrapper != wrapper) {
wrapper.setExtending(baseWrapper);
}
}
}
// includes more than one type
if (wrapper.getExtending() != null) {
// base contains type typediscriminator
wrapper.setMultipleTypes(false);
} else {
// if a concrete base
if (hasAnyConcreteBaseWithSubClass(trafoConfig, aclass)) {
wrapper.setMultipleTypes(true);
} else if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(inheritanceStrategy) && hasAnyConreteExtension(aclass)) {
// newClass and any concrete extensions
wrapper.setMultipleTypes(true);
} else if (TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(inheritanceStrategy) && hasAnyConreteExtensionWithoutNewClass(trafoConfig, aclass)) {
// newAndSubClass and any concrete extensions without newClass or newAndSubClass
wrapper.setMultipleTypes(true);
} else {
wrapper.setMultipleTypes(false);
}
aclass.getDirectExtensions();
}
ret.add(aclass, wrapper);
} else if (TrafoConfigNames.INHERITANCE_TRAFO_SUPERCLASS.equals(inheritanceStrategy)) {
// add props of extensions with superclass strategy to base-class
// find base
Viewable base = (Viewable) aclass.getExtending();
while (base != null) {
String baseInheritanceStrategy = trafoConfig.getViewableConfig(base, TrafoConfigNames.INHERITANCE_TRAFO);
if (TrafoConfigNames.INHERITANCE_TRAFO_NEWCLASS.equals(baseInheritanceStrategy) || TrafoConfigNames.INHERITANCE_TRAFO_NEWANDSUBCLASS.equals(baseInheritanceStrategy)) {
break;
}
base = (Viewable) base.getExtending();
}
ViewableWrapper wrapper = ret.get(base);
List<ViewableTransferElement> props = wrapper.getAttrv();
// add props of extension
addProps(wrapper, props, aclass.getDefinedAttributesAndRoles2());
wrapper.setAttrv(props);
ret.add(aclass, wrapper);
} else if (TrafoConfigNames.INHERITANCE_TRAFO_SUBCLASS.equals(inheritanceStrategy)) {
// skip it; props already added when visiting subclass
} else {
throw new IllegalStateException("unexpected inheritance config <" + inheritanceStrategy + ">");
}
}
}
return ret;
}
use of ch.interlis.ili2c.metamodel.Element 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