-
Notifications
You must be signed in to change notification settings - Fork 4
Story/cite 163 - We need a bulk upload for files. #295
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
Open
PradnyaC11
wants to merge
25
commits into
develop
Choose a base branch
from
story/CITE-163
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
4312013
[CITE-163] Created collection.html page to import collection
PradnyaC11 d426a75
[CITE-163] Added POST for collection upload in ImportCollectionContro…
PradnyaC11 fe9e22d
[CITE-163] Added response to uploadCollection method
PradnyaC11 57563b4
[CITE-163] Fixing errors in upload collection code
PradnyaC11 505c2f2
[CITE-163] Updated the POST URL for upload collection
PradnyaC11 a108d4a
[CITE-163] Removed IUploadJobCollection class
PradnyaC11 c5c9cfc
[CITE-163] Added select for collections on import collection page
PradnyaC11 2134b97
[CITE-163] Added method to get collections of selected group
PradnyaC11 da60307
[CITE-163] Updated Upload job to include collectionId
PradnyaC11 57314c4
[CITE-163] Resolved importing to collection issue
PradnyaC11 ccb03f8
[CITE-163] Added option to select new collection
PradnyaC11 5a668b0
[CITE-163] Added methods to create new collection
PradnyaC11 75f108a
[CITE-163] Resolved issues with create new citation
PradnyaC11 35a4143
Merge branch 'develop' into story/CITE-163
PradnyaC11 8bac348
[CITE-163] Updated get group item API call
PradnyaC11 70cd62b
[CITE-163] Updated ItemTextController to added headers
PradnyaC11 13d0810
[CITE-163] Updated JobInfoController with giles token
PradnyaC11 9c4e0e9
[CITE-163] Updated get item API to use headers
PradnyaC11 073a5a0
[CITE-163] Updated versions in pom.xml
PradnyaC11 34721c4
[CITE-163] Cleaned-up code
PradnyaC11 9754d1b
[CITE-163] Removed unwanted changes
PradnyaC11 3d72e60
[CITE-163] Resolved code factor issue
PradnyaC11 d19ed0a
Merge branch 'develop' into story/CITE-163
PradnyaC11 1eba17e
Merge branch 'develop' into story/CITE-163
PradnyaC11 9f35112
[CITE-163] Addressed PR comments
PradnyaC11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...rc/main/java/edu/asu/diging/citesphere/core/service/jobs/IUploadCollectionJobManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package edu.asu.diging.citesphere.core.service.jobs; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import edu.asu.diging.citesphere.core.exceptions.GroupDoesNotExistException; | ||
| import edu.asu.diging.citesphere.core.model.jobs.IUploadJob; | ||
| import edu.asu.diging.citesphere.user.IUser; | ||
|
|
||
| public interface IUploadCollectionJobManager { | ||
|
|
||
| List<IUploadJob> createUploadJob(IUser user, MultipartFile[] files, List<byte[]> fileBytes, String groupId, String collectionId) throws GroupDoesNotExistException; | ||
|
|
||
|
|
||
| } |
140 changes: 140 additions & 0 deletions
140
...ain/java/edu/asu/diging/citesphere/core/service/jobs/impl/UploadCollectionJobManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package edu.asu.diging.citesphere.core.service.jobs.impl; | ||
|
|
||
| import java.time.OffsetDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import javax.transaction.Transactional; | ||
|
|
||
| import org.apache.tika.Tika; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.PropertySource; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import edu.asu.diging.citesphere.core.exceptions.FileStorageException; | ||
| import edu.asu.diging.citesphere.core.exceptions.GroupDoesNotExistException; | ||
| import edu.asu.diging.citesphere.core.exceptions.MessageCreationException; | ||
| import edu.asu.diging.citesphere.core.kafka.IKafkaRequestProducer; | ||
| import edu.asu.diging.citesphere.core.model.jobs.IUploadJob; | ||
| import edu.asu.diging.citesphere.core.model.jobs.JobStatus; | ||
| import edu.asu.diging.citesphere.core.model.jobs.impl.JobPhase; | ||
| import edu.asu.diging.citesphere.core.model.jobs.impl.UploadJob; | ||
| import edu.asu.diging.citesphere.core.repository.jobs.UploadJobRepository; | ||
| import edu.asu.diging.citesphere.core.service.IGroupManager; | ||
| import edu.asu.diging.citesphere.core.service.jobs.IUploadCollectionJobManager; | ||
| import edu.asu.diging.citesphere.core.service.jwt.IJwtTokenService; | ||
| import edu.asu.diging.citesphere.core.service.upload.IFileStorageManager; | ||
| import edu.asu.diging.citesphere.messages.KafkaTopics; | ||
| import edu.asu.diging.citesphere.messages.model.KafkaJobMessage; | ||
| import edu.asu.diging.citesphere.model.bib.ICitationGroup; | ||
| import edu.asu.diging.citesphere.user.IUser; | ||
|
|
||
| @Service | ||
| @Transactional | ||
| @PropertySource("classpath:/config.properties") | ||
| public class UploadCollectionJobManager implements IUploadCollectionJobManager { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
|
||
| @Value("${_job_page_size}") | ||
| private int jobPageSize; | ||
|
|
||
| @Autowired | ||
| private UploadJobRepository uploadJobRepository; | ||
|
|
||
| @Autowired | ||
| private IFileStorageManager fileManager; | ||
|
|
||
| @Autowired | ||
| private IKafkaRequestProducer kafkaProducer; | ||
|
|
||
| @Autowired | ||
| private IJwtTokenService tokenService; | ||
|
|
||
| @Autowired | ||
| private IGroupManager groupManager; | ||
|
|
||
| @Override | ||
| public List<IUploadJob> createUploadJob(IUser user, MultipartFile[] files, List<byte[]> fileBytes, | ||
| String groupId, String collectionId) throws GroupDoesNotExistException { | ||
| ICitationGroup group = groupManager.getGroup(user, groupId); | ||
| if (group == null) { | ||
| throw new GroupDoesNotExistException(); | ||
| } | ||
|
|
||
| List<IUploadJob> jobs = new ArrayList<>(); | ||
| int i = 0; | ||
| for (MultipartFile f : files) { | ||
| String filename = f.getOriginalFilename(); | ||
|
|
||
| byte[] bytes = null; | ||
| UploadJob job = new UploadJob(); | ||
| jobs.add(job); | ||
| job.setFilename(filename); | ||
| job.setCreatedOn(OffsetDateTime.now()); | ||
| job.setUsername(user.getUsername()); | ||
| job.setCitationGroup(groupId); | ||
| job.setCitationCollection(collectionId); | ||
| job.setPhases(new ArrayList<>()); | ||
| try { | ||
| if (fileBytes != null && fileBytes.size() == files.length) { | ||
| bytes = fileBytes.get(i); | ||
| } else { | ||
| job.setStatus(JobStatus.FAILURE); | ||
| job.getPhases().add(new JobPhase(JobStatus.FAILURE, | ||
| "There is a mismatch between file metadata and file contents.")); | ||
| continue; | ||
| } | ||
|
|
||
| if (bytes == null) { | ||
| job.setStatus(JobStatus.FAILURE); | ||
| job.getPhases().add(new JobPhase(JobStatus.FAILURE, "There is not file content.")); | ||
| continue; | ||
| } | ||
| job = uploadJobRepository.save(job); | ||
| fileManager.saveFile(user.getUsername(), job.getId(), filename, bytes); | ||
|
|
||
| job.setStatus(JobStatus.PREPARED); | ||
| } catch (FileStorageException e) { | ||
| logger.error("Could not store file.", e); | ||
| job.setStatus(JobStatus.FAILURE); | ||
| job.getPhases().add(new JobPhase(JobStatus.FAILURE, e.getMessage())); | ||
| continue; | ||
| } finally { | ||
| i++; | ||
| uploadJobRepository.save(job); | ||
| } | ||
|
|
||
| String contentType = null; | ||
|
|
||
| if (bytes != null) { | ||
| Tika tika = new Tika(); | ||
| contentType = tika.detect(bytes); | ||
| } | ||
|
|
||
| if (contentType == null) { | ||
| contentType = f.getContentType(); | ||
| } | ||
|
|
||
| job.setContentType(contentType); | ||
| job.setFileSize(f.getSize()); | ||
| uploadJobRepository.save(job); | ||
| String token = tokenService.generateJobApiToken(job); | ||
| try { | ||
| kafkaProducer.sendRequest(new KafkaJobMessage(token), KafkaTopics.COLLECTION_IMPORT_TOPIC); | ||
| } catch (MessageCreationException e) { | ||
| logger.error("Could not send Kafka message.", e); | ||
| job.setStatus(JobStatus.FAILURE); | ||
| job.getPhases().add(new JobPhase(JobStatus.FAILURE, e.getMessage())); | ||
| uploadJobRepository.save(job); | ||
| } | ||
| } | ||
|
|
||
| return jobs; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,4 +81,7 @@ CitationResults getCollectionItems(IUser user, String groupId, String collection | |
| long getLatestGroupVersion(IUser user, String groupId); | ||
|
|
||
| Map<ItemDeletionResponse, List<String>> deleteMultipleItems(IUser user, String groupId, List<String> citationKeys, Long citationVersion) throws ZoteroConnectionException, ZoteroHttpStatusException; | ||
|
|
||
| ICitationCollection createCitationCollection(IUser user, String groupId, String collectionName, String parentCollection) throws ZoteroItemCreationFailedException, ZoteroConnectionException; | ||
|
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. needs javadoc (including explanation of parentCollection: null if no parent collection?). |
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,4 +369,29 @@ public Map<ItemDeletionResponse, List<String>> deleteMultipleItems(IUser user, S | |
| Zotero zotero = getApi(user); | ||
| return zotero.getGroupsOperations().deleteMultipleItems(groupId, citationKeys, citationVersion); | ||
| } | ||
|
|
||
| @Override | ||
| public Collection createCitationCollection(IUser user, String groupId, String collectionName, | ||
| String parentCollection) throws ZoteroItemCreationFailedException, ZoteroConnectionException { | ||
| Zotero zotero = getApi(user); | ||
|
|
||
| ItemCreationResponse response = zotero.getGroupCollectionsOperations().createCollection(groupId, collectionName, parentCollection); | ||
|
|
||
| // let's give Zotero a minute to process | ||
| try { | ||
| TimeUnit.SECONDS.sleep(1); | ||
| } catch (InterruptedException e) { | ||
| logger.error("Could not sleep.", e); | ||
| // well if something goes wrong here, let's just ignore it | ||
| } | ||
|
|
||
| Map<String, String> success = response.getSuccess(); | ||
| if (success.isEmpty()) { | ||
| logger.error("Could not create collection: " + response.getFailed().get("0")); | ||
| throw new ZoteroItemCreationFailedException(response); | ||
| } | ||
|
|
||
| // since we only submitted one item, there should only be one in the map | ||
|
||
| return getCitationCollection(user, groupId, success.values().iterator().next()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
how is this different from the UploadJobManager? looks like a lot of duplicate code (almost all of it)?