Commit 3ecec3f5 authored by Klas, Claus-Peter's avatar Klas, Claus-Peter
Browse files

- added caching for thymleaf question preview

- some cleanup of code
- added cache rest controller to remove all caches
parent 770c827c
package org.gesis.stardat.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CachingController {
private final static Logger logger = LoggerFactory.getLogger(CachingController.class);
@Autowired
CachingService cachingService;
@GetMapping("clearAllCaches")
public void clearAllCaches() {
logger.debug("Clear all caches called");
cachingService.evictAllCaches();
}
}
\ No newline at end of file
package org.gesis.stardat.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
@Service
public class CachingService {
@Autowired
CacheManager cacheManager;
public void evictSingleCacheValue(String cacheName, String cacheKey) {
cacheManager.getCache(cacheName).evict(cacheKey);
}
public void evictAllCacheValues(String cacheName) {
cacheManager.getCache(cacheName).clear();
}
public void evictAllCaches() {
cacheManager.getCacheNames().stream().forEach(cacheName -> cacheManager.getCache(cacheName).clear());
}
}
......@@ -2,37 +2,45 @@ package org.gesis.stardat.service;
import java.util.Locale;
import org.gesis.stardat.entity.*;
import org.gesis.stardat.entity.DDIElement;
import org.gesis.stardat.entity.Instrument;
import org.gesis.stardat.entity.QuestionConstruct;
import org.gesis.stardat.entity.Statement;
import org.gesis.stardat.entity.StudyUnit;
import org.gesis.stardat.helper.ExportQuestion;
import org.gesis.stardat.helper.QuestionGridRow;
import org.gesis.stardat.helper.StudyPreview;
import org.gesis.stardat.service.dto.GroupDTO;
import org.gesis.stardat.ui.utils.PreviewUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.thymeleaf.spring5.SpringTemplateEngine;
@Service
public class ExportService
{
public enum DownloadType
{
public class ExportService {
private static final String _HTML = "_html";
private static final String HTML_EXPORT = "html/export_";
private final static Logger logger = LoggerFactory.getLogger(ExportService.class);
public enum DownloadType {
SKOS("rdf"), PDF("pdf"), HTML("html"), WORD("docx");
private final String type;
private DownloadType( String s )
{
private DownloadType(String s) {
type = s;
}
public boolean equalsType( String otherType )
{
return type.equals( otherType );
public boolean equalsType(String otherType) {
return type.equals(otherType);
}
@Override
public String toString()
{
public String toString() {
return this.type;
}
}
......@@ -45,97 +53,105 @@ public class ExportService
this.entityService = entityService;
}
public String generatePreviewByTemplate(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument, GroupDTO currentGroup, String lang, String mode)
{
@Cacheable(value = "thymeleafQuestionPreviewCache", key = "{#questionRow.id, #instrument.id}")
public String generatePreviewByTemplate(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument,
GroupDTO currentGroup, String lang, String mode) {
logger.debug("calling method generatePreviewByTemplate");
if (mode.equals("question")) {
return generateThymeleafPreview(fillExportQuestion (questionRow, studyUnit, instrument, lang), currentGroup, mode);
}
else {
return generateThymeleafPreview(fillExportStatement (questionRow, studyUnit, instrument, lang), currentGroup, mode);
return generateThymeleafPreview(fillExportQuestion(questionRow, studyUnit, instrument, lang), currentGroup,
mode);
} else {
return generateThymeleafPreview(fillExportStatement(questionRow, studyUnit, instrument, lang), currentGroup,
mode);
}
}
public String generateStudyPreviewByTemplate(StudyUnit studyUnit, Instrument instrument, GroupDTO currentGroup, String lang, String mode)
{
return generateThymeleafPreviewForStudy(prepareStudyPreview(studyUnit, instrument), currentGroup, mode);
@CacheEvict(value = "thymeleafQuestionPreviewCache", key = "{#questionRow.id, #instrument.id}")
public void generatePreviewByTemplateCleanCache(QuestionGridRow questionRow, Instrument instrument) {
// on purpose empty
// only for cache cleanup of an entry
}
public String generateStudyPreviewByTemplate(StudyUnit studyUnit, Instrument instrument, GroupDTO currentGroup,
String lang, String mode) {
return generateThymeleafPreviewForStudy(prepareStudyPreview(studyUnit, instrument), currentGroup, mode);
}
private String generateThymeleafPreview(ExportQuestion eq, GroupDTO currentGroup, String mode)
{
private String generateThymeleafPreview(ExportQuestion eq, GroupDTO currentGroup, String mode) {
String filledTemplate = null;
org.thymeleaf.context.Context ctx = new org.thymeleaf.context.Context();
ctx.setVariable( "q", eq );
if ( currentGroup.getHeaderImage() != null ) {
ctx.setVariable( "logo", currentGroup.getHeaderImage() );
ctx.setVariable("q", eq);
if (currentGroup.getHeaderImage() != null) {
ctx.setVariable("logo", currentGroup.getHeaderImage());
}
if (mode.equals("question")) {
filledTemplate = templateEngine.process("html/export_" + ( I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de": "en") + "_html", ctx);
filledTemplate = templateEngine.process(
HTML_EXPORT + (I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de" : "en") + _HTML, ctx);
} else {
filledTemplate = templateEngine.process(HTML_EXPORT
+ (I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de" : "en") + _HTML + "_statement", ctx);
}
else {
filledTemplate = templateEngine.process("html/export_" + ( I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de": "en") + "_html"+"_statement", ctx);
}
// remove <!DOCTYPE html><html><head></head><body> ... </body></html>
String onlyTemplateContent = filledTemplate.substring( 41, filledTemplate.length() - 15);
return onlyTemplateContent;
return filledTemplate.substring(41, filledTemplate.length() - 15);
}
private String generateThymeleafPreviewForStudy(StudyPreview eq, GroupDTO currentGroup, String mode)
{
private String generateThymeleafPreviewForStudy(StudyPreview eq, GroupDTO currentGroup, String mode) {
String filledTemplate = null;
org.thymeleaf.context.Context ctx = new org.thymeleaf.context.Context();
ctx.setVariable( "q", eq );
if ( currentGroup.getHeaderImage() != null ) {
ctx.setVariable( "logo", currentGroup.getHeaderImage() );
ctx.setVariable("q", eq);
if (currentGroup.getHeaderImage() != null) {
ctx.setVariable("logo", currentGroup.getHeaderImage());
}
if (mode.equals("study")) {
filledTemplate = templateEngine.process("html/export_" + ( I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de": "en") + "_html"+"_study", ctx);
filledTemplate = templateEngine.process(
HTML_EXPORT + (I18N.getCurrentUILocale().equals(Locale.GERMAN) ? "de" : "en") + _HTML + "_study",
ctx);
}
// remove <!DOCTYPE html><html><head></head><body> ... </body></html>
String onlyTemplateContent = filledTemplate.substring( 41, filledTemplate.length() - 15);
return onlyTemplateContent;
return filledTemplate.substring(41, filledTemplate.length() - 15);
}
private ExportQuestion fillExportQuestion(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument, String lang)
{
private ExportQuestion fillExportQuestion(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument,
String lang) {
ExportQuestion eq = prepareExportQuestion(studyUnit, instrument);
// get question construct
QuestionConstruct qc = null;
if ( questionRow.getObject().getType().equals( DDIElement.QUESTIONCONSTRUCT ) )
if (questionRow.getObject().getType().equals(DDIElement.QUESTIONCONSTRUCT))
qc = (QuestionConstruct) questionRow.getObject();
if ( qc == null )
if (qc == null)
return null;
if ( qc.getQuestionType().contains( "Item" ) )
entityService.fillQuestion( qc );
if (qc.getQuestionType().contains("Item"))
entityService.fillQuestion(qc);
else
entityService.fillMultipleQuestion( qc );
entityService.fillMultipleQuestion(qc);
PreviewUtils.fillQuestionExportWithPropertyMap(questionRow, eq, qc, lang,false);
PreviewUtils.fillQuestionExportWithPropertyMap(questionRow, eq, qc, lang, false);
return eq;
}
private ExportQuestion fillExportStatement(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument, String lang)
{
private ExportQuestion fillExportStatement(QuestionGridRow questionRow, StudyUnit studyUnit, Instrument instrument,
String lang) {
ExportQuestion eq = prepareExportQuestion(studyUnit, instrument);
Statement s = (Statement) questionRow.getObject();
final String sourceLang = s.getLanguage();
// check if target/translation is needed
if( !lang.equals(sourceLang) )
if (!lang.equals(sourceLang))
s.setLanguage(lang);
eq.setTitle(s.getLabel());
eq.setText(s.getDisplayText());
eq.setDescription(s.getDescription());
// set back to the source language
if( !s.getLanguage().equals(sourceLang) )
if (!s.getLanguage().equals(sourceLang))
s.setLanguage(sourceLang);
return eq;
......@@ -149,7 +165,7 @@ public class ExportService
eq.setDoi(studyUnit.getDOI());
return eq;
}
private StudyPreview prepareStudyPreview(StudyUnit studyUnit, Instrument instrument) {
StudyPreview eq = new StudyPreview();
eq.setTitle(instrument.getLabel());
......@@ -160,14 +176,14 @@ public class ExportService
eq.setPublicationYear(studyUnit.getPublicationYearString());
eq.setCollectionBegin(studyUnit.getDateofCollectionStart());
eq.setCollectionEnd(studyUnit.getDateofCollectionEnd());
if( !studyUnit.getPeriodofCollectionAsString().contains("null"))
if (!studyUnit.getPeriodofCollectionAsString().contains("null"))
eq.setPeriod(studyUnit.getPeriodofCollectionAsString());
eq.setPublisher(studyUnit.getPublisher());
if( !studyUnit.getCategoriesAsString().equals("<ul></ul>"))
if (!studyUnit.getCategoriesAsString().equals("<ul></ul>"))
eq.setCategories(studyUnit.getCategoriesAsString());
eq.setTopics(studyUnit.getTopicsAsString());
eq.setContent(studyUnit.getContent());
return eq;
}
}
......@@ -54,10 +54,9 @@ public class AnswerGrid extends MGrid<Answer> implements Translatable{
gridDropTarget.addGridDropListener( event -> {
// Accepting dragged items from another Grid in the same UI
event.getDragSourceExtension().ifPresent( source -> {
if ( source instanceof GridDragSource )
if ( source instanceof GridDragSource && event.getDropTargetRow().isPresent())
{
if ( event.getDropTargetRow().isPresent() )
{
Answer targetRow = event.getDropTargetRow().get();
Answer draggedRow = draggedItems.iterator().next();
......@@ -71,7 +70,7 @@ public class AnswerGrid extends MGrid<Answer> implements Translatable{
this.getDataProvider().refreshAll();
// set flag to update answer scheme
this.answerSchemeWindow.setShouldStore( true );
}
}
} );
} );
......
......@@ -68,7 +68,12 @@
<persistence strategy="none" />
</cache>
<cache name="thymeleafQuestionPreviewCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="false" >
<persistence strategy="none" />
</cache>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment