Skip to content
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

1361 Add image deletion #1603

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/ai/elimu/dao/ImageContributionEventDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public interface ImageContributionEventDao extends GenericDao<ImageContributionE
List<ImageContributionEvent> readAll(Image image) throws DataAccessException;

List<ImageContributionEvent> readAll(Contributor contributor) throws DataAccessException;

List<ImageContributionEvent> readMostRecent(int maxResults) throws DataAccessException;


Long readCount(Contributor contributor) throws DataAccessException;

void deleteAllEventsForImage(Image image);
}
3 changes: 3 additions & 0 deletions src/main/java/ai/elimu/dao/StoryBookDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import ai.elimu.model.content.StoryBook;

import ai.elimu.model.content.multimedia.Image;
import ai.elimu.model.v2.enums.ReadingLevel;

import org.springframework.dao.DataAccessException;
Expand All @@ -16,4 +17,6 @@ public interface StoryBookDao extends GenericDao<StoryBook> {
List<StoryBook> readAllOrdered(ReadingLevel readingLevel) throws DataAccessException;

List<StoryBook> readAllUnleveled() throws DataAccessException;

List<StoryBook> readAllWithImage(Image image) throws DataAccessException;
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 10 additions & 10 deletions src/main/java/ai/elimu/dao/jpa/ImageContributionEventDaoJpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ public List<ImageContributionEvent> readAll(Contributor contributor) throws Data
.getResultList();
}

@Override
public List<ImageContributionEvent> readMostRecent(int maxResults) throws DataAccessException {
return em.createQuery(
"SELECT ice " +
"FROM ImageContributionEvent ice " +
"ORDER BY ice.time DESC")
.setMaxResults(maxResults)
.getResultList();
}

@Override
public Long readCount(Contributor contributor) throws DataAccessException {
return (Long) em.createQuery("SELECT COUNT(ice) " +
Expand All @@ -49,4 +39,14 @@ public Long readCount(Contributor contributor) throws DataAccessException {
.setParameter("contributor", contributor)
.getSingleResult();
}

@Override
public void deleteAllEventsForImage(Image image) {
em.createQuery("DELETE " +
"FROM ImageContributionEvent ice " +
"WHERE ice.image = :image")
.setParameter("image", image)
.executeUpdate();
}

}
13 changes: 13 additions & 0 deletions src/main/java/ai/elimu/dao/jpa/StoryBookDaoJpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.persistence.NoResultException;
import ai.elimu.dao.StoryBookDao;

import ai.elimu.model.content.multimedia.Image;
import org.springframework.dao.DataAccessException;

import ai.elimu.model.content.StoryBook;
Expand Down Expand Up @@ -54,4 +55,16 @@ public List<StoryBook> readAllUnleveled() throws DataAccessException {
"ORDER BY book.title")
.getResultList();
}

@Override
public List<StoryBook> readAllWithImage(Image image) throws DataAccessException {
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
return em.createQuery(
"SELECT book " +
"FROM StoryBook book " +
"WHERE book.coverImage.id = :image ")
.setParameter("image", image.getId())
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
.getResultList();

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ai.elimu.web.content.multimedia.image;

import ai.elimu.dao.*;
import ai.elimu.model.content.Emoji;
import ai.elimu.model.content.Word;
import ai.elimu.model.content.multimedia.Audio;
import ai.elimu.model.content.multimedia.Image;
import ai.elimu.model.enums.ContentLicense;
import ai.elimu.model.v2.enums.content.LiteracySkill;
import ai.elimu.model.v2.enums.content.NumeracySkill;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ImageComponent {

private final Logger logger = LogManager.getLogger();

@Autowired
private LetterDao letterDao;

@Autowired
private NumberDao numberDao;

@Autowired
private WordDao wordDao;

@Autowired
private EmojiDao emojiDao;

@Autowired
private AudioDao audioDao;

@Autowired
private ImageContributionEventDao imageContributionEventDao;

public void setImageModel(Model model, Image image) {
model.addAttribute("image", image);
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());

model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("imageContributionEvents", imageContributionEventDao.readAll(image));

model.addAttribute("letters", letterDao.readAllOrdered());
model.addAttribute("numbers", numberDao.readAllOrdered());
model.addAttribute("words", wordDao.readAllOrdered());
model.addAttribute("emojisByWordId", getEmojisByWordId());

Audio audio = audioDao.readByTranscription(image.getTitle());
model.addAttribute("audio", audio);
}

private Map<Long, String> getEmojisByWordId() {
logger.info("getEmojisByWordId");

Map<Long, String> emojisByWordId = new HashMap<>();

for (Word word : wordDao.readAll()) {
String emojiGlyphs = "";

List<Emoji> emojis = emojiDao.readAllLabeled(word);
for (Emoji emoji : emojis) {
emojiGlyphs += emoji.getGlyph();
}

if (StringUtils.isNotBlank(emojiGlyphs)) {
emojisByWordId.put(word.getId(), emojiGlyphs);
}
}

return emojisByWordId;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package ai.elimu.web.content.multimedia.image;

import ai.elimu.dao.*;
import ai.elimu.model.content.multimedia.Image;
import org.apache.logging.log4j.Logger;
import ai.elimu.dao.ImageDao;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -10,6 +11,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashSet;

@Controller
@RequestMapping("/content/multimedia/image/delete")
public class ImageDeleteController {
Expand All @@ -19,20 +22,39 @@ public class ImageDeleteController {
@Autowired
private ImageDao imageDao;

@Autowired
private StoryBookDao storyBookDao;

@Autowired
private ImageComponent imageComponent;

@Autowired
private ImageContributionEventDao imageContributionEventDao;

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
logger.info("handleRequest");

// Before deleting, check if the image is used in any StoryBooks
// TODO

Image image = imageDao.read(id);
if (storyBookDao.readAllWithImage(image).size() > 0) {
imageComponent.setImageModel(model, image);
model.addAttribute("errorCode", "StoryBookContainImage");

return "content/multimedia/image/edit";
}

// Before deleting, remove any labels
// TODO

image.setLetters(new HashSet<>());
image.setWords(new HashSet<>());
image.setNumbers(new HashSet<>());
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
imageDao.update(image);

// Delete all contribution event connected with image
imageContributionEventDao.deleteAllEventsForImage(image);

// Delete the image from the database
// TODO
// Image image = imageDao.read(id);
// imageDao.delete(image);
imageDao.delete(image);

// Store contribution event
// TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@
import ai.elimu.dao.LetterDao;
import ai.elimu.dao.NumberDao;
import ai.elimu.dao.WordDao;
import ai.elimu.model.content.Emoji;
import ai.elimu.model.content.Letter;
import ai.elimu.model.content.Number;
import ai.elimu.model.content.Word;
import ai.elimu.model.content.multimedia.Audio;
import ai.elimu.model.content.multimedia.Image;
import ai.elimu.model.contributor.Contributor;
import ai.elimu.model.contributor.ImageContributionEvent;
import ai.elimu.model.enums.ContentLicense;
import ai.elimu.model.enums.Platform;
import ai.elimu.model.v2.enums.content.ImageFormat;
import ai.elimu.model.v2.enums.content.LiteracySkill;
import ai.elimu.model.v2.enums.content.NumeracySkill;
import ai.elimu.util.DiscordHelper;
import ai.elimu.util.ImageHelper;
import ai.elimu.web.context.EnvironmentContextLoaderListener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -79,28 +71,17 @@ public class ImageEditController {
@Autowired
private AudioDao audioDao;

@Autowired
ImageComponent imageComponent;

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(
Model model,
@PathVariable Long id) {
logger.info("handleRequest");

Image image = imageDao.read(id);
model.addAttribute("image", image);
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());

model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("imageContributionEvents", imageContributionEventDao.readAll(image));

model.addAttribute("letters", letterDao.readAllOrdered());
model.addAttribute("numbers", numberDao.readAllOrdered());
model.addAttribute("words", wordDao.readAllOrdered());
model.addAttribute("emojisByWordId", getEmojisByWordId());

Audio audio = audioDao.readByTranscription(image.getTitle());
model.addAttribute("audio", audio);
imageComponent.setImageModel(model, image);

return "content/multimedia/image/edit";
}
Expand Down Expand Up @@ -175,22 +156,7 @@ public String handleSubmit(
}

if (result.hasErrors()) {
model.addAttribute("image", image);
model.addAttribute("contentLicenses", ContentLicense.values());
model.addAttribute("literacySkills", LiteracySkill.values());
model.addAttribute("numeracySkills", NumeracySkill.values());

model.addAttribute("timeStart", System.currentTimeMillis());
model.addAttribute("imageContributionEvents", imageContributionEventDao.readAll(image));

model.addAttribute("letters", letterDao.readAllOrdered());
model.addAttribute("numbers", numberDao.readAllOrdered());
model.addAttribute("words", wordDao.readAllOrdered());
model.addAttribute("emojisByWordId", getEmojisByWordId());

Audio audio = audioDao.readByTranscription(image.getTitle());
model.addAttribute("audio", audio);

imageComponent.setImageModel(model, image);
return "content/multimedia/image/edit";
} else {
image.setTitle(image.getTitle().toLowerCase());
Expand Down Expand Up @@ -349,25 +315,5 @@ public String handleRemoveContentLabelRequest(

return "success";
}

private Map<Long, String> getEmojisByWordId() {
logger.info("getEmojisByWordId");

Map<Long, String> emojisByWordId = new HashMap<>();

for (Word word : wordDao.readAll()) {
String emojiGlyphs = "";

List<Emoji> emojis = emojiDao.readAllLabeled(word);
for (Emoji emoji : emojis) {
emojiGlyphs += emoji.getGlyph();
}

if (StringUtils.isNotBlank(emojiGlyphs)) {
emojisByWordId.put(word.getId(), emojiGlyphs);
}
}

return emojisByWordId;
}

}
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/i18n/errors_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ formatHint.Integer=Number (e.g. "5000")
image.too.small=The image width must be at least 640px
emoji.unicode.version=Only emojis up to Unicode version 9
WordSpace=Spaces are not allowed
StoryBookContainImage=Story book contain image
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
<div class="card-panel">
<form:form modelAttribute="image" enctype="multipart/form-data">
<tag:formErrors modelAttribute="image" />


<c:if test="${not empty errorCode}">
<div id="errorPanel" class="card-panel red lighten-3">
<fmt:message key="${errorCode}" />
</div>
</c:if>

<form:hidden path="revisionNumber" value="${image.revisionNumber}" />
<form:hidden path="imageFormat" value="${image.imageFormat}" />
<form:hidden path="contentType" value="${image.contentType}" />
Expand Down
Loading