Skip to content

Commit f8d1f53

Browse files
committed
Changed page hash loading to work on whole comic books [#2433]
* Moved the classes to the .comicbooks package. * Renamed the classes to ProcessUnhashedComics Also refactored AbstractComicBookReader and its children to move the ComicBookService reference into the parent class.
1 parent 9747513 commit f8d1f53

File tree

37 files changed

+451
-371
lines changed

37 files changed

+451
-371
lines changed

comixed-app/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ spring.session.jdbc.initialize-schema=always
7272
# Batch processing
7373
spring.batch.jdbc.initialize-schema=always
7474
spring.batch.job.enabled=true
75-
spring.batch.job.name=loadPageHashesJob
75+
spring.batch.job.name=processUnhashedComicsJob
7676
spring.batch.jdbc.isolation-level-for-create=repeatable_read
7777
comixed.batch.thread-pool-size=-1
7878
comixed.batch.page-reader.chunk-size=1

comixed-batch/src/main/java/org/comixedproject/batch/comicpages/LoadPageHashesConfiguration.java renamed to comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/ProcessUnhashedComicsConfiguration.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses>
1717
*/
1818

19-
package org.comixedproject.batch.comicpages;
19+
package org.comixedproject.batch.comicbooks;
2020

2121
import lombok.extern.log4j.Log4j2;
22-
import org.comixedproject.batch.comicpages.listeners.LoadPageHashChunkListener;
23-
import org.comixedproject.batch.comicpages.listeners.LoadPageHashesJobListener;
24-
import org.comixedproject.batch.comicpages.processors.LoadPageHashProcessor;
25-
import org.comixedproject.batch.comicpages.readers.LoadPageHashReader;
26-
import org.comixedproject.batch.comicpages.writers.LoadPageHashWriter;
27-
import org.comixedproject.model.comicpages.ComicPage;
22+
import org.comixedproject.batch.comicbooks.listeners.ProcessUnhashedComicsChunkListener;
23+
import org.comixedproject.batch.comicbooks.listeners.ProcessUnhashedComicsJobListener;
24+
import org.comixedproject.batch.comicbooks.processors.ProcessUnhashedComicsProcessor;
25+
import org.comixedproject.batch.comicbooks.readers.ProcessUnhashedComicsReader;
26+
import org.comixedproject.batch.comicbooks.writers.ProcessUnhashedComicsWriter;
27+
import org.comixedproject.model.comicbooks.ComicBook;
2828
import org.springframework.batch.core.Job;
2929
import org.springframework.batch.core.Step;
3030
import org.springframework.batch.core.job.builder.JobBuilder;
@@ -38,29 +38,31 @@
3838
import org.springframework.transaction.PlatformTransactionManager;
3939

4040
/**
41-
* <code>LoadPageHashesConfiguration</code> defines a batch job that loads the hashes for comic
42-
* pages.
41+
* <code>ProcessUnhashedComicsConfiguration</code> defines a batch job that loads the hashes for
42+
* comic pages.
4343
*
4444
* @author Darryl L. Pierce
4545
*/
4646
@Configuration
4747
@Log4j2
48-
public class LoadPageHashesConfiguration {
49-
public static final String LOAD_PAGE_HASHES_JOB = "loadPageHashesJob";
50-
public static final String JOB_LOAD_PAGE_HASHES_STARTED = "job.load-page-hashes.started";
48+
public class ProcessUnhashedComicsConfiguration {
49+
public static final String PROCESS_UNHASHED_COMICS_JOB = "processUnhashedComicsJob";
50+
public static final String PROCESS_UNHASHED_COMICS_STEP = "processUnhashedComicsStep";
51+
public static final String JOB_PROCESS_UNHASHED_COMICS_STARTED =
52+
"job.process-unhashed-comics.started";
5153

5254
@Value("${comixed.batch.load-page-hashes.chunk-size:10}")
5355
private int chunkSize;
5456

55-
@Bean(name = LOAD_PAGE_HASHES_JOB)
57+
@Bean(name = PROCESS_UNHASHED_COMICS_JOB)
5658
public Job loadPageHashesJob(
5759
final JobRepository jobRepository,
58-
final LoadPageHashesJobListener listener,
59-
@Qualifier("loadPageHashStep") final Step loadPageHashStep) {
60-
return new JobBuilder(LOAD_PAGE_HASHES_JOB, jobRepository)
60+
final ProcessUnhashedComicsJobListener listener,
61+
@Qualifier(PROCESS_UNHASHED_COMICS_STEP) final Step processUnhashedComicsStep) {
62+
return new JobBuilder(PROCESS_UNHASHED_COMICS_JOB, jobRepository)
6163
.incrementer(new RunIdIncrementer())
6264
.listener(listener)
63-
.start(loadPageHashStep)
65+
.start(processUnhashedComicsStep)
6466
.build();
6567
}
6668

@@ -75,16 +77,16 @@ public Job loadPageHashesJob(
7577
* @param chunkListener the chunk listener
7678
* @return the step
7779
*/
78-
@Bean(name = "loadPageHashStep")
79-
public Step loadPageHashStep(
80+
@Bean(name = PROCESS_UNHASHED_COMICS_STEP)
81+
public Step processUnhashedComicsStep(
8082
final JobRepository jobRepository,
8183
final PlatformTransactionManager platformTransactionManager,
82-
final LoadPageHashReader reader,
83-
final LoadPageHashProcessor processor,
84-
final LoadPageHashWriter writer,
85-
final LoadPageHashChunkListener chunkListener) {
86-
return new StepBuilder("loadPageHashStep", jobRepository)
87-
.<ComicPage, ComicPage>chunk(this.chunkSize, platformTransactionManager)
84+
final ProcessUnhashedComicsReader reader,
85+
final ProcessUnhashedComicsProcessor processor,
86+
final ProcessUnhashedComicsWriter writer,
87+
final ProcessUnhashedComicsChunkListener chunkListener) {
88+
return new StepBuilder(PROCESS_UNHASHED_COMICS_STEP, jobRepository)
89+
.<ComicBook, ComicBook>chunk(this.chunkSize, platformTransactionManager)
8890
.reader(reader)
8991
.processor(processor)
9092
.writer(writer)

comixed-batch/src/main/java/org/comixedproject/batch/comicpages/listeners/LoadPageHashChunkListener.java renamed to comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/listeners/ProcessUnhashedComicsChunkListener.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,42 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses>
1717
*/
1818

19-
package org.comixedproject.batch.comicpages.listeners;
19+
package org.comixedproject.batch.comicbooks.listeners;
2020

2121
import static org.comixedproject.model.messaging.batch.ProcessComicBooksStatus.*;
2222

2323
import lombok.extern.log4j.Log4j2;
24-
import org.comixedproject.batch.comicbooks.listeners.AbstractBatchProcessChunkListener;
2524
import org.springframework.batch.core.configuration.annotation.StepScope;
2625
import org.springframework.stereotype.Component;
2726

2827
/**
29-
* <code>LoadPageHashChunkListener</code> sends notifications as the page hash job processes each
30-
* chunk of data.
28+
* <code>ProcessUnhashedComicsChunkListener</code> sends notifications as the page hash job
29+
* processes each chunk of data.
3130
*
3231
* @author Darryl L. Pierce
3332
*/
3433
@Component
3534
@StepScope
3635
@Log4j2
37-
public class LoadPageHashChunkListener extends AbstractBatchProcessChunkListener {
36+
public class ProcessUnhashedComicsChunkListener extends AbstractBatchProcessChunkListener {
3837
@Override
3938
protected String getStepName() {
4039
return LOAD_PAGE_HASH_STEP;
4140
}
4241

4342
@Override
4443
protected boolean isActive() {
45-
return this.comicPageService.getPagesWithoutHashCount() > 0;
44+
return this.comicBookService.findComicsWithUnhashedPagesCount() > 0;
4645
}
4746

4847
@Override
4948
protected long getProcessedElements() {
50-
return this.comicPageService.getCount() - this.comicPageService.getPagesWithoutHashCount();
49+
return this.comicBookService.getComicBookCount()
50+
- this.comicBookService.findComicsWithUnhashedPagesCount();
5151
}
5252

5353
@Override
5454
protected long getTotalElements() {
55-
return this.comicPageService.getCount();
55+
return this.comicBookService.getComicBookCount();
5656
}
5757
}

comixed-batch/src/main/java/org/comixedproject/batch/comicpages/listeners/LoadPageHashesJobListener.java renamed to comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/listeners/ProcessUnhashedComicsJobListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses>
1717
*/
1818

19-
package org.comixedproject.batch.comicpages.listeners;
19+
package org.comixedproject.batch.comicbooks.listeners;
2020

2121
import lombok.extern.log4j.Log4j2;
2222
import org.comixedproject.batch.listeners.AbstractBatchProcessListener;
@@ -27,14 +27,14 @@
2727
import org.springframework.stereotype.Component;
2828

2929
/**
30-
* <code>LoadPageHashesJobListener</code> sends job updates while loading page hashes.
30+
* <code>ProcessUnhashedComicsJobListener</code> sends job updates while loading page hashes.
3131
*
3232
* @author Darryl L. Pierce
3333
*/
3434
@Component
3535
@JobScope
3636
@Log4j2
37-
public class LoadPageHashesJobListener extends AbstractBatchProcessListener
37+
public class ProcessUnhashedComicsJobListener extends AbstractBatchProcessListener
3838
implements JobExecutionListener {
3939
@Override
4040
public void beforeJob(final JobExecution jobExecution) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* ComiXed - A digital comic book library management application.
3+
* Copyright (C) 2024, The ComiXed Project
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses>
17+
*/
18+
19+
package org.comixedproject.batch.comicbooks.processors;
20+
21+
import java.awt.image.BufferedImage;
22+
import java.io.ByteArrayInputStream;
23+
import java.util.Objects;
24+
import javax.imageio.ImageIO;
25+
import lombok.extern.log4j.Log4j2;
26+
import org.comixedproject.adaptors.GenericUtilitiesAdaptor;
27+
import org.comixedproject.adaptors.comicbooks.ComicBookAdaptor;
28+
import org.comixedproject.model.comicbooks.ComicBook;
29+
import org.springframework.batch.item.ItemProcessor;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.stereotype.Component;
32+
import org.springframework.util.StringUtils;
33+
34+
/**
35+
* <code>ProcessUnhashedComicsProcessor</code> loads the contents for the unhashed pages of a comic
36+
* book, then sets the page's hash.
37+
*
38+
* @author Darryl L. Pierce
39+
*/
40+
@Component
41+
@Log4j2
42+
public class ProcessUnhashedComicsProcessor implements ItemProcessor<ComicBook, ComicBook> {
43+
@Autowired private ComicBookAdaptor comicBookAdaptor;
44+
@Autowired private GenericUtilitiesAdaptor genericUtilitiesAdaptor;
45+
46+
@Override
47+
public ComicBook process(final ComicBook comicBook) {
48+
log.debug(
49+
"Loading page hashes for comic book: {}", comicBook.getComicDetail().getBaseFilename());
50+
comicBook.getPages().stream()
51+
.filter(page -> Objects.nonNull(page))
52+
.filter(page -> !StringUtils.hasLength(page.getHash()))
53+
.forEach(
54+
page -> {
55+
try {
56+
final byte[] content =
57+
this.comicBookAdaptor.loadPageContent(comicBook, page.getPageNumber());
58+
log.trace("Setting page hash");
59+
page.setHash(this.genericUtilitiesAdaptor.createHash(content));
60+
log.trace("Setting page dimensions");
61+
final BufferedImage image = ImageIO.read(new ByteArrayInputStream(content));
62+
page.setWidth(image.getWidth());
63+
page.setHeight(image.getHeight());
64+
} catch (Exception error) {
65+
log.error("Failed to set page details", error);
66+
}
67+
});
68+
69+
return comicBook;
70+
}
71+
}

comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/readers/AbstractComicReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import lombok.Setter;
2424
import lombok.extern.log4j.Log4j2;
2525
import org.comixedproject.model.comicbooks.ComicBook;
26+
import org.comixedproject.service.comicbooks.ComicBookService;
2627
import org.springframework.batch.core.StepExecutionListener;
2728
import org.springframework.batch.item.ItemReader;
29+
import org.springframework.beans.factory.annotation.Autowired;
2830

2931
/**
3032
* <code>AbstractComicReader</code> provides a foundation for building new {@link ItemReader}
@@ -34,6 +36,8 @@
3436
*/
3537
@Log4j2
3638
public abstract class AbstractComicReader implements ItemReader<ComicBook>, StepExecutionListener {
39+
@Autowired ComicBookService comicBookService;
40+
3741
@Getter String batchName;
3842
@Getter @Setter List<ComicBook> comicBookList = null;
3943

comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/readers/CreateMetadataSourceReader.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import lombok.Getter;
2323
import lombok.extern.log4j.Log4j2;
2424
import org.comixedproject.model.comicbooks.ComicBook;
25-
import org.comixedproject.service.comicbooks.ComicBookService;
26-
import org.springframework.beans.factory.annotation.Autowired;
2725
import org.springframework.beans.factory.annotation.Value;
2826
import org.springframework.stereotype.Component;
2927

@@ -35,8 +33,6 @@ public class CreateMetadataSourceReader extends AbstractComicReader {
3533
@Getter
3634
private int chunkSize;
3735

38-
@Autowired private ComicBookService comicBookService;
39-
4036
@Override
4137
protected List<ComicBook> doLoadComics() {
4238
return this.comicBookService.findComicsWithCreateMetadataFlagSet(this.chunkSize);

comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/readers/LoadFileContentsReader.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import lombok.Getter;
2323
import lombok.extern.log4j.Log4j2;
2424
import org.comixedproject.model.comicbooks.ComicBook;
25-
import org.comixedproject.service.comicbooks.ComicBookService;
2625
import org.springframework.batch.core.configuration.annotation.StepScope;
27-
import org.springframework.beans.factory.annotation.Autowired;
2826
import org.springframework.beans.factory.annotation.Value;
2927
import org.springframework.stereotype.Component;
3028

@@ -41,8 +39,6 @@ public class LoadFileContentsReader extends AbstractComicReader {
4139
@Getter
4240
private int chunkSize;
4341

44-
@Autowired private ComicBookService comicBookService;
45-
4642
@Override
4743
protected List<ComicBook> doLoadComics() {
4844
return this.comicBookService.findComicsWithContentToLoad(this.chunkSize);

comixed-batch/src/main/java/org/comixedproject/batch/comicpages/readers/LoadPageHashReader.java renamed to comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/readers/ProcessUnhashedComicsReader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses>
1717
*/
1818

19-
package org.comixedproject.batch.comicpages.readers;
19+
package org.comixedproject.batch.comicbooks.readers;
2020

2121
import java.util.List;
2222
import lombok.Getter;
2323
import lombok.extern.log4j.Log4j2;
24-
import org.comixedproject.model.comicpages.ComicPage;
24+
import org.comixedproject.model.comicbooks.ComicBook;
2525
import org.springframework.batch.core.configuration.annotation.StepScope;
2626
import org.springframework.beans.factory.annotation.Value;
2727
import org.springframework.stereotype.Component;
2828

2929
/**
30-
* <code>LoadPageHashReader</code> loads pages that do not have a hash.
30+
* <code>ProcessUnhashedComicsReader</code> loads pages that do not have a hash.
3131
*
3232
* @author Darryl L. Pierce
3333
*/
3434
@StepScope
3535
@Component
3636
@Log4j2
37-
public class LoadPageHashReader extends AbstractPageReader {
37+
public class ProcessUnhashedComicsReader extends AbstractComicReader {
3838
@Value("${comixed.batch.mark-blocked-pages.chunk-size:1}")
3939
@Getter
4040
private int chunkSize;
4141

4242
@Override
43-
protected List<ComicPage> doLoadPages() {
43+
protected List<ComicBook> doLoadComics() {
4444
log.trace("Loading pages without a hash");
45-
return this.comicPageService.getPagesWithoutHash(this.chunkSize);
45+
return this.comicBookService.findComicsWithUnhashedPages(this.chunkSize);
4646
}
4747
}

comixed-batch/src/main/java/org/comixedproject/batch/comicbooks/readers/PurgeMarkedComicsReader.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import lombok.Getter;
2323
import lombok.extern.log4j.Log4j2;
2424
import org.comixedproject.model.comicbooks.ComicBook;
25-
import org.comixedproject.service.comicbooks.ComicBookService;
2625
import org.springframework.batch.core.configuration.annotation.StepScope;
27-
import org.springframework.beans.factory.annotation.Autowired;
2826
import org.springframework.beans.factory.annotation.Value;
2927
import org.springframework.stereotype.Component;
3028

@@ -41,8 +39,6 @@ public class PurgeMarkedComicsReader extends AbstractComicReader {
4139
@Getter
4240
private int chunkSize;
4341

44-
@Autowired private ComicBookService comicBookService;
45-
4642
@Override
4743
protected List<ComicBook> doLoadComics() {
4844
log.trace("Loading comics marked for purging");

0 commit comments

Comments
 (0)