diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IBiblioBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IBiblioBlockManager.java new file mode 100644 index 000000000..b4c4d7cc1 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IBiblioBlockManager.java @@ -0,0 +1,14 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; +import edu.asu.diging.vspace.core.model.IBiblioBlock; +import edu.asu.diging.vspace.core.model.impl.BiblioBlock; + +public interface IBiblioBlockManager extends IGenericContentBlockManager { + + IBiblioBlock createBiblioBlock(String slideId, String title, String description); + + void deleteBiblioBlockById(String id) throws BlockDoesNotExistException; + + void updateBiblioBlock(BiblioBlock biblioBlock); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IChoiceBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IChoiceBlockManager.java new file mode 100644 index 000000000..a5ee54f53 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IChoiceBlockManager.java @@ -0,0 +1,10 @@ +package edu.asu.diging.vspace.core.services; + +import java.util.List; + +import edu.asu.diging.vspace.core.model.IChoiceBlock; + +public interface IChoiceBlockManager extends IGenericContentBlockManager { + + IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, boolean showsAll); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java index 743148ce3..2dd7f7601 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IContentBlockManager.java @@ -23,17 +23,17 @@ public interface IContentBlockManager { - IBiblioBlock createBiblioBlock(String slideId, String title, String description, Integer contentOrder); + IBiblioBlock createBiblioBlock(String slideId, String title, String description); List getAllContentBlocks(String slideId); - ITextBlock createTextBlock(String slideId, String content, Integer contentOrder); + ITextBlock createTextBlock(String slideId, String content); - CreationReturnValue createImageBlock(String slideId, byte[] image, String filename, Integer contentOrder) throws ImageCouldNotBeStoredException; + CreationReturnValue createImageBlock(String slideId, byte[] image, String filename) throws ImageCouldNotBeStoredException; - CreationReturnValue createImageBlock(String slideId, IVSImage image, Integer contentOrder); + CreationReturnValue createImageBlock(String slideId, IVSImage image); - public CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, String url, Integer contentOrder, String title) throws VideoCouldNotBeStoredException; + public CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, String url, String title) throws VideoCouldNotBeStoredException; void deleteTextBlockById(String blockid, String slideId) throws BlockDoesNotExistException; @@ -65,11 +65,11 @@ public interface IContentBlockManager { void updateVideoBlock(IVideoBlock videoBlock, byte[] video, Long fileSize, String url, String filename, String title) throws VideoCouldNotBeStoredException; - IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, Integer contentOrder, boolean showsAll); + IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, boolean showsAll); Integer findMaxContentOrder(String slideId); - ISpaceBlock createSpaceBlock(String slideId, String title, Integer contentOrder, ISpace space); + ISpaceBlock createSpaceBlock(String slideId, String title, ISpace space); ISpaceBlock getSpaceBlock(String spaceBlockId); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IGenericContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IGenericContentBlockManager.java new file mode 100644 index 000000000..68409a642 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IGenericContentBlockManager.java @@ -0,0 +1,17 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; +import edu.asu.diging.vspace.core.model.IContentBlock; + +public interface IGenericContentBlockManager { + + T createContentBlock(String slideId); + + void updateContentBlock(T contentBlock); + + T getContentBlock(String blockId); + + void deleteContentBlock(String blockId, String slideId) throws BlockDoesNotExistException; + + void saveContentBlock(T contentBlock); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IImageBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IImageBlockManager.java new file mode 100644 index 000000000..2dcad5256 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IImageBlockManager.java @@ -0,0 +1,19 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.exception.ImageCouldNotBeStoredException; +import edu.asu.diging.vspace.core.model.IImageBlock; +import edu.asu.diging.vspace.core.model.IVSImage; +import edu.asu.diging.vspace.core.services.impl.CreationReturnValue; + +public interface IImageBlockManager extends IGenericContentBlockManager { + + CreationReturnValue createImageBlock(String slideId, byte[] image, String filename) + throws ImageCouldNotBeStoredException; + + CreationReturnValue createImageBlock(String slideId, IVSImage image); + + void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename) + throws ImageCouldNotBeStoredException; + + void updateImageBlock(IImageBlock imageBlock, IVSImage image); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/ISpaceBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/ISpaceBlockManager.java new file mode 100644 index 000000000..1be4662a7 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/ISpaceBlockManager.java @@ -0,0 +1,11 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.model.ISpace; +import edu.asu.diging.vspace.core.model.ISpaceBlock; + +public interface ISpaceBlockManager extends IGenericContentBlockManager { + + ISpaceBlock createSpaceBlock(String slideId, String title, ISpace space); + + void saveSpaceBlock(ISpaceBlock spaceBlock); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/ITextBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/ITextBlockManager.java new file mode 100644 index 000000000..775ec3e97 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/ITextBlockManager.java @@ -0,0 +1,11 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.model.ITextBlock; +import edu.asu.diging.vspace.core.model.impl.TextBlock; + +public interface ITextBlockManager extends IGenericContentBlockManager { + + ITextBlock createTextBlock(String slideId, String text); + + void updateTextBlock(TextBlock textBlock); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/IVideoBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IVideoBlockManager.java new file mode 100644 index 000000000..27c125975 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/IVideoBlockManager.java @@ -0,0 +1,16 @@ +package edu.asu.diging.vspace.core.services; + +import edu.asu.diging.vspace.core.exception.VideoCouldNotBeStoredException; +import edu.asu.diging.vspace.core.model.IVideoBlock; +import edu.asu.diging.vspace.core.services.impl.CreationReturnValue; + +public interface IVideoBlockManager extends IGenericContentBlockManager { + + CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, + String url, String title) throws VideoCouldNotBeStoredException; + + void updateVideoBlock(IVideoBlock videoBlock, byte[] video, Long fileSize, String url, + String filename, String title) throws VideoCouldNotBeStoredException; + + void saveVideoBlock(IVideoBlock videoBlock); +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/BiblioBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/BiblioBlockManager.java new file mode 100644 index 000000000..d27871399 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/BiblioBlockManager.java @@ -0,0 +1,75 @@ +package edu.asu.diging.vspace.core.services.impl; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.BiblioBlockRepository; +import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; +import edu.asu.diging.vspace.core.model.IBiblioBlock; +import edu.asu.diging.vspace.core.model.ISlide; +import edu.asu.diging.vspace.core.model.impl.BiblioBlock; +import edu.asu.diging.vspace.core.services.IBiblioBlockManager; + +@Service +public class BiblioBlockManager extends GenericContentBlockManager + implements IBiblioBlockManager { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private BiblioBlockRepository biblioBlockRepo; + + @Override + protected BiblioBlockRepository getRepository() { + return biblioBlockRepo; + } + + @Override + public IBiblioBlock createContentBlock(String slideId) { + return createBiblioBlock(slideId, "Default Bibliography", "Default Description"); + } + + @Override + public IBiblioBlock createBiblioBlock(String slideId, String title, String description) { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + IBiblioBlock biblioBlock = new BiblioBlock(); + biblioBlock.setDescription(description); + biblioBlock.setBiblioTitle(title); + biblioBlock.setSlide(slide); + biblioBlock.setContentOrder(contentOrder); + return biblioBlockRepo.save((BiblioBlock) biblioBlock); + } + + @Override + public void deleteBiblioBlockById(String id) throws BlockDoesNotExistException { + if (id == null) { + logger.warn("Attempted to delete biblio block with null id."); + return; + } + + try { + biblioBlockRepo.deleteById(id); + } catch (IllegalArgumentException e) { + throw new BlockDoesNotExistException("Biblio block with id " + id + " does not exist.", e); + } + } + + @Override + public void updateContentBlock(IBiblioBlock biblioBlock) { + updateBiblioBlock((BiblioBlock) biblioBlock); + } + + @Override + public void updateBiblioBlock(BiblioBlock biblioBlock) { + biblioBlockRepo.save(biblioBlock); + } + + @Override + public void saveContentBlock(IBiblioBlock biblioBlock) { + biblioBlockRepo.save((BiblioBlock) biblioBlock); + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ChoiceBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ChoiceBlockManager.java new file mode 100644 index 000000000..09706a96b --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ChoiceBlockManager.java @@ -0,0 +1,66 @@ +package edu.asu.diging.vspace.core.services.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.ChoiceContentBlockRepository; +import edu.asu.diging.vspace.core.factory.IChoiceBlockFactory; +import edu.asu.diging.vspace.core.model.IChoice; +import edu.asu.diging.vspace.core.model.IChoiceBlock; +import edu.asu.diging.vspace.core.model.impl.ChoiceBlock; +import edu.asu.diging.vspace.core.services.IChoiceBlockManager; + +@Service +public class ChoiceBlockManager extends GenericContentBlockManager + implements IChoiceBlockManager { + + @Autowired + private IChoiceBlockFactory choiceBlockFactory; + + @Autowired + private ChoiceContentBlockRepository choiceBlockRepo; + + @Override + protected ChoiceContentBlockRepository getRepository() { + return choiceBlockRepo; + } + + @Override + public IChoiceBlock createContentBlock(String slideId) { + return createChoiceBlock(slideId, new ArrayList<>(), true); + } + + @Override + public IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, boolean showsAll) { + Integer contentOrder = getNextContentOrder(slideId); + + List choices = new ArrayList(); + if (!showsAll) { + choices = selectedChoices.stream() + .map(choice -> slideManager.getChoice(choice)) + .collect(Collectors.toList()); + } + + IChoiceBlock choiceBlock = choiceBlockFactory.createChoiceBlock( + slideManager.getSlide(slideId), + contentOrder, + choices, + showsAll + ); + return choiceBlockRepo.save((ChoiceBlock) choiceBlock); + } + + @Override + public void updateContentBlock(IChoiceBlock choiceBlock) { + choiceBlockRepo.save((ChoiceBlock) choiceBlock); + } + + @Override + public void saveContentBlock(IChoiceBlock choiceBlock) { + choiceBlockRepo.save((ChoiceBlock) choiceBlock); + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java index 872e12ddf..a1e6bcace 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ContentBlockManager.java @@ -1,41 +1,16 @@ package edu.asu.diging.vspace.core.services.impl; -import java.util.ArrayList; import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; -import org.apache.tika.Tika; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.stereotype.Service; - -import edu.asu.diging.vspace.core.data.BiblioBlockRepository; import org.springframework.transaction.annotation.Transactional; -import edu.asu.diging.vspace.core.data.ChoiceContentBlockRepository; + import edu.asu.diging.vspace.core.data.ContentBlockRepository; -import edu.asu.diging.vspace.core.data.ImageContentBlockRepository; -import edu.asu.diging.vspace.core.data.ImageRepository; -import edu.asu.diging.vspace.core.data.SpaceContentBlockRepository; -import edu.asu.diging.vspace.core.data.TextContentBlockRepository; -import edu.asu.diging.vspace.core.data.VideoContentBlockRepository; -import edu.asu.diging.vspace.core.data.VideoRepository; import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; -import edu.asu.diging.vspace.core.exception.FileStorageException; import edu.asu.diging.vspace.core.exception.ImageCouldNotBeStoredException; import edu.asu.diging.vspace.core.exception.VideoCouldNotBeStoredException; -import edu.asu.diging.vspace.core.factory.IChoiceBlockFactory; -import edu.asu.diging.vspace.core.factory.IImageBlockFactory; -import edu.asu.diging.vspace.core.factory.IImageFactory; -import edu.asu.diging.vspace.core.factory.ISpaceBlockFactory; -import edu.asu.diging.vspace.core.factory.ITextBlockFactory; -import edu.asu.diging.vspace.core.factory.IVideoBlockFactory; -import edu.asu.diging.vspace.core.factory.IVideoFactory; -import edu.asu.diging.vspace.core.file.IStorageEngine; import edu.asu.diging.vspace.core.model.IBiblioBlock; -import edu.asu.diging.vspace.core.model.IChoice; import edu.asu.diging.vspace.core.model.IChoiceBlock; import edu.asu.diging.vspace.core.model.IContentBlock; import edu.asu.diging.vspace.core.model.IImageBlock; @@ -44,560 +19,201 @@ import edu.asu.diging.vspace.core.model.ISpaceBlock; import edu.asu.diging.vspace.core.model.ITextBlock; import edu.asu.diging.vspace.core.model.IVSImage; -import edu.asu.diging.vspace.core.model.impl.BiblioBlock; -import edu.asu.diging.vspace.core.model.IVSVideo; import edu.asu.diging.vspace.core.model.IVideoBlock; -import edu.asu.diging.vspace.core.model.impl.ChoiceBlock; +import edu.asu.diging.vspace.core.model.impl.BiblioBlock; import edu.asu.diging.vspace.core.model.impl.ContentBlock; -import edu.asu.diging.vspace.core.model.impl.ImageBlock; -import edu.asu.diging.vspace.core.model.impl.SpaceBlock; import edu.asu.diging.vspace.core.model.impl.TextBlock; -import edu.asu.diging.vspace.core.model.impl.VSImage; -import edu.asu.diging.vspace.core.model.impl.VSVideo; -import edu.asu.diging.vspace.core.model.impl.VideoBlock; +import edu.asu.diging.vspace.core.services.IBiblioBlockManager; +import edu.asu.diging.vspace.core.services.IChoiceBlockManager; import edu.asu.diging.vspace.core.services.IContentBlockManager; +import edu.asu.diging.vspace.core.services.IImageBlockManager; import edu.asu.diging.vspace.core.services.ISlideManager; +import edu.asu.diging.vspace.core.services.ISpaceBlockManager; +import edu.asu.diging.vspace.core.services.ITextBlockManager; +import edu.asu.diging.vspace.core.services.IVideoBlockManager; @Transactional(rollbackFor = { Exception.class }) @Service public class ContentBlockManager implements IContentBlockManager { - - private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private ISlideManager slideManager; - - @Autowired - private IImageFactory imageFactory; - - @Autowired - private IVideoFactory videoFactory; - - @Autowired - private ITextBlockFactory textBlockFactory; - - @Autowired - private ISpaceBlockFactory spaceBlockFactory; - - @Autowired - private IImageBlockFactory imageBlockFactory; - - @Autowired - private IVideoBlockFactory videoBlockFactory; @Autowired - private IChoiceBlockFactory choiceBlockFactory; - - @Autowired - private ImageRepository imageRepo; - - @Autowired - private VideoRepository videoRepo; - - @Autowired - private TextContentBlockRepository textBlockRepo; - - @Autowired - private BiblioBlockRepository biblioBlockRepo; + private ContentBlockRepository contentBlockRepository; @Autowired - private SpaceContentBlockRepository spaceBlockRepo; + private ITextBlockManager textBlockManager; @Autowired - private ImageContentBlockRepository imageBlockRepo; + private IImageBlockManager imageBlockManager; @Autowired - private VideoContentBlockRepository videoBlockRepo; + private IVideoBlockManager videoBlockManager; @Autowired - private ChoiceContentBlockRepository choiceBlockRepo; + private ISpaceBlockManager spaceBlockManager; @Autowired - private IStorageEngine storage; + private IChoiceBlockManager choiceBlockManager; @Autowired - private ContentBlockRepository contentBlockRepository; + private IBiblioBlockManager biblioBlockManager; - /* - * (non-Javadoc) - * - * @see java.util.List# - * getAllContentBlocks(java.lang.String) - */ @Override public List getAllContentBlocks(String slideId) { ISlide slide = slideManager.getSlide(slideId); return slide.getContents(); } - /* - * (non-Javadoc) - * - * @see - * edu.asu.diging.vspace.core.services.impl.ITextBlock#createTextBlock(java. - * lang.String, java.lang.String) - */ @Override - public ITextBlock createTextBlock(String slideId, String text, Integer contentOrder) { - ISlide slide = slideManager.getSlide(slideId); - ITextBlock textBlock = textBlockFactory.createTextBlock(slide, text); - textBlock.setContentOrder(contentOrder); - textBlock = textBlockRepo.save((TextBlock) textBlock); - return textBlock; + public ITextBlock createTextBlock(String slideId, String text) { + return textBlockManager.createTextBlock(slideId, text); } @Override - public ISpaceBlock createSpaceBlock(String slideId, String title, Integer contentOrder, ISpace space) { - ISlide slide = slideManager.getSlide(slideId); - ISpaceBlock spaceBlock = spaceBlockFactory.createSpaceBlock(slide, title, space); - spaceBlock.setContentOrder(contentOrder); - spaceBlock = spaceBlockRepo.save((SpaceBlock) spaceBlock); - return spaceBlock; + public ISpaceBlock createSpaceBlock(String slideId, String title, ISpace space) { + return spaceBlockManager.createSpaceBlock(slideId, title, space); } - private IVSImage saveImage(byte[] image, String filename) { - if (image != null && image.length > 0) { - Tika tika = new Tika(); - String contentType = tika.detect(image); - IVSImage slideContentImage = imageFactory.createImage(filename, contentType); - slideContentImage = imageRepo.save((VSImage) slideContentImage); - return slideContentImage; - } - return null; - } - - private IVSVideo saveVideoWithUrl(String url, String title) { - IVSVideo vidContent = videoFactory.createVideo(url); - vidContent.setTitle(title); - return videoRepo.save((VSVideo) vidContent); - } - - private IVSVideo saveVideo(byte[] video, Long size, String filename, String title) { - if (video != null && video.length > 0) { - Tika tika = new Tika(); - String contentType = tika.detect(video); - IVSVideo slideContentVideo = videoFactory.createVideo(filename, size, contentType); - slideContentVideo.setTitle(title); - slideContentVideo = videoRepo.save((VSVideo) slideContentVideo); - return slideContentVideo; - } - return null; - } - - private void storeImageFile(byte[] image, IVSImage slideContentImage, String filename) throws ImageCouldNotBeStoredException { - if (slideContentImage != null) { - String relativePath = null; - try { - relativePath = storage.storeFile(image, filename, slideContentImage.getId()); - } catch (FileStorageException e) { - throw new ImageCouldNotBeStoredException(e); - } - slideContentImage.setParentPath(relativePath); - imageRepo.save((VSImage) slideContentImage); - } - } - - private void storeVideoFile(byte[] video, IVSVideo slideContentVideo, String filename) throws VideoCouldNotBeStoredException { - if (slideContentVideo != null) { - String relativePath = null; - try { - relativePath = storage.storeFile(video, filename, slideContentVideo.getId()); - } catch (FileStorageException e) { - throw new VideoCouldNotBeStoredException(e); - } - slideContentVideo.setParentPath(relativePath); - videoRepo.save((VSVideo) slideContentVideo); - } + @Override + public CreationReturnValue createImageBlock(String slideId, byte[] image, String filename) + throws ImageCouldNotBeStoredException { + return imageBlockManager.createImageBlock(slideId, image, filename); } - /* - * (non-Javadoc) - * - * @see edu.asu.diging.vspace.core.services.impl.IContentBlockManager# - * createImageBlock(java.lang.String, java.util.Arrays, java.lang.String) - */ @Override - public CreationReturnValue createImageBlock(String slideId, byte[] image, String filename, Integer contentOrder) throws ImageCouldNotBeStoredException { - ISlide slide = slideManager.getSlide(slideId); - IVSImage slideContentImage = saveImage(image, filename); - CreationReturnValue returnValue = new CreationReturnValue(); - returnValue.setErrorMsgs(new ArrayList<>()); - storeImageFile(image, slideContentImage, filename); - IImageBlock imgBlock = imageBlockFactory.createImageBlock(slide, slideContentImage); - imgBlock.setContentOrder(contentOrder); - ImageBlock imageBlock = imageBlockRepo.save((ImageBlock) imgBlock); - - returnValue.setElement(imageBlock); - return returnValue; + public CreationReturnValue createImageBlock(String slideId, IVSImage image) { + return imageBlockManager.createImageBlock(slideId, image); } - /** - * (non-Javadoc) - * - * @see edu.asu.diging.vspace.core.services.impl.IContentBlockManager# - * createImageBlock(java.lang.String, - * edu.asu.diging.vspace.core.model.IVSImage, java.lang.Integer) - */ @Override - public CreationReturnValue createImageBlock(String slideId, IVSImage image, Integer contentOrder) { - CreationReturnValue returnValue = new CreationReturnValue(); - returnValue.setErrorMsgs(new ArrayList<>()); - ISlide slide = slideManager.getSlide(slideId); - IImageBlock imgBlock = imageBlockFactory.createImageBlock(slide, image); - imgBlock.setContentOrder(contentOrder); - ImageBlock imageBlock = imageBlockRepo.save((ImageBlock) imgBlock); - returnValue.setElement(imageBlock); - return returnValue; + public CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, + String url, String title) throws VideoCouldNotBeStoredException { + return videoBlockManager.createVideoBlock(slideId, video, size, fileName, url, title); } - /* - * (non-Javadoc) - * - * @see edu.asu.diging.vspace.core.services.impl.IContentBlockManager# - * createVideoBlock(java.lang.String, java.util.Arrays, java.lang.String) - */ @Override - public CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, String url, - Integer contentOrder, String title) throws VideoCouldNotBeStoredException { - ISlide slide = slideManager.getSlide(slideId); - CreationReturnValue returnValue = new CreationReturnValue(); - returnValue.setErrorMsgs(new ArrayList<>()); - IVSVideo slideContentVideo = storeVideo(video, size, fileName, url, title); - IVideoBlock vidBlock = videoBlockFactory.createVideoBlock(slide, slideContentVideo); - vidBlock.setContentOrder(contentOrder); - VideoBlock videoBlock = videoBlockRepo.save((VideoBlock) vidBlock); - returnValue.setElement(videoBlock); - return returnValue; + public IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, boolean showsAll) { + return choiceBlockManager.createChoiceBlock(slideId, selectedChoices, showsAll); } - private IVSVideo storeVideo(byte[] video, Long size, String fileName, String url, String title) - throws VideoCouldNotBeStoredException { - IVSVideo slideContentVideo = null; - if (video != null) { - slideContentVideo = saveVideo(video, size, fileName, title); - storeVideoFile(video, slideContentVideo, fileName); - slideContentVideo.setUrl(null); - } else if (url != null && !url.isEmpty()) { - slideContentVideo = saveVideoWithUrl(url, title); - } - return slideContentVideo; + @Override + public IBiblioBlock createBiblioBlock(String slideId, String title, String description) { + return biblioBlockManager.createBiblioBlock(slideId, title, description); } - /** - * Delete a text block using an id and also decrease content order by 1 of all - * the slide's block which are after this block - * - * @param blockId - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * @param slideId - id of the slide in which the text block with blockId is - * present. - * - */ - @Override public void deleteTextBlockById(String blockId, String slideId) throws BlockDoesNotExistException { - if (blockId == null) { - return; - } - Integer contentOrder = null; - Optional contentBlock = contentBlockRepository.findById(blockId); - if (contentBlock.isPresent()) { - contentOrder = contentBlock.get().getContentOrder(); - } else { - throw new BlockDoesNotExistException("Block Id not present"); - } - try { - textBlockRepo.deleteById(blockId); - updateContentOrder(slideId, contentOrder); - } catch (EmptyResultDataAccessException e) { - throw new BlockDoesNotExistException(e); - } - + textBlockManager.deleteContentBlock(blockId, slideId); } - /** - * Delete a space block using an id and also decrease content order by 1 of all - * the slide's block which are after this block - * - * @param blockId - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * @param slideId - id of the slide in which the text block with blockId is - * present. - * - */ @Override public void deleteSpaceBlockById(String blockId, String slideId) throws BlockDoesNotExistException { - if (blockId == null) { - return; - } - Integer contentOrder = null; - Optional contentBlock = contentBlockRepository.findById(blockId); - if (contentBlock.isPresent()) { - contentOrder = contentBlock.get().getContentOrder(); - } else { - throw new BlockDoesNotExistException("Block Id not present"); - } - try { - spaceBlockRepo.deleteById(blockId); - updateContentOrder(slideId, contentOrder); - } catch (EmptyResultDataAccessException e) { - throw new BlockDoesNotExistException(e); - } - + spaceBlockManager.deleteContentBlock(blockId, slideId); } - /** - * Delete an image block using an id and also decrease content order by 1 of all - * the slide's block which are after this block - * - * @param blockId - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * @param slideId - id of the slide in which the Image block with blockId is - * present. - */ - @Override public void deleteImageBlockById(String blockId, String slideId) throws BlockDoesNotExistException { - if (blockId == null) { - return; - } - Integer contentOrder = null; - Optional contentBlock = contentBlockRepository.findById(blockId); - if (contentBlock.isPresent()) { - contentOrder = contentBlock.get().getContentOrder(); - } else { - throw new BlockDoesNotExistException("Block Id not present"); - } - try { - imageBlockRepo.deleteById(blockId); - updateContentOrder(slideId, contentOrder); - } catch (EmptyResultDataAccessException e) { - throw new BlockDoesNotExistException(e); - } - + imageBlockManager.deleteContentBlock(blockId, slideId); } - /** - * Delete an video block using an id and also decrease content order by 1 of all - * the slide's block which are after this block - * - * @param blockId - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * @param slideId - id of the slide in which the Image block with blockId is - * present. - */ - @Override public void deleteVideoBlockById(String blockId, String slideId) throws BlockDoesNotExistException { - if (blockId == null) { - return; - } - Integer contentOrder = null; - Optional contentBlock = contentBlockRepository.findById(blockId); - if (contentBlock.isPresent()) { - contentOrder = contentBlock.get().getContentOrder(); - } else { - throw new BlockDoesNotExistException("Block Id not present"); - } - try { - videoBlockRepo.deleteById(blockId); - updateContentOrder(slideId, contentOrder); - } catch (EmptyResultDataAccessException e) { - throw new BlockDoesNotExistException(e); - } + videoBlockManager.deleteContentBlock(blockId, slideId); } - /** - * Delete a choices block using an id and also decrease content order by 1 of - * all the slide's block which are after this block - * - * @param blockId - id of resource to be deleted. If the id is null then the - * functions returns nothing. - * @param slideId - id of the slide in which the choice block with blockId is - * present. - */ - @Override public void deleteChoiceBlockById(String blockId, String slideId) throws BlockDoesNotExistException { - if (blockId == null) { - return; - } - Integer contentOrder = null; - Optional contentBlock = contentBlockRepository.findById(blockId); - if (contentBlock.isPresent()) { - contentOrder = contentBlock.get().getContentOrder(); - } else { - throw new BlockDoesNotExistException("Block Id not present"); - } - try { - choiceBlockRepo.deleteById(blockId); - updateContentOrder(slideId, contentOrder); - } catch (EmptyResultDataAccessException e) { - throw new BlockDoesNotExistException(e); - } + choiceBlockManager.deleteContentBlock(blockId, slideId); + } + @Override + public void deleteBiblioBlockById(String blockId) throws BlockDoesNotExistException { + biblioBlockManager.deleteBiblioBlockById(blockId); } + @Override public void updateTextBlock(TextBlock textBlock) { - textBlockRepo.save((TextBlock) textBlock); + textBlockManager.updateTextBlock(textBlock); } @Override public void saveSpaceBlock(ISpaceBlock spaceBlock) { - spaceBlockRepo.save((SpaceBlock) spaceBlock); + spaceBlockManager.saveSpaceBlock(spaceBlock); } @Override public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename) throws ImageCouldNotBeStoredException { - IVSImage slideContentImage = saveImage(image, filename); - storeImageFile(image, slideContentImage, filename); - imageBlock.setImage(slideContentImage); - imageBlockRepo.save((ImageBlock) imageBlock); + imageBlockManager.updateImageBlock(imageBlock, image, filename); } @Override public void updateImageBlock(IImageBlock imageBlock, IVSImage image) { - imageBlock.setImage(image); - imageBlockRepo.save((ImageBlock) imageBlock); + imageBlockManager.updateImageBlock(imageBlock, image); } @Override public void updateVideoBlock(IVideoBlock videoBlock, byte[] video, Long fileSize, String url, String filename, String title) throws VideoCouldNotBeStoredException { - IVSVideo slideContentVideo = storeVideo(video, fileSize, filename, url, title); - videoBlock.setVideo(slideContentVideo); - videoBlockRepo.save((VideoBlock) videoBlock); + videoBlockManager.updateVideoBlock(videoBlock, video, fileSize, url, filename, title); } @Override public IImageBlock getImageBlock(String imgBlockId) { - Optional imgBlock = imageBlockRepo.findById(imgBlockId); - if (imgBlock.isPresent()) { - return imgBlock.get(); - } - return null; + return imageBlockManager.getContentBlock(imgBlockId); } @Override public IVideoBlock getVideoBlock(String videoBlockId) { - Optional videoBlock = videoBlockRepo.findById(videoBlockId); - if (videoBlock.isPresent()) { - return videoBlock.get(); - } - return null; + return videoBlockManager.getContentBlock(videoBlockId); } @Override public ITextBlock getTextBlock(String textBlockId) { - Optional textBlock = textBlockRepo.findById(textBlockId); - if (textBlock.isPresent()) { - return textBlock.get(); - } - return null; + return textBlockManager.getContentBlock(textBlockId); } @Override public ISpaceBlock getSpaceBlock(String spaceBlockId) { - Optional spaceBlock = spaceBlockRepo.findById(spaceBlockId); - if (spaceBlock.isPresent()) { - return spaceBlock.get(); - } - return null; + return spaceBlockManager.getContentBlock(spaceBlockId); } @Override public IChoiceBlock getChoiceBlock(String choiceBlockId) { - Optional choiceBlock = choiceBlockRepo.findById(choiceBlockId); - if (choiceBlock.isPresent()) { - return choiceBlock.get(); - } - return null; + return choiceBlockManager.getContentBlock(choiceBlockId); } - /* - * (non-Javadoc) - * - * @see - * edu.asu.diging.vspace.core.services.impl.IChoiceBlock#createTextBlock(java. - * lang.String, java.lang.String, java.lang.Integer) - */ @Override - public IChoiceBlock createChoiceBlock(String slideId, List selectedChoices, Integer contentOrder, - boolean showsAll) { - List choices = new ArrayList(); - if (!showsAll) { - choices = selectedChoices.stream().map(choice -> slideManager.getChoice(choice)) - .collect(Collectors.toList()); - } - IChoiceBlock choiceBlock = choiceBlockFactory.createChoiceBlock(slideManager.getSlide(slideId), contentOrder, - choices, showsAll); - return choiceBlockRepo.save((ChoiceBlock) choiceBlock); - } - - @Override - public IBiblioBlock createBiblioBlock(String slideId, String title, String description, Integer contentOrder) { - ISlide slide = slideManager.getSlide(slideId); - IBiblioBlock bilioBlock = new BiblioBlock(); - bilioBlock.setDescription(description); - bilioBlock.setBiblioTitle(title); - bilioBlock.setSlide(slide); - bilioBlock.setContentOrder(contentOrder); - return biblioBlockRepo.save((BiblioBlock) bilioBlock); - } - - @Override - public void deleteBiblioBlockById(String id) throws BlockDoesNotExistException { - if (id == null) { - logger.warn("Attempted to delete biblio block with null id."); - return; - } - - try { - biblioBlockRepo.deleteById(id); - } catch (IllegalArgumentException e) { - throw new BlockDoesNotExistException("Biblio block with id " + id + " does not exist.", e); - } + public BiblioBlock getBiblioBlock(String biblioBlockId) { + return (BiblioBlock) biblioBlockManager.getContentBlock(biblioBlockId); } @Override public void updateBiblioBlock(BiblioBlock biblioBlock) { - biblioBlockRepo.save((BiblioBlock) biblioBlock); + biblioBlockManager.updateBiblioBlock(biblioBlock); } @Override - public BiblioBlock getBiblioBlock(String biblioBlockId) { - Optional biblioBlock = biblioBlockRepo.findById(biblioBlockId); - if (biblioBlock.isPresent()) { - return (BiblioBlock) biblioBlock.get(); - } - return null; + public void saveVideoBlock(IVideoBlock videoBlock) { + videoBlockManager.saveVideoBlock(videoBlock); } - - /** - * Retrieving the maximum content order for a slide - */ + @Override public Integer findMaxContentOrder(String slideId) { return contentBlockRepository.findMaxContentOrder(slideId); } - /** - * Adjusting the content order of the blocks of slide once it is dragged and - * changed position. - * - * @param contentBlockList - The list contains the blocks and the updated - * content order corresponding to each blocks. - */ @Override public void updateContentOrder(List contentBlockList) throws BlockDoesNotExistException { if (contentBlockList == null) { return; } - List contentBlocks = new ArrayList<>(); + List contentBlocks = new java.util.ArrayList<>(); for (ContentBlock eachBlock : contentBlockList) { String blockId = eachBlock.getId(); int contentOrder = eachBlock.getContentOrder(); - Optional contentBlock = contentBlockRepository.findById(blockId); + java.util.Optional contentBlock = contentBlockRepository.findById(blockId); if (contentBlock.isPresent()) { ContentBlock contentBlockObj = contentBlock.get(); contentBlockObj.setContentOrder(contentOrder); @@ -608,30 +224,4 @@ public void updateContentOrder(List contentBlockList) throws Block } contentBlockRepository.saveAll(contentBlocks); } - - /** - * Decreasing content order by 1 of the slide's block which are after the - * specified contentOrder - * - * @param slideId The Id of the slide for which content orders will be - * updated - * @param contentOrder The content orders of the slides which are greater than - * contentOrder will be updated - */ - private void updateContentOrder(String slideId, Integer contentOrder) { - List contentBlockList = contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan(slideId, - contentOrder); - if (contentBlockList != null) { - for (ContentBlock eachContentBlock : contentBlockList) { - eachContentBlock.setContentOrder(eachContentBlock.getContentOrder() - 1); - } - contentBlockRepository.saveAll(contentBlockList); - } - } - - @Override - public void saveVideoBlock(IVideoBlock videoBlock) { - videoRepo.save((VSVideo) videoBlock.getVideo()); - - } } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/GenericContentBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/GenericContentBlockManager.java new file mode 100644 index 000000000..b96ce70e4 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/GenericContentBlockManager.java @@ -0,0 +1,82 @@ +package edu.asu.diging.vspace.core.services.impl; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.data.repository.CrudRepository; +import org.springframework.transaction.annotation.Transactional; + +import edu.asu.diging.vspace.core.data.ContentBlockRepository; +import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException; +import edu.asu.diging.vspace.core.model.IContentBlock; +import edu.asu.diging.vspace.core.model.impl.ContentBlock; +import edu.asu.diging.vspace.core.services.IGenericContentBlockManager; +import edu.asu.diging.vspace.core.services.ISlideManager; + +@Transactional(rollbackFor = { Exception.class }) +public abstract class GenericContentBlockManager> + implements IGenericContentBlockManager { + + @Autowired + protected ISlideManager slideManager; + + @Autowired + protected ContentBlockRepository contentBlockRepository; + + protected abstract R getRepository(); + + public abstract T createContentBlock(String slideId); + + @Override + @SuppressWarnings("unchecked") + public T getContentBlock(String blockId) { + Optional block = getRepository().findById(blockId); + if (block.isPresent()) { + return (T) block.get(); + } + return null; + } + + @Override + public void deleteContentBlock(String blockId, String slideId) throws BlockDoesNotExistException { + if (blockId == null) { + return; + } + + Integer contentOrder = null; + Optional contentBlock = contentBlockRepository.findById(blockId); + if (contentBlock.isPresent()) { + contentOrder = contentBlock.get().getContentOrder(); + } else { + throw new BlockDoesNotExistException("Block Id not present"); + } + + try { + getRepository().deleteById(blockId); + updateContentOrderAfterDeletion(slideId, contentOrder); + } catch (EmptyResultDataAccessException e) { + throw new BlockDoesNotExistException(e); + } + } + + protected Integer getNextContentOrder(String slideId) { + Integer maxContentOrder = contentBlockRepository.findMaxContentOrder(slideId); + return maxContentOrder == null ? 0 : maxContentOrder + 1; + } + + private void updateContentOrderAfterDeletion(String slideId, Integer deletedContentOrder) { + if (deletedContentOrder == null) { + return; + } + + List contentBlockList = contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan(slideId, deletedContentOrder); + if (contentBlockList != null) { + for (ContentBlock eachContentBlock : contentBlockList) { + eachContentBlock.setContentOrder(eachContentBlock.getContentOrder() - 1); + } + contentBlockRepository.saveAll(contentBlockList); + } + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ImageBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ImageBlockManager.java new file mode 100644 index 000000000..50b4e4d13 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/ImageBlockManager.java @@ -0,0 +1,144 @@ +package edu.asu.diging.vspace.core.services.impl; + +import org.apache.tika.Tika; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.ImageContentBlockRepository; +import edu.asu.diging.vspace.core.data.ImageRepository; +import edu.asu.diging.vspace.core.exception.FileStorageException; +import edu.asu.diging.vspace.core.exception.ImageCouldNotBeStoredException; +import edu.asu.diging.vspace.core.factory.IImageBlockFactory; +import edu.asu.diging.vspace.core.factory.IImageFactory; +import edu.asu.diging.vspace.core.file.IStorageEngine; +import edu.asu.diging.vspace.core.model.IImageBlock; +import edu.asu.diging.vspace.core.model.ISlide; +import edu.asu.diging.vspace.core.model.IVSImage; +import edu.asu.diging.vspace.core.model.impl.ImageBlock; +import edu.asu.diging.vspace.core.model.impl.VSImage; +import edu.asu.diging.vspace.core.services.IImageBlockManager; + +@Service +public class ImageBlockManager extends GenericContentBlockManager + implements IImageBlockManager { + + @Autowired + private IImageFactory imageFactory; + + @Autowired + private IImageBlockFactory imageBlockFactory; + + @Autowired + private ImageRepository imageRepo; + + @Autowired + private ImageContentBlockRepository imageBlockRepo; + + @Autowired + private IStorageEngine storage; + + @Override + protected ImageContentBlockRepository getRepository() { + return imageBlockRepo; + } + + @Override + public IImageBlock createContentBlock(String slideId) { + try { + return createImageBlock(slideId, null, "default.jpg").getElement(); + } catch (ImageCouldNotBeStoredException e) { + throw new RuntimeException("Failed to create default image block", e); + } + } + + @Override + public CreationReturnValue createImageBlock(String slideId, byte[] image, String filename) + throws ImageCouldNotBeStoredException { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + IVSImage slideContentImage = saveImage(image, filename); + CreationReturnValue returnValue = new CreationReturnValue(); + returnValue.setErrorMsgs(new java.util.ArrayList<>()); + + storeImageFile(image, slideContentImage, filename); + IImageBlock imgBlock = imageBlockFactory.createImageBlock(slide, slideContentImage); + imgBlock.setContentOrder(contentOrder); + ImageBlock imageBlock = imageBlockRepo.save((ImageBlock) imgBlock); + + returnValue.setElement(imageBlock); + return returnValue; + } + + + @Override + public CreationReturnValue createImageBlock(String slideId, IVSImage image) { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + CreationReturnValue returnValue = new CreationReturnValue(); + returnValue.setErrorMsgs(new java.util.ArrayList<>()); + + IImageBlock imgBlock = imageBlockFactory.createImageBlock(slide, image); + imgBlock.setContentOrder(contentOrder); + ImageBlock imageBlock = imageBlockRepo.save((ImageBlock) imgBlock); + returnValue.setElement(imageBlock); + return returnValue; + } + + /** + * Updates an image block with new image data. + * + * @param imageBlock The image block to update + * @param image The new image bytes + * @param filename The filename + * @throws ImageCouldNotBeStoredException if image storage fails + */ + public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename) + throws ImageCouldNotBeStoredException { + IVSImage slideContentImage = saveImage(image, filename); + storeImageFile(image, slideContentImage, filename); + imageBlock.setImage(slideContentImage); + imageBlockRepo.save((ImageBlock) imageBlock); + } + + @Override + public void updateImageBlock(IImageBlock imageBlock, IVSImage image) { + imageBlock.setImage(image); + imageBlockRepo.save((ImageBlock) imageBlock); + } + + @Override + public void updateContentBlock(IImageBlock imageBlock) { + imageBlockRepo.save((ImageBlock) imageBlock); + } + + @Override + public void saveContentBlock(IImageBlock imageBlock) { + imageBlockRepo.save((ImageBlock) imageBlock); + } + + private IVSImage saveImage(byte[] image, String filename) { + if (image != null && image.length > 0) { + Tika tika = new Tika(); + String contentType = tika.detect(image); + IVSImage slideContentImage = imageFactory.createImage(filename, contentType); + return imageRepo.save((VSImage) slideContentImage); + } + return null; + } + + private void storeImageFile(byte[] image, IVSImage slideContentImage, String filename) + throws ImageCouldNotBeStoredException { + if (slideContentImage != null) { + String relativePath = null; + try { + relativePath = storage.storeFile(image, filename, slideContentImage.getId()); + } catch (FileStorageException e) { + throw new ImageCouldNotBeStoredException(e); + } + slideContentImage.setParentPath(relativePath); + imageRepo.save((VSImage) slideContentImage); + } + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/SpaceBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/SpaceBlockManager.java new file mode 100644 index 000000000..7d9c58096 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/SpaceBlockManager.java @@ -0,0 +1,58 @@ +package edu.asu.diging.vspace.core.services.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.SpaceContentBlockRepository; +import edu.asu.diging.vspace.core.factory.ISpaceBlockFactory; +import edu.asu.diging.vspace.core.model.ISlide; +import edu.asu.diging.vspace.core.model.ISpace; +import edu.asu.diging.vspace.core.model.ISpaceBlock; +import edu.asu.diging.vspace.core.model.impl.SpaceBlock; +import edu.asu.diging.vspace.core.services.ISpaceBlockManager; + +@Service +public class SpaceBlockManager extends GenericContentBlockManager + implements ISpaceBlockManager { + + @Autowired + private ISpaceBlockFactory spaceBlockFactory; + + @Autowired + private SpaceContentBlockRepository spaceBlockRepo; + + @Override + protected SpaceContentBlockRepository getRepository() { + return spaceBlockRepo; + } + + @Override + public ISpaceBlock createContentBlock(String slideId) { + return createSpaceBlock(slideId, "Default Space", null); + } + + @Override + public ISpaceBlock createSpaceBlock(String slideId, String title, ISpace space) { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + ISpaceBlock spaceBlock = spaceBlockFactory.createSpaceBlock(slide, title, space); + spaceBlock.setContentOrder(contentOrder); + return spaceBlockRepo.save((SpaceBlock) spaceBlock); + } + + @Override + public void updateContentBlock(ISpaceBlock spaceBlock) { + saveSpaceBlock(spaceBlock); + } + + @Override + public void saveSpaceBlock(ISpaceBlock spaceBlock) { + spaceBlockRepo.save((SpaceBlock) spaceBlock); + } + + @Override + public void saveContentBlock(ISpaceBlock spaceBlock) { + saveSpaceBlock(spaceBlock); + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/TextBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/TextBlockManager.java new file mode 100644 index 000000000..377d9963e --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/TextBlockManager.java @@ -0,0 +1,57 @@ +package edu.asu.diging.vspace.core.services.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.TextContentBlockRepository; +import edu.asu.diging.vspace.core.factory.ITextBlockFactory; +import edu.asu.diging.vspace.core.model.ISlide; +import edu.asu.diging.vspace.core.model.ITextBlock; +import edu.asu.diging.vspace.core.model.impl.TextBlock; +import edu.asu.diging.vspace.core.services.ITextBlockManager; + +@Service +public class TextBlockManager extends GenericContentBlockManager + implements ITextBlockManager { + + @Autowired + private ITextBlockFactory textBlockFactory; + + @Autowired + private TextContentBlockRepository textBlockRepo; + + @Override + protected TextContentBlockRepository getRepository() { + return textBlockRepo; + } + + @Override + public ITextBlock createContentBlock(String slideId) { + return createTextBlock(slideId, ""); + } + + @Override + public ITextBlock createTextBlock(String slideId, String text) { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + ITextBlock textBlock = textBlockFactory.createTextBlock(slide, text); + textBlock.setContentOrder(contentOrder); + return textBlockRepo.save((TextBlock) textBlock); + } + + @Override + public void updateContentBlock(ITextBlock textBlock) { + updateTextBlock((TextBlock) textBlock); + } + + @Override + public void updateTextBlock(TextBlock textBlock) { + textBlockRepo.save(textBlock); + } + + @Override + public void saveContentBlock(ITextBlock textBlock) { + textBlockRepo.save((TextBlock) textBlock); + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/VideoBlockManager.java b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/VideoBlockManager.java new file mode 100644 index 000000000..927f769d7 --- /dev/null +++ b/vspace/src/main/java/edu/asu/diging/vspace/core/services/impl/VideoBlockManager.java @@ -0,0 +1,137 @@ +package edu.asu.diging.vspace.core.services.impl; + +import org.apache.tika.Tika; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import edu.asu.diging.vspace.core.data.VideoContentBlockRepository; +import edu.asu.diging.vspace.core.data.VideoRepository; +import edu.asu.diging.vspace.core.exception.FileStorageException; +import edu.asu.diging.vspace.core.exception.VideoCouldNotBeStoredException; +import edu.asu.diging.vspace.core.factory.IVideoBlockFactory; +import edu.asu.diging.vspace.core.factory.IVideoFactory; +import edu.asu.diging.vspace.core.file.IStorageEngine; +import edu.asu.diging.vspace.core.model.ISlide; +import edu.asu.diging.vspace.core.model.IVSVideo; +import edu.asu.diging.vspace.core.model.IVideoBlock; +import edu.asu.diging.vspace.core.model.impl.VSVideo; +import edu.asu.diging.vspace.core.model.impl.VideoBlock; +import edu.asu.diging.vspace.core.services.IVideoBlockManager; + +@Service +public class VideoBlockManager extends GenericContentBlockManager + implements IVideoBlockManager { + + @Autowired + private IVideoFactory videoFactory; + + @Autowired + private IVideoBlockFactory videoBlockFactory; + + @Autowired + private VideoRepository videoRepo; + + @Autowired + private VideoContentBlockRepository videoBlockRepo; + + @Autowired + private IStorageEngine storage; + + @Override + protected VideoContentBlockRepository getRepository() { + return videoBlockRepo; + } + + @Override + public IVideoBlock createContentBlock(String slideId) { + try { + return createVideoBlock(slideId, null, null, "default.mp4", null, "Default Video").getElement(); + } catch (VideoCouldNotBeStoredException e) { + throw new RuntimeException("Failed to create default video block", e); + } + } + + @Override + public CreationReturnValue createVideoBlock(String slideId, byte[] video, Long size, String fileName, + String url, String title) throws VideoCouldNotBeStoredException { + ISlide slide = slideManager.getSlide(slideId); + Integer contentOrder = getNextContentOrder(slideId); + + CreationReturnValue returnValue = new CreationReturnValue(); + returnValue.setErrorMsgs(new java.util.ArrayList<>()); + + IVSVideo slideContentVideo = storeVideo(video, size, fileName, url, title); + IVideoBlock vidBlock = videoBlockFactory.createVideoBlock(slide, slideContentVideo); + vidBlock.setContentOrder(contentOrder); + VideoBlock videoBlock = videoBlockRepo.save((VideoBlock) vidBlock); + returnValue.setElement(videoBlock); + return returnValue; + } + + @Override + public void updateVideoBlock(IVideoBlock videoBlock, byte[] video, Long fileSize, String url, + String filename, String title) throws VideoCouldNotBeStoredException { + IVSVideo slideContentVideo = storeVideo(video, fileSize, filename, url, title); + videoBlock.setVideo(slideContentVideo); + videoBlockRepo.save((VideoBlock) videoBlock); + } + + @Override + public void updateContentBlock(IVideoBlock videoBlock) { + videoBlockRepo.save((VideoBlock) videoBlock); + } + + @Override + public void saveVideoBlock(IVideoBlock videoBlock) { + videoRepo.save((VSVideo) videoBlock.getVideo()); + } + + @Override + public void saveContentBlock(IVideoBlock videoBlock) { + saveVideoBlock(videoBlock); + } + + private IVSVideo storeVideo(byte[] video, Long size, String fileName, String url, String title) + throws VideoCouldNotBeStoredException { + IVSVideo slideContentVideo = null; + if (video != null) { + slideContentVideo = saveVideo(video, size, fileName, title); + storeVideoFile(video, slideContentVideo, fileName); + slideContentVideo.setUrl(null); + } else if (url != null && !url.isEmpty()) { + slideContentVideo = saveVideoWithUrl(url, title); + } + return slideContentVideo; + } + + private IVSVideo saveVideoWithUrl(String url, String title) { + IVSVideo vidContent = videoFactory.createVideo(url); + vidContent.setTitle(title); + return videoRepo.save((VSVideo) vidContent); + } + + private IVSVideo saveVideo(byte[] video, Long size, String filename, String title) { + if (video != null && video.length > 0) { + Tika tika = new Tika(); + String contentType = tika.detect(video); + IVSVideo slideContentVideo = videoFactory.createVideo(filename, size, contentType); + slideContentVideo.setTitle(title); + return videoRepo.save((VSVideo) slideContentVideo); + } + return null; + } + + private void storeVideoFile(byte[] video, IVSVideo slideContentVideo, String filename) + throws VideoCouldNotBeStoredException { + if (slideContentVideo != null) { + String relativePath = null; + try { + relativePath = storage.storeFile(video, filename, slideContentVideo.getId()); + } catch (FileStorageException e) { + throw new VideoCouldNotBeStoredException(e); + } + slideContentVideo.setParentPath(relativePath); + videoRepo.save((VSVideo) slideContentVideo); + } + } +} diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddBiblioBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddBiblioBlockController.java index 4ec6a610a..c15e8589a 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddBiblioBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddBiblioBlockController.java @@ -35,10 +35,8 @@ public ResponseEntity addBiblioBlock(@PathVariable("id") String sli String biblioTitle = biblioBlockData.getBiblioTitle(); String description = biblioBlockData.getDescription(); - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; - IBiblioBlock biblioBlock = contentBlockManager.createBiblioBlock(slideId, biblioTitle, description, contentOrder); + IBiblioBlock biblioBlock = contentBlockManager.createBiblioBlock(slideId, biblioTitle, description); return new ResponseEntity<>((BiblioBlock) biblioBlock, HttpStatus.OK); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java index 6cad36731..7ea26da1f 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddChoiceBlockController.java @@ -30,11 +30,7 @@ public ResponseEntity> addChoiceBlock(@PathVariable("id") St @PathVariable("moduleId") String moduleId, @RequestParam("selectedChoices") List selectedChoices, @RequestParam("showsAll") boolean showsAll) throws IOException { - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; - - IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, contentOrder, - showsAll); + IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, showsAll); /* * After annotating sequence attribute with JsonIgnore in Choice model to fix * stack overflow issue the sequences are not returned as part of choiceBlock diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java index 5ef79b090..94396febc 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddImageBlockController.java @@ -45,8 +45,7 @@ public ResponseEntity addImageBlock(@PathVariable("id") String slideId, @RequestParam(value = "imageId", required = false) String imageId, RedirectAttributes attributes) throws IOException { - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; + IVSpaceElement imageBlock; String imageBlockId = null; @@ -58,8 +57,7 @@ public ResponseEntity addImageBlock(@PathVariable("id") String slideId, logger.error("Image does not exist.", e); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } - CreationReturnValue imageBlockReturnValue = contentBlockManager.createImageBlock(slideId, image, - contentOrder); + CreationReturnValue imageBlockReturnValue = contentBlockManager.createImageBlock(slideId, image); imageBlock = imageBlockReturnValue.getElement(); imageBlockId = imageBlock.getId(); } else { @@ -68,7 +66,7 @@ public ResponseEntity addImageBlock(@PathVariable("id") String slideId, String filename = file.getOriginalFilename(); try { CreationReturnValue imageBlockReturnValue = contentBlockManager.createImageBlock(slideId, image, - filename, contentOrder); + filename); imageBlock = imageBlockReturnValue.getElement(); } catch (ImageCouldNotBeStoredException e) { ObjectMapper mapper = new ObjectMapper(); diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddSpaceBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddSpaceBlockController.java index f890ba56e..26369c775 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddSpaceBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddSpaceBlockController.java @@ -27,9 +27,7 @@ public class AddSpaceBlockController { public ResponseEntity addSpaceBlock(@RequestParam("title") String title,@RequestParam("spaceId") String spaceId, @PathVariable("slideId") String slideId, @PathVariable("moduleId") String moduleId){ ISpace space = spaceManager.getSpace(spaceId); - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; - ISpaceBlock spaceBlock = contentBlockManager.createSpaceBlock(slideId, title, contentOrder, space); + ISpaceBlock spaceBlock = contentBlockManager.createSpaceBlock(slideId, title, space); return new ResponseEntity<>(spaceBlock.getId(), HttpStatus.OK); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java index fac4734cc..eb044ee0d 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddTextBlockController.java @@ -24,10 +24,7 @@ public class AddTextBlockController { public ResponseEntity addTextBlock(@PathVariable("id") String slideId, @PathVariable("moduleId") String moduleId, @RequestParam("content") String content) throws IOException { - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; - - ITextBlock textBlock = contentBlockManager.createTextBlock(slideId, content, contentOrder); + ITextBlock textBlock = contentBlockManager.createTextBlock(slideId, content); return new ResponseEntity<>(textBlock.getId(), HttpStatus.OK); } diff --git a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddVideoBlockController.java b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddVideoBlockController.java index 4fa2efbf4..749dad0f3 100644 --- a/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddVideoBlockController.java +++ b/vspace/src/main/java/edu/asu/diging/vspace/web/staff/AddVideoBlockController.java @@ -37,8 +37,7 @@ public ResponseEntity addVideoBlock(@PathVariable("id") String slideId, @RequestParam(required = false) String url, @RequestParam(required = false) String videoTitle, Principal principal, RedirectAttributes attributes) throws IOException { - Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId); - contentOrder = contentOrder == null ? 0 : contentOrder + 1; + String videoId; try { if (videoFile == null && url.isEmpty()) { @@ -51,7 +50,7 @@ public ResponseEntity addVideoBlock(@PathVariable("id") String slideId, fileName = videoFile.getOriginalFilename(); } CreationReturnValue videoBlockValue = contentBlockManager.createVideoBlock(slideId, video, - (videoFile != null) ? videoFile.getSize() : null, fileName, url, contentOrder, videoTitle); + (videoFile != null) ? videoFile.getSize() : null, fileName, url, videoTitle); videoId = videoBlockValue.getElement().getId(); } catch (VideoCouldNotBeStoredException e) { logger.warn("Video block could not be stored, bad request.", e);