Skip to content

Commit

Permalink
add meta.xtf export
Browse files Browse the repository at this point in the history
  • Loading branch information
edigonzales committed Jul 1, 2023
1 parent 000c40a commit 207f214
Show file tree
Hide file tree
Showing 15 changed files with 531 additions and 154 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
testImplementation 'org.apache.hadoop:hadoop-client:3.3.5'
testImplementation 'org.apache.parquet:parquet-avro:1.13.1'


}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/ch/so/agi/csv2parquet/IoxWkfConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ private IoxWkfConfig() {}

// "Human readable", damit das config.ini file von Menschen verwendet
// werden kann.

public final static String INI_SECTION_PARAMETER = "PARAMETER";
public final static String INI_FIRSTLINE_IS_HEADER = "firstLineIsHeader";
public final static String INI_VALUEDELIMITER = "valueDelimiter";
public final static String INI_VALUESEPARATOR = "valueSeparator";
public final static String INI_ENCODING = "encoding";
public final static String INI_MODELS = "models";
public final static String SETTING_TRANSFERDESCRIPTION = "transferDescription";
public final static String INI_META_TITLE = "title";
public final static String INI_META_DESCRIPTION = "description";
// public final static String SETTING_TRANSFERDESCRIPTION = "transferDescription";
// public final static String INI_META_TITLE = "title";
// public final static String INI_META_DESCRIPTION = "description";
// public final static String INI_META_PARENT_TITLE = "parentTitle";
// public final static String INI_META_PARENT_DESCRIPTION = "parentDescription";

}
28 changes: 0 additions & 28 deletions src/main/java/ch/so/agi/csv2parquet/Meta2Xtf.java

This file was deleted.

67 changes: 41 additions & 26 deletions src/main/java/ch/so/agi/csv2parquet/SettingsMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import org.interlis2.validator.Validator;
Expand All @@ -25,40 +26,54 @@ public static Settings run(File configFile, String identifier) throws IOExceptio
Settings settings = new Settings();

TomlParseResult tomlContent = Toml.parse(configFile.toPath());

Map<String,Object> tomlMap = tomlContent.toMap();

// Falls kein Identifier gesetzt wurde, wird das ganze Toml-File
// durchsucht und der erste Match verwendet.
if (identifier == null) {
setSettingsFromTomlTable(tomlContent, settings);
} else {
String parentIdentifier = identifier.substring(0, identifier.lastIndexOf("."));
TomlTable parentTable = tomlContent.getTable(parentIdentifier);
TomlTable parentTable = (TomlTable) tomlMap.get(parentIdentifier);

TomlTable resourceTable = tomlContent.getTable(identifier);
TomlTable resourceTable = (TomlTable) tomlMap.get(identifier);
if (resourceTable != null) {
setSettingsFromTomlTable(resourceTable, settings);
}

// TODO wird erst interessant, wenn wir neben tool-config-Daten auch Metadatenauslesen.

// In der Methode wird also immer der identifier (oder falls null irgendeins) verwendet.
// Falls ein parentTable vorhanden ist, muss man diesen noch behandeln. Gewisse Teile
// werden überschrieben? / verschoben?
if (parentTable != null) {
// Theme title von parent
// ...
} else {
// Theme title = resource title
// ...
}

// // TODO wird erst interessant, wenn wir neben tool-config-Daten auch Metadatenauslesen.
//
// // In der Methode wird also immer der identifier (oder falls null irgendeins) verwendet.
// // Falls ein parentTable vorhanden ist, muss man diesen noch behandeln. Gewisse Teile
// // werden überschrieben? / verschoben?
// if (parentTable != null) {
// // Theme title von parent
// // ...
// String title = parentTable.getString(IoxWkfConfig.INI_META_TITLE);
// if (title != null) {
// settings.setValue(IoxWkfConfig.INI_META_PARENT_TITLE, title);
// }
//
// String description = parentTable.getString(IoxWkfConfig.INI_META_DESCRIPTION);
// if (description != null) {
// settings.setValue(IoxWkfConfig.INI_META_PARENT_DESCRIPTION, description);
// }
// }
}

if (settings.getValue(Validator.SETTING_MODELNAMES) != null) {
TransferDescription td = getTransferDescriptionFromModelName(settings.getValue(Validator.SETTING_MODELNAMES), configFile.getParentFile().toPath());
settings.setTransientObject(IoxWkfConfig.SETTING_TRANSFERDESCRIPTION, td);
}
// if (settings.getValue(Validator.SETTING_MODELNAMES) != null) {
// TransferDescription td = getTransferDescriptionFromModelName(settings.getValue(Validator.SETTING_MODELNAMES), configFile.getParentFile().toPath());
// settings.setTransientObject(IoxWkfConfig.SETTING_TRANSFERDESCRIPTION, td);
// }
//
//
// System.out.println(settings);

// TomlMapper tomlMapper = new TomlMapper();
// tomlMapper.registerModule(new JavaTimeModule());
// Map<String, Object> tomlMap = tomlMapper.readValue(configFile, Map.class);
// System.out.println(tomlMap);

return settings;
}

Expand Down Expand Up @@ -101,12 +116,12 @@ private static void setSettingsFromTomlTable(TomlTable tomlTable, Settings setti
}
}

if (key.endsWith(IoxWkfConfig.INI_META_TITLE)) {
String title = tomlTable.getString(key);
if (title != null) {
settings.setValue(IoxWkfConfig.INI_META_TITLE, title);
}
}
// if (key.endsWith(IoxWkfConfig.INI_META_TITLE)) {
// String title = tomlTable.getString(key);
// if (title != null) {
// settings.setValue(IoxWkfConfig.INI_META_TITLE, title);
// }
// }

//...
}
Expand Down
236 changes: 236 additions & 0 deletions src/main/java/ch/so/agi/csv2parquet/Toml2Xtf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package ch.so.agi.csv2parquet;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;

import org.tomlj.Toml;
import org.tomlj.TomlArray;
import org.tomlj.TomlParseResult;
import org.tomlj.TomlTable;

import ch.interlis.ili2c.Ili2c;
import ch.interlis.ili2c.Ili2cException;
import ch.interlis.ili2c.Ili2cFailure;
import ch.interlis.ili2c.metamodel.TransferDescription;
import ch.interlis.iom.IomObject;
import ch.interlis.iom_j.Iom_jObject;
import ch.interlis.iom_j.xtf.XtfReader;
import ch.interlis.iom_j.xtf.XtfWriter;
import ch.interlis.iox.IoxEvent;
import ch.interlis.iox.IoxException;
import ch.interlis.iox.IoxWriter;
import ch.interlis.iox_j.ObjectEvent;
import ch.interlis.iox_j.EndBasketEvent;
import ch.interlis.iox_j.EndTransferEvent;
import ch.interlis.iox_j.StartBasketEvent;
import ch.interlis.iox_j.StartTransferEvent;
import ch.so.agi.csv2parquet.meta.AttributeDescription;
import ch.so.agi.csv2parquet.meta.ClassDescription;
import ch.so.agi.csv2parquet.meta.ModelMetaDescription;
import ch.interlis.ili2c.metamodel.TransferDescription;

public class Toml2Xtf {

// TODO:
// -

private static final String MODEL_NAME = "SO_OGD_Metadata_20230629";
private static final String ILI_TOPIC = MODEL_NAME+".Datasets";
private static final String TAG = ILI_TOPIC+".Dataset";
private static final String BID = MODEL_NAME+".Datasets";

private static final String ILI_MODEL_METADATA = "SO_OGD_Metadata_20230629.ili";
private static final String METAPUBLISHER_RESOURCE_DIR = "";
private static final String ILI_DIR_NAME = "ili";
private static final String CORE_DATA_DIR_NAME = "core_data";

private static final String RESOURCE_STRUCTURE_TAG = MODEL_NAME+".Resource";
private static final String MODELLINK_STRUCTURE_TAG = MODEL_NAME+".ModelLink";
private static final String CLASS_DESCRIPTION_TAG = MODEL_NAME+".ClassDescription";
private static final String ATTRIBUTE_DESCRIPTION_TAG = MODEL_NAME+".AttributeDescription";

private static final String OFFICE_STRUCTURE_TAG = MODEL_NAME+".Office_";
private static final String CORE_DATA_OFFICES = "offices.xtf";

private static final String FILEFORMAT_STRUCTURE_TAG = MODEL_NAME+".FileFormat";
private static final String CORE_DATA_FILEFORMATS = "fileformats.xtf";

public void run(File configFile, Path targetPath) throws IOException, IoxException, Ili2cException {
TomlParseResult tomlContent = Toml.parse(configFile.toPath());
Map<String,Object> tomlMap = tomlContent.toMap();

Entry<String,Object> parentEntry = null;
for (Map.Entry<String, Object> entry : tomlMap.entrySet()) {
if (parentEntry == null || parentEntry.getKey().length() > entry.getKey().length()) {
parentEntry = entry;
}
}

TomlTable parentTable = (TomlTable) parentEntry.getValue();
String identifier = parentEntry.getKey();
String title = parentTable.getString("title");
String description = parentTable.getString("description");
String publisher = parentTable.getString("publisher");
List<Object> themeList = parentTable.getArray("theme").toList();
List<String> themeStringList = themeList
.stream()
.map((obj) -> Objects.toString(obj, null))
.collect(Collectors.toList());
String theme = String.join(",",themeStringList);
List<Object> keywordsList = parentTable.getArray("keywords").toList();
List<String> keywordsStringList = keywordsList
.stream()
.map((obj) -> Objects.toString(obj, null))
.collect(Collectors.toList());
String keywords = String.join(",",keywordsStringList);
LocalDate startDate = parentTable.getLocalDate("startDate");
LocalDate endDate = parentTable.getLocalDate("endDate");

File xtfFile = Paths.get(targetPath.toFile().getAbsolutePath(), "meta-"+identifier+".xtf").toFile();
TransferDescription td = getTransferdescription();
IoxWriter ioxWriter = new XtfWriter(xtfFile, td);

ioxWriter.write(new StartTransferEvent("SOGIS-20230701", "", null));
ioxWriter.write(new StartBasketEvent(ILI_TOPIC,BID));

Iom_jObject iomObj = new Iom_jObject(TAG, identifier);
iomObj.setattrvalue("Identifier", identifier);
iomObj.setattrvalue("Title", title);
if (description!=null) iomObj.setattrvalue("Description", description);

IomObject publisherIomObject = getIomObjectById(publisher, CORE_DATA_OFFICES);

if (publisherIomObject!=null) {
convertIomObjectToStructure(publisherIomObject, OFFICE_STRUCTURE_TAG);
iomObj.addattrobj("Publisher", publisherIomObject);
}

if (theme!=null) iomObj.setattrvalue("Theme", theme);
if (keywords!=null) iomObj.setattrvalue("Keywords", keywords);

iomObj.setattrvalue("StartDate", startDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
iomObj.setattrvalue("EndDate", endDate.format(DateTimeFormatter.ISO_LOCAL_DATE));

for (Map.Entry<String, Object> entry : tomlMap.entrySet()) {
if (tomlMap.size() > 1 && entry.getKey().equals(parentEntry.getKey())) {
continue;
}
TomlTable tomlTable = (TomlTable) entry.getValue();

String resourceIdentifier = entry.getKey();
String resourceTitle = tomlTable.getString("title");
String resourceDescription = tomlTable.getString("description");
String models = tomlTable.getString("models");

Iom_jObject resourceObj = new Iom_jObject(RESOURCE_STRUCTURE_TAG, null);
resourceObj.setattrvalue("Identifier", resourceIdentifier);
resourceObj.setattrvalue("Title", resourceTitle);
if (resourceDescription != null) resourceObj.setattrvalue("description", resourceDescription);

Iom_jObject modelObj = new Iom_jObject(MODELLINK_STRUCTURE_TAG, null);
modelObj.setattrvalue("Name", models);
modelObj.setattrvalue("LocationHint", "https://geo.so.ch/models");
resourceObj.addattrobj("Model", modelObj);

resourceObj.setattrvalue("ConfigId", resourceIdentifier);
resourceObj.setattrvalue("lastPublishingDate", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));

Map<String,ClassDescription> classDescriptions = ModelMetaDescription.getDescription(models, configFile.getParentFile());
for (Map.Entry<String, ClassDescription> classEntry : classDescriptions.entrySet()) {
Iom_jObject classDescObj = new Iom_jObject(CLASS_DESCRIPTION_TAG, null);

ClassDescription classDescription = classEntry.getValue();

classDescObj.setattrvalue("Name", classDescription.getName());
classDescObj.setattrvalue("Title", classDescription.getTitle());
if (classDescription.getDescription()!=null) classDescObj.setattrvalue("Description", classDescription.getDescription());

List<AttributeDescription> attributeDescriptions = classDescription.getAttributes();
for (AttributeDescription attributeDescription : attributeDescriptions) {
Iom_jObject attributeDescObj = new Iom_jObject(ATTRIBUTE_DESCRIPTION_TAG, null);

attributeDescObj.setattrvalue("Name", attributeDescription.getName());
if (attributeDescription.getDescription()!=null) attributeDescObj.setattrvalue("Description", attributeDescription.getDescription());
attributeDescObj.setattrvalue("DataType", attributeDescription.getDataType().name());
attributeDescObj.setattrvalue("isMandatory", attributeDescription.isMandatory()?"true":"false");
classDescObj.addattrobj("AttributeDescription", attributeDescObj);
}
resourceObj.addattrobj("ClassDescription", classDescObj);
}

List<Object> fileFormatList = tomlTable.getArray("fileFormats").toList();

List<IomObject> formatIomObjects = new ArrayList<>();
for (Object format : fileFormatList) {
IomObject formatObj = getIomObjectById(format.toString(), CORE_DATA_FILEFORMATS);
convertIomObjectToStructure(formatObj, FILEFORMAT_STRUCTURE_TAG);
formatIomObjects.add(formatObj);
}

for (IomObject formatStructure : formatIomObjects) {
resourceObj.addattrobj("FileFormats", formatStructure);
}

iomObj.addattrobj("Resources", resourceObj);
}

ioxWriter.write(new ObjectEvent(iomObj));

ioxWriter.write(new EndBasketEvent());
ioxWriter.write(new EndTransferEvent());
ioxWriter.flush();
ioxWriter.close();

}

private TransferDescription getTransferdescription() throws IOException, Ili2cFailure {
//File iliFile = Utils.copyResourceToTmpDir(METAPUBLISHER_RESOURCE_DIR +"/"+ ILI_DIR_NAME + "/" + ILI_MODEL_METADATA);
File iliFile = Utils.copyResourceToTmpDir(ILI_DIR_NAME + "/" + ILI_MODEL_METADATA);

ArrayList<String> filev = new ArrayList<String>() {{ add(iliFile.getAbsolutePath()); }};
TransferDescription td = Ili2c.compileIliFiles(filev, null);

if (td == null) {
throw new IllegalArgumentException("INTERLIS compiler failed");
}

return td;
}

private IomObject getIomObjectById(String id, String coreDataFileName) throws IOException, IoxException {
//File xtfFile = Utils.copyResourceToTmpDir(METAPUBLISHER_RESOURCE_DIR + "/" + CORE_DATA_DIR_NAME + "/" + coreDataFileName);
File xtfFile = Utils.copyResourceToTmpDir(CORE_DATA_DIR_NAME + "/" + coreDataFileName);

XtfReader xtfReader = new XtfReader(xtfFile);

IoxEvent event = xtfReader.read();
while (event instanceof IoxEvent) {
if (event instanceof ObjectEvent) {
ObjectEvent objectEvent = (ObjectEvent) event;
IomObject iomObj = objectEvent.getIomObject();
if (iomObj.getobjectoid().equalsIgnoreCase(id)) {
return iomObj;
}
}
event = xtfReader.read();
}
return null;
}

private void convertIomObjectToStructure(IomObject iomObj, String tag) {
iomObj.setobjecttag(tag);
iomObj.setobjectoid(null);
}

}
Loading

0 comments on commit 207f214

Please sign in to comment.