-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bernd Ritter
committed
Apr 21, 2024
1 parent
39ff175
commit 6983138
Showing
5 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/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<String> 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); | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/de/holarse/queues/consumers/NewsImportWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Context path=""/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|