Search in sources :

Example 11 with Translations

use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.

the class ServerRestV2Controller method sendTestEmail.

@PreAuthorize("isAdmin()")
@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) {
    try {
        Translations translations = Common.getTranslations();
        Map<String, Object> model = new HashMap<>();
        model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
        MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
        EmailWorkItem.queueEmail(email, cnt);
        return new ResponseEntity<String>(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()), HttpStatus.OK);
    } catch (Exception e) {
        throw new GenericRestException(HttpStatus.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) IOException(java.io.IOException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with Translations

use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.

the class ServerRestController method sendTestEmail.

@ApiOperation(value = "Send a test email", notes = "Sends email to supplied address")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv" }, produces = { "application/json", "text/csv" }, value = "/email/test")
public ResponseEntity<String> sendTestEmail(@RequestParam(value = "email", required = true, defaultValue = "") String email, @RequestParam(value = "username", required = true, defaultValue = "") String username, HttpServletRequest request) throws RestValidationFailedException {
    RestProcessResult<String> result = new RestProcessResult<String>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        if (Permissions.hasAdmin(user)) {
            try {
                Translations translations = Common.getTranslations();
                Map<String, Object> model = new HashMap<>();
                model.put("message", new TranslatableMessage("ftl.userTestEmail", username));
                MangoEmailContent cnt = new MangoEmailContent("testEmail", model, translations, translations.translate("ftl.testEmail"), Common.UTF8);
                EmailWorkItem.queueEmail(email, cnt);
                return result.createResponseEntity(new TranslatableMessage("common.testEmailSent", email).translate(Common.getTranslations()));
            } catch (Exception e) {
                result.addRestMessage(this.getInternalServerErrorMessage(e.getMessage()));
            }
        } else {
            LOG.warn("Non admin user: " + user.getUsername() + " attempted to send a test email.");
            result.addRestMessage(this.getUnauthorizedMessage());
        }
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) MangoEmailContent(com.serotonin.m2m2.email.MangoEmailContent) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Translations(com.serotonin.m2m2.i18n.Translations) RestValidationFailedException(com.serotonin.m2m2.web.mvc.rest.v1.exception.RestValidationFailedException) InvalidRQLRestException(com.infiniteautomation.mango.rest.v2.exception.InvalidRQLRestException) IOException(java.io.IOException) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with Translations

use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.

the class TranslationsController method publicNamespacedTranslations.

@ApiOperation(value = "Get translations for public namespaces", notes = "Namespace must be base , ie public not public.messages. Returns sub-namespaces too. For > 1 use comma common,public")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/public/{namespaces}")
public ResponseEntity<Map<String, ?>> publicNamespacedTranslations(@ApiParam(value = "Message Namespaces, simmilar to java package structure", allowMultiple = true) @PathVariable String[] namespaces, @ApiParam(value = "Language for translation (must have language pack installed)", allowMultiple = false) @RequestParam(value = "language", required = false) String language, @ApiParam(value = "Use server language for translation", allowMultiple = false) @RequestParam(value = "server", required = false, defaultValue = "false") boolean server, @RequestParam(value = "browser", required = false, defaultValue = "false") boolean browser, HttpServletRequest request) {
    RestProcessResult<Map<String, ?>> result = new RestProcessResult<Map<String, ?>>(HttpStatus.OK);
    // Confirm the requested namespace is indeed public
    for (String namespace : namespaces) {
        if (!this.publicNamespaces.contains(namespace)) {
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
    }
    if (result.isOk()) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Locale locale = this.getLocale(language, server, browser, request, Common.getHttpUser());
        resultMap.put("locale", locale.toLanguageTag());
        resultMap.put("translations", getTranslationMap(namespaces, locale));
        return result.createResponseEntity(resultMap);
    }
    return result.createResponseEntity();
}
Also used : Locale(java.util.Locale) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Translations

use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.

the class TranslationsController method namespacedTranslations.

@ApiOperation(value = "Get translations based on namespaces", notes = "Namespace must be base namespace, ie common not common.messages. Returns sub-namespaces too.  For > 1 use comma common,public")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/{namespaces}")
public ResponseEntity<Map<String, ?>> namespacedTranslations(@ApiParam(value = "Message Namespaces, simmilar to java package structure", allowMultiple = true) @PathVariable String[] namespaces, @ApiParam(value = "Language for translation (must have language pack installed)", allowMultiple = false) @RequestParam(value = "language", required = false) String language, @ApiParam(value = "Use server language for translation", allowMultiple = false) @RequestParam(value = "server", required = false, defaultValue = "false") boolean server, @RequestParam(value = "browser", required = false, defaultValue = "false") boolean browser, HttpServletRequest request) {
    RestProcessResult<Map<String, ?>> result = new RestProcessResult<Map<String, ?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Locale locale = this.getLocale(language, server, browser, request, user);
        resultMap.put("locale", locale.toLanguageTag());
        resultMap.put("translations", getTranslationMap(namespaces, locale));
        return result.createResponseEntity(resultMap);
    }
    return result.createResponseEntity();
}
Also used : Locale(java.util.Locale) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with Translations

use of com.serotonin.m2m2.i18n.Translations in project ma-modules-public by infiniteautomation.

the class TranslationsController method getTranslationMap.

/**
 * Get a set of translations for many namespaces
 * @param namespaces
 * @param locale
 * @return
 */
private Map<String, Map<String, String>> getTranslationMap(String[] namespaces, Locale locale) {
    Translations translations = Translations.getTranslations(locale);
    Map<String, Map<String, String>> resultMap = new HashMap<String, Map<String, String>>();
    for (String namespace : namespaces) {
        Map<String, Map<String, String>> tranMap = translations.asMap(namespace);
        Iterator<String> it = tranMap.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            Map<String, String> submap = resultMap.get(key);
            if (submap == null) {
                submap = new HashMap<String, String>();
                resultMap.put(key, submap);
            }
            submap.putAll(tranMap.get(key));
        }
    }
    return resultMap;
}
Also used : HashMap(java.util.HashMap) Translations(com.serotonin.m2m2.i18n.Translations) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Translations (com.serotonin.m2m2.i18n.Translations)17 HashMap (java.util.HashMap)14 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)8 MangoEmailContent (com.serotonin.m2m2.email.MangoEmailContent)7 User (com.serotonin.m2m2.vo.User)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)5 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)5 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 Map (java.util.Map)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 InvalidArgumentException (com.serotonin.InvalidArgumentException)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)3 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)3 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)3