Skip to content

Commit

Permalink
Attachment type yes_or_no, Grouping attachments after single query load
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Feb 23, 2024
1 parent 187f5ee commit 966381e
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 13 deletions.
16 changes: 16 additions & 0 deletions doc/db2/01_schema/16-migration-20240302-0728.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
begin transaction;

-- set migration version
update migration_version set version='20240222-2250';

-- Interne Downloads und Interne Tool-Downloads per Attachment
alter type attachment_data_type add value 'yes_or_no';
insert into attachment_groups (code, label) values ('internal', 'Internes');

insert into attachment_types (code, label, attachment_group_id, datatype) values
('internalfs', 'Fileserver', (select id from attachment_groups where code = 'internal'), 'yes_or_no');

insert into attachment_types (code, label, attachment_group_id, datatype) values
('internalfs-tools', 'Fileserver Tools', (select id from attachment_groups where code = 'internal'), 'yes_or_no');

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

import de.holarse.backend.db.AttachmentGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository

public interface AttachmentGroupRepository extends JpaRepository<AttachmentGroup, Integer> {

AttachmentGroup findByCode(final String code);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import de.holarse.backend.db.Attachment;
import java.util.List;
import java.util.stream.Stream;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository

public interface AttachmentRepository extends JpaRepository<Attachment, Integer> {

Expand All @@ -14,5 +19,10 @@ public interface AttachmentRepository extends JpaRepository<Attachment, Integer>
"where a.nodeId = :nodeId and ag.code = :code " +
"order by a.weight, a.id")
List<Attachment> findByGroup(@Param("nodeId") Integer nodeId, @Param("code") String code);


@Query("from Attachment a " +
"join fetch a.attachmentType at " +
"join fetch at.attachmentGroup ag " +
"where a.nodeId = :nodeId")
List<Attachment> findByNode(@Param("nodeId") Integer nodeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

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

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

AttachmentType findByCode(String code);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package de.holarse.backend.types;

public enum AttachmentDataType {

/* uri */
url,
storage
/* file path */
storage,
/* yes or no */
yes_or_no

}
26 changes: 16 additions & 10 deletions src/main/java/de/holarse/web/controller/WikiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
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.AttachmentTypeRepository;
import de.holarse.backend.db.repositories.NodeSlugRepository;
import de.holarse.backend.db.repositories.TagRepository;
import de.holarse.backend.db.repositories.*;
import de.holarse.backend.types.AttachmentDataType;
import de.holarse.backend.types.NodeType;
import de.holarse.backend.view.ArticleView;
import de.holarse.backend.view.AttachmentView;
Expand All @@ -23,6 +19,7 @@
import de.holarse.web.defines.WebDefines;
import static de.holarse.web.defines.WebDefines.WIKI_ARTICLES_DEFAULT_PAGE_SIZE;
import de.holarse.web.renderer.Renderer;
import de.holarse.web.services.AttachmentService;
import de.holarse.web.services.SlugService;
import de.holarse.web.services.TagService;
import jakarta.persistence.EntityNotFoundException;
Expand All @@ -32,6 +29,8 @@
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -71,13 +70,19 @@ public class WikiController {

@Autowired
private AttachmentTypeRepository attachmentTypeRepository;

@Autowired
private AttachmentGroupRepository attachmentGroupRepository;

@Autowired
private SlugService slugService;

@Autowired
private TagService tagService;


@Autowired
private AttachmentService attachmentService;

@Autowired
private Renderer renderer;

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

final List<Attachment> websiteLinks = attachmentRepository.findByGroup(article.getNodeId(), "website");
final List<Attachment> websiteLinks = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode("website"));

// View zusammenstellen
final ArticleView view = ArticleView.of(articleRevision);
Expand Down Expand Up @@ -161,8 +166,9 @@ public ModelAndView edit(@PathVariable("nodeId") final Integer nodeId, final Mod
form.setTitle6(articleRevision.getTitle6());
form.setTitle7(articleRevision.getTitle7());
form.setContent(articleRevision.getContent());

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

// Attachments
final List<Attachment> websiteLinks = attachmentService.getAttachments(article, attachmentGroupRepository.findByCode("website"));
logger.debug("Links: {}", websiteLinks);
form.setWebsiteLinks(websiteLinks.stream().map(AttachmentView::of).toList());

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/de/holarse/web/services/AttachmentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.holarse.web.services;

import de.holarse.backend.db.Article;
import de.holarse.backend.db.Attachment;
import de.holarse.backend.db.AttachmentGroup;
import de.holarse.backend.db.AttachmentType;
import de.holarse.backend.db.repositories.AttachmentRepository;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequestScope
public class AttachmentService {

@Autowired
private AttachmentRepository attachmentRepository;
private Optional<Map<String, List<Attachment>>> nodeAttachmentGroups = Optional.empty();

protected Map<String, List<Attachment>> splitAttachments(final List<Attachment> attachments) {
// TODO Sorting by weight
return attachments.stream().collect(Collectors.groupingBy(a -> a.getAttachmentType().getAttachmentGroup().getCode()));
}

public List<Attachment> getAttachments(final Article article, final AttachmentGroup attachmentGroup) {
if (article == null) {
throw new IllegalArgumentException("article is null");
}
if (attachmentGroup == null) {
throw new IllegalArgumentException("attachmentGroup is null");
}

if (nodeAttachmentGroups.isEmpty()) {
// Daten laden und aufsplitten
nodeAttachmentGroups = Optional.of(splitAttachments(attachmentRepository.findByNode(article.getNodeId())));
}

return nodeAttachmentGroups.get().get(attachmentGroup.getCode());
}
}

0 comments on commit 966381e

Please sign in to comment.