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 all 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);
}
4 changes: 4 additions & 0 deletions src/main/java/ai/elimu/dao/StoryBookChapterDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import ai.elimu.model.content.StoryBook;
import ai.elimu.model.content.StoryBookChapter;

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

public interface StoryBookChapterDao extends GenericDao<StoryBookChapter> {

List<StoryBookChapter> readAll(StoryBook storyBook) throws DataAccessException;

List<StoryBookChapter> readAllWithImage(Image image) throws DataAccessException;

}
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> readAllWithCoverImage(Image image) throws DataAccessException;
}
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/StoryBookChapterDaoJpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ai.elimu.dao.StoryBookChapterDao;
import java.util.List;

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

import ai.elimu.model.content.StoryBook;
Expand All @@ -20,4 +21,16 @@ public List<StoryBookChapter> readAll(StoryBook storyBook) throws DataAccessExce
.setParameter("storyBook", storyBook)
.getResultList();
}

@Override
public List<StoryBookChapter> readAllWithImage(Image image) throws DataAccessException {
return em.createQuery(
"SELECT ch " +
"FROM StoryBookChapter ch " +
"WHERE ch.image = :image ")
.setParameter("image", image)
.getResultList();

}

}
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> readAllWithCoverImage(Image image) throws DataAccessException {
return em.createQuery(
"SELECT book " +
"FROM StoryBook book " +
"WHERE book.coverImage = :image")
.setParameter("image", image)
.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();
}

Check warning on line 74 in src/main/java/ai/elimu/web/content/multimedia/image/ImageComponent.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ai/elimu/web/content/multimedia/image/ImageComponent.java#L73-L74

Added lines #L73 - L74 were not covered by tests

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

Check warning on line 77 in src/main/java/ai/elimu/web/content/multimedia/image/ImageComponent.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ai/elimu/web/content/multimedia/image/ImageComponent.java#L77

Added line #L77 was not covered by tests
}
}

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,42 @@ public class ImageDeleteController {
@Autowired
private ImageDao imageDao;

@Autowired
private StoryBookDao storyBookDao;

@Autowired
private ImageComponent imageComponent;

@Autowired
private ImageContributionEventDao imageContributionEventDao;

@Autowired
private StoryBookChapterDao storyBookChapterDao;

@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.readAllWithCoverImage(image).size() > 0 || storyBookChapterDao.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 @@
@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);

Check warning on line 84 in src/main/java/ai/elimu/web/content/multimedia/image/ImageEditController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ai/elimu/web/content/multimedia/image/ImageEditController.java#L84

Added line #L84 was not covered by tests

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

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);

Check warning on line 159 in src/main/java/ai/elimu/web/content/multimedia/image/ImageEditController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ai/elimu/web/content/multimedia/image/ImageEditController.java#L159

Added line #L159 was not covered by tests
return "content/multimedia/image/edit";
} else {
image.setTitle(image.getTitle().toLowerCase());
Expand Down Expand Up @@ -351,25 +317,5 @@

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
Loading