Skip to content

Commit

Permalink
Merge pull request #131 from diging/develop
Browse files Browse the repository at this point in the history
CITE-138
  • Loading branch information
jdamerow authored Feb 4, 2021
2 parents 22638f6 + 9a10f6d commit c48358e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface Indexer {

void indexCitation(ICitation citation);

void deleteCitation(ICitation citation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public void indexCitation(ICitation citation) {
referenceRepo.save(reference);
}

@Override
public void deleteCitation(ICitation citation) {
referenceRepo.deleteById(citation.getKey());
}

private void mapCitation(ICitation citation, Reference reference) {
reference.setAbstractNote(citation.getAbstractNote());
reference.setAccessDate(citation.getAccessDate());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.asu.diging.citesphere.core.service;

import java.util.Optional;

import edu.asu.diging.citesphere.model.bib.ICitation;

public interface ICitationStore {

/**
* Method to save citations. This method will also index
* the citation before saving.
*
* @param citation Citation to be saved.
* @return the saved Citation
*/
ICitation save(ICitation citation);

Optional<ICitation> findById(String id);

void delete(ICitation citation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -11,7 +12,6 @@
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.management.RuntimeErrorException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -22,16 +22,15 @@
import edu.asu.diging.citesphere.core.model.jobs.JobStatus;
import edu.asu.diging.citesphere.core.model.jobs.impl.GroupSyncJob;
import edu.asu.diging.citesphere.core.repository.jobs.JobRepository;
import edu.asu.diging.citesphere.core.search.service.Indexer;
import edu.asu.diging.citesphere.core.service.IAsyncCitationProcessor;
import edu.asu.diging.citesphere.core.service.ICitationStore;
import edu.asu.diging.citesphere.core.service.jobs.ISyncJobManager;
import edu.asu.diging.citesphere.core.zotero.DeletedZoteroElements;
import edu.asu.diging.citesphere.core.zotero.IZoteroManager;
import edu.asu.diging.citesphere.core.zotero.ZoteroCollectionsResponse;
import edu.asu.diging.citesphere.core.zotero.ZoteroGroupItemsResponse;
import edu.asu.diging.citesphere.data.bib.CitationCollectionRepository;
import edu.asu.diging.citesphere.data.bib.CitationGroupRepository;
import edu.asu.diging.citesphere.data.bib.CitationRepository;
import edu.asu.diging.citesphere.model.bib.ICitation;
import edu.asu.diging.citesphere.model.bib.ICitationCollection;
import edu.asu.diging.citesphere.model.bib.ICitationGroup;
Expand All @@ -49,7 +48,7 @@ public class AsyncCitationProcessor implements IAsyncCitationProcessor {
private IZoteroManager zoteroManager;

@Autowired
private CitationRepository citationRepo;
private ICitationStore citationStore;

@Autowired
private CitationGroupRepository groupRepo;
Expand All @@ -63,14 +62,11 @@ public class AsyncCitationProcessor implements IAsyncCitationProcessor {
@Autowired
private ISyncJobManager jobManager;

@Autowired
private Indexer indexer;

private List<JobStatus> inactiveJobStatuses;

@PostConstruct
public void init() {
inactiveJobStatuses = new ArrayList<>();
inactiveJobStatuses = Collections.synchronizedList(new ArrayList<JobStatus>());
inactiveJobStatuses.add(JobStatus.CANCELED);
inactiveJobStatuses.add(JobStatus.DONE);
inactiveJobStatuses.add(JobStatus.FAILURE);
Expand Down Expand Up @@ -145,7 +141,7 @@ private void syncCitations(IUser user, String groupId, GroupSyncJob job, Map<Str
AtomicInteger counter) {
List<String> keysToRetrieve = new ArrayList<>();
for (String key : versions.keySet()) {
Optional<ICitation> citation = citationRepo.findByKey(key);
Optional<ICitation> citation = citationStore.findById(key);

if (citation.isPresent()) {
if (citation.get().getVersion() != versions.get(key)) {
Expand Down Expand Up @@ -198,9 +194,9 @@ private void syncCollections(IUser user, String groupId, GroupSyncJob job, Map<S
private void removeDeletedItems(DeletedZoteroElements deletedElements, GroupSyncJob job) {
if (deletedElements.getItems() != null) {
for (String key : deletedElements.getItems()) {
Optional<ICitation> citation = citationRepo.findByKey(key);
Optional<ICitation> citation = citationStore.findById(key);
if (citation.isPresent()) {
citationRepo.delete((Citation) citation.get());
citationStore.delete((Citation) citation.get());
}
job.setCurrent(job.getCurrent() + 1);
jobRepo.save(job);
Expand Down Expand Up @@ -235,12 +231,12 @@ private long retrieveCollections(IUser user, String groupId, List<String> keysTo
}

private void storeCitation(ICitation citation) {
Optional<ICitation> optional = citationRepo.findByKey(citation.getKey());
Optional<ICitation> optional = citationStore.findById(citation.getKey());
if (optional.isPresent()) {
citationRepo.delete((Citation) optional.get());
citationStore.delete((Citation) optional.get());
}
citationRepo.save((Citation) citation);
indexer.indexCitation(citation);

citationStore.save((Citation) citation);
}

private void storeCitationCollection(ICitationCollection collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@
import edu.asu.diging.citesphere.core.service.IAsyncCitationProcessor;
import edu.asu.diging.citesphere.core.service.ICitationCollectionManager;
import edu.asu.diging.citesphere.core.service.ICitationManager;
import edu.asu.diging.citesphere.core.service.ICitationStore;
import edu.asu.diging.citesphere.core.service.IGroupManager;
import edu.asu.diging.citesphere.core.zotero.IZoteroManager;
import edu.asu.diging.citesphere.data.bib.CitationGroupRepository;
import edu.asu.diging.citesphere.data.bib.CitationRepository;
import edu.asu.diging.citesphere.data.bib.ICitationDao;
import edu.asu.diging.citesphere.model.bib.ICitation;
import edu.asu.diging.citesphere.model.bib.ICitationCollection;
import edu.asu.diging.citesphere.model.bib.ICitationGroup;
import edu.asu.diging.citesphere.model.bib.ItemType;
import edu.asu.diging.citesphere.model.bib.impl.BibField;
import edu.asu.diging.citesphere.model.bib.impl.Citation;
import edu.asu.diging.citesphere.model.bib.impl.CitationGroup;
import edu.asu.diging.citesphere.model.bib.impl.CitationResults;
import edu.asu.diging.citesphere.user.IUser;
Expand All @@ -67,8 +66,8 @@ public class CitationManager implements ICitationManager {
private CitationGroupRepository groupRepository;

@Autowired
private CitationRepository citationRepository;

private ICitationStore citationStore;
@Autowired
private ICitationDao citationDao;

Expand Down Expand Up @@ -96,7 +95,7 @@ public void init() {
@Override
public ICitation getCitation(IUser user, String groupId, String key)
throws GroupDoesNotExistException, CannotFindCitationException, ZoteroHttpStatusException {
Optional<ICitation> optional = citationRepository.findByKey(key);
Optional<ICitation> optional = citationStore.findById(key);
if (optional.isPresent()) {
ICitation citation = optional.get();
ICitationGroup group = groupManager.getGroup(user, groupId);
Expand Down Expand Up @@ -138,17 +137,17 @@ public ICitation getCitationFromZotero(IUser user, String groupId, String key) t
public void updateCitation(IUser user, String groupId, ICitation citation)
throws ZoteroConnectionException, CitationIsOutdatedException, ZoteroHttpStatusException {
long citationVersion = zoteroManager.getGroupItemVersion(user, groupId, citation.getKey());
Optional<ICitation> storedCitationOptional = citationRepository.findByKey(citation.getKey());
Optional<ICitation> storedCitationOptional = citationStore.findById(citation.getKey());
if (storedCitationOptional.isPresent()) {
ICitation storedCitation = storedCitationOptional.get();
if (storedCitation.getVersion() != citationVersion) {
throw new CitationIsOutdatedException();
}
citationRepository.delete((Citation) storedCitationOptional.get());
citationStore.delete(storedCitationOptional.get());
}

ICitation updatedCitation = zoteroManager.updateCitation(user, groupId, citation);
citationRepository.save((Citation) updatedCitation);
citationStore.save(updatedCitation);
}

@Override
Expand All @@ -161,7 +160,7 @@ public ICitation createCitation(IUser user, String groupId, List<String> collect
}

ICitation newCitation = zoteroManager.createCitation(user, groupId, collectionIds, citation);
return citationRepository.save((Citation) newCitation);
return citationStore.save(newCitation);

// mark group outdated, so it'll be updated on the next loading
// ICitationGroup group = groupOptional.get();
Expand All @@ -182,11 +181,11 @@ public ICitation updateCitationFromZotero(IUser user, String groupId, String ite
ICitation citation = zoteroManager.getGroupItem(user, groupId, itemKey);
citation.setGroup(groupOptional.get().getGroupId() + "");

Optional<ICitation> oldCitation = citationRepository.findByKey(itemKey);
Optional<ICitation> oldCitation = citationStore.findById(itemKey);
if(oldCitation.isPresent()) {
citationRepository.delete((Citation)oldCitation.get());
citationStore.delete(oldCitation.get());
}
citationRepository.save((Citation) citation);
citationStore.save(citation);
return citation;
} catch (HttpClientErrorException ex) {
throw new CannotFindCitationException(ex);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package edu.asu.diging.citesphere.core.service.impl;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.asu.diging.citesphere.core.search.service.Indexer;
import edu.asu.diging.citesphere.core.service.ICitationStore;
import edu.asu.diging.citesphere.data.bib.CitationRepository;
import edu.asu.diging.citesphere.model.bib.ICitation;
import edu.asu.diging.citesphere.model.bib.impl.Citation;

/**
* Service class to store and retrieve {@link ICitation}s.
*
* @author Julia Damerow
*
*/
@Service
public class CitationStore implements ICitationStore {

@Autowired
private CitationRepository citationRepository;

@Autowired
private Indexer indexer;

/* (non-Javadoc)
* @see edu.asu.diging.citesphere.core.service.impl.ICitationStore#save(edu.asu.diging.citesphere.model.bib.ICitation)
*/
@Override
public ICitation save(ICitation citation) {
indexer.indexCitation(citation);
return citationRepository.save((Citation) citation);
}

/* (non-Javadoc)
* @see edu.asu.diging.citesphere.core.service.impl.ICitationStore#findById(java.lang.String)
*/
@Override
public Optional<ICitation> findById(String id) {
return citationRepository.findByKey(id);
}

@Override
public void delete(ICitation citation) {
indexer.deleteCitation(citation);
citationRepository.delete((Citation) citation);
}
}
4 changes: 2 additions & 2 deletions citesphere/src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
<level value="${log.level}" />
</logger>
<logger name="org.springframework.data">
<level value="debug" />
<level value="${log.level}" />
</logger>
<logger name="org.elasticsearch">
<level value="debug" />
<level value="${log.level}" />
</logger>

<logger name="org.hibernate.SQL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import edu.asu.diging.citesphere.core.exceptions.GroupDoesNotExistException;
import edu.asu.diging.citesphere.core.exceptions.ZoteroHttpStatusException;
import edu.asu.diging.citesphere.core.exceptions.ZoteroItemCreationFailedException;
import edu.asu.diging.citesphere.core.service.ICitationStore;
import edu.asu.diging.citesphere.core.service.IGroupManager;
import edu.asu.diging.citesphere.core.zotero.IZoteroManager;
import edu.asu.diging.citesphere.data.bib.CitationGroupRepository;
Expand All @@ -44,7 +45,7 @@ public class CitationManagerTest {
private CitationGroupRepository groupRepository;

@Mock
private CitationRepository citationRepository;
private ICitationStore citationStore;

@Mock
private IGroupManager groupManager;
Expand Down Expand Up @@ -87,11 +88,11 @@ public void setUp() throws ZoteroHttpStatusException {
existingCitation.setKey(EXISTING_ID);
existingCitation.setVersion(currentVersion);
existingCitation.setGroup(GROUP_ID);
Mockito.when(citationRepository.findByKey(EXISTING_ID)).thenReturn(Optional.of(existingCitation));
Mockito.when(citationStore.findById(EXISTING_ID)).thenReturn(Optional.of(existingCitation));

zoteroCitation.setKey(ZOTERO_CITATION_ID);
Mockito.when(zoteroManager.getGroupItem(user, GROUP_ID, ZOTERO_CITATION_ID)).thenReturn(zoteroCitation);
Mockito.when(citationRepository.findByKey(ZOTERO_CITATION_ID)).thenReturn(Optional.empty());
Mockito.when(citationStore.findById(ZOTERO_CITATION_ID)).thenReturn(Optional.empty());

group1 = new CitationGroup();
group1.setGroupId(GROUP1_ID);
Expand Down Expand Up @@ -146,7 +147,7 @@ public void test_updateCitation_success() throws ZoteroConnectionException, Cita
Mockito.when(zoteroManager.updateCitation(user, GROUP_ID, existingCitation)).thenReturn(updatedCitation);

managerToTest.updateCitation(user, GROUP_ID, existingCitation);
Mockito.verify(citationRepository).save((Citation)updatedCitation);
Mockito.verify(citationStore).save(updatedCitation);
}

@Test(expected=CitationIsOutdatedException.class)
Expand All @@ -162,7 +163,7 @@ public void test_updateCitationFromZotero_success() throws GroupDoesNotExistExce
updatedCitation.setVersion(new Long(2));
Mockito.when(zoteroManager.getGroupItem(user, GROUP_ID, EXISTING_ID)).thenReturn(updatedCitation);
ICitation actual = managerToTest.updateCitationFromZotero(user, GROUP_ID, EXISTING_ID);
Mockito.verify(citationRepository).save((Citation)updatedCitation);
Mockito.verify(citationStore).save(updatedCitation);
Assert.assertEquals(updatedCitation, actual);
}

Expand Down Expand Up @@ -238,10 +239,10 @@ public void test_createCitation_success() throws ZoteroConnectionException, Zote

Citation newCitation = new Citation();
Mockito.when(zoteroManager.createCitation(user, GROUP_ID, new ArrayList<>(), newCitation)).thenReturn(createdCitation);
Mockito.when(citationRepository.save(createdCitation)).thenReturn(createdCitation);
Mockito.when(citationStore.save(createdCitation)).thenReturn(createdCitation);

ICitation actual = managerToTest.createCitation(user, GROUP_ID, new ArrayList<>(), newCitation);
Mockito.verify(citationRepository).save((Citation)createdCitation);
Mockito.verify(citationStore).save(createdCitation);
Assert.assertEquals(createdCitation, actual);
}

Expand Down

0 comments on commit c48358e

Please sign in to comment.