11package io .github .patternatlas .api .service ;
22
3- import java .net .URI ;
3+ import java .net .URLEncoder ;
44import java .time .Instant ;
55import java .util .ArrayList ;
66import java .util .List ;
77import java .util .Map ;
88import java .util .UUID ;
99
10- import org .apache .commons .text .similarity .JaccardSimilarity ;
10+ import org .slf4j .Logger ;
11+ import org .slf4j .LoggerFactory ;
1112import org .springframework .beans .factory .annotation .Autowired ;
12- import org .springframework .http . HttpEntity ;
13+ import org .springframework .beans . factory . annotation . Value ;
1314import org .springframework .http .HttpHeaders ;
1415import org .springframework .http .MediaType ;
1516import org .springframework .http .ResponseEntity ;
2122import io .github .patternatlas .api .entities .Image ;
2223import io .github .patternatlas .api .entities .Pattern ;
2324import io .github .patternatlas .api .rest .model .LatexContent ;
25+ import lombok .extern .slf4j .Slf4j ;
2426
27+ @ Slf4j
2528@ Component
2629public 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 ;
0 commit comments