Skip to content

Commit 967d876

Browse files
committed
Add command and infrastructure for performing deposit record transformations. Allow for integration tests of subcommands. Initialize deposit record base path constant.
1 parent 5dfb166 commit 967d876

File tree

13 files changed

+752
-3
lines changed

13 files changed

+752
-3
lines changed

fcrepo-clients/src/main/java/edu/unc/lib/dl/fcrepo4/RepositoryPaths.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.CONTENT_BASE;
1919
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.CONTENT_ROOT_ID;
20+
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.DEPOSIT_RECORD_BASE;
2021
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.METADATA_CONTAINER;
2122
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.REPOSITORY_ROOT_ID;
2223

@@ -183,5 +184,6 @@ private static void setContentBase(String uri) {
183184
contentBase = URIUtil.join(baseUri, CONTENT_BASE);
184185
contentBasePid = new FedoraPID(CONTENT_BASE, REPOSITORY_ROOT_ID, null, URI.create(contentBase));
185186
contentRootPid = PIDs.get(URIUtil.join(contentBase, CONTENT_ROOT_ID));
187+
depositRecordBase = URIUtil.join(baseUri, DEPOSIT_RECORD_BASE);
186188
}
187189
}

migration-util/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
3535
<mainClass>edu.unc.lib.dcr.migration.MigrationCLI</mainClass>
3636
</transformer>
37+
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
38+
<resource>META-INF/spring.handlers</resource>
39+
</transformer>
40+
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
41+
<resource>META-INF/spring.schemas</resource>
42+
</transformer>
3743
</transformers>
3844
<createDependencyReducedPom>false</createDependencyReducedPom>
3945
<artifactSet>

migration-util/src/main/java/edu/unc/lib/dcr/migration/MigrationCLI.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
TransformPremis.class,
3838
PathIndexCommand.class,
3939
TransformContentCommand.class,
40+
TransformDepositRecordsCommand.class,
4041
ViewDepositModelCommand.class,
4142
SubmitDepositCommand.class
4243
})
@@ -78,7 +79,7 @@ public class MigrationCLI implements Callable<Integer> {
7879
description = "Port for redis. Default 6379.")
7980
protected int redisPort;
8081

81-
private MigrationCLI() {
82+
protected MigrationCLI() {
8283
}
8384

8485
@Override
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright 2008 The University of North Carolina at Chapel Hill
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package edu.unc.lib.dcr.migration;
17+
18+
import static edu.unc.lib.dcr.migration.MigrationConstants.OUTPUT_LOGGER;
19+
import static org.slf4j.LoggerFactory.getLogger;
20+
21+
import java.nio.file.Path;
22+
import java.util.concurrent.Callable;
23+
24+
import org.slf4j.Logger;
25+
import org.springframework.context.ConfigurableApplicationContext;
26+
import org.springframework.context.support.ClassPathXmlApplicationContext;
27+
28+
import edu.unc.lib.dcr.migration.deposit.DepositRecordTransformationService;
29+
import edu.unc.lib.dcr.migration.paths.PathIndex;
30+
import picocli.CommandLine.Command;
31+
import picocli.CommandLine.Option;
32+
import picocli.CommandLine.Parameters;
33+
import picocli.CommandLine.ParentCommand;
34+
35+
/**
36+
* @author bbpennel
37+
*/
38+
@Command(name = "transform_deposit_records", aliases = {"tdr"},
39+
description = "Transforms a bxc3 deposit records into boxc5 ones in a repository")
40+
public class TransformDepositRecordsCommand implements Callable<Integer> {
41+
42+
private static final Logger output = getLogger(OUTPUT_LOGGER);
43+
44+
@ParentCommand
45+
private MigrationCLI parentCommand;
46+
47+
@Parameters(index = "0",
48+
description = "Path of file containing a list of deposit record pids to transform")
49+
private Path pidListPath;
50+
51+
@Option(names = {"-g", "--generate-ids"},
52+
description = "Generate new ids for transformed objects, for testing.")
53+
private boolean generateIds;
54+
55+
@Option(names = {"-s", "--storage-location"},
56+
defaultValue = "primary_storage",
57+
description = "Identifier of the storage location manifests will be put in.")
58+
private String storageLocationId;
59+
60+
private String applicationContextPath = "spring/service-context.xml";
61+
62+
@Override
63+
public Integer call() throws Exception {
64+
long start = System.currentTimeMillis();
65+
66+
output.info("Using properties from {}", System.getProperty("config.properties.uri"));
67+
68+
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(applicationContextPath);
69+
70+
PathIndex pathIndex = (PathIndex) context.getBean("pathIndex");
71+
pathIndex.setDatabaseUrl(parentCommand.databaseUrl);
72+
73+
DepositRecordTransformationService transformService =
74+
(DepositRecordTransformationService) context.getBean("depositRecordTransformationService");
75+
transformService.setGenerateIds(generateIds);
76+
77+
output.info("Transforming deposit records from {}", pidListPath);
78+
output.info("===========================================");
79+
80+
int result = transformService.perform(pidListPath, storageLocationId);
81+
82+
output.info("Finished transformation in {}ms", System.currentTimeMillis() - start);
83+
84+
context.close();
85+
86+
return result;
87+
}
88+
89+
public void setApplicationContextPath(String applicationContextPath) {
90+
this.applicationContextPath = applicationContextPath;
91+
}
92+
93+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright 2008 The University of North Carolina at Chapel Hill
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package edu.unc.lib.dcr.migration.deposit;
17+
18+
import static edu.unc.lib.dl.fcrepo4.RepositoryPathConstants.DEPOSIT_RECORD_BASE;
19+
20+
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.stream.Stream;
24+
25+
import edu.unc.lib.dl.exceptions.RepositoryException;
26+
import edu.unc.lib.dl.fcrepo4.PIDs;
27+
import edu.unc.lib.dl.fcrepo4.RepositoryPIDMinter;
28+
import edu.unc.lib.dl.fedora.PID;
29+
import edu.unc.lib.dl.persist.api.storage.StorageLocation;
30+
import edu.unc.lib.dl.persist.api.storage.StorageLocationManager;
31+
import edu.unc.lib.dl.persist.api.transfer.BinaryTransferService;
32+
import edu.unc.lib.dl.persist.api.transfer.BinaryTransferSession;
33+
34+
/**
35+
* Service which transforms a set of deposit records from a list of pids.
36+
*
37+
* @author bbpennel
38+
*/
39+
public class DepositRecordTransformationService {
40+
41+
private DepositRecordTransformerManager transformerManager;
42+
43+
private BinaryTransferService transferService;
44+
45+
private StorageLocationManager locationManager;
46+
47+
private RepositoryPIDMinter pidMinter;
48+
49+
private boolean generateIds;
50+
51+
/**
52+
* Perform the transformation of the tree of content objects
53+
*
54+
* @return result code
55+
*/
56+
public int perform(Path pidListPath, String destLocId) {
57+
StorageLocation destLoc = locationManager.getStorageLocationById(destLocId);
58+
try (
59+
Stream<String> pidStringStream = Files.lines(pidListPath);
60+
BinaryTransferSession transferSession = transferService.getSession(destLoc);
61+
) {
62+
63+
pidStringStream.forEach(originalString -> {
64+
PID originalPid = PIDs.get(DEPOSIT_RECORD_BASE, originalString);
65+
PID newPid = getTransformedPid(originalPid);
66+
transformerManager.createTransformer(originalPid, newPid, transferSession)
67+
.fork();
68+
});
69+
} catch (IOException e) {
70+
throw new RepositoryException("Failed to load " + pidListPath, e);
71+
}
72+
73+
// Wait for all transformers to finish
74+
return transformerManager.awaitTransformers();
75+
}
76+
77+
public PID getTransformedPid(PID originalPid) {
78+
return generateIds ? pidMinter.mintDepositRecordPid() : originalPid;
79+
}
80+
81+
public void setGenerateIds(boolean generateIds) {
82+
this.generateIds = generateIds;
83+
}
84+
85+
public void setPidMinter(RepositoryPIDMinter pidMinter) {
86+
this.pidMinter = pidMinter;
87+
}
88+
89+
public void setTransformerManager(DepositRecordTransformerManager transformerManager) {
90+
this.transformerManager = transformerManager;
91+
}
92+
93+
public void setTransferService(BinaryTransferService transferService) {
94+
this.transferService = transferService;
95+
}
96+
97+
public void setLocationManager(StorageLocationManager locationManager) {
98+
this.locationManager = locationManager;
99+
}
100+
}

migration-util/src/main/java/edu/unc/lib/dcr/migration/deposit/DepositRecordTransformer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.jena.rdf.model.ModelFactory;
4040
import org.apache.jena.rdf.model.Property;
4141
import org.apache.jena.rdf.model.Resource;
42+
import org.apache.jena.rdf.model.Statement;
4243
import org.apache.jena.vocabulary.DC;
4344
import org.apache.jena.vocabulary.RDF;
4445
import org.jdom2.Document;
@@ -128,6 +129,7 @@ protected void compute() {
128129

129130
FedoraTransaction tx = txManager.startTransaction();
130131
try {
132+
log.info("Ingesting deposit record {} as {}", bxc3Pid.getId(), bxc5Pid.getRepositoryPath());
131133
DepositRecord depRecord = repoObjFactory.createDepositRecord(bxc5Pid, bxc5Model);
132134

133135
transformAndPopulatePremis(depRecord);
@@ -140,6 +142,8 @@ protected void compute() {
140142
} finally {
141143
tx.close();
142144
}
145+
146+
log.debug("Finished ingest of deposit record {}", bxc3Pid.getId());
143147
}
144148

145149
private boolean isDepositRecord(Resource bxc3Resc) {
@@ -177,7 +181,13 @@ private void populateDepositProperties(Resource bxc3Resc, Resource bxc5Resc) {
177181
private void addLiteralIfPresent(Resource bxc3Resc, Property bxc3Property,
178182
Resource bxc5Resc, Property bxc5Property) {
179183
if (bxc3Resc.hasProperty(bxc3Property)) {
180-
String val = bxc3Resc.getProperty(bxc3Property).getString();
184+
Statement prop = bxc3Resc.getProperty(bxc3Property);
185+
String val;
186+
if (prop.getObject().isResource()) {
187+
val = prop.getResource().getURI();
188+
} else {
189+
val = prop.getString();
190+
}
181191
bxc5Resc.addLiteral(bxc5Property, val);
182192
}
183193
}

0 commit comments

Comments
 (0)