Skip to content

Commit

Permalink
OK-639: remove namespace from XML as requested by QDR maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
pkalliok committed Feb 26, 2025
1 parent 3b9c6d2 commit d0de4f1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,29 @@ object EuropassConversion {
"http://data.europa.eu/snb/isced-f/" ++ koodi

def konfoUrl(lang: String, oid: String): Elem =
<loq:homepage
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<loq:language uri={langCodes.getOrElse(lang, "")}/>
<loq:contentUrl>{s"https://opintopolku.fi/konfo/$lang/toteutus/$oid"}</loq:contentUrl>
</loq:homepage>
<homepage>
<language uri={langCodes.getOrElse(lang, "")}/>
<contentUrl>{s"https://opintopolku.fi/konfo/$lang/toteutus/$oid"}</contentUrl>
</homepage>

def nimiAsElmXml(lang: String, nimi: String): Elem =
<loq:title xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/"
language={lang}>{nimi}</loq:title>
<title language={lang}>{nimi}</title>

def nimetAsElmXml(nimet: Kielistetty): List[Elem] =
nimet.keys.map{lang => nimiAsElmXml(lang.name, nimet(lang))}.toList

def toteutusAsElmXml(toteutus: ToteutusIndexed): Elem = {
val oid: String = toteutus.oid.map(_.toString).getOrElse("")
val langs = List("en", "fi", "sv")
<loq:learningOpportunity id={toteutusUrl(oid)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<learningOpportunity id={toteutusUrl(oid)}>
{nimetAsElmXml(toteutus.nimi)}
{langs.map(konfoUrl(_, oid))}
{toteutus.tarjoajat.map{t =>
<loq:providedBy idref={organisaatioUrl(t.oid.toString)}/>}}
<loq:learningAchievementSpecification
<providedBy idref={organisaatioUrl(t.oid.toString)}/>}}
<learningAchievementSpecification
idref={koulutusUrl((toteutus.koulutusOid.map(_.toString).getOrElse("")))}/>
<loq:defaultLanguage uri={langCodes.getOrElse(toteutus.kielivalinta(0).name, "")}/>
</loq:learningOpportunity>
<defaultLanguage uri={langCodes.getOrElse(toteutus.kielivalinta(0).name, "")}/>
</learningOpportunity>
}

def koulutusToTutkintonimikkeet(koulutus: KoulutusIndexed): Seq[TutkintoNimike] =
Expand All @@ -85,17 +82,15 @@ object EuropassConversion {
koulutusToTutkintonimikkeet(koulutus) match {
case empty if empty.isEmpty =>
List(
<loq:learningOutcome id={tulosUrl(oid)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<learningOutcome id={tulosUrl(oid)}>
{nimetAsElmXml(koulutus.nimi)}
</loq:learningOutcome>
</learningOutcome>
)
case nonempty =>
nonempty.map{tutkintonimike: TutkintoNimike =>
<loq:learningOutcome id={tutkintoUrl(tutkintonimike.koodiUri)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<learningOutcome id={tutkintoUrl(tutkintonimike.koodiUri)}>
{nimetAsElmXml(tutkintonimike.nimi)}
</loq:learningOutcome>
</learningOutcome>
}.toList
}
}
Expand All @@ -107,14 +102,11 @@ object EuropassConversion {
case _ => None
}

def iscedfAsElmXml(koodi: String) =
<loq:ISCEDFCode uri={iscedfUrl(koodi)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/"/>
def iscedfAsElmXml(koodi: String) = <ISCEDFCode uri={iscedfUrl(koodi)}/>

def koulutusAsElmXml(koulutus: KoulutusIndexed): Elem = {
val oid: String = koulutus.oid.map(_.toString).getOrElse("")
<loq:learningAchievementSpecification id={koulutusUrl(oid)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<learningAchievementSpecification id={koulutusUrl(oid)}>
{nimetAsElmXml(koulutus.nimi)}
{koulutus.koulutuskoodienAlatJaAsteet
.flatMap(_.koulutusalaKoodiUrit)
Expand All @@ -123,23 +115,22 @@ object EuropassConversion {
.toSet
.map(iscedfAsElmXml)}
{koulutus.kielivalinta.map{lang =>
<loq:language uri={langCodes.getOrElse(lang.name, "")}/>}}
<language uri={langCodes.getOrElse(lang.name, "")}/>}}
{koulutusTuloksetAsElmXml(koulutus).map{tulos =>
<loq:learningOutcome idref={tulos \@ "id"}/>}}
</loq:learningAchievementSpecification>
<learningOutcome idref={tulos \@ "id"}/>}}
</learningAchievementSpecification>
}

def toteutusToKoulutusDependents(toteutus: ToteutusIndexed): Iterable[String] =
toteutus.koulutusOid.map(_.toString)

def tarjoajaAsElmXml(tarjoaja: Organisaatio): Elem = {
val nimet = tarjoaja.nimi.getOrElse(Map())
<loq:organisation id={organisaatioUrl(tarjoaja.oid.toString)}
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<organisation id={organisaatioUrl(tarjoaja.oid.toString)}>
{nimet.keys.map{lang =>
<loq:legalName language={lang.name}>{nimet(lang)}</loq:legalName>}}
<loq:location idref="http://rdf.oph.fi/sijainti/suomi"/>
</loq:organisation>
<legalName language={lang.name}>{nimet(lang)}</legalName>}}
<location idref="http://rdf.oph.fi/sijainti/suomi"/>
</organisation>
}

def toteutusToTarjoajaDependents(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ object Publisher extends Logging {
def toteutusToFile(oid: String, dest: BufferedWriter) = {
val toteutusXml = EuropassConversion.toteutusAsElmXml(ElasticClient.getToteutus(oid))
dest.write(
<loq:Courses xsdVersion="3.1.0"
xmlns:loq="http://data.europa.eu/snb/model/ap/loq-constraints/">
<loq:learningOpportunityReferences>
<Courses xsdVersion="3.1.0"
xsi:schemaLocation="http://data.europa.eu/snb/model/ap/loq-constraints/loq.xsd">
<learningOpportunityReferences>
{toteutusXml}
</loq:learningOpportunityReferences>
</loq:Courses>.toString
</learningOpportunityReferences>
</Courses>.toString
)
dest.close()
}
Expand All @@ -40,13 +40,13 @@ object Publisher extends Logging {
}

def koulutuksetToFile(dest: BufferedWriter, koulutusStream: Stream[KoulutusIndexed]) = {
dest.write("<loq:learningAchievementSpecificationReferences>\n")
dest.write("<learningAchievementSpecificationReferences>\n")
foreachWithLogging(
koulutusStream.map(EuropassConversion.koulutusAsElmXml),
"koulutukset",
{koulutus => dest.write(koulutus.toString)}
)
dest.write("</loq:learningAchievementSpecificationReferences>\n")
dest.write("</learningAchievementSpecificationReferences>\n")
}

def koulutusDependentsOfToteutukset(
Expand All @@ -60,24 +60,24 @@ object Publisher extends Logging {

def toteutuksetToFile(dest: BufferedWriter, toteutusStream: Stream[ToteutusIndexed]) = {
val toteutusXmlStream = toteutusStream.map(EuropassConversion.toteutusAsElmXml)
dest.write("<loq:learningOpportunityReferences>\n")
dest.write("<learningOpportunityReferences>\n")
foreachWithLogging(
toteutusXmlStream,
"toteutukset",
{toteutus => dest.write(toteutus.toString)}
)
dest.write("</loq:learningOpportunityReferences>\n")
dest.write("</learningOpportunityReferences>\n")
}

def tuloksetToFile(dest: BufferedWriter, koulutusStream: Stream[KoulutusIndexed]) = {
val tulosXmlStream = koulutusStream.flatMap(EuropassConversion.koulutusTuloksetAsElmXml)
dest.write("<loq:learningOutcomeReferences>\n")
dest.write("<learningOutcomeReferences>\n")
foreachWithLogging(
tulosXmlStream,
"koulutuksen tulokset",
{tulos => dest.write(tulos.toString)}
)
dest.write("</loq:learningOutcomeReferences>\n")
dest.write("</learningOutcomeReferences>\n")
}

def tarjoajaDependentsOfToteutukset(
Expand All @@ -90,41 +90,41 @@ object Publisher extends Logging {

def tarjoajatToFile(dest: BufferedWriter, tarjoajaStream: Stream[Organisaatio]) = {
val organisaatioXmlStream = tarjoajaStream.map(EuropassConversion.tarjoajaAsElmXml)
dest.write("<loq:agentReferences>\n")
dest.write("<agentReferences>\n")
foreachWithLogging(
organisaatioXmlStream,
"koulutuksen tarjoajat",
{org => dest.write(org.toString)}
)
dest.write("</loq:agentReferences>\n")
dest.write("</agentReferences>\n")
}

def suomiLocationToFile(dest: BufferedWriter) =
dest.write("""
<loq:locationReferences>
<loq:location id="http://rdf.oph.fi/sijainti/suomi">
<loq:geographicName language="fi">Suomi</loq:geographicName>
<loq:geographicName language="sv">Finland</loq:geographicName>
<loq:geographicName language="en">Finland</loq:geographicName>
<loq:address>
<loq:countryCode uri="http://publications.europa.eu/resource/authority/country/FIN"/>
</loq:address>
</loq:location>
</loq:locationReferences>
<locationReferences>
<location id="http://rdf.oph.fi/sijainti/suomi">
<geographicName language="fi">Suomi</geographicName>
<geographicName language="sv">Finland</geographicName>
<geographicName language="en">Finland</geographicName>
<address>
<countryCode uri="http://publications.europa.eu/resource/authority/country/FIN"/>
</address>
</location>
</locationReferences>
""")

def koulutustarjontaToFile(dest: BufferedWriter) = {
val toteutusStream = ElasticClient.listPublished(None)
lazy val koulutusStream = koulutusDependentsOfToteutukset(toteutusStream)
lazy val tarjoajaStream = tarjoajaDependentsOfToteutukset(toteutusStream)
dest.write("<loq:Courses xsdVersion=\"3.1.0\"\n" +
"xmlns:loq=\"http://data.europa.eu/snb/model/ap/loq-constraints/\">\n")
dest.write("<Courses xsdVersion=\"3.1.0\"\n" +
"xsi:schemaLocation=\"http://data.europa.eu/snb/model/ap/loq-constraints/loq.xsd\">\n")
toteutuksetToFile(dest, toteutusStream)
koulutuksetToFile(dest, koulutusStream)
tuloksetToFile(dest, koulutusStream)
tarjoajatToFile(dest, tarjoajaStream)
suomiLocationToFile(dest)
dest.write("\n</loq:Courses>")
dest.write("\n</Courses>")
}

def koulutustarjontaAsFile(): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class ConversionSpec extends ScalatraFlatSpec with KoutaJsonFormats {

it should "have correct namespace when converted" in {
val toteutusXml: Elem = EuropassConversion.toteutusAsElmXml(example_toteutus)
assert(toteutusXml.namespace == "http://data.europa.eu/snb/model/ap/loq-constraints/")
assert(toteutusXml.namespace == null)
val serialisedOutput = new StringWriter()
XML.write(serialisedOutput, toteutusXml, "utf-8", true, null)
val toteutusStr = serialisedOutput.toString
assert(toteutusStr.startsWith("<?xml"))
assert(toteutusStr.contains("loq:learningOpportunity"))
assert(toteutusStr.contains("learningOpportunity"))
}

it should "contain all required elements" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PublisherSpec extends ScalatraFlatSpec with ElasticFixture with KoutaJsonF
val bwriter = new BufferedWriter(writer)
Publisher.toteutusToFile("1.2.246.562.17.00000000000000000001", bwriter)
bwriter.close()
assert(writer.toString.contains("<loq:contentUrl>https://opintopolku.fi/konfo/sv/toteutus/1.2.246.562.17.00000000000000000001</loq:contentUrl>"))
assert(writer.toString.contains("<loq:providedBy idref=\"https://rdf.oph.fi/organisaatio/1.2.246.562.10.594252633210\"/>"))
assert(writer.toString.contains("<contentUrl>https://opintopolku.fi/konfo/sv/toteutus/1.2.246.562.17.00000000000000000001</contentUrl>"))
assert(writer.toString.contains("<providedBy idref=\"https://rdf.oph.fi/organisaatio/1.2.246.562.10.594252633210\"/>"))

// Want to have the test XML as a file? Here you go:
// val w = new BufferedWriter(new FileWriter("test.txt"))
Expand All @@ -39,10 +39,10 @@ class PublisherSpec extends ScalatraFlatSpec with ElasticFixture with KoutaJsonF
bwriter.close()
val content = writer.toString
assert(content.contains(
"<loq:learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000001\""
"<learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000001\""
))
assert(content.contains(
"<loq:learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000002\""
"<learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000002\""
))
}

Expand All @@ -57,10 +57,10 @@ class PublisherSpec extends ScalatraFlatSpec with ElasticFixture with KoutaJsonF
Stream(koulutusWithTutkintonimike, koulutusWithoutKoulutusKoodi))
bwriter.close()
assert(writer.toString.contains(
"<loq:learningOutcome id=\"https://rdf.oph.fi/tutkintonimike/tutkintonimikekk_339#2\""
"<learningOutcome id=\"https://rdf.oph.fi/tutkintonimike/tutkintonimikekk_339#2\""
))
assert(writer.toString.contains(
"<loq:learningOutcome id=\"https://rdf.oph.fi/koulutus-tulos/1.2.246.562.13.00000000000000008162\""
"<learningOutcome id=\"https://rdf.oph.fi/koulutus-tulos/1.2.246.562.13.00000000000000008162\""
))
}

Expand All @@ -71,31 +71,31 @@ class PublisherSpec extends ScalatraFlatSpec with ElasticFixture with KoutaJsonF
bwriter.close()
val content = writer.toString
assert(content.contains(
"<loq:learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000002\""
"<learningOpportunity id=\"https://rdf.oph.fi/koulutus-toteutus/1.2.246.562.17.00000000000000000002\""
))
assert(content.contains(
"<loq:learningAchievementSpecification id=\"https://rdf.oph.fi/koulutus/1.2.246.562.13.00000000000000000001\""
"<learningAchievementSpecification id=\"https://rdf.oph.fi/koulutus/1.2.246.562.13.00000000000000000001\""
))
assert(content.contains("http://data.europa.eu/snb/isced-f/02"))
assert(content.contains(
"<loq:learningOutcome id=\"https://rdf.oph.fi/tutkintonimike/tutkintonimikkeet_02\""
"<learningOutcome id=\"https://rdf.oph.fi/tutkintonimike/tutkintonimikkeet_02\""
))
assert(content.contains(
"<loq:organisation id=\"https://rdf.oph.fi/organisaatio/1.2.246.562.10.81934895871\""
"<organisation id=\"https://rdf.oph.fi/organisaatio/1.2.246.562.10.81934895871\""
))
assert(content.contains(
"<loq:legalName language=\"fi\">Koulutuskeskus Salpaus</loq:legalName>"
"<legalName language=\"fi\">Koulutuskeskus Salpaus</legalName>"
))
assert(content.contains(
"<loq:geographicName language=\"sv\">Finland</loq:geographicName>"
"<geographicName language=\"sv\">Finland</geographicName>"
))
}

"koulutustarjontaAsFile" should "return XML filename" in {
val fileName = Publisher.koulutustarjontaAsFile()
assert(fileName.contains("europass-export"))
val content = Source.fromFile(fileName).mkString
assert(content.contains("<loq:title language=\"sv\">nimi sv</loq:title>"))
assert(content.contains("<title language=\"sv\">nimi sv</title>"))
}

"koulutusDependentsOfToteutukset" should "have all koulutukset" in {
Expand Down

0 comments on commit d0de4f1

Please sign in to comment.