Skip to content
Snippets Groups Projects
Commit a7a648c2 authored by Goik Martin's avatar Goik Martin
Browse files

Handling no-title qandasets + better mongodb read performance

parent 7c8a018c
Branches
No related tags found
No related merge requests found
# Connect to DB:
```
mongo -u nodebb -p secret --authenticationDatabase=nodebb
```
# Useful queries:
```
use nodebb
show collections
db.objects.find({hdmId: { $exists: true }})
db.objects.find({hdmId: "goiklecture:sd1QandaExtendInterest" })
```
\ No newline at end of file
......@@ -57,13 +57,13 @@
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.11.2</version>
</dependency>
<dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.6</version>
......@@ -109,6 +109,7 @@
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hdm_stuttgart.mi.bb.Id2Topic</Main-Class>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</transformer>
</transformers>
......
......@@ -12,9 +12,13 @@ import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import javax.ws.rs.client.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
......@@ -42,19 +46,19 @@ public class Id2Topic {
final File file = new File(configPathName);
ResourceBundle rb = null;
ResourceBundle tmpConfig = null;
try {
final URL[] urls = new URL[]{file.toURI().toURL()};
final ClassLoader loader = new URLClassLoader(urls);
rb = ResourceBundle.getBundle(configFileBaseName, Locale.getDefault(), loader);
tmpConfig = ResourceBundle.getBundle(configFileBaseName, Locale.getDefault(), loader);
} catch (final MalformedURLException | MissingResourceException e) {
log.fatal("Unable to open URI " + configFileFullyQualifiedName, e);
System.exit(1);
}
config = rb;
config = tmpConfig;
final Client clientTmp = client = ClientBuilder.newClient(new ClientConfig().register(new JacksonFeature()));
topicsTarget = clientTmp.target(rb.getString("nodebbWriteApiTopicsEndpoint"));
final Client client = ClientBuilder.newClient(new ClientConfig().register(new JacksonFeature()));
topicsTarget = client.target(tmpConfig.getString("nodebbWriteApiTopicsEndpoint"));
}
static private final ResourceBundle config;
......@@ -64,8 +68,6 @@ public class Id2Topic {
config.getString("mongoUri"),
config.getString("nodebbDb"));
static private final Client client;
static private final String bearerToken = config.getString("nodebbBearerToken");
static private final String
......
......@@ -3,24 +3,44 @@ package de.hdm_stuttgart.mi.bb;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.exists;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Updates.combine;
import static com.mongodb.client.model.Updates.set;
import com.mongodb.client.model.Projections;
import org.bson.Document;
import org.bson.conversions.Bson;
import java.util.HashMap;
import java.util.Map;
class ModifyNodebbTopic {
static public final String hdmIdAttrib = "hdmId", tidAttrib = "tid";
static private Bson hdmId_tid_attrib = Projections.fields(Projections.include(hdmIdAttrib, tidAttrib));
ModifyNodebbTopic(final String uri, final String databaseName) {
final MongoDatabase database = new MongoClient(new MongoClientURI(uri)).getDatabase(databaseName);
objectCollection = database.getCollection("objects");
try (final MongoCursor<Document> topics = objectCollection
.find( exists(hdmIdAttrib))
.projection(hdmId_tid_attrib) .iterator()) {
while (topics.hasNext()) {
final Document topic = topics.next();
existingHdmIds.put(topic.get(hdmIdAttrib), (Integer) topic.get(tidAttrib));
}
}
}
private final MongoCollection<Document> objectCollection;
private final Map<Object, Integer> existingHdmIds = new HashMap<>();
/**
* Check for the existence of the hdmId attribute. If present, return
* the topic's tid value, else return a negative value.
......@@ -28,14 +48,16 @@ class ModifyNodebbTopic {
* @param hdmId The hdmId value to be searched for.
* @return If a topic carrying the hdmId value exists, return its tid, else return a negative value.
*/
int hdmIdExists(final String hdmId) {
int hdmIdExists(final String hdmId) {
return existingHdmIds.get(hdmId);
/*
final Document res = objectCollection.find(eq("hdmId", hdmId)).first();
if (null == res) {
return -1;
} else {
return res.getInteger("tid");
return res.getInteger(tidAttrib);
}
*/
}
/**
......@@ -50,12 +72,12 @@ class ModifyNodebbTopic {
if (null != topic) {
final String currentHdmId = topic.getString("hdmId");
final String currentHdmId = topic.getString(hdmIdAttrib);
if (null == currentHdmId) {
objectCollection.updateOne(
eq("_key", searchKey),
combine(set("hdmId", hdmId)));
combine(set(hdmIdAttrib, hdmId)));
System.out.println("Setting new hdmId value '" + hdmId + "'");
} else if (currentHdmId.equals(hdmId)) {
......@@ -65,4 +87,4 @@ class ModifyNodebbTopic {
}
}
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://docbook.org/ns/docbook" xmlns:db="http://docbook.org/ns/docbook">
xmlns="http://docbook.org/ns/docbook" xmlns:d="http://docbook.org/ns/docbook">
<xsl:import href="../../docbook-xsl/xhtml/chunk.xsl"/>
<xsl:include href="common.xsl"/>
<xsl:include href="slideReference.xsl"/>
<xsl:output indent="no"/>
<xsl:variable name = "figurelinkPrefix" select="'__figurelink_'"/>
<xsl:variable name="topicLookup" select="document('topics.tid.xml', .)"/>
<xsl:template match="@* | node()">
......@@ -17,7 +18,7 @@
</xsl:copy>
</xsl:template>
<xsl:template match="db:chapter/db:title | db:section/db:title | db:appendix/db:title">
<xsl:template match="d:chapter/d:title | d:section/d:title | d:appendix/d:title">
<xsl:copy-of select="."/>
<xsl:for-each select="parent::*[contains(@annotations, 'slide') or descendant::*[contains(@annotations, 'slide')]]">
<para>
......@@ -27,15 +28,15 @@
</xsl:template>
<xsl:template match="db:figure[not(ancestor::db:qandaset)]/db:title">
<xsl:template match="d:figure[not(ancestor::d:qandaset)]/d:title">
<xsl:variable name="slideContainerId" select="ancestor::*[contains(@annotations, 'slide')]/@xml:id"/>
<title>
<xsl:copy-of select="text()|*"/>
<xsl:variable name="htmlPresentationForumFileBasename">
<xsl:text>__figurelink_</xsl:text>
<xsl:value-of select="parent::db:figure/@xml:id"/>
<xsl:value-of select="$figurelinkPrefix"/>
<xsl:value-of select="parent::d:figure/@xml:id"/>
<xsl:text>.html</xsl:text>
</xsl:variable>
......@@ -65,7 +66,7 @@
<xsl:attribute name="href">
<xsl:call-template name="slideReference">
<xsl:with-param name="slideContainerId" select="$slideContainerId"/>
<xsl:with-param name="slideObjectId" select="parent::db:figure/@xml:id"/>
<xsl:with-param name="slideObjectId" select="parent::d:figure/@xml:id"/>
</xsl:call-template>
</xsl:attribute>
<img title="Slide presentation" alt="Slide presentation" style="vertical-align: middle;"
......@@ -76,7 +77,7 @@
<!-- Link to forum -->
<xsl:call-template name="bbComment">
<xsl:with-param name="elementId" select="parent::db:figure/@xml:id"/>
<xsl:with-param name="elementId" select="parent::d:figure/@xml:id"/>
</xsl:call-template>
</span>
</xsl:result-document>
......@@ -90,14 +91,14 @@
</xsl:processing-instruction>
</title>
<xsl:if test="not($slideContainerId) and ends-with(parent::db:figure/db:mediaobject/db:imageobject/db:imagedata/@fileref, '.fig')">
<xsl:if test="not($slideContainerId) and ends-with(parent::d:figure/d:mediaobject/d:imageobject/d:imagedata/@fileref, '.fig')">
<para>
<uri>
<xsl:attribute name="xlink:href">
<xsl:call-template name="hdm.get.leftmost.of.separator">
<xsl:with-param name="path">
<xsl:call-template name="relative-uri">
<xsl:with-param name="filename" select="parent::db:figure/db:mediaobject/db:imageobject/db:imagedata/@fileref"/>
<xsl:with-param name="filename" select="parent::d:figure/d:mediaobject/d:imageobject/d:imagedata/@fileref"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="separator" select="'.'"/>
......@@ -109,12 +110,12 @@
</para>
</xsl:if>
<xsl:if test="not($slideContainerId) and ends-with(parent::db:figure/db:mediaobject/db:imageobject/db:imagedata/@fileref, '.svg')">
<xsl:if test="not($slideContainerId) and ends-with(parent::d:figure/d:mediaobject/d:imageobject/d:imagedata/@fileref, '.svg')">
<para>
<uri>
<xsl:attribute name="xlink:href">
<xsl:call-template name="relative-uri">
<xsl:with-param name="filename" select="parent::db:figure/db:mediaobject/db:imageobject/db:imagedata/@fileref"/>
<xsl:with-param name="filename" select="parent::d:figure/d:mediaobject/d:imageobject/d:imagedata/@fileref"/>
</xsl:call-template>
</xsl:attribute>
<xsl:text>Separate SVG image link</xsl:text>
......@@ -123,14 +124,70 @@
</xsl:if>
</xsl:template>
<xsl:template match="db:qandaset/db:title">
<xsl:template match="d:qandaset">
<d:qandaset>
<xsl:copy-of select="@*"/>
<d:title>
<xsl:for-each select="d:title">
<xsl:copy-of select="text()|*"/>
</xsl:for-each>
<xsl:variable name="htmlPresentationForumFileBasename">
<xsl:value-of select="$figurelinkPrefix"/>
<xsl:value-of select="@xml:id"/>
<xsl:text>.html</xsl:text>
</xsl:variable>
<xsl:variable name="htmlPresentationForumFilename">
<xsl:if test="ancestor::*/@xml:base">
<xsl:call-template name="hdm.get.leftmost.of.separator">
<xsl:with-param name="path">
<xsl:value-of select="ancestor::*/@xml:base"/>
</xsl:with-param>
<xsl:with-param name="separator">/</xsl:with-param>
</xsl:call-template>
<xsl:text>/</xsl:text>
</xsl:if>
<xsl:value-of select="$htmlPresentationForumFileBasename"/>
</xsl:variable>
<!--
Hack circumventing docbook XSL bug https://sourceforge.net/p/docbook/bugs/1399
preventing nesting of <inlinemediaobject> descendants inside <figure>/<title>.
-->
<xsl:result-document href="target/profile/{$htmlPresentationForumFilename}" method="html" encoding="utf-8" indent="no"
exclude-result-prefixes="#all" xmlns = "http://www.w3.org/1999/xhtml">
<span>
<!-- Link to forum -->
<xsl:call-template name="bbComment">
<xsl:with-param name="elementId" select="@xml:id"/>
</xsl:call-template>
</span>
</xsl:result-document>
<xsl:text> </xsl:text>
<xsl:processing-instruction name="dbhtml-include">
<xsl:text>href="</xsl:text>
<xsl:value-of select="$htmlPresentationForumFileBasename"/>
<xsl:text>"</xsl:text>
</xsl:processing-instruction>
</d:title>
<xsl:copy-of select="*"/>
</d:qandaset>
</xsl:template>
<!--
<xsl:template match="d:qandaset/d:title">
<title>
<d:title>
<xsl:copy-of select="text()|*"/>
<xsl:variable name="htmlPresentationForumFileBasename">
<xsl:text>__figurelink_</xsl:text>
<xsl:value-of select="parent::db:qandaset/@xml:id"/>
<xsl:value-of select="$figurelinkPrefix"/>
<xsl:value-of select="parent::d:qandaset/@xml:id"/>
<xsl:text>.html</xsl:text>
</xsl:variable>
......@@ -147,17 +204,14 @@
<xsl:value-of select="$htmlPresentationForumFileBasename"/>
</xsl:variable>
<!--
Hack circumventing docbook XSL bug https://sourceforge.net/p/docbook/bugs/1399
preventing nesting of <inlinemediaobject> descendants inside <figure>/<title>.
-->
<xsl:result-document href="target/profile/{$htmlPresentationForumFilename}" method="html" encoding="utf-8" indent="no"
exclude-result-prefixes="#all" xmlns = "http://www.w3.org/1999/xhtml">
<span>
<!-- Link to forum -->
<xsl:call-template name="bbComment">
<xsl:with-param name="elementId" select="parent::db:qandaset/@xml:id"/>
<xsl:with-param name="elementId" select="parent::d:qandaset/@xml:id"/>
</xsl:call-template>
</span>
</xsl:result-document>
......@@ -169,10 +223,11 @@
<xsl:value-of select="$htmlPresentationForumFileBasename"/>
<xsl:text>"</xsl:text>
</xsl:processing-instruction>
</title>
</d:title>
</xsl:template>
-->
<xsl:template match="db:imagedata[ends-with(@fileref, '.fig')]">
<xsl:template match="d:imagedata[ends-with(@fileref, '.fig')]">
<xsl:copy>
<xsl:attribute name="fileref">
<xsl:value-of select="@fileref"/>
......@@ -182,7 +237,7 @@
</xsl:copy>
</xsl:template>
<xsl:template match="db:imagedata[ends-with(@fileref, '.pdf')]">
<xsl:template match="d:imagedata[ends-with(@fileref, '.pdf')]">
<xsl:copy>
<xsl:attribute name="fileref">
<xsl:value-of select="@fileref"/>
......
......@@ -6,7 +6,7 @@
<xsl:include href="common.xsl"/>
<xsl:output indent="no" method="xml"/>
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<map prefix="goiklecture">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment