Skip to content

Commit

Permalink
Starting on persistence of multiple project languages
Browse files Browse the repository at this point in the history
  • Loading branch information
hatton committed Apr 22, 2024
1 parent 9d7c791 commit c9622f3
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 140 deletions.
2 changes: 2 additions & 0 deletions archive-configurations/ELAR/fields.json5
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
project: [
{ key: "vernacularIso3CodeAndName", visibility: "never" },
{ key: "analysisIso3CodeAndName", visibility: "never" },
{ key: "collectionSubjectLanguages", visibility: "always" },
{ key: "collectionWorkingLanguages", visibility: "always" },

Expand Down
2 changes: 2 additions & 0 deletions archive-configurations/lameta/fields.json5
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
{
key: "collectionSubjectLanguages",
englishLabel: "Subject Languages",
xmlTag: "CollectionSubjectLanguages",
visibility: "never",
form: "primary",
type: "languageChoices",
Expand All @@ -40,6 +41,7 @@
},
{
key: "collectionWorkingLanguages",
xmlTag: "CollectionWorkingLanguages",
englishLabel: "Working Languages",
visibility: "never",
form: "primary",
Expand Down
9 changes: 4 additions & 5 deletions src/components/LanguageChoicesEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const LanguageChoicesEditor: React.FunctionComponent<
};

const currentValueArray = props.field.text
.split(";")
.split(";") // TODO: move this serialization logic into the Field class
.filter((c) => c.length > 0)
.map((c) => c.trim())
.map((code) => {
Expand Down Expand Up @@ -129,13 +129,12 @@ export const LanguageChoicesEditor: React.FunctionComponent<
)
: []; // if you delete the last member, you get null instead of []

// TODO: move this serialization logic into the Field class
const s: string = newChoices
.map((o) => {
if (o.value.startsWith("qaa-x-") && !o.label.startsWith("qaa")) {
return `${o.value}|${o.label}`;
} else return o.value;
return o.value;
})
.join(";");
.join(";"); // why semicolong instead of comma? The particpants field as used semicolon for years.
console.log("saving: " + s);
props.field.setValueFromString(s);
},
Expand Down
1 change: 1 addition & 0 deletions src/model/Project/Session/ReadSession.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { SessionMetadataFile } from "./Session";
import * as temp from "temp";
import fs from "fs";
Expand Down
24 changes: 24 additions & 0 deletions src/model/Project/WriteProject.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import fs from "fs";
import Path from "path";
import { EncounteredVocabularyRegistry } from "./EncounteredVocabularyRegistry";
import { describe, expect, it, beforeEach, afterEach } from "vitest";
import {
setResultXml,
xexpect,
count,
value
} from "../../other/xmlUnitTestUtils";
let projectDirectory;
let projectName;

Expand Down Expand Up @@ -53,6 +59,24 @@ describe("Project Write", () => {
});
});

it("should write languages", () => {
const f = GetProjectFileWithOneField("unused", "");
f.properties.setText("collectionSubjectLanguages", "ab;cd;ef");
f.properties.setText("collectionWorkingLanguages", "gh;ij");
setResultXml(f.getXml());
xexpect("Project/CollectionSubjectLanguages").toHaveCount(1);
xexpect("Project/CollectionSubjectLanguages[text()='ab;cd;ef']").toHaveCount(
1
);
xexpect("Project/CollectionWorkingLanguages").toHaveCount(1);
xexpect("Project/CollectionWorkingLanguages[text()='gh;ij']").toHaveCount(1);
// and these we output for backwards compatibility
xexpect("Project/VernacularISO3CodeAndName").toHaveCount(1);
xexpect("Project/VernacularISO3CodeAndName[text()='ab']").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName").toHaveCount(1);
xexpect("Project/AnalysisISO3CodeAndName[text()='gh']").toHaveCount(1);
});

function AttemptRoundTripOfOneField(
key: string,
xmlTag: string,
Expand Down
1 change: 0 additions & 1 deletion src/model/file/File.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { beforeEach, afterEach, describe, it } from "vitest";
import { OtherFile } from "./File";
import * as fs from "fs-extra";
import * as Path from "path";
Expand Down
44 changes: 42 additions & 2 deletions src/model/file/GetSayMoreXml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function getSayMoreXml(
throw error;
}
}
//function writeElementsWeDontUnderstand() {}

function writeSimplePropertyElements(
root: xmlbuilder.XMLElementOrXMLNode,
properties: Field[],
Expand All @@ -103,7 +103,23 @@ function writeSimplePropertyElements(
doOutputTypeInXmlTags,
false,
"AccessProtocol",
"lameta 3 and following prefer archiveConfigurationName, this is written for compatibility with SayMore and older versions of lameta"
"lameta 3 and following use <ArchiveConfigurationName> as that matches what we're actually doing with it now. We still emit <AccessProtocol> for compatibility with SayMore and older versions of lameta"
);
}
if (field.key === "collectionSubjectLanguages") {
writeLanguageFieldForBackwardsCompat(
root,
field,
"VernacularISO3CodeAndName",
"lameta 3 and following use <CollectionSubjectLanguages> to store multiple languages. We still emit the first language as <VernacularISO3CodeAndName> for compatibility with SayMore and older versions of lameta"
);
}
if (field.key === "collectionWorkingLanguages") {
writeLanguageFieldForBackwardsCompat(
root,
field,
"AnalysisISO3CodeAndName",
"lameta 3 and following use <CollectionWorkingLanguages> to store multiple languages. We still emit the first language as <AnalysisISO3CodeAndName> for compatibility with SayMore and older versions of lameta"
);
}
});
Expand Down Expand Up @@ -180,6 +196,30 @@ function writeContributions(
});
}

function writeLanguageFieldForBackwardsCompat(
root: xmlbuilder.XMLElementOrXMLNode,
field: Field,
overrideTag?: string,
comment?: string
) {
const { type, value } = field.typeAndValueEscapedForXml();

// SayMore Windows, at least through version 3.3, has inconsistent capitalization...
// for now we just use those same tags when writing so that the file can be opened in that SM
const tag =
overrideTag ||
(field.definition && field.definition.xmlTag
? field.definition.xmlTag
: field.key);

if (comment) {
root.comment(comment);
}
const attributes: any = {};

root.element(tag, attributes, value.split(";")[0] || "").up();
}

function writeField(
root: xmlbuilder.XMLElementOrXMLNode,
field: Field,
Expand Down
2 changes: 1 addition & 1 deletion src/other/xmlUnitTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ expect.extend({
toHaveCount(xpath, expectedValue) {
const matchCount = select(xpath).length;
if (matchCount !== expectedValue) {
// console.log(resultXml);
console.log(resultXml);
return {
message: () =>
`expected ${xpath} to have ${expectedValue} matches, but got ${matchCount}`,
Expand Down
Loading

0 comments on commit c9622f3

Please sign in to comment.