Dear Gitlab users, due to maintenance reasons, Gitlab will not be available on Thursday 30.09.2021 from 5:00 pm to approximately 5:30 pm.

Commit 8664c7b7 authored by Andreas Wagner's avatar Andreas Wagner
Browse files

Add option to add change to revisionDesc.

parent 2cdbecac
......@@ -153,6 +153,7 @@ The WriteDOI section contains these entries:
- `elementType` specifies which type of element is holding the DOI. The service will look up the first of `parentPath`'s children of this type. If `attributeName` is not set, the DOI will be written as this element's textual content. (It defaults to "idno".)
- `attributeName` - if set, the service will write the DOI to the respective attribute. If empty (default), the DOI will be written to a text node.
- `otherAttributes` a list of json objects made up of `attName`/`value` pairs. These allow you to set other attributes of the element holding the DOI to fixed values. It defaults to a single `"type": "DOI"` pair.
- `addChangeDesc` takes a boolean value indicating whether or not to add a `change` entry in the `revisionDesc` section of the file (if the file has changed).
Here is a complete WriteDOI section, reflecting the default values. This results in new DOI values being accumulated in `/TEI/teiHeader/fileDesc/publicationStmt/idno/idno[@type="DOI"]` elements, in reverse chronological order, i.e. the most recent one first:
......@@ -168,7 +169,8 @@ Here is a complete WriteDOI section, reflecting the default values. This results
}
],
"position": "firstChild",
"attributeName": ""
"attributeName": "",
"addChangeDesc": true
}
```
......
......@@ -35,7 +35,8 @@
}
],
"position": "firstChild",
"attributeName": ""
"attributeName": "",
"addChangeDesc": true
},
"metadata": {
"fields": [
......
......@@ -51,6 +51,7 @@ func Configure(Config *tei2zenodo.Config) error {
"position": "firstChild",
"attributeName": "",
"otherAttributes": doiWriteAtts,
"addChangeDesc": true,
})
viper.SetEnvPrefix("T2Z")
......
......@@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"strings"
"time"
log "github.com/sirupsen/logrus"
......@@ -316,6 +317,7 @@ func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
log.Debugf("Mixin DOI - could not find element")
}
// Add or replace the DOI value
if tElmnt == nil || c.WriteDOI.Mode == "add" { // If mode = "add" or pElemnt/elementType cannot be found ...
tElmnt := etree.NewElement(c.WriteDOI.ElementType) // ... create a new element ...
......@@ -351,8 +353,36 @@ func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
log.Errorf("Invalid value %s in WriteDOI.Position configuration (must be 'firstChild' or 'lastChild').", c.WriteDOI.Position)
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("invalid value %s in WriteDOI.Position configuration (must be 'firstChild' or 'lastChild')", c.WriteDOI.Position), 500, nil)
}
// optionally add change element to revisionDesc
if c.WriteDOI.AddChangeDesc {
currentTime := time.Now()
rdElmnt := doc.FindElement("/TEI/teiHeader/revisionDesc")
if rdElmnt == nil {
log.Debugf("Mixin DOI/record change - create revisionDesc")
teiHeader := doc.FindElement("/TEI/teiHeader")
rdElmnt = etree.NewElement("revisionDesc")
teiHeader.InsertChildAt(len(teiHeader.FindElements(`/*`))+100, rdElmnt)
}
chlstElmnt := rdElmnt.FindElement("listChange")
chgElmnt := etree.NewElement("change")
chgElmnt.CreateAttr("when", currentTime.Format("2006-01-02"))
chgText := chgElmnt.CreateText(fmt.Sprintf("Added DOI %s (automatically added by tei2zenodo service).", doi))
if chgText == nil {
log.Errorf("Problem creating text for change entry")
return "", tei2zenodo.NewError("errParse", fmt.Sprintf("problem creating text for change entry"), 500, nil)
}
if chlstElmnt != nil {
chlstElmnt.InsertChildAt(0, chgElmnt)
} else {
rdElmnt.InsertChildAt(0, chgElmnt)
}
log.Debugf("Mixin DOI/record change - created change entry")
}
} else if c.WriteDOI.Mode == "replace" { // If, on the other hand, we have mode ="replace" AND pElmnt/elementType is an element
var oldDoi string
if c.WriteDOI.AttributeName != "" { // if the value is in an attribute ...
oldDoi = tElmnt.SelectAttr(c.WriteDOI.AttributeName).Value
tAttr := tElmnt.CreateAttr(c.WriteDOI.AttributeName, doi) // ... then we only replace this attribute ...
if tAttr == nil {
log.Errorf("Problem creating %s/%s/@%s='%s'.", c.WriteDOI.ParentPath, c.WriteDOI.ElementType, c.WriteDOI.AttributeName, doi)
......@@ -366,6 +396,7 @@ func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
}
}
} else {
oldDoi = tElmnt.Text()
tElmnt.SetText(doi) // else the value is in the text node, and we replace this text ...
if tElmnt.Text() != doi {
log.Errorf("Problem creating text '%s' at %s/%s.", doi, c.WriteDOI.ParentPath, c.WriteDOI.ElementType)
......@@ -379,6 +410,33 @@ func MixinDOI(r io.Reader, doi string, c *tei2zenodo.Config) (string, error) {
}
}
}
// optionally add change element to revisionDesc
if c.WriteDOI.AddChangeDesc {
currentTime := time.Now()
rdElmnt := doc.FindElement("/TEI/teiHeader/revisionDesc")
if rdElmnt == nil {
log.Debugf("Mixin DOI/record change - create revisionDesc")
teiHeader := doc.FindElement("/TEI/teiHeader")
rdElmnt = etree.NewElement("revisionDesc")
teiHeader.InsertChildAt(len(teiHeader.FindElements(`/*`))+100, rdElmnt)
}
chlstElmnt := rdElmnt.FindElement("listChange")
chgElmnt := etree.NewElement("change")
chgElmnt.CreateAttr("when", currentTime.Format("2006-01-02"))
chgText := chgElmnt.CreateText(fmt.Sprintf("Replaced DOI from %s to %s (automatically changed by tei2zenodo service).", oldDoi, doi))
if chgText == nil {
log.Errorf("Problem creating text for change entry")
return "", tei2zenodo.NewError("errParse", fmt.Sprintf("problem creating text for change entry"), 500, nil)
}
if chlstElmnt != nil {
chlstElmnt.InsertChildAt(0, chgElmnt)
} else {
rdElmnt.InsertChildAt(0, chgElmnt)
}
log.Debugf("Mixin DOI/record change - created change entry")
}
} else { // We have an invalid mode
log.Errorf("Invalid value %s in WriteDOI.mode configuration (must be 'add', 'replace' or 'ignore').", c.WriteDOI.Mode)
return "", tei2zenodo.NewError("errBadConfig", fmt.Sprintf("invalid value %s in WriteDOI.mode configuration (must be 'add', 'replace' or 'ignore')", c.WriteDOI.Mode), 500, nil)
......
......@@ -27,6 +27,7 @@ type WriteDOIConfig struct {
Position string // 'firstChild' or 'lastChild'
AttributeName string // if the DOI goes into an attribute rather than a text node
OtherAttributes []AttributeSpec // if we need to add more attributes to an element that we're creating
AddChangeDesc bool
}
// AttributeSpec specifies an (xml) attribute and its value
......
......@@ -145,6 +145,14 @@
<input class="form-control" type="text" id="writeDOIattributeName" name="writeDOIattributeName" value="" disabled />
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="writeDOIaddChangeDesc">Add &lt;change/&gt; entry:</label>
<div class="col-sm-10 text-left">
<input type="checkbox" id="writeDOIaddChangeDesc" name="writeDOIaddChangeDesc" value="True" checked disabled />
</div>
</div>
<!--
"WriteDOI": {
"otherAttributes": [
......@@ -265,6 +273,7 @@
document.getElementById('writeDOIotherAttName').disabled = isDisabled;
document.getElementById('writeDOIotherAttValue').disabled = isDisabled;
document.getElementById('writeDOIattributeName').disabled = isDisabled;
document.getElementById('writeDOIaddChangeDesc').disabled = isDisabled;
}
function getFileSignature() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment