Skip to content

Commit

Permalink
(#24): factors out connector stuff into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Jun 18, 2024
1 parent 58d82ae commit 1fd01fd
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 58 deletions.
45 changes: 45 additions & 0 deletions globodiet/connectors/nutridb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor
license agreements. See the NOTICE file distributed with this work for
additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-globodiet-connectors</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>dita-globodiet-connectors-nutridb</artifactId>
<name>Dita - Globodiet Connectors (NutriDb)</name>
<description>
Dita library integrating Globodiet with NutriDb (an external FCDB system).
</description>

<properties>
<jar-plugin.automaticModuleName>at.ac.univie.nutrition.dita.nutridb</jar-plugin.automaticModuleName>
<git-plugin.propertiesDir>at/ac/univie/nutrition/dita/nutridb</git-plugin.propertiesDir>
</properties>

<dependencies>

<dependency>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-recall24-dto</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package dita.globodiet.connectors.nutridb.util;

import java.util.stream.Collectors;

import org.apache.causeway.commons.internal.base._Strings;

import lombok.experimental.UtilityClass;

import dita.commons.format.FormatUtils;
import dita.recall24.dto.RecallNode24;
import dita.recall24.dto.RecallNode24.Builder24;
import dita.recall24.dto.Record24;

@UtilityClass
public class NutriDbConverters {

@UtilityClass
public class ToNutriDB {
public String convertFacet(final String facetGd) {
var facet = facetGd.substring(0, 2);
var descriptor = facetGd.substring(2);
return "gd:F" + FormatUtils.noLeadingZeros(facet) + "." + descriptor;
}
public String convertFacetList(final String facetSids) {
return _Strings.splitThenStream(facetSids, ",")
.map(ToNutriDB::convertFacet)
.collect(Collectors.joining(","));
}
public String convertFood(final Record24.Type recordType, final String sid) {
return switch(recordType) {
case FOOD, TYPE_OF_FAT_USED, TYPE_OF_MILK_OR_LIQUID_USED, FRYING_FAT ->
// ndb system-id = 'gd'
"gd:N" + FormatUtils.noLeadingZeros(sid);
case COMPOSITE ->
// ndb system-id = 'gdr'
"gdr:" + FormatUtils.noLeadingZeros(sid);
case PRODUCT ->
// ndb system-id = 'ndb' (supplements only)
"ndb:" + FormatUtils.noLeadingZeros(sid);
};
}
}

/**
* Converts GloboDiet to NutriDb identifiers.
*/
public record ToNutriDbTransfomer() implements RecallNode24.Transfomer {
@Override
public void accept(final Builder24<?> builder) {
switch (builder) {
case Record24.Builder recBuilder -> toNutriDbPrefixes(recBuilder);
default -> {}
};
}
private void toNutriDbPrefixes(final Record24.Builder recBuilder) {
recBuilder.sid(ToNutriDB.convertFood(recBuilder.type(), recBuilder.sid()));
recBuilder.facetSids(ToNutriDB.convertFacetList(recBuilder.facetSids()));
}
}

}
38 changes: 38 additions & 0 deletions globodiet/connectors/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Licensed to the Apache Software Foundation (ASF) under one or more
contributor
license agreements. See the NOTICE file distributed with this work for
additional
information regarding copyright ownership. The ASF licenses this file to
you under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License. -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-globodiet</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>dita-globodiet-connectors</artifactId>
<name>Dita - Globodiet Connectors</name>
<description>
Globodiet connectors to external systems.
</description>

<packaging>pom</packaging>

<modules>
<module>nutridb</module>
</modules>


</project>
1 change: 1 addition & 0 deletions globodiet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<module>manager</module>
<module>schema</module>
<module>survey</module>
<module>connectors</module>
</modules>

</project>
6 changes: 6 additions & 0 deletions globodiet/survey/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ additional

<!-- TESTING -->

<dependency>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-globodiet-connectors-nutridb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.causeway.testing</groupId>
<artifactId>causeway-testing-integtestsupport-applib</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,24 @@
package dita.globodiet.survey;

import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.inject.Inject;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;

import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.testing.integtestsupport.applib.CausewayIntegrationTestAbstract;

import lombok.NonNull;

import dita.commons.food.composition.FoodCompositionRepository;
import dita.commons.format.FormatUtils;
import dita.commons.qmap.QualifiedMap;
import dita.commons.types.Message;
import dita.globodiet.survey.recall24.InterviewXmlParser;
import dita.globodiet.survey.util.InterviewUtils;
import dita.recall24.dto.Correction24;
import dita.recall24.dto.InterviewSet24;
import dita.recall24.dto.RecallNode24;
import dita.recall24.dto.RecallNode24.Builder24;
import dita.recall24.dto.Record24;
import dita.recall24.dto.util.Recall24DtoUtils;
import io.github.causewaystuff.blobstore.applib.BlobStore;
import io.github.causewaystuff.commons.base.types.NamedPath;
Expand Down Expand Up @@ -82,47 +76,4 @@ protected Stream<InterviewSet24.Dto> loadAndStreamInterviews(
.map(Recall24DtoUtils.correct(correction));
}

/**
* Converts ingredient identifiers to NutriDb (prefixed) identifiers.
*/
protected RecallNode24.Transfomer nutriDbTransfomer(){

record NutriDbTransfomer() implements RecallNode24.Transfomer {

@Override
public void accept(final Builder24<?> builder) {
switch (builder) {
case Record24.Builder recBuilder -> toNutriDbPrefixes(recBuilder);
default -> {}
};
}
private void toNutriDbPrefixes(final Record24.Builder recBuilder) {
switch(recBuilder.type()) {
case FOOD, TYPE_OF_FAT_USED, TYPE_OF_MILK_OR_LIQUID_USED, FRYING_FAT -> {
// ndb system-id = 'gd'
recBuilder.sid("gd:N" + FormatUtils.noLeadingZeros(recBuilder.sid()));
}
case COMPOSITE -> {
// ndb system-id = 'gdr'
recBuilder.sid("gdr:" + FormatUtils.noLeadingZeros(recBuilder.sid()));
}
case PRODUCT -> {
// ndb system-id = 'ndb' (supplements only)
recBuilder.sid("ndb:" + FormatUtils.noLeadingZeros(recBuilder.sid()));
}
}
recBuilder.facetSids(_Strings.splitThenStream(recBuilder.facetSids(), ",")
.map(this::toNutriDbFacet)
.collect(Collectors.joining(",")));
}
private String toNutriDbFacet(final String f) {
var facet = f.substring(0, 2);
var descriptor = f.substring(2);
return "gd:F" + FormatUtils.noLeadingZeros(facet) + "." + descriptor;
}
}

return new NutriDbTransfomer();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dita.commons.qmap.QualifiedMap;
import dita.commons.qmap.QualifiedMap.QualifiedMapKey;
import dita.commons.qmap.QualifiedMapEntry;
import dita.globodiet.connectors.nutridb.util.NutriDbConverters;
import dita.globodiet.survey.DitaGdSurveyIntegrationTest;
import dita.globodiet.survey.DitaTestModuleGdSurvey;
import dita.globodiet.survey.PrivateDataTest;
Expand All @@ -54,9 +55,11 @@ class InterviewXmlParserIntegrationTest extends DitaGdSurveyIntegrationTest {
@Test
void parsingFromBlobStore() {

final var systemId = "GD-AT20240507";

var nutMapping = loadNutMapping();
var stats = new Recall24SummaryStatistics();
var recordProcessor = new RecordProcessor(stats, "GD-AT20240507", nutMapping);
var recordProcessor = new RecordProcessor(stats, systemId, nutMapping);

var correction = Correction24.tryFromYaml("""
respondents:
Expand All @@ -77,9 +80,9 @@ void parsingFromBlobStore() {

var interviewSet = InterviewUtils
.interviewSetFromBlobStrore(NamedPath.of("at-national-2026"), surveyBlobStore, correction, null)
.transform(nutriDbTransfomer());
.transform(new NutriDbConverters.ToNutriDbTransfomer());

var todoReporter = new TodoReportUtils.TodoReporter("GD-AT20240507", nutMapping, interviewSet);
var todoReporter = new TodoReportUtils.TodoReporter(systemId, nutMapping, interviewSet);
todoReporter.report(
factoryService,
DataSink.ofFile(new File("d:/tmp/_scratch/mapping-todos.txt")));
Expand Down
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ additional
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<printSummary>false</printSummary>
<argLine>${surefire-plugin.argLine}</argLine>
Expand Down Expand Up @@ -188,22 +189,20 @@ additional
<artifactId>dita-globodiet-schema</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>


<dependency>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-recall24-dto</artifactId>
<artifactId>dita-globodiet-connectors-nutridb</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-recall24-reporter</artifactId>
<artifactId>dita-recall24-dto</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>at.ac.univie.nutrition.dita</groupId>
<artifactId>dita-vault</artifactId>
<artifactId>dita-recall24-reporter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

Expand Down Expand Up @@ -434,6 +433,7 @@ additional
<module>causeway</module>
<module>foodex</module>
<module>globodiet</module>
<module>nutridb</module>
</modules>

<!-- META DATA -->
Expand Down

0 comments on commit 1fd01fd

Please sign in to comment.