Search in sources :

Example 1 with JtwigModel

use of org.jtwig.JtwigModel in project sakuli by ConSol.

the class AbstractTemplateOutputBuilder method createOutput.

/**
     * Converts the current test suite to a string based on the template for the concrete converter.
     *
     * @return
     */
public String createOutput() throws SakuliForwarderException {
    try {
        JtwigModel model = createModel();
        EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().extensions().add(new SpacelessExtension(new SpacelessConfiguration(new LeadingWhitespaceRemover()))).and().functions().add(new IsBlankFunction()).add(new GetOutputStateFunction()).add(new GetOutputDurationFunction()).add(new ExtractScreenshotFunction(screenshotDivConverter)).add(new AbbreviateFunction()).and().build();
        JtwigTemplate template = JtwigTemplate.fileTemplate(getTemplatePath().toFile(), configuration);
        logger.debug(String.format("Render model into JTwig template. Model: '%s'", model));
        return template.render(model);
    } catch (Throwable thr) {
        throw new SakuliForwarderException(thr, "Exception during rendering of Twig template occurred!");
    }
}
Also used : SpacelessExtension(org.jtwig.spaceless.SpacelessExtension) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) JtwigTemplate(org.jtwig.JtwigTemplate) SpacelessConfiguration(org.jtwig.spaceless.configuration.SpacelessConfiguration) JtwigModel(org.jtwig.JtwigModel) EnvironmentConfiguration(org.jtwig.environment.EnvironmentConfiguration)

Example 2 with JtwigModel

use of org.jtwig.JtwigModel in project amos-ss17-alexa by c-i-ber.

the class BudgetReportService method onIntent.

@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) throws SpeechletException {
    IntentRequest request = requestEnvelope.getRequest();
    if (request.getIntent().getName().equals(BUDGET_REPORT_EMAIL_INTENT)) {
        // Load the mail template from resources
        JtwigTemplate template = JtwigTemplate.classpathTemplate("html-templates/budget-report.twig");
        List<BudgetReportCategory> categories = new ArrayList<>();
        // DynamoDbClient.instance.getItems(Category.TABLE_NAME, Category::new);
        List<Category> dbCategories = DynamoDbMapper.getInstance().loadAll(Category.class);
        for (Category cat : dbCategories) {
            categories.add(new BudgetReportCategory(cat.getName(), cat.getSpending(), cat.getLimit()));
        }
        JtwigModel model = JtwigModel.newModel().with("categories", categories);
        // Render mail template
        String body = template.render(model);
        String answer = "Okay, ich habe dir deinen Ausgabenreport per E-Mail gesendet.";
        boolean isDebug = SessionStorage.getInstance().getStorage(requestEnvelope.getSession().getSessionId()).containsKey("DEBUG");
        if (!isDebug && !EMailClient.SendHTMLEMail("Ausgabenreport", body)) {
            answer = "Leider konnte der Ausgabenreport nicht gesendet werden.";
        }
        return getResponse("Ausgabenreport", answer);
    }
    return null;
}
Also used : Category(model.db.Category) JtwigModel(org.jtwig.JtwigModel) IntentRequest(com.amazon.speech.speechlet.IntentRequest) ArrayList(java.util.ArrayList) JtwigTemplate(org.jtwig.JtwigTemplate)

Example 3 with JtwigModel

use of org.jtwig.JtwigModel in project sakuli by ConSol.

the class AbstractTemplateOutputBuilder method createOutput.

/**
 * Converts the current test data entity to a string based on the template for the concrete converter.
 *
 * @param abstractTestDataEntity Test data entity, which has to be converted
 * @return A string representation of the provided test data entity
 */
public String createOutput(AbstractTestDataEntity abstractTestDataEntity) throws SakuliForwarderCheckedException {
    try {
        JtwigModel model = createModel(abstractTestDataEntity);
        EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().extensions().add(new SpacelessExtension(new SpacelessConfiguration(new LeadingWhitespaceRemover()))).and().functions().add(new IsBlankFunction()).add(new GetOutputStateFunction()).add(new GetOutputDurationFunction()).add(new ExtractScreenshotFunction(screenshotDivConverter)).add(new AbbreviateFunction()).add(new UnixTimestampConverterFunction()).add(new GetTestDataEntityTypeFunction()).and().build();
        JtwigTemplate template = JtwigTemplate.fileTemplate(getTemplatePath().toFile(), configuration);
        logger.debug(String.format("Render model into JTwig template. Model: '%s'", model));
        return template.render(model);
    } catch (Exception e) {
        throw new SakuliForwarderCheckedException(e, "Exception during rendering of Twig template occurred!");
    }
}
Also used : SpacelessExtension(org.jtwig.spaceless.SpacelessExtension) JtwigTemplate(org.jtwig.JtwigTemplate) FileNotFoundException(java.io.FileNotFoundException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SpacelessConfiguration(org.jtwig.spaceless.configuration.SpacelessConfiguration) JtwigModel(org.jtwig.JtwigModel) EnvironmentConfiguration(org.jtwig.environment.EnvironmentConfiguration) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Example 4 with JtwigModel

use of org.jtwig.JtwigModel in project sakuli by ConSol.

the class AbstractTemplateOutputBuilderTest method createModel.

@Test
public void createModel() {
    JtwigModel model = testling.createModel(testSuite);
    assertEquals(model.get(TEST_DATA_ENTITY.getName()).get().getValue(), testSuite);
    assertEquals(model.get(SAKULI_PROPERTIES.getName()).get().getValue(), sakuliProperties);
    assertEquals(model.get(CHECK_MK_PROPERTIES.getName()).get().getValue(), checkMKProperties);
}
Also used : JtwigModel(org.jtwig.JtwigModel) BaseTest(org.sakuli.BaseTest) Test(org.testng.annotations.Test)

Aggregations

JtwigModel (org.jtwig.JtwigModel)4 JtwigTemplate (org.jtwig.JtwigTemplate)3 EnvironmentConfiguration (org.jtwig.environment.EnvironmentConfiguration)2 SpacelessExtension (org.jtwig.spaceless.SpacelessExtension)2 SpacelessConfiguration (org.jtwig.spaceless.configuration.SpacelessConfiguration)2 IntentRequest (com.amazon.speech.speechlet.IntentRequest)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Category (model.db.Category)1 BaseTest (org.sakuli.BaseTest)1 SakuliForwarderCheckedException (org.sakuli.exceptions.SakuliForwarderCheckedException)1 SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)1 Test (org.testng.annotations.Test)1