Skip to content

Commit

Permalink
Grundlegender Import von Artikeln funktioniert, inkl. Tag-Anlage
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernd Ritter committed Apr 19, 2024
1 parent abef757 commit 110e331
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 11 deletions.
2 changes: 1 addition & 1 deletion doc/db2/01_schema/05_tags.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ create table if not exists taggroups(

name varchar(255),
code varchar(255) not null unique,
weight integer,
weight integer default 0,
description varchar(255),

created timestamptz not null default CURRENT_TIMESTAMP,
Expand Down
3 changes: 2 additions & 1 deletion doc/db2/02_data/01_roles_taggroups_forums.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ INSERT INTO taggroups(id, name, code) VALUES
(nextval('hibernate_sequence'), 'Plattform', 'PLATFORM'),
(nextval('hibernate_sequence'), 'Technisch', 'TECHNICAL'),
(nextval('hibernate_sequence'), 'Engine', 'ENGINE'),
(nextval('hibernate_sequence'), 'Paketmanager', 'PACKAGEMANAGER');
(nextval('hibernate_sequence'), 'Paketmanager', 'PACKAGEMANAGER'),
(nextval('hibernate_sequence'), 'Sonstiges', 'OTHER');


-- 3. Forums
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/de/holarse/api/imports/Article.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.holarse.api.imports;

import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static de.holarse.config.JmsQueueTypes.*;
import static de.holarse.config.RoleApiTypes.*;

@Secured({ROLE_API_ADMIN, ROLE_API_IMPORT})
@RestController
@RequestMapping({"/api/import/articles", "/api/import/articles/"})
public class Article {

private final static transient Logger log = LoggerFactory.getLogger(Article.class);

@Autowired
private JmsTemplate jmsTemplate;

@Transactional
@PostMapping(consumes = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<String> upload(@Valid @RequestBody final de.holarse.backend.api.Article importItem) throws Exception {
try {
jmsTemplate.convertAndSend(QUEUE_IMPORTS_ARTICLES, importItem);
} catch (JmsException je) {
throw new RuntimeException("error while jms send", je);
}
return new ResponseEntity<>("", HttpStatus.CREATED);
}

}
4 changes: 2 additions & 2 deletions src/main/java/de/holarse/api/imports/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.holarse.api.imports;

import de.holarse.config.JmsQueueTypes;
import static de.holarse.config.JmsQueueTypes.QUEUE_IMPORTS;
import static de.holarse.config.JmsQueueTypes.*;
import static de.holarse.config.RoleApiTypes.*;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class User {
@PostMapping(consumes = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<String> upload(@RequestBody final de.holarse.backend.api.User importUser) throws Exception {
try {
jmsTemplate.convertAndSend(QUEUE_IMPORTS, importUser);
jmsTemplate.convertAndSend(QUEUE_IMPORTS_USERS, importUser);
} catch (JmsException je) {
throw new RuntimeException("error while jms send", je);
}
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/de/holarse/backend/db/ArticleRevision.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,39 @@ public class ArticleRevision extends RevisionedNode {

@Column(length = 512)
private String title1;
@Column(length = 12, name = "title1_lang")
private String title1Lang;

@Column(length = 512)
private String title2;
@Column(length = 12, name = "title2_lang")
private String title2Lang;

@Column(length = 512)
private String title3;
@Column(length = 12, name = "title3_lang")
private String title3Lang;

@Column(length = 512)
private String title4;
@Column(length = 12, name = "title4_lang")
private String title4Lang;

@Column(length = 512)
private String title5;
@Column(length = 12, name = "title5_lang")
private String title5Lang;

@Column(length = 512)
private String title6;
@Column(length = 12, name = "title6_lang")
private String title6Lang;

@Column(length = 512)
private String title7;
@Column(length = 12, name = "title7_lang")
private String title7Lang;

@Column(length = 16384)
private String content;

Expand Down Expand Up @@ -110,4 +131,61 @@ public Article getArticle() {
public void setArticle(Article article) {
this.article = article;
}

public String getTitle1Lang() {
return title1Lang;
}

public void setTitle1Lang(String title1Lang) {
this.title1Lang = title1Lang;
}

public String getTitle2Lang() {
return title2Lang;
}

public void setTitle2Lang(String title2Lang) {
this.title2Lang = title2Lang;
}

public String getTitle3Lang() {
return title3Lang;
}

public void setTitle3Lang(String title3Lang) {
this.title3Lang = title3Lang;
}

public String getTitle4Lang() {
return title4Lang;
}

public void setTitle4Lang(String title4Lang) {
this.title4Lang = title4Lang;
}

public String getTitle5Lang() {
return title5Lang;
}

public void setTitle5Lang(String title5Lang) {
this.title5Lang = title5Lang;
}

public String getTitle6Lang() {
return title6Lang;
}

public void setTitle6Lang(String title6Lang) {
this.title6Lang = title6Lang;
}

public String getTitle7Lang() {
return title7Lang;
}

public void setTitle7Lang(String title7Lang) {
this.title7Lang = title7Lang;
}

}
1 change: 1 addition & 0 deletions src/main/java/de/holarse/backend/db/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public Tag() {

public Tag(final String name) {
this.name = name;
this.nameLang = "german";
}

@Column(length = 255)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface TagGroupRepository extends JpaRepository<TagGroup, Integer> {
@Query("from TagGroup tg join fetch tg.tags t")
List<TagGroup> findAllTagGroups(final Sort sort);

TagGroup findByCode(final String code);

}
2 changes: 1 addition & 1 deletion src/main/java/de/holarse/config/JmsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ConnectionFactory jmsConnectionFactory() {
connectionFactory.setUserName(amqUsername);
connectionFactory.setPassword(amqPassword);

connectionFactory.setTrustedPackages(Arrays.asList("de.holarse.backend.api.drückblick", "de.holarse.backend.types", "de.holarse.queues.commands"));
connectionFactory.setTrustedPackages(Arrays.asList("de.holarse.backend.api", "de.holarse.backend.types", "de.holarse.queues.commands", "java.util", "java.lang"));

return connectionFactory;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/de/holarse/config/JmsQueueTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

public class JmsQueueTypes {

public final static String QUEUE_DRÜCKBLICK = "drueckblick";
public final static String QUEUE_IMPORTS = "import";
public final static String QUEUE_DRÜCKBLICK = "import/drueckblick";
public final static String QUEUE_IMPORTS_USERS = "import/users";
public final static String QUEUE_IMPORTS_ARTICLES = "import/articles";
public final static String QUEUE_IMPORTS_NEWS = "import/news";
public final static String QUEUE_MAIL = "mail";
public final static String QUEUE_SEARCH = "search";

Expand Down
93 changes: 93 additions & 0 deletions src/main/java/de/holarse/queues/consumers/ArticleImportWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package de.holarse.queues.consumers;

import de.holarse.backend.api.Title;
import de.holarse.backend.db.Article;
import de.holarse.backend.db.ArticleRevision;
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.RoleRepository;
import de.holarse.backend.db.repositories.UserRepository;
import static de.holarse.config.JmsQueueTypes.*;
import de.holarse.web.services.SlugService;
import de.holarse.web.services.TagService;
import jakarta.transaction.Transactional;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleImportWorker {

private final static transient Logger log = LoggerFactory.getLogger(ArticleImportWorker.class);

@Autowired
ArticleRepository articleRepository;

@Autowired
ArticleRevisionRepository articleRevisionRepository;

@Autowired
SlugService slugService;

@Autowired
private TagService tagService;

@Transactional
@JmsListener(destination = QUEUE_IMPORTS_ARTICLES)
public void importArticles(final de.holarse.backend.api.Article queueEntry) {
log.info("Import Article {}", queueEntry.getTitles().stream().map(t -> t.getValue()).collect(Collectors.joining(", ")));

int nodeId = articleRepository.nextNodeId();
int revision = articleRevisionRepository.nextRevision();

final ArticleRevision articleRevision = new ArticleRevision();
int i = 0;
for (final Title title : queueEntry.getTitles()) {
i++;
switch (i) {
case 1 -> { articleRevision.setTitle1(title.getValue()); articleRevision.setTitle1Lang("german"); }
case 2 -> { articleRevision.setTitle2(title.getValue()); articleRevision.setTitle2Lang("german"); }
case 3 -> { articleRevision.setTitle3(title.getValue()); articleRevision.setTitle3Lang("german"); }
case 4 -> { articleRevision.setTitle4(title.getValue()); articleRevision.setTitle4Lang("german"); }
case 5 -> { articleRevision.setTitle5(title.getValue()); articleRevision.setTitle5Lang("german"); }
case 6 -> { articleRevision.setTitle6(title.getValue()); articleRevision.setTitle6Lang("german"); }
case 7 -> { articleRevision.setTitle7(title.getValue()); articleRevision.setTitle7Lang("german"); }
default -> log.warn("Too many titles (>7) in this article, ignoring the rest");
}
}
articleRevision.setContent(queueEntry.getContent().getValue());
articleRevision.setNodeId(nodeId);
articleRevision.setRevision(revision);
articleRevision.setChangelog(queueEntry.getRevision().getChangelog());
articleRevisionRepository.saveAndFlush(articleRevision);

final NodeStatus nodeStatus = new NodeStatus();
nodeStatus.setNodeId(nodeId);
nodeStatus.setPublished(true);

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

final Set<Tag> tags = tagService.extract(queueEntry);

final Article article = new Article();
article.setDrupalId(queueEntry.getUid().intValue());
article.setNodeId(nodeId);
article.setNodeRevision(articleRevision);
article.setNodeStatus(nodeStatus);
article.getNodeSlugs().add(nodeSlug);
article.setTags(tags);
articleRepository.saveAndFlush(article);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Component
public class DrückblickWorker {

private final static transient Logger log = LoggerFactory.getLogger(UserWorker.class);
private final static transient Logger log = LoggerFactory.getLogger(ArticleImportWorker.class);

@Autowired
private DrückblickRepository drückblickRepository;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/holarse/queues/consumers/UserWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import de.holarse.backend.db.repositories.RoleRepository;
import de.holarse.backend.db.repositories.UserRepository;
import de.holarse.backend.types.PasswordType;
import static de.holarse.config.JmsQueueTypes.QUEUE_IMPORTS;
import static de.holarse.config.JmsQueueTypes.*;
import de.holarse.config.RoleUserTypes;
import de.holarse.web.services.SlugService;
import java.time.OffsetDateTime;
Expand All @@ -32,7 +32,7 @@ public class UserWorker {
@Autowired
SlugService slugService;

@JmsListener(destination = QUEUE_IMPORTS)
@JmsListener(destination = QUEUE_IMPORTS_USERS)
public void importUsers(final de.holarse.backend.api.User queueEntry) {
// Gibt es einen solchen User bereits? Dann updaten, sonst anlegen
User user = userRepository.findByLogin(queueEntry.getLogin());
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/de/holarse/web/services/TagService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.holarse.web.services;

import de.holarse.backend.api.Article;
import de.holarse.backend.db.Tag;
import de.holarse.backend.db.repositories.TagGroupRepository;
import de.holarse.backend.db.repositories.TagRepository;
import de.holarse.web.controller.commands.ArticleForm;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -14,6 +18,9 @@ public class TagService {
@Autowired
private TagRepository tagRepository;

@Autowired
private TagGroupRepository tagGroupRepository;

@Autowired
private SlugService slugService;

Expand All @@ -32,4 +39,24 @@ public Set<Tag> extract(final ArticleForm form) {
return articleTags;
}

public Set<Tag> extract(final Article importArticle) {
final Set<Tag> articleTags = new HashSet<>();

// Tags auslesen, in Entities umwandeln und ggf. erzeugen
final List<String> tagNames = importArticle.getTags().stream().map(de.holarse.backend.api.Tag::getValue).collect(Collectors.toList());
for (final String tagName : tagNames) {
final Tag tag = tagRepository.findByName(tagName.trim()).orElseGet(() -> new Tag(tagName.trim()));
// Bei Neuanlage müssen wir noch die Taggruppe setzen
if (tag.getId() == null) {
tag.setTagGroup(tagGroupRepository.findByCode("OTHER"));
}
articleTags.add(tag);
}
// Alle Tags mit Slugs versehen, bestehende werden nicht angefasst
articleTags.stream().forEach(t -> slugService.slugify(t));

return articleTags;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2 class="h5 text-uppercase g-color-gray-dark-v1">Filter</h2>
<!-- Tags -->
<h2 class="h5 text-uppercase g-color-gray-dark-v1">Aktivierte Tags</h2>
<hr class="g-brd-gray-light-v4 g-my-15">
<span th:replace="~{sites/search/tags :: tagcloud(tags=${selectedTags})}"></span>
<span th:replace="~{sites/shared/tags/list :: tagcloud(tags=${selectedTags})}"></span>
<!-- End Tags -->

<!-- Categories -->
Expand Down

0 comments on commit 110e331

Please sign in to comment.