-
Notifications
You must be signed in to change notification settings - Fork 4
Story/CITE-173 : Reprocessing Documents #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 12 commits
c3c5bb7
c4344a2
6eba322
2fc2246
7be90f9
ed05b54
050995d
97cb808
3cee57b
0ece8b6
b662de3
ec2db03
459db44
8520d9f
eb8a85f
8286dfd
f7a8991
c8bfddc
85de85a
b0922ad
cfb6579
d7e2c63
8e5b213
b660fb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| package edu.asu.diging.citesphere.core.service.giles; | ||
|
|
||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
|
|
||
| import edu.asu.diging.citesphere.user.IUser; | ||
|
|
||
| public interface IGilesConnector { | ||
|
|
||
| <T> ResponseEntity<T> sendRequest(IUser user, String endpoint, Class<T> returnType) | ||
| <T> ResponseEntity<T> sendRequest(IUser user, String endpoint, Class<T> returnType, HttpMethod httpMethod) | ||
| throws HttpClientErrorException; | ||
|
|
||
| byte[] getFile(IUser user, String fileId); | ||
|
|
||
| } | ||
|
|
||
| ResponseEntity<String> reprocessDocument(IUser user, String documentId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| package edu.asu.diging.citesphere.core.service.impl; | ||
|
|
||
| import java.io.IOException; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.function.BiFunction; | ||
|
|
||
|
|
@@ -18,12 +22,16 @@ | |
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.PropertySource; | ||
| import org.springframework.data.util.CloseableIterator; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.social.zotero.api.ZoteroUpdateItemsStatuses; | ||
| import org.springframework.social.zotero.exception.ZoteroConnectionException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
||
| import edu.asu.diging.citesphere.core.exceptions.AccessForbiddenException; | ||
| import edu.asu.diging.citesphere.core.exceptions.CannotFindCitationException; | ||
|
|
@@ -37,16 +45,21 @@ | |
| 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.service.giles.GilesUploadChecker; | ||
| import edu.asu.diging.citesphere.core.service.giles.IGilesConnector; | ||
| import edu.asu.diging.citesphere.core.zotero.IZoteroManager; | ||
| import edu.asu.diging.citesphere.data.bib.CitationGroupRepository; | ||
| import edu.asu.diging.citesphere.data.bib.ICitationDao; | ||
| import edu.asu.diging.citesphere.model.bib.GilesStatus; | ||
| 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.IGilesUpload; | ||
| 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.CitationGroup; | ||
| import edu.asu.diging.citesphere.model.bib.impl.CitationResults; | ||
| import edu.asu.diging.citesphere.model.bib.impl.GilesUpload; | ||
| import edu.asu.diging.citesphere.user.IUser; | ||
|
|
||
| @Service | ||
|
|
@@ -83,6 +96,12 @@ public class CitationManager implements ICitationManager { | |
|
|
||
| @Autowired | ||
| private IAsyncCitationProcessor asyncCitationProcessor; | ||
|
|
||
| @Autowired | ||
| private IGilesConnector gilesConnector; | ||
|
|
||
| @Autowired | ||
| private GilesUploadChecker gilesUploadChecker; | ||
|
|
||
| private Map<String, BiFunction<ICitation, ICitation, Integer>> sortFunctions; | ||
|
|
||
|
|
@@ -494,4 +513,77 @@ public CitationPage getPrevAndNextCitation(IUser user, String groupId, String co | |
| public void deleteLocalGroupCitations(String groupId) { | ||
| citationStore.deleteCitationByGroupId(groupId); | ||
| } | ||
|
|
||
| @Override | ||
| public void reprocessFile(IUser user, String zoteroGroupId, String itemId, String documentId) | ||
| throws GroupDoesNotExistException, CannotFindCitationException, ZoteroHttpStatusException, | ||
| ZoteroConnectionException, CitationIsOutdatedException, ZoteroItemCreationFailedException { | ||
| ICitation citation = getCitation(user, zoteroGroupId, itemId); | ||
| for (Iterator<IGilesUpload> gilesUpload = citation.getGilesUploads().iterator(); gilesUpload.hasNext();) { | ||
|
||
| IGilesUpload upload = gilesUpload.next(); | ||
| if (upload.getDocumentId() != null && upload.getDocumentId().equals(documentId)) { | ||
| ResponseEntity<String> reprocessingResponse = gilesConnector.reprocessDocument(user, documentId); | ||
| if (reprocessingResponse.getStatusCode().equals(HttpStatus.OK)) { | ||
| IGilesUpload reprocessedUpload = new GilesUpload(); | ||
| String responseBody = reprocessingResponse.getBody(); | ||
| ObjectMapper objectMapper = new ObjectMapper(); | ||
| JsonNode jsonNode; | ||
| try { | ||
| jsonNode = objectMapper.readTree(responseBody); | ||
| String checkUrl = jsonNode.get("checkUrl").asText(); | ||
| String[] urlSegments = checkUrl.split("/"); | ||
|
||
| String progressId = urlSegments[urlSegments.length - 1]; | ||
| reprocessedUpload.setUploadingUser(user.getUsername()); | ||
| reprocessedUpload.setProgressId(progressId); | ||
| Set<IGilesUpload> checkedUploads = new HashSet<>(); | ||
| checkedUploads.add(reprocessedUpload); | ||
| updateCitationWithUpdatedGilesUpload(checkedUploads, user, citation, documentId); | ||
| gilesUploadChecker.add(citation); | ||
| } catch (IOException e) { | ||
|
||
| logger.error("Could not deserialize response.", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| private void updateCitationWithUpdatedGilesUpload(Set<IGilesUpload> checkedUploads, IUser user, ICitation citation, String documentId) { | ||
|
||
| ICitation currentCitation = getCurrentCitation(citation, user); | ||
| if (currentCitation != null) { | ||
| for (IGilesUpload upload : checkedUploads) { | ||
| Optional<IGilesUpload> oldUpload = currentCitation | ||
| .getGilesUploads().stream() | ||
| .filter(u -> u.getDocumentId() != null && u | ||
| .getDocumentId().equals(documentId)) | ||
| .findFirst(); | ||
| if (oldUpload.isPresent()) { | ||
| currentCitation.getGilesUploads().remove(oldUpload.get()); | ||
| } | ||
| currentCitation.getGilesUploads().add(upload); | ||
| } | ||
|
|
||
| try { | ||
| updateCitation(user, citation.getGroup(), | ||
| currentCitation); | ||
| } catch (ZoteroConnectionException | CitationIsOutdatedException | ||
| | ZoteroHttpStatusException | ZoteroItemCreationFailedException e) { | ||
| logger.error("Could not update citation.", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private ICitation getCurrentCitation(ICitation citation, IUser user) { | ||
| try { | ||
| return getCitation(user, | ||
| citation.getGroup(), citation.getKey()); | ||
| } catch (GroupDoesNotExistException e) { | ||
| logger.error("Could not get citation.", e); | ||
| } catch (CannotFindCitationException e) { | ||
| logger.error("Could not get citation.", e); | ||
| } catch (ZoteroHttpStatusException e) { | ||
| logger.error("Could not get citation.", e); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, it seems like a bad idea to just ignore this |
||
| return null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,22 @@ | |
| import java.util.List; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.social.zotero.exception.ZoteroConnectionException; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
|
|
||
| import edu.asu.diging.citesphere.core.exceptions.CannotFindCitationException; | ||
| import edu.asu.diging.citesphere.core.exceptions.CitationIsOutdatedException; | ||
| 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.search.service.SearchEngine; | ||
| import edu.asu.diging.citesphere.core.service.ICitationManager; | ||
| import edu.asu.diging.citesphere.core.service.impl.CitationPage; | ||
|
|
@@ -66,4 +72,15 @@ public String getItem(Authentication authentication, Model model, @PathVariable( | |
| } | ||
| return "auth/group/item"; | ||
| } | ||
|
|
||
| @RequestMapping(value = "/auth/group/{zoteroGroupId}/file/reprocess", method = RequestMethod.POST) | ||
| public ResponseEntity<String> deleteFile(Authentication authentication, | ||
| @PathVariable("zoteroGroupId") String zoteroGroupId, | ||
| @RequestParam(value = "documentId", required = false) String documentId, | ||
| @RequestParam(value = "itemId", required = false) String itemId) | ||
| throws GroupDoesNotExistException, CannotFindCitationException, ZoteroHttpStatusException, | ||
| ZoteroConnectionException, CitationIsOutdatedException, ZoteroItemCreationFailedException { | ||
| citationManager.reprocessFile((IUser) authentication.getPrincipal(), zoteroGroupId, itemId, documentId); | ||
| return new ResponseEntity<>(HttpStatus.OK); | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge with line 75