@@ -58,16 +58,17 @@ class CollectionRepository(
58
58
// 2. Load the resource container
59
59
val metadata = project.resourceContainer
60
60
if (metadata != null ) {
61
- val container = ResourceContainer .load(metadata.path)
62
- // 3. Remove the project from the manifest
63
- container.manifest.projects = container.manifest.projects.filter { it.identifier != project.slug }
64
- // 4a. If the manifest has more projects, write out the new manifest
65
- if (container.manifest.projects.isNotEmpty()) {
66
- container.writeManifest()
67
- } else {
68
- // 4b. If the manifest has no projects left, delete the RC folder and the metadata from the database
69
- metadata.path.deleteRecursively()
70
- metadataDao.delete(metadataMapper.mapToEntity(metadata))
61
+ ResourceContainer .load(metadata.path).use { container ->
62
+ // 3. Remove the project from the manifest
63
+ container.manifest.projects = container.manifest.projects.filter { it.identifier != project.slug }
64
+ // 4a. If the manifest has more projects, write out the new manifest
65
+ if (container.manifest.projects.isNotEmpty()) {
66
+ container.writeManifest()
67
+ } else {
68
+ // 4b. If the manifest has no projects left, delete the RC folder and the metadata from the database
69
+ metadata.path.deleteRecursively()
70
+ metadataDao.delete(metadataMapper.mapToEntity(metadata))
71
+ }
71
72
}
72
73
}
73
74
}.andThen(
@@ -212,6 +213,7 @@ class CollectionRepository(
212
213
dublinCore.mapToMetadata(File (" ." ), targetLanguage),
213
214
metadata
214
215
)
216
+ // TODO 2/14/19: Move create to ResourceContainer to be able to create a zip resource container?
215
217
val container = ResourceContainer .create(directory) {
216
218
// Set up the manifest
217
219
manifest = Manifest (
@@ -240,16 +242,17 @@ class CollectionRepository(
240
242
241
243
val metadataEntity = if (matches.isEmpty()) {
242
244
// This combination of identifier and language does not already exist; create it
243
- val container = createResourceContainer(source, language)
244
- // Convert DublinCore to ResourceMetadata
245
- val metadata = container.manifest.dublinCore
246
- .mapToMetadata(container.dir , language)
245
+ createResourceContainer(source, language).use { container ->
246
+ // Convert DublinCore to ResourceMetadata
247
+ val metadata = container.manifest.dublinCore
248
+ .mapToMetadata(container.file , language)
247
249
248
- // Insert ResourceMetadata into database
249
- val entity = metadataMapper.mapToEntity(metadata)
250
- entity.derivedFromFk = source.resourceContainer?.id
251
- entity.id = metadataDao.insert(entity, dsl)
252
- /* return@if */ entity
250
+ // Insert ResourceMetadata into database
251
+ val entity = metadataMapper.mapToEntity(metadata)
252
+ entity.derivedFromFk = source.resourceContainer?.id
253
+ entity.id = metadataDao.insert(entity, dsl)
254
+ /* return@if */ entity
255
+ }
253
256
} else {
254
257
// Use the existing metadata
255
258
/* return@if */ matches.first()
@@ -273,27 +276,26 @@ class CollectionRepository(
273
276
274
277
// Add a project to the container if necessary
275
278
// Load the existing resource container and see if we need to add another project
276
- val container = ResourceContainer .load(File (metadataEntity.path))
277
- if (container.manifest.projects.none { it.identifier == source.slug }) {
278
- container.manifest.projects = container.manifest.projects.plus(
279
- project {
280
- sort = if (metadataEntity.subject.toLowerCase() == " bible"
281
- && projectEntity.sort > 39 ) {
282
- projectEntity.sort + 1
283
- } else {
284
- projectEntity.sort
279
+ ResourceContainer .load(File (metadataEntity.path)).use { container ->
280
+ if (container.manifest.projects.none { it.identifier == source.slug }) {
281
+ container.manifest.projects = container.manifest.projects.plus(
282
+ project {
283
+ sort = if (metadataEntity.subject.toLowerCase() == " bible"
284
+ && projectEntity.sort > 39 ) {
285
+ projectEntity.sort + 1
286
+ } else {
287
+ projectEntity.sort
288
+ }
289
+ identifier = projectEntity.slug
290
+ path = " ./${projectEntity.slug} "
291
+ // This title will not be localized into the target language
292
+ title = projectEntity.title
293
+ // Unable to get categories and versification from the source collection
285
294
}
286
- identifier = projectEntity.slug
287
- path = " ./${projectEntity.slug} "
288
- // This title will not be localized into the target language
289
- title = projectEntity.title
290
- // Unable to get these fields from the source collection
291
- categories = listOf ()
292
- versification = " "
293
- }
294
- )
295
- // Update the container
296
- container.write()
295
+ )
296
+ // Update the container
297
+ container.write()
298
+ }
297
299
}
298
300
}
299
301
}
0 commit comments