Skip to content

Commit

Permalink
Attachments als Liste
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Dec 1, 2023
1 parent 9bd0cdb commit 7634651
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 73 deletions.
15 changes: 1 addition & 14 deletions src/main/java/de/holarse/backend/db/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.annotations.FilterJoinTable;

@Table(name = "articles")
@Entity
public class Article extends Base {

private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;

@Column(name = "nodeid")
private int nodeId;
Expand Down Expand Up @@ -47,10 +46,6 @@ public class Article extends Base {
@JoinColumn(name="nodeid", insertable=false, nullable=false, updatable = false, referencedColumnName = "nodeid")
private Set<NodeSlug> nodeSlugs = new HashSet<>();

@OneToMany
@JoinColumn(name="nodeid", insertable=false, nullable=false, updatable = false, referencedColumnName = "nodeid")
private List<Attachment> attachments = new ArrayList<>();

public int getNodeId() {
return nodeId;
}
Expand Down Expand Up @@ -98,13 +93,5 @@ public Set<NodeSlug> getNodeSlugs() {
public void setNodeSlugs(Set<NodeSlug> nodeSlugs) {
this.nodeSlugs = nodeSlugs;
}

public List<Attachment> getAttachments() {
return attachments;
}

public void setAttachments(List<Attachment> attachments) {
this.attachments = attachments;
}

}
19 changes: 19 additions & 0 deletions src/main/java/de/holarse/backend/db/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package de.holarse.backend.db;

import de.holarse.backend.view.AttachmentView;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
Expand Down Expand Up @@ -77,4 +78,22 @@ public String toString() {
return "Attachment{" + "nodeId=" + nodeId + ", weight=" + weight + ", description=" + description + ", attachmentType=" + attachmentType + ", data=" + data + '}';
}

/**
* Ein neues Attachment aus einem View erzeugen
* @param av
* @param nodeId
* @param attachmentType
* @return
*/
public static Attachment build(final AttachmentView av, final Integer nodeId, final AttachmentType attachmentType) {
final Attachment att = new Attachment();
att.setData(av.getData());
att.setDescription(av.getDescription());
att.setWeight(av.getWeight());
att.setNodeId(nodeId);
att.setAttachmentType(attachmentType);

return att;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface ArticleRepository extends JpaRepository<Article, Integer>, Node
@Query(value = "FROM Article a " +
"JOIN FETCH a.articleRevision " +
"JOIN FETCH a.nodeStatus as ns " +
"LEFT JOIN FETCH a.websiteLinks as link_web " +
"LEFT JOIN FETCH a.screenshots as link_scr " +
"JOIN a.nodeSlugs as sl " +
"LEFT JOIN FETCH a.tags " +
"WHERE NOT ns.deleted " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface AttachmentRepository extends JpaRepository<Attachment, Integer>
"join a.attachmentType at " +
"join at.attachmentGroup ag " +
"where a.nodeId = :nodeId and ag.code = :code " +
"order by a.weight")
"order by a.weight, a.id")
List<Attachment> findByGroup(@Param("nodeId") Integer nodeId, @Param("code") String code);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.holarse.backend.db.repositories;

import de.holarse.backend.db.AttachmentType;
import org.springframework.data.jpa.repository.JpaRepository;

/**
*
* @author comrad
*/
public interface AttachmentTypeRepository extends JpaRepository<AttachmentType, Integer> {

AttachmentType findByCode(String code);

}
6 changes: 3 additions & 3 deletions src/main/java/de/holarse/backend/view/ArticleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ArticleView {
// Tags as views for display
private List<TagView> tagList = new ArrayList<>();

private WebsiteLinkView websiteLinks = new WebsiteLinkView();
private List<AttachmentView> websiteLinks = new ArrayList<>();

public static ArticleView of(final ArticleRevision ar) {
final ArticleView av = new ArticleView();
Expand Down Expand Up @@ -159,11 +159,11 @@ public void setSlug(String slug) {
this.slug = slug;
}

public WebsiteLinkView getWebsiteLinks() {
public List<AttachmentView> getWebsiteLinks() {
return websiteLinks;
}

public void setWebsiteLinks(WebsiteLinkView websiteLinks) {
public void setWebsiteLinks(List<AttachmentView> websiteLinks) {
this.websiteLinks = websiteLinks;
}

Expand Down
65 changes: 65 additions & 0 deletions src/main/java/de/holarse/backend/view/AttachmentView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package de.holarse.backend.view;

import de.holarse.backend.db.Attachment;

public class AttachmentView {

private Integer id;
private String description;
private String data;
private Integer weight;
private boolean markAsDeleted;

public static AttachmentView of(final Attachment attachment) {
final AttachmentView view = new AttachmentView();
view.setId(attachment.getId());
view.setData(attachment.getData());
view.setDescription(attachment.getDescription());
view.setWeight(attachment.getWeight());

return view;
}

public Integer getWeight() {
return weight;
}

public void setWeight(Integer weight) {
this.weight = weight;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}

public boolean isMarkAsDeleted() {
return markAsDeleted;
}

public void setMarkAsDeleted(boolean markAsDeleted) {
this.markAsDeleted = markAsDeleted;
}



}
40 changes: 0 additions & 40 deletions src/main/java/de/holarse/backend/view/WebsiteLinkView.java

This file was deleted.

64 changes: 54 additions & 10 deletions src/main/java/de/holarse/web/controller/WikiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import de.holarse.backend.db.repositories.ArticleRepository;
import de.holarse.backend.db.repositories.ArticleRevisionRepository;
import de.holarse.backend.db.repositories.AttachmentRepository;
import de.holarse.backend.db.repositories.AttachmentTypeRepository;
import de.holarse.backend.db.repositories.NodeSlugRepository;
import de.holarse.backend.db.repositories.TagRepository;
import de.holarse.backend.view.ArticleView;
import de.holarse.backend.view.AttachmentView;
import de.holarse.backend.view.SettingsView;
import de.holarse.backend.view.TagView;
import static de.holarse.config.JmsQueueTypes.QUEUE_SEARCH;
Expand All @@ -24,7 +26,10 @@
import de.holarse.web.services.TagService;
import jakarta.persistence.EntityNotFoundException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -63,6 +68,9 @@ public class WikiController {
@Autowired
private AttachmentRepository attachmentRepository;

@Autowired
private AttachmentTypeRepository attachmentTypeRepository;

@Autowired
private SlugService slugService;

Expand Down Expand Up @@ -115,7 +123,7 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd
// }
// }

final List<Attachment> websiteLinks = attachmentRepository.findByGroup(article.getNodeId(), "website").stream().limit(3).toList();
final List<Attachment> websiteLinks = attachmentRepository.findByGroup(article.getNodeId(), "website");

// View zusammenstellen
final ArticleView view = ArticleView.of(articleRevision);
Expand All @@ -124,10 +132,8 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd
view.setTagList(tags.stream().map(TagView::of).toList()); // TODO Sort by weight
view.setContent(renderer.render(view.getContent(), null));
//view.setSlug(mainSlug.getName());
view.getWebsiteLinks().setLink1(websiteLinks.size() >= 1 ? websiteLinks.get(0) : null);
view.getWebsiteLinks().setLink1(websiteLinks.size() >= 2 ? websiteLinks.get(1) : null);
view.getWebsiteLinks().setLink1(websiteLinks.size() >= 3 ? websiteLinks.get(2) : null);
//
view.setWebsiteLinks(websiteLinks.stream().map(AttachmentView::of).toList());

mv.addObject("view", view);

return mv;
Expand Down Expand Up @@ -155,11 +161,9 @@ public ModelAndView edit(@PathVariable("nodeId") final Integer nodeId, final Mod
form.setTitle7(articleRevision.getTitle7());
form.setContent(articleRevision.getContent());

final List<Attachment> websiteLinks = attachmentRepository.findByGroup(article.getNodeId(), "website").stream().limit(3).toList();
final List<Attachment> websiteLinks = attachmentRepository.findByGroup(article.getNodeId(), "website");
logger.debug("Links: {}", websiteLinks);
form.getWebsiteLinks().setLink1(websiteLinks.size() >= 1 ? websiteLinks.get(0) : new Attachment());
form.getWebsiteLinks().setLink2(websiteLinks.size() >= 2 ? websiteLinks.get(1) : new Attachment());
form.getWebsiteLinks().setLink3(websiteLinks.size() >= 3 ? websiteLinks.get(2) : new Attachment());
form.setWebsiteLinks(websiteLinks.stream().map(AttachmentView::of).toList());

form.setTags(tags.stream().map(t -> t.getName()).collect(Collectors.joining(", ")));
form.setSettings(SettingsView.of(article.getNodeStatus()));
Expand Down Expand Up @@ -194,9 +198,31 @@ public ModelAndView update(@PathVariable("nodeId") final int nodeId, @ModelAttri

// TODO Bilder, Anhänge

//
// Website Links
//
final Map<Boolean, List<AttachmentView>> websiteLinksMap = form.getWebsiteLinks().stream().collect(Collectors.partitioningBy(av -> av.isMarkAsDeleted()));
// Die zu Löschenden verarbeiten
attachmentRepository.deleteAllById(websiteLinksMap.get(Boolean.TRUE).stream().map(av -> av.getId()).toList());

final List<Attachment> createdAndUpdatedAttachments = new ArrayList<>();
// Die neuen Entities umwandeln und speichern
createdAndUpdatedAttachments.addAll(websiteLinksMap.get(Boolean.FALSE).stream()
.filter(av -> av.getId() == null)
.map(av -> Attachment.build(av, nodeId, attachmentTypeRepository.findByCode("link")))
.toList());
// Die bestehenden finden und updaten

for (final AttachmentView av : websiteLinksMap.get(Boolean.FALSE).stream().filter(av -> av.getId() != null).toList()) {
final Attachment att = attachmentRepository.findById(av.getId()).orElseThrow(EntityNotFoundException::new);
att.setWeight(av.getWeight());
att.setData(av.getData());
att.setDescription(av.getDescription());

createdAndUpdatedAttachments.add(att);
}
attachmentRepository.saveAllAndFlush(createdAndUpdatedAttachments);

// Status
final NodeStatus nodeStatus = article.getNodeStatus();
// TODO Nur vom Moderator zusetzen!
Expand All @@ -216,6 +242,24 @@ public ModelAndView update(@PathVariable("nodeId") final int nodeId, @ModelAttri

final NodeSlug nodeSlug = nodeSlugRepository.findMainSlug(nodeId).orElseThrow(EntityNotFoundException::new);
return new ModelAndView(String.format("redirect:{}", nodeSlug.getName()));
}
}

protected Attachment createOrUpdate(final Integer nodeId, final Attachment formAttachment) {
Attachment dbAttachment;
if (formAttachment.getId() == null) {
// Create
dbAttachment = new Attachment();
dbAttachment.setNodeId(nodeId);

} else {
dbAttachment = attachmentRepository.findById(formAttachment.getId()).orElseThrow(IllegalArgumentException::new);
}

dbAttachment.setDescription(formAttachment.getDescription());
dbAttachment.setData(formAttachment.getData());
dbAttachment.setWeight(formAttachment.getWeight());

return dbAttachment;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package de.holarse.web.controller.commands;

import de.holarse.backend.view.AttachmentView;
import de.holarse.backend.view.SettingsView;
import de.holarse.backend.view.WebsiteLinkView;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

public class ArticleForm {

Expand All @@ -24,7 +26,7 @@ public class ArticleForm {
@NotBlank
private String tags;

private WebsiteLinkView websiteLinks = new WebsiteLinkView();
private List<AttachmentView> websiteLinks = new ArrayList<>();

private SettingsView settings = new SettingsView();

Expand Down Expand Up @@ -108,11 +110,11 @@ public void setTags(String tags) {
this.tags = tags;
}

public WebsiteLinkView getWebsiteLinks() {
public List<AttachmentView> getWebsiteLinks() {
return websiteLinks;
}

public void setWebsiteLinks(WebsiteLinkView websiteLinks) {
public void setWebsiteLinks(List<AttachmentView> websiteLinks) {
this.websiteLinks = websiteLinks;
}

Expand Down
Loading

0 comments on commit 7634651

Please sign in to comment.