Search in sources :

Example 36 with Context

use of org.apache.velocity.context.Context in project openolat by klemens.

the class TaskFolderCallback method getTaskDeletedMailBody.

private String getTaskDeletedMailBody(UserRequest ureq, String fileName, String courseName, String courseLink) {
    // grab standard text
    String confirmation = translate("task.deleted.body");
    Context c = new VelocityContext();
    Identity identity = ureq.getIdentity();
    c.put("first", identity.getUser().getProperty(UserConstants.FIRSTNAME, getLocale()));
    c.put("last", identity.getUser().getProperty(UserConstants.LASTNAME, getLocale()));
    c.put("email", UserManager.getInstance().getUserDisplayEmail(identity, ureq.getLocale()));
    c.put("filename", fileName);
    c.put("coursename", courseName);
    c.put("courselink", courseLink);
    return VelocityHelper.getInstance().evaluateVTL(confirmation, c);
}
Also used : Context(org.apache.velocity.context.Context) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) VelocityContext(org.apache.velocity.VelocityContext) MailContext(org.olat.core.util.mail.MailContext) VelocityContext(org.apache.velocity.VelocityContext) Identity(org.olat.core.id.Identity)

Example 37 with Context

use of org.apache.velocity.context.Context in project openolat by klemens.

the class DropboxController method getConfirmation.

private String getConfirmation(UserRequest ureq, String filename) {
    // grab confirmation-text from bb-config
    String confirmation = config.getStringValue(TACourseNode.CONF_DROPBOX_CONFIRMATION);
    Context c = new VelocityContext();
    Identity identity = ureq.getIdentity();
    c.put("login", identity.getName());
    c.put("first", identity.getUser().getProperty(UserConstants.FIRSTNAME, getLocale()));
    c.put("last", identity.getUser().getProperty(UserConstants.LASTNAME, getLocale()));
    c.put("email", UserManager.getInstance().getUserDisplayEmail(identity, getLocale()));
    c.put("filename", filename);
    Date now = new Date();
    Formatter f = Formatter.getInstance(ureq.getLocale());
    c.put("date", f.formatDate(now));
    c.put("time", f.formatTime(now));
    // update attempts counter for this user: one file - one attempts
    AssessableCourseNode acn = (AssessableCourseNode) node;
    acn.incrementUserAttempts(userCourseEnv, Role.user);
    // log entry for this file
    UserNodeAuditManager am = userCourseEnv.getCourseEnvironment().getAuditManager();
    am.appendToUserNodeLog(node, identity, identity, "FILE UPLOADED: " + filename);
    return VelocityHelper.getInstance().evaluateVTL(confirmation, c);
}
Also used : Context(org.apache.velocity.context.Context) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) VelocityContext(org.apache.velocity.VelocityContext) MailContext(org.olat.core.util.mail.MailContext) AssessableCourseNode(org.olat.course.nodes.AssessableCourseNode) UserNodeAuditManager(org.olat.course.auditing.UserNodeAuditManager) VelocityContext(org.apache.velocity.VelocityContext) Formatter(org.olat.core.util.Formatter) Identity(org.olat.core.id.Identity) Date(java.util.Date)

Example 38 with Context

use of org.apache.velocity.context.Context in project openolat by klemens.

the class LocalizedXSLTransformer method initTransformer.

/**
 * Helper to create XSLT transformer for this instance
 */
private void initTransformer() {
    // translate xsl with velocity
    Context vcContext = new VelocityContext();
    vcContext.put("t", pT);
    vcContext.put("staticPath", StaticMediaDispatcher.createStaticURIFor(""));
    String xslAsString = "";
    try (InputStream xslin = getClass().getResourceAsStream("/org/olat/ims/resources/xsl/" + XSLFILENAME)) {
        xslAsString = slurp(xslin);
    } catch (IOException e) {
        log.error("Could not convert xsl to string!", e);
    }
    String replacedOutput = evaluateValue(xslAsString, vcContext);
    TransformerFactory tfactory = TransformerFactory.newInstance();
    XMLReader reader;
    try {
        reader = XMLReaderFactory.createXMLReader();
        reader.setEntityResolver(er);
        Source xsltsource = new SAXSource(reader, new InputSource(new StringReader(replacedOutput)));
        templates = tfactory.newTemplates(xsltsource);
    } catch (SAXException e) {
        throw new OLATRuntimeException("Could not initialize transformer!", e);
    } catch (TransformerConfigurationException e) {
        throw new OLATRuntimeException("Could not initialize transformer (wrong config)!", e);
    }
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) VelocityContext(org.apache.velocity.VelocityContext) InputStream(java.io.InputStream) IOException(java.io.IOException) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) DocumentSource(org.dom4j.io.DocumentSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader)

Example 39 with Context

use of org.apache.velocity.context.Context in project dbflute-core by dbflute.

the class DfAbstractTexenTask method setupControlContext.

protected Context setupControlContext() {
    final Context ctx;
    try {
        ctx = initControlContext();
    } catch (Exception e) {
        String msg = "Failed to initialize control context:";
        msg = msg + " templatePath=" + templatePath + " useClasspath=" + useClasspath;
        throw new IllegalStateException(msg, e);
    }
    try {
        populateInitialContext(ctx);
    } catch (Exception e) {
        String msg = "Failed to populate initial context:";
        msg = msg + " templatePath=" + templatePath + " useClasspath=" + useClasspath;
        throw new IllegalStateException(msg, e);
    }
    if (contextProperties != null) {
        for (Iterator<?> i = contextProperties.getKeys(); i.hasNext(); ) {
            String property = (String) i.next();
            String value = contextProperties.getString(property);
            try {
                ctx.put(property, new Integer(value));
            } catch (NumberFormatException nfe) {
                String booleanString = contextProperties.testBoolean(value);
                if (booleanString != null) {
                    ctx.put(property, Boolean.valueOf(booleanString));
                } else {
                    if (property.endsWith("file.contents")) {
                        final String canonicalPath;
                        try {
                            canonicalPath = getProject().resolveFile(value).getCanonicalPath();
                        } catch (IOException e) {
                            String msg = "Failed to get the canonical path:";
                            msg = msg + " property=" + property + " value=" + value;
                            throw new IllegalStateException(msg, e);
                        }
                        value = fileContentsToString(canonicalPath);
                        property = property.substring(0, property.indexOf("file.contents") - 1);
                    }
                    ctx.put(property, value);
                }
            }
        }
    }
    return ctx;
}
Also used : Context(org.apache.velocity.context.Context) IOException(java.io.IOException) SQLException(java.sql.SQLException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 40 with Context

use of org.apache.velocity.context.Context in project entando-core by entando.

the class BaseEntityRenderer method render.

@Override
public String render(IApsEntity entity, String velocityTemplate, String langCode, boolean convertSpecialCharacters) {
    String renderedEntity = null;
    List<TextAttributeCharReplaceInfo> conversions = null;
    try {
        if (convertSpecialCharacters) {
            conversions = this.convertSpecialCharacters(entity, langCode);
        }
        Context velocityContext = new VelocityContext();
        EntityWrapper entityWrapper = this.getEntityWrapper(entity);
        entityWrapper.setRenderingLang(langCode);
        velocityContext.put(this.getEntityWrapperContextName(), entityWrapper);
        I18nManagerWrapper i18nWrapper = new I18nManagerWrapper(langCode, this.getI18nManager());
        velocityContext.put("i18n", i18nWrapper);
        StringWriter stringWriter = new StringWriter();
        boolean isEvaluated = Velocity.evaluate(velocityContext, stringWriter, "render", velocityTemplate);
        if (!isEvaluated) {
            throw new ApsSystemException("Rendering error");
        }
        stringWriter.flush();
        renderedEntity = stringWriter.toString();
    } catch (Throwable t) {
        _logger.error("Rendering error. entity {}", entity.getTypeCode(), t);
        // ApsSystemUtils.logThrowable(t, this, "render", "Rendering error");
        renderedEntity = "";
    } finally {
        if (convertSpecialCharacters && null != conversions) {
            this.replaceSpecialCharacters(conversions);
        }
    }
    return renderedEntity;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) I18nManagerWrapper(com.agiletec.aps.system.services.i18n.I18nManagerWrapper)

Aggregations

Context (org.apache.velocity.context.Context)41 VelocityContext (org.apache.velocity.VelocityContext)33 StringWriter (java.io.StringWriter)9 IOException (java.io.IOException)8 Identity (org.olat.core.id.Identity)8 File (java.io.File)6 ArrayList (java.util.ArrayList)6 AssessmentRenderFunctions.contentAsString (org.olat.ims.qti21.ui.components.AssessmentRenderFunctions.contentAsString)6 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)4 StringReader (java.io.StringReader)4 Locale (java.util.Locale)4 Map (java.util.Map)4 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)4 ValidationError (org.olat.core.gui.components.form.ValidationError)4 MailContext (org.olat.core.util.mail.MailContext)4 I18nManagerWrapper (com.agiletec.aps.system.services.i18n.I18nManagerWrapper)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 Renderer (org.olat.core.gui.render.Renderer)3