-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DD-443: Associate datasets with the chosen depositor-account (#39)
* replace user in amd.xml * provenance for replaced user * group DDM related actions * account-substitutes.csv optional * speed up --help
- Loading branch information
Showing
26 changed files
with
588 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
removed-account, chosen-account | ||
user001,USer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/scala/nl.knaw.dans.easy.bag2deposit/UserTransformer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright (C) 2020 DANS - Data Archiving and Networked Services ([email protected]) | ||
* | ||
* Licensed 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 nl.knaw.dans.easy.bag2deposit | ||
|
||
import better.files.File | ||
import nl.knaw.dans.easy.bag2deposit.ddm.Provenance | ||
import org.apache.commons.csv.CSVFormat.RFC4180 | ||
|
||
import java.nio.charset.Charset | ||
import scala.util.Try | ||
import scala.xml.Node | ||
import scala.xml.transform.{ RewriteRule, RuleTransformer } | ||
|
||
class UserTransformer(cfgDir: File) { | ||
private val csvFile: File = cfgDir / "account-substitutes.csv" | ||
private val userMap = if (!csvFile.exists || csvFile.isEmpty) | ||
Map[String,String]() | ||
else parseCsv( | ||
csvFile, | ||
nrOfHeaderLines = 1, | ||
format = RFC4180.withHeader("old", "new"), | ||
).map(r => r.get("old") -> r.get("new")).toMap | ||
|
||
private val userRewriteRule: RewriteRule = new RewriteRule { | ||
override def transform(node: Node): Seq[Node] = { | ||
if (!Seq("depositorId", "signerId").contains(node.label)) node | ||
else userMap | ||
.get(node.text).map(id => <id>{ id }</id>.copy(label = node.label)) | ||
.getOrElse(node) | ||
} | ||
} | ||
private val transformer = new RuleTransformer(userRewriteRule) | ||
|
||
// The default charset is determined during virtual-machine startup and typically | ||
// depends upon the locale and charset of the underlying operating system. | ||
implicit val charset: Charset = Charset.forName("UTF-8") | ||
|
||
def transform(file: File): Try[Seq[Node]] = { | ||
for { | ||
xmlIn <- loadXml(file) | ||
xmlOut = transformer.transform(xmlIn).headOption | ||
.getOrElse(throw new Exception("programming error: AmdTransformer returned multiple roots")) | ||
_ = file.writeText(xmlOut.serialize) | ||
diff = Provenance.compare(xmlIn, xmlOut) | ||
_ = trace(diff.map(_.serialize)) | ||
} yield diff | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.