Skip to content

Commit

Permalink
Merge pull request #244 from diging/develop
Browse files Browse the repository at this point in the history
prepare release
  • Loading branch information
jdamerow authored Aug 18, 2021
2 parents 35e8976 + 4b38b9c commit 3098621
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package edu.asu.diging.vspace.core.data;

import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

import javax.transaction.Transactional;
import edu.asu.diging.vspace.core.model.impl.ContentBlock;

@Repository
@JaversSpringDataAuditable
public interface ContentBlockRepository extends PagingAndSortingRepository<ContentBlock, String> {

@Query("SELECT max(contentOrder) FROM ContentBlock d WHERE d.slide.id = ?1")
public Integer findMaxContentOrder(String slideId);

public List<ContentBlock> findBySlide_IdAndContentOrderGreaterThan(String slideId,Integer contentOrder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ CreationReturnValue createImageBlock(String slideId, byte[] image, String filena

List<IContentBlock> getAllContentBlocks(String slideId);

void deleteTextBlockById(String blockid) throws BlockDoesNotExistException;
void deleteTextBlockById(String blockid,String slideId) throws BlockDoesNotExistException;

void deleteImageBlockById(String blockid) throws BlockDoesNotExistException;
void deleteImageBlockById(String blockid,String slideId) throws BlockDoesNotExistException;

void deleteChoiceBlockById(String blockid) throws BlockDoesNotExistException;
void deleteChoiceBlockById(String blockid,String slideId) throws BlockDoesNotExistException;

void updateTextBlock(TextBlock textBlock);

Expand All @@ -34,9 +34,10 @@ CreationReturnValue createImageBlock(String slideId, byte[] image, String filena

IChoiceBlock getChoiceBlock(String choiceBlockId);

void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename, Integer contentOrder)
void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename)
throws ImageCouldNotBeStoredException;

IChoiceBlock createChoiceBlock(String slideId, List<String> selectedChoices, Integer contentOrder, boolean showsAll);


Integer findMaxContentOrder(String slideId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.stereotype.Service;

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.TextContentBlockRepository;
Expand All @@ -32,6 +33,7 @@
import edu.asu.diging.vspace.core.model.ITextBlock;
import edu.asu.diging.vspace.core.model.IVSImage;
import edu.asu.diging.vspace.core.model.impl.ChoiceBlock;
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.TextBlock;
import edu.asu.diging.vspace.core.model.impl.VSImage;
Expand Down Expand Up @@ -72,6 +74,9 @@ public class ContentBlockManager implements IContentBlockManager {
@Autowired
private IStorageEngine storage;

@Autowired
private ContentBlockRepository contentBlockRepository;

/*
* (non-Javadoc)
*
Expand All @@ -87,7 +92,8 @@ public List<IContentBlock> getAllContentBlocks(String slideId) {
/*
* (non-Javadoc)
*
* @see edu.asu.diging.vspace.core.services.impl.ITextBlock#createTextBlock(java.
* @see
* edu.asu.diging.vspace.core.services.impl.ITextBlock#createTextBlock(java.
* lang.String, java.lang.String)
*/
@Override
Expand Down Expand Up @@ -148,62 +154,93 @@ public CreationReturnValue createImageBlock(String slideId, byte[] image, String
}

/**
* Delete a text block using an id
* 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.
*
* @param id - id of resource to be deleted. If the id is null then the
* functions returns nothing.
*
*/

@Override
public void deleteTextBlockById(String id) throws BlockDoesNotExistException {
if (id == null) {
public void deleteTextBlockById(String blockId, String slideId) throws BlockDoesNotExistException {
if (blockId == null) {
return;
}
Integer contentOrder = null;
Optional<ContentBlock> contentBlock = contentBlockRepository.findById(blockId);
if (contentBlock.isPresent()) {
contentOrder = contentBlock.get().getContentOrder();
} else {
throw new BlockDoesNotExistException("Block Id not present");
}
try {
textBlockRepo.deleteById(id);
textBlockRepo.deleteById(blockId);
updateContentOrder(slideId, contentOrder);
} catch (EmptyResultDataAccessException e) {
throw new BlockDoesNotExistException(e);
}

}

/**
* Delete an image block using an id
* 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 id - id of resource to be deleted. If the id is null then the
* functions returns nothing.
*
* @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 id) throws BlockDoesNotExistException {
if (id == null) {
public void deleteImageBlockById(String blockId, String slideId) throws BlockDoesNotExistException {
if (blockId == null) {
return;
}
Integer contentOrder = null;
Optional<ContentBlock> contentBlock = contentBlockRepository.findById(blockId);
if (contentBlock.isPresent()) {
contentOrder = contentBlock.get().getContentOrder();
} else {
throw new BlockDoesNotExistException("Block Id not present");
}
try {
imageBlockRepo.deleteById(id);
imageBlockRepo.deleteById(blockId);
updateContentOrder(slideId, contentOrder);
} catch (EmptyResultDataAccessException e) {
throw new BlockDoesNotExistException(e);
}

}

/**
* Delete a choices block using an id
* 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 id - id of resource to be deleted. If the id is null then the
* functions returns nothing.
*
* @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 id) throws BlockDoesNotExistException {
if (id == null) {
public void deleteChoiceBlockById(String blockId, String slideId) throws BlockDoesNotExistException {
if (blockId == null) {
return;
}
Integer contentOrder = null;
Optional<ContentBlock> contentBlock = contentBlockRepository.findById(blockId);
if (contentBlock.isPresent()) {
contentOrder = contentBlock.get().getContentOrder();
} else {
throw new BlockDoesNotExistException("Block Id not present");
}
try {
choiceBlockRepo.deleteById(id);
choiceBlockRepo.deleteById(blockId);
updateContentOrder(slideId, contentOrder);
} catch (EmptyResultDataAccessException e) {
throw new BlockDoesNotExistException(e);
}
Expand All @@ -215,7 +252,7 @@ public void updateTextBlock(TextBlock textBlock) {
}

@Override
public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename, Integer contentOrder)
public void updateImageBlock(IImageBlock imageBlock, byte[] image, String filename)
throws ImageCouldNotBeStoredException {
IVSImage slideContentImage = saveImage(image, filename);
storeImageFile(image, slideContentImage, filename);
Expand Down Expand Up @@ -253,17 +290,49 @@ public IChoiceBlock getChoiceBlock(String choiceBlockId) {
/*
* (non-Javadoc)
*
* @see edu.asu.diging.vspace.core.services.impl.IChoiceBlock#createTextBlock(java.
* @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<String> selectedChoices, Integer contentOrder, boolean showsAll) {
public IChoiceBlock createChoiceBlock(String slideId, List<String> selectedChoices, Integer contentOrder,
boolean showsAll) {
List<IChoice> choices = new ArrayList<IChoice>();
if(!showsAll) {
choices = selectedChoices.stream().map(choice -> slideManager.getChoice(choice)).collect(Collectors.toList());
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);
IChoiceBlock choiceBlock = choiceBlockFactory.createChoiceBlock(slideManager.getSlide(slideId), contentOrder,
choices, showsAll);
return choiceBlockRepo.save((ChoiceBlock) choiceBlock);
}

/**
* Retrieving the maximum content order for a slide
*/
@Override
public Integer findMaxContentOrder(String slideId) {
return contentBlockRepository.findMaxContentOrder(slideId);
}

/**
* 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<ContentBlock> contentBlockList = contentBlockRepository.findBySlide_IdAndContentOrderGreaterThan(slideId,
contentOrder);
if (contentBlockList != null) {
for (ContentBlock eachContentBlock : contentBlockList) {
eachContentBlock.setContentOrder(eachContentBlock.getContentOrder() - 1);
}
contentBlockRepository.saveAll(contentBlockList);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import edu.asu.diging.vspace.core.data.ContentBlockRepository;
import edu.asu.diging.vspace.core.model.IChoiceBlock;
import edu.asu.diging.vspace.core.model.ISequence;
import edu.asu.diging.vspace.core.services.IContentBlockManager;
Expand All @@ -26,20 +27,27 @@ public class AddChoiceBlockController {
private IContentBlockManager contentBlockManager;

@RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/choice/content", method = RequestMethod.POST)
public ResponseEntity<Map<String,Object>> addChoiceBlock(@PathVariable("id") String slideId,
@PathVariable("moduleId") String moduleId,
@RequestParam("contentOrder") Integer contentOrder, @RequestParam("selectedChoices") List<String> selectedChoices,
public ResponseEntity<Map<String, Object>> addChoiceBlock(@PathVariable("id") String slideId,
@PathVariable("moduleId") String moduleId, @RequestParam("selectedChoices") List<String> selectedChoices,
@RequestParam("showsAll") boolean showsAll) throws IOException {

IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, contentOrder, showsAll);
/*After annotating sequence attribute with JsonIgnore in Choice model to fix stack overflow issue
* the sequences are not returned as part of choiceBlock object, hence sequences are explicitly passed
* as part of Response in a list form*/
Map<String,Object> responseData = new HashMap<String,Object>();
responseData.put("choiceBlock",choiceBlock);
if(!choiceBlock.isShowsAll()) {
List<ISequence> selectedSequences = choiceBlock.getChoices().stream().map(choice->choice.getSequence()).collect(Collectors.toList());
responseData.put("selectedSequences",selectedSequences);
Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId);
contentOrder = contentOrder == null ? 0 : contentOrder + 1;

IChoiceBlock choiceBlock = contentBlockManager.createChoiceBlock(slideId, selectedChoices, contentOrder,
showsAll);
/*
* After annotating sequence attribute with JsonIgnore in Choice model to fix
* stack overflow issue the sequences are not returned as part of choiceBlock
* object, hence sequences are explicitly passed as part of Response in a list
* form
*/
Map<String, Object> responseData = new HashMap<String, Object>();
responseData.put("choiceBlock", choiceBlock);
if (!choiceBlock.isShowsAll()) {
List<ISequence> selectedSequences = choiceBlock.getChoices().stream().map(choice -> choice.getSequence())
.collect(Collectors.toList());
responseData.put("selectedSequences", selectedSequences);
}
return new ResponseEntity<>(responseData, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import edu.asu.diging.vspace.core.data.ContentBlockRepository;
import edu.asu.diging.vspace.core.exception.ImageCouldNotBeStoredException;
import edu.asu.diging.vspace.core.model.IVSpaceElement;
import edu.asu.diging.vspace.core.services.IContentBlockManager;
Expand All @@ -30,9 +31,8 @@ public class AddImageBlockController {

@RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/image", method = RequestMethod.POST)
public ResponseEntity<String> addImageBlock(@PathVariable("id") String slideId,
@PathVariable("moduleId") String moduleId, @RequestParam("file") MultipartFile file,
@RequestParam("contentOrder") Integer contentOrder, Principal principal, RedirectAttributes attributes)
throws IOException {
@PathVariable("moduleId") String moduleId, @RequestParam("file") MultipartFile file, Principal principal,
RedirectAttributes attributes) throws IOException {

byte[] image = null;
String filename = null;
Expand All @@ -41,6 +41,9 @@ public ResponseEntity<String> addImageBlock(@PathVariable("id") String slideId,
filename = file.getOriginalFilename();
}
String imageId;
Integer contentOrder = contentBlockManager.findMaxContentOrder(slideId);
contentOrder = contentOrder == null ? 0 : contentOrder + 1;

try {
CreationReturnValue imageBlockReturnValue = contentBlockManager.createImageBlock(slideId, image, filename,
contentOrder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import edu.asu.diging.vspace.core.data.ContentBlockRepository;
import edu.asu.diging.vspace.core.model.ITextBlock;
import edu.asu.diging.vspace.core.services.IContentBlockManager;

Expand All @@ -22,9 +23,11 @@ public class AddTextBlockController {

@RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/textcontent", method = RequestMethod.POST)
public ResponseEntity<String> addTextBlock(@PathVariable("id") String slideId,
@PathVariable("moduleId") String moduleId, @RequestParam("content") String content,
@RequestParam("contentOrder") Integer contentOrder) throws IOException {
@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);

return new ResponseEntity<>(textBlock.getId(), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.asu.diging.vspace.core.data.ContentBlockRepository;
import edu.asu.diging.vspace.core.exception.BlockDoesNotExistException;
import edu.asu.diging.vspace.core.services.IContentBlockManager;

Expand All @@ -24,9 +25,10 @@ public class DeleteChoiceBlockController {
private IContentBlockManager contentBlockManager;

@RequestMapping(value = "/staff/module/{moduleId}/slide/{id}/choiceBlock/{blockId}", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteChoiceBlock(@PathVariable("blockId") String blockId) throws IOException {
public ResponseEntity<String> deleteChoiceBlock(@PathVariable("id") String slideId,@PathVariable("blockId") String blockId) throws IOException {

try {
contentBlockManager.deleteChoiceBlockById(blockId);
contentBlockManager.deleteChoiceBlockById(blockId,slideId);
} catch (BlockDoesNotExistException e) {
logger.warn("Choice Block Id does not exist, bad request.", e);
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
Expand Down
Loading

0 comments on commit 3098621

Please sign in to comment.