Skip to content

Commit 62c03d5

Browse files
authored
Merge pull request #25 from PatternAtlas/feature/adapt-to-rewriting-renderer
Feature/adapt to rewriting renderer
2 parents b57e14f + bf71284 commit 62c03d5

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

src/main/java/io/github/patternatlas/api/service/PatternRenderServiceImpl.java

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.github.patternatlas.api.service;
22

3-
import java.net.URI;
3+
import java.net.URLEncoder;
44
import java.time.Instant;
55
import java.util.ArrayList;
66
import java.util.List;
77
import java.util.Map;
88
import java.util.UUID;
99

10-
import org.apache.commons.text.similarity.JaccardSimilarity;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1112
import org.springframework.beans.factory.annotation.Autowired;
12-
import org.springframework.http.HttpEntity;
13+
import org.springframework.beans.factory.annotation.Value;
1314
import org.springframework.http.HttpHeaders;
1415
import org.springframework.http.MediaType;
1516
import org.springframework.http.ResponseEntity;
@@ -21,9 +22,21 @@
2122
import io.github.patternatlas.api.entities.Image;
2223
import io.github.patternatlas.api.entities.Pattern;
2324
import io.github.patternatlas.api.rest.model.LatexContent;
25+
import lombok.extern.slf4j.Slf4j;
2426

27+
@Slf4j
2528
@Component
2629
public class PatternRenderServiceImpl implements PatternRenderService {
30+
private final String baseAPIEndpoint;
31+
Logger logger = LoggerFactory.getLogger(PatternRenderServiceImpl.class);
32+
33+
public PatternRenderServiceImpl(
34+
@Value("${io.github.patternatlas.api.latexrenderer.hostname}") String hostname,
35+
@Value("${io.github.patternatlas.api.latexrenderer.port}") int port
36+
) {
37+
this.baseAPIEndpoint = String.format("http://%s:%d/renderLatex/", hostname, port);
38+
}
39+
2740
@Autowired
2841
private ImageService imageService;
2942

@@ -88,8 +101,6 @@ public Object renderContent(Pattern pattern, Pattern oldVersion) {
88101
}
89102

90103
int countQuantikz = 0;
91-
//JaccardSimilarity is ued to check if the Quantikz Occurance is similar to the previous one. If that is the case all comments of the old graphic get copied to the new one
92-
JaccardSimilarity jaccardSimilarity = new JaccardSimilarity();
93104
while (true) {
94105
Integer[] occuranceStartEnd = getNextOccurance(jsonString, "\\\\begin{quantikz}", "\\end{quantikz}");
95106
if (occuranceStartEnd[0] != -1 && occuranceStartEnd[1] != -1) {
@@ -113,9 +124,7 @@ public Object renderContent(Pattern pattern, Pattern oldVersion) {
113124
String id = saveAndUploadFile(renderedFile, "svg");
114125
jsonString = jsonString.replace(jsonString.substring(occuranceStartEnd[0], occuranceStartEnd[1] + 14), " " + id + " ");
115126
if (countQuantikz < oldContentOccurances.size()) {
116-
if (jaccardSimilarity.apply(oldContentOccurances.get(countQuantikz), renderContent) > 0.8) {
117-
this.discussionService.updateTopicsByImageId(UUID.fromString(oldSVGOccurances.get(countQuantikz).substring(5, oldSVGOccurances.get(countQuantikz).length() - 6)), UUID.fromString(id.substring(5, id.length() - 6)));
118-
}
127+
this.discussionService.updateTopicsByImageId(UUID.fromString(oldSVGOccurances.get(countQuantikz).substring(5, oldSVGOccurances.get(countQuantikz).length() - 6)), UUID.fromString(id.substring(5, id.length() - 6)));
119128
}
120129
}
121130
countQuantikz++;
@@ -151,9 +160,7 @@ public Object renderContent(Pattern pattern, Pattern oldVersion) {
151160
String id = saveAndUploadFile(renderedFile, "svg");
152161
jsonString = jsonString.replace(jsonString.substring(occuranceStartEnd[0], occuranceStartEnd[1] + 4), " " + id + " ");
153162
if (countQcircuit < oldContentOccurances.size()) {
154-
if (jaccardSimilarity.apply(oldContentOccurances.get(countQcircuit), renderContent) > 0.8) {
155-
this.discussionService.updateTopicsByImageId(UUID.fromString(oldSVGOccurances.get(countQcircuit).substring(5, oldSVGOccurances.get(countQcircuit).length() - 6)), UUID.fromString(id.substring(5, id.length() - 6)));
156-
}
163+
this.discussionService.updateTopicsByImageId(UUID.fromString(oldSVGOccurances.get(countQcircuit).substring(5, oldSVGOccurances.get(countQcircuit).length() - 6)), UUID.fromString(id.substring(5, id.length() - 6)));
157164
}
158165
}
159166
countQcircuit++;
@@ -172,7 +179,7 @@ public Object renderContent(Pattern pattern, Pattern oldVersion) {
172179
}
173180

174181
public Integer[] getNextOccurance(String content, String begin, String end) {
175-
return new Integer[] {content.indexOf(begin), content.indexOf(end)};
182+
return new Integer[] {content.indexOf(begin, 0), content.indexOf(end, 0)};
176183
}
177184

178185
/**
@@ -186,14 +193,18 @@ public byte[] renderContentViaAPI(String content, List<String> packages, String
186193
byte[] file = null;
187194
try {
188195
RestTemplate restTemplate = new RestTemplate();
189-
final String baseUrl = "http://localhost:" + 8082 + "/renderLatex/";
190-
URI uri = new URI(baseUrl);
191196
HttpHeaders headers = new HttpHeaders();
192197
headers.setContentType(MediaType.APPLICATION_JSON);
193-
HttpEntity<LatexContent> entity = new HttpEntity<>(latexContent, headers);
194-
ResponseEntity<byte[]> result = restTemplate.postForEntity(uri, entity, byte[].class);
198+
String url = baseAPIEndpoint + "?content="
199+
+ URLEncoder.encode(latexContent.getContent(), "UTF-8");
200+
for (String latexPackage : latexContent.getLatexPackages()
201+
) {
202+
url += "&packages=" + URLEncoder.encode(latexPackage, "UTF-8");
203+
}
204+
ResponseEntity<byte[]> result = restTemplate.getForEntity(url, byte[].class);
195205
file = result.getBody();
196206
} catch (Exception e) {
207+
log.error("could not render LaTeX: " + e.getMessage());
197208
return null;
198209
}
199210
return file;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
server.port=1977
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
3+
spring.datasource.username=postgres
4+
spring.datasource.password=postgres
5+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
6+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
7+
spring.jpa.hibernate.ddl-auto=update
8+
logging.level.io.github.patternatlas.api=debug
9+
spring.datasource.initialization-mode=always
10+
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
11+
springdoc.swagger-ui.url=/patternpedia/v3/api-docs
12+
springdoc.swagger-ui.path=/swagger-ui
13+
springdoc.swagger-ui.config-url=/patternpedia/v3/api-docs/swagger-config
14+
springdoc.swagger-ui.operationsSorter=alpha
15+
springdoc.default-produces-media-type=application/hal+json
16+
security.oauth2.resource.jwk.key-set-uri=http://localhost:8081/.well-known/jwks.json
17+
#okta.oauth2.issuer=https://dev-918271.okta.com/oauth2/default
18+
#okta.oauth2.clientId=0oa1eflyl1wZDVLLg357
19+
#----------------------------
20+
# LaTex Renderer Service
21+
#----------------------------
22+
io.github.patternatlas.api.latexrenderer.hostname=${LATEX_RENDERER_HOST_NAME}
23+
io.github.patternatlas.api.latexrenderer.port=${LATEX_RENDERER_PORT}
24+

src/main/resources/application.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ spring.datasource.password=postgres
55
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
66
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
77
spring.jpa.hibernate.ddl-auto=update
8-
logging.level.com.patternatlas.api=debug
8+
logging.level.io.github.patternatlas.api=debug
99
spring.datasource.initialization-mode=always
1010
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
1111
springdoc.swagger-ui.url=/patternatlas/v3/api-docs
@@ -16,5 +16,10 @@ springdoc.default-produces-media-type=application/hal+json
1616
security.oauth2.resource.jwk.key-set-uri=http://localhost:8081/.well-known/jwks.json
1717
#okta.oauth2.issuer=https://dev-918271.okta.com/oauth2/default
1818
#okta.oauth2.clientId=0oa1eflyl1wZDVLLg357
19+
#----------------------------
20+
# Latex Service
21+
#----------------------------
22+
com.patternatlas.api.latexrenderer.hostname=localhost
23+
com.patternatlas.api.latexrenderer.port=5030
1924
# Embedded Tomcat
2025
server.servlet.contextPath=/patternatlas

src/test/resources/application.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ spring.jpa.properties.hibernate.use_sql_comments=true
1717
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
1818
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8081/auth/realms/patternpedia
1919
spring.security.oauth2.resourceserver.jwt-set-uri=http://localhost:8081/auth/realms/patternpedia/protocol/openid-connect/cer
20+
#----------------------------
21+
# Latex Service
22+
#----------------------------
23+
com.patternatlas.api.latexrenderer.hostname=localhost
24+
com.patternatlas.api.latexrenderer.port=5030

0 commit comments

Comments
 (0)