Skip to content

Instantly share code, notes, and snippets.

@wyaeld
Created November 15, 2011 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wyaeld/1365805 to your computer and use it in GitHub Desktop.
Save wyaeld/1365805 to your computer and use it in GitHub Desktop.
DI Style Service
public class ArchiveServiceMessagesJob extends AbstractJob {
private static final int PAGE_SIZE = 10;
private ServiceMessageRepository messageRepository;
private AlfrescoAtomPubClient alfrescoClient;
@Autowired
public ArchiveServiceMessagesJob(ServiceMessageRepository messageRepository,
AlfrescoAtomPubClient alfrescoClient) {
this.messageRepository = messageRepository;
this.alfrescoClient = alfrescoClient;
}
public void archiveMessagesOlderThanNinetyDays() {
alfrescoClient.connect();
Folder archiveFolder = findArchiveFolder();
int currentPage = 1;
boolean hasMorePages = true;
while(hasMorePages) {
PageRequest pageRequest = new PageRequest(currentPage, PAGE_SIZE);
//Processing the messages inside a transaction
hasMorePages = processPage(archiveFolder, pageRequest);
currentPage++;
}
alfrescoClient.disconnect();
}
@Transactional
private boolean processPage(Folder archiveFolder, PageRequest pageRequest) {
Page<ServiceMessage> page =
messageRepository.findAll(allOf(application(), olderThanNinetyDays(), notAlreadyArchived()), pageRequest);
for (ServiceMessage message : page.getContent()) {
DocumentRequest document = new DocumentRequest(message.getContent().getBytes(),
message.getId() + ".json", DocumentRequest.PLAINTEXT);
AlfrescoDocument alfDoc =
alfrescoClient.saveDocument(archiveFolder, document, FilingStrategy.BY_DATE);
message.setContent(alfDoc.getId());
message.setStatus(Status.ARCHIVED);
messageRepository.save(message);
}
return page.hasNextPage();
}
private Folder findArchiveFolder() {
try {
return alfrescoClient.getFolderByPath("/Archive");
} catch (CmisObjectNotFoundException e) {
return alfrescoClient.createApplicationFolder("Archive");
}
}
}
@RunWith(MockitoJUnitRunner.class)
public class ArchiveServiceMessagesJobTest {
@Mock ServiceMessageRepository messageRepository;
@Mock AlfrescoAtomPubClient alfrescoClient;
@Mock AlfrescoDocument mockDocument;
@InjectMocks ArchiveServiceMessagesJob job;
@Test
public void givenNoMessagesWorks() {
List<ServiceMessage> messages = Lists.newArrayList();
Page<ServiceMessage> page = new PageImpl<ServiceMessage>(messages);
when(messageRepository.findAll(any(Predicate.class), any(Pageable.class)))
.thenReturn(page);
when(alfrescoClient.saveDocument(
any(Folder.class), any(DocumentRequest.class), any(FilingStrategy.class)))
.thenReturn(mockDocument);
job.archiveMessagesOlderThanNinetyDays();
}
@Test
public void givenOneMessageWorks() {
ServiceMessage message = new ServiceMessage();
message.setDateCreated(DateUtils.addDays(message.getDateCreated(), -100));
message.setContent("{some json application content}");
message.setId(123L);
List<ServiceMessage> messages = Lists.newArrayList(message);
Pageable pageable = new PageRequest(1, 10);
Page<ServiceMessage> page = new PageImpl<ServiceMessage>(messages,pageable,1);
when(messageRepository.findAll(any(Predicate.class), any(Pageable.class)))
.thenReturn(page);
when(alfrescoClient.saveDocument(
any(Folder.class), any(DocumentRequest.class), any(FilingStrategy.class)))
.thenReturn(mockDocument);
when(mockDocument.getId()).thenReturn("alf-1");
job.archiveMessagesOlderThanNinetyDays();
assertThat(message.getContent(), is("alf-1"));
assertThat(message.getStatus(), is(Status.ARCHIVED));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment