Skip to content

Commit

Permalink
Links edit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Nov 24, 2023
1 parent 4fe3ece commit 9bd0cdb
Show file tree
Hide file tree
Showing 15 changed files with 373 additions and 123 deletions.
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<spring.security.version>6.1.4</spring.security.version>
<slf4jVersion>2.0.9</slf4jVersion>
<jackson.version>2.15.3</jackson.version>
<thymeleaf.version>3.1.2.RELEASE</thymeleaf.version>

<maven.compiler.release>17</maven.compiler.release>
</properties>
Expand Down Expand Up @@ -144,17 +145,17 @@
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.1.2.RELEASE</version>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring6</artifactId>
<version>3.1.2.RELEASE</version>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
<version>3.1.2.RELEASE</version>
<version>${thymeleaf.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/de/holarse/backend/db/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.annotations.FilterJoinTable;

@Table(name = "articles")
@Entity
Expand Down Expand Up @@ -44,6 +47,10 @@ 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 @@ -92,4 +99,12 @@ public void setNodeSlugs(Set<NodeSlug> nodeSlugs) {
this.nodeSlugs = nodeSlugs;
}

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

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

}
15 changes: 10 additions & 5 deletions src/main/java/de/holarse/backend/db/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Attachment extends TimestampedBase {

@ManyToOne
@JoinColumn(name="attachment_type_id", nullable=false, referencedColumnName = "id")
private AttachmentGroup group;
private AttachmentType attachmentType;

@Column(name = "attachment_data", length = 4096)
private String data;
Expand Down Expand Up @@ -56,12 +56,12 @@ public void setDescription(String description) {
this.description = description;
}

public AttachmentGroup getGroup() {
return group;
public AttachmentType getAttachmentType() {
return attachmentType;
}

public void setGroup(AttachmentGroup group) {
this.group = group;
public void setAttachmentType(AttachmentType attachmentType) {
this.attachmentType = attachmentType;
}

public String getData() {
Expand All @@ -71,5 +71,10 @@ public String getData() {
public void setData(String data) {
this.data = data;
}

@Override
public String toString() {
return "Attachment{" + "nodeId=" + nodeId + ", weight=" + weight + ", description=" + description + ", attachmentType=" + attachmentType + ", data=" + data + '}';
}

}
5 changes: 5 additions & 0 deletions src/main/java/de/holarse/backend/db/AttachmentGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public String getLabel() {
public void setLabel(String label) {
this.label = label;
}

@Override
public String toString() {
return "AttachmentGroup{" + "code=" + code + ", label=" + label + '}';
}

}
5 changes: 5 additions & 0 deletions src/main/java/de/holarse/backend/db/AttachmentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@ public AttachmentDataType getDataType() {
public void setDataType(AttachmentDataType dataType) {
this.dataType = dataType;
}

@Override
public String toString() {
return "AttachmentType{" + "code=" + code + ", label=" + label + ", attachmentGroup=" + attachmentGroup + ", dataType=" + dataType + '}';
}

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

import de.holarse.backend.db.Attachment;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AttachmentRepository extends JpaRepository<Attachment, Integer> {

@Query("from Attachment a " +
"join a.attachmentType at " +
"join at.attachmentGroup ag " +
"where a.nodeId = :nodeId and ag.code = :code " +
"order by a.weight")
List<Attachment> findByGroup(@Param("nodeId") Integer nodeId, @Param("code") String code);

}
11 changes: 11 additions & 0 deletions src/main/java/de/holarse/backend/view/ArticleView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.holarse.backend.view;

import de.holarse.backend.db.ArticleRevision;
import de.holarse.backend.db.Attachment;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -26,6 +27,8 @@ public class ArticleView {
private String tags;
// Tags as views for display
private List<TagView> tagList = new ArrayList<>();

private WebsiteLinkView websiteLinks = new WebsiteLinkView();

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

public WebsiteLinkView getWebsiteLinks() {
return websiteLinks;
}

public void setWebsiteLinks(WebsiteLinkView websiteLinks) {
this.websiteLinks = websiteLinks;
}

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

import de.holarse.backend.db.NodeStatus;

public class SettingsView {

private boolean archived;
private boolean commentable;
private boolean published;
private boolean deleted;

public static SettingsView of(final NodeStatus nodeStatus) {
final SettingsView view = new SettingsView();
view.setArchived(nodeStatus.isArchived());
view.setCommentable(nodeStatus.isCommentable());
view.setPublished(nodeStatus.isPublished());
view.setDeleted(nodeStatus.isDeleted());
return view;
}

public boolean isPublished() {
return published;
}

public void setPublished(boolean published) {
this.published = published;
}

public boolean isArchived() {
return archived;
}

public void setArchived(boolean archived) {
this.archived = archived;
}

public boolean isCommentable() {
return commentable;
}

public void setCommentable(boolean commentable) {
this.commentable = commentable;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

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

import de.holarse.backend.db.Attachment;

public class WebsiteLinkView {

private Attachment link1;
private Attachment link2;
private Attachment link3;

public Attachment getLink1() {
return link1;
}

public void setLink1(Attachment link1) {
this.link1 = link1;
}

public Attachment getLink2() {
return link2;
}

public void setLink2(Attachment link2) {
this.link2 = link2;
}

public Attachment getLink3() {
return link3;
}

public void setLink3(Attachment link3) {
this.link3 = link3;
}

@Override
public String toString() {
return "WebsiteLinkView{" + "link1=" + link1 + ", link2=" + link2 + ", link3=" + link3 + '}';
}

}
35 changes: 30 additions & 5 deletions src/main/java/de/holarse/web/controller/WikiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import de.holarse.backend.db.Article;
import de.holarse.backend.db.ArticleRevision;
import de.holarse.backend.db.Attachment;
import de.holarse.backend.db.NodeSlug;
import de.holarse.backend.db.NodeStatus;
import de.holarse.backend.db.Tag;
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.NodeSlugRepository;
import de.holarse.backend.db.repositories.TagRepository;
import de.holarse.backend.view.ArticleView;
import de.holarse.backend.view.SettingsView;
import de.holarse.backend.view.TagView;
import de.holarse.config.JmsQueueTypes;
import static de.holarse.config.JmsQueueTypes.QUEUE_SEARCH;
import de.holarse.queues.commands.SearchRefresh;
import de.holarse.web.controller.commands.ArticleForm;
Expand All @@ -22,6 +24,7 @@
import de.holarse.web.services.TagService;
import jakarta.persistence.EntityNotFoundException;
import java.security.Principal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,6 +59,9 @@ public class WikiController {

@Autowired
private TagRepository tagRepository;

@Autowired
private AttachmentRepository attachmentRepository;

@Autowired
private SlugService slugService;
Expand Down Expand Up @@ -109,13 +115,18 @@ public ModelAndView show(@PathVariable("slug") final String slug, final ModelAnd
// }
// }

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

// View zusammenstellen
final ArticleView view = ArticleView.of(articleRevision);
view.setNodeId(article.getNodeId());
view.setTags(tags.stream().map(t -> t.getName()).collect(Collectors.joining(", ")));
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);
//
mv.addObject("view", view);

Expand Down Expand Up @@ -144,8 +155,17 @@ 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();
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.setTags(tags.stream().map(t -> t.getName()).collect(Collectors.joining(", ")));
form.setSettings(SettingsView.of(article.getNodeStatus()));

logger.debug("WebsiteLinks: {}", form.getWebsiteLinks());

return mv.addObject("form", form);
}

Expand Down Expand Up @@ -174,9 +194,16 @@ public ModelAndView update(@PathVariable("nodeId") final int nodeId, @ModelAttri

// TODO Bilder, Anhänge

// Website Links


// Status
final NodeStatus nodeStatus = article.getNodeStatus();
nodeStatus.setPublished(form.isPublished());
// TODO Nur vom Moderator zusetzen!
nodeStatus.setPublished(form.getSettings().isPublished());
nodeStatus.setArchived(form.getSettings().isArchived());
nodeStatus.setCommentable(form.getSettings().isCommentable());
nodeStatus.setDeleted(form.getSettings().isDeleted());

// Artikel auf neue Revision setzen
article.setArticleRevision(articleRevision);
Expand All @@ -185,9 +212,7 @@ public ModelAndView update(@PathVariable("nodeId") final int nodeId, @ModelAttri
articleRepository.saveAndFlush(article);

// Suche aktualisieren
if (nodeStatus.isPublished()) {
jmsTemplate.convertAndSend(QUEUE_SEARCH, new SearchRefresh());
}
jmsTemplate.convertAndSend(QUEUE_SEARCH, new SearchRefresh());

final NodeSlug nodeSlug = nodeSlugRepository.findMainSlug(nodeId).orElseThrow(EntityNotFoundException::new);
return new ModelAndView(String.format("redirect:{}", nodeSlug.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ModelAndView addArticle(final ModelAndView mv) {
mv.addObject(WebDefines.DEFAULT_VIEW_ATTRIBUTE_NAME, "sites/workspace/article");

final ArticleForm articleForm = new ArticleForm();
articleForm.setPublished(true);
articleForm.getSettings().setPublished(true);
mv.addObject("form", articleForm);

return mv;
Expand Down Expand Up @@ -116,7 +116,7 @@ public ModelAndView saveArticle(@Valid @ModelAttribute("form") final ArticleForm
// Status anlegen
final NodeStatus nodeStatus = new NodeStatus();
nodeStatus.setNodeId(nodeId);
nodeStatus.setPublished(form.isPublished());
nodeStatus.setPublished(form.getSettings().isPublished());

// Slug anlegen
final NodeSlug nodeSlug = slugService.slugify(articleRevision);
Expand Down
Loading

0 comments on commit 9bd0cdb

Please sign in to comment.