diff --git a/doc/tomcat/conf/context.xml b/doc/tomcat/conf/context.xml index 472d1db2..1dafdcae 100644 --- a/doc/tomcat/conf/context.xml +++ b/doc/tomcat/conf/context.xml @@ -16,18 +16,17 @@ limitations under the License. --> - + WEB-INF/web.xml WEB-INF/tomcat-web.xml ${catalina.base}/conf/web.xml + WEB-INF/classes/ - diff --git a/src/main/java/de/holarse/api/imports/News.java b/src/main/java/de/holarse/api/imports/News.java new file mode 100644 index 00000000..0b8620ab --- /dev/null +++ b/src/main/java/de/holarse/api/imports/News.java @@ -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/news", "/api/import/news/"}) +public class News { + + private final static transient Logger log = LoggerFactory.getLogger(News.class); + + @Autowired + private JmsTemplate jmsTemplate; + + @Transactional + @PostMapping(consumes = MediaType.APPLICATION_XML_VALUE) + public ResponseEntity upload(@Valid @RequestBody final de.holarse.backend.api.News importItem) throws Exception { + try { + jmsTemplate.convertAndSend(QUEUE_IMPORTS_NEWS, importItem); + } catch (JmsException je) { + throw new RuntimeException("error while jms send", je); + } + return new ResponseEntity<>("", HttpStatus.CREATED); + } + +} \ No newline at end of file diff --git a/src/main/java/de/holarse/queues/consumers/NewsImportWorker.java b/src/main/java/de/holarse/queues/consumers/NewsImportWorker.java new file mode 100644 index 00000000..b842dc4f --- /dev/null +++ b/src/main/java/de/holarse/queues/consumers/NewsImportWorker.java @@ -0,0 +1,88 @@ +package de.holarse.queues.consumers; + +import de.holarse.backend.db.News; +import de.holarse.backend.db.NewsRevision; +import de.holarse.backend.db.NodeSlug; +import de.holarse.backend.db.NodeStatus; +import de.holarse.backend.db.repositories.NewsRepository; +import de.holarse.backend.db.repositories.NewsRevisionRepository; +import static de.holarse.config.JmsQueueTypes.*; +import de.holarse.web.services.SlugService; +import de.holarse.web.services.TagService; +import jakarta.transaction.Transactional; +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; + +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.Optional; + +@Component +public class NewsImportWorker { + + private final static transient Logger log = LoggerFactory.getLogger(NewsImportWorker.class); + + @Autowired + NewsRepository newsRepository; + + @Autowired + NewsRevisionRepository newsRevisionRepository; + + @Autowired + SlugService slugService; + + @Autowired + private TagService tagService; + + @Transactional + @JmsListener(destination = QUEUE_IMPORTS_NEWS) + public void importNews(final de.holarse.backend.api.News queueEntry) { + log.info("Import News '{}'", queueEntry.getTitle()); + + int nodeId = 0; + int revision = 0; + + // Bei Drupal-Import übernehmen wir die Ids aus Drupal + // Die neuen Ids fangen ab 10000 an und können so unterschieden werden. + if (queueEntry.getUid() != null) { + nodeId = queueEntry.getUid().intValue(); + } else { + nodeId = newsRepository.nextNodeId(); + } + if (queueEntry.getVid() != null) { + revision = queueEntry.getVid().intValue(); + } else { + revision = newsRevisionRepository.nextRevision(); + } + + final OffsetDateTime created = OffsetDateTime.ofInstant(queueEntry.getCreated().toInstant(), ZoneId.systemDefault()); + + final NewsRevision newsRevision = new NewsRevision(); + newsRevision.setTitle(queueEntry.getTitle()); + newsRevision.setContent(queueEntry.getContent().getValue()); + newsRevision.setCreated(created); + newsRevision.setNodeId(nodeId); + newsRevision.setRevision(revision); + newsRevisionRepository.saveAndFlush(newsRevision); + + NodeStatus nodeStatus = new NodeStatus(); + nodeStatus.setPublished(true); + nodeStatus.setNodeId(nodeId); + nodeStatus.setCreated(created); + + // Slug anlegen + final NodeSlug nodeSlug = slugService.slugify(newsRevision); + + News news = new News(); + news.setNodeId(nodeId); + news.setNodeStatus(nodeStatus); + news.setNodeRevision(newsRevision); + news.setDrupalId(queueEntry.getUid().intValue()); + news.getNodeSlugz().add(nodeSlug); + newsRepository.saveAndFlush(news); + } +} \ No newline at end of file diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml index fd2fac1f..8452a2fd 100644 --- a/src/main/webapp/META-INF/context.xml +++ b/src/main/webapp/META-INF/context.xml @@ -1,2 +1,3 @@ + diff --git a/tools/scripts/import_news b/tools/scripts/import_news new file mode 100755 index 00000000..f1188242 --- /dev/null +++ b/tools/scripts/import_news @@ -0,0 +1,16 @@ +#!/bin/bash + +APIUSER=dummy +APITOKEN=ADDB0F5E7826C857D7376D1BD9BC33C0C544790A2EAC96144A8AF22B1298C940 +PG_PASSWORD=geheim +ARTICLE_DIR=$1 + +BASE_FILES=$(find $1/news-*.xml -type f | sort) + +PG_PASSWORD=geheim + +for i in $BASE_FILES; do + echo "Importing $i" + curl -u $APIUSER:$APITOKEN -X POST -H "Accept: application/xml" -H "Content-Type: application/xml" http://localhost:8080/holarseweb/api/import/news -d @$i +done +