Search in sources :

Example 6 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class CommandlineTemplateProvider method importData.

@Override
protected Set<RuleTemplate> importData(URL url, Parser<RuleTemplate> parser, InputStreamReader inputStreamReader) throws ParsingException {
    Set<RuleTemplate> providedObjects = parser.parse(inputStreamReader);
    if (providedObjects != null && !providedObjects.isEmpty()) {
        List<String> portfolio = new ArrayList<String>();
        synchronized (providerPortfolio) {
            providerPortfolio.put(url, portfolio);
        }
        List<ParsingNestedException> importDataExceptions = new ArrayList<ParsingNestedException>();
        for (RuleTemplate ruleT : providedObjects) {
            List<ParsingNestedException> exceptions = new ArrayList<ParsingNestedException>();
            String uid = ruleT.getUID();
            checkExistence(uid, exceptions);
            if (exceptions.isEmpty()) {
                portfolio.add(uid);
                synchronized (providedObjectsHolder) {
                    notifyListeners(providedObjectsHolder.put(uid, ruleT), ruleT);
                }
            } else {
                importDataExceptions.addAll(exceptions);
            }
        }
        if (!importDataExceptions.isEmpty()) {
            throw new ParsingException(importDataExceptions);
        }
    }
    return providedObjects;
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ArrayList(java.util.ArrayList) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate)

Example 7 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class TemplateResource method getByUID.

@GET
@Path("/{templateUID}")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets a template corresponding to the given UID.", response = Template.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = Template.class), @ApiResponse(code = 404, message = "Template corresponding to the given UID does not found.") })
public Response getByUID(@HeaderParam("Accept-Language") @ApiParam(value = "language") String language, @PathParam("templateUID") @ApiParam(value = "templateUID", required = true) String templateUID) {
    Locale locale = LocaleUtil.getLocale(language);
    RuleTemplate template = templateRegistry.get(templateUID, locale);
    if (template != null) {
        return Response.ok(RuleTemplateDTOMapper.map(template)).build();
    } else {
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : Locale(java.util.Locale) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class RuleTemplateRegistry method getByTag.

@Override
public Collection<RuleTemplate> getByTag(String tag, Locale locale) {
    Collection<RuleTemplate> result = new ArrayList<RuleTemplate>(20);
    for (Provider<RuleTemplate> provider : elementMap.keySet()) {
        for (RuleTemplate resultTemplate : elementMap.get(provider)) {
            Collection<String> tags = resultTemplate.getTags();
            RuleTemplate t = locale == null ? resultTemplate : ((RuleTemplateProvider) provider).getTemplate(resultTemplate.getUID(), locale);
            if (tag == null) {
                result.add(t);
            } else if (tags != null && tags.contains(tag)) {
                result.add(t);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate)

Example 9 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class TemplateResourceBundleProvider method getPerLocale.

/**
 * This method is used to localize the {@link Template}s.
 *
 * @param element is the {@link Template} that must be localized.
 * @param locale represents a specific geographical, political, or cultural region.
 * @return the localized {@link Template}.
 */
private RuleTemplate getPerLocale(RuleTemplate defTemplate, Locale locale) {
    if (locale == null || defTemplate == null || i18nProvider == null) {
        return defTemplate;
    }
    String uid = defTemplate.getUID();
    Bundle bundle = getBundle(uid);
    if (defTemplate instanceof RuleTemplate) {
        String llabel = RuleTemplateI18nUtil.getLocalizedRuleTemplateLabel(i18nProvider, bundle, uid, defTemplate.getLabel(), locale);
        String ldescription = RuleTemplateI18nUtil.getLocalizedRuleTemplateDescription(i18nProvider, bundle, uid, defTemplate.getDescription(), locale);
        List<ConfigDescriptionParameter> lconfigDescriptions = getLocalizedConfigurationDescription(i18nProvider, defTemplate.getConfigurationDescriptions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Action> lactions = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getActions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Condition> lconditions = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getConditions(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        List<Trigger> ltriggers = ModuleI18nUtil.getLocalizedModules(i18nProvider, defTemplate.getTriggers(), bundle, uid, RuleTemplateI18nUtil.RULE_TEMPLATE, locale);
        return new RuleTemplate(uid, llabel, ldescription, defTemplate.getTags(), ltriggers, lconditions, lactions, lconfigDescriptions, defTemplate.getVisibility());
    }
    return null;
}
Also used : Condition(org.eclipse.smarthome.automation.Condition) Action(org.eclipse.smarthome.automation.Action) Trigger(org.eclipse.smarthome.automation.Trigger) Bundle(org.osgi.framework.Bundle) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) ConfigDescriptionParameter(org.eclipse.smarthome.config.core.ConfigDescriptionParameter)

Example 10 with RuleTemplate

use of org.eclipse.smarthome.automation.template.RuleTemplate in project smarthome by eclipse.

the class MarketplaceRuleTemplateProvider method addTemplateAsJSON.

/**
 * This adds a new rule template to the persistent storage.
 *
 * @param uid the UID to be used for the template
 * @param json the template content as a json string
 *
 * @throws ParsingException if the content cannot be parsed correctly
 */
public void addTemplateAsJSON(String uid, String json) throws ParsingException {
    try (InputStreamReader isr = new InputStreamReader(IOUtils.toInputStream(json))) {
        Set<RuleTemplate> templates = parser.parse(isr);
        if (templates.size() != 1) {
            throw new IllegalArgumentException("JSON must contain exactly one template!");
        } else {
            RuleTemplate entry = templates.iterator().next();
            RuleTemplate template = new RuleTemplate(uid, entry.getLabel(), entry.getDescription(), entry.getTags(), entry.getTriggers(), entry.getConditions(), entry.getActions(), entry.getConfigurationDescriptions(), entry.getVisibility());
            add(template);
        }
    } catch (IOException e) {
        logger.error("Cannot close input stream.", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) IOException(java.io.IOException)

Aggregations

RuleTemplate (org.eclipse.smarthome.automation.template.RuleTemplate)10 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)2 ParsingException (org.eclipse.smarthome.automation.parser.ParsingException)2 ParsingNestedException (org.eclipse.smarthome.automation.parser.ParsingNestedException)2 Template (org.eclipse.smarthome.automation.template.Template)2 JsonReader (com.google.gson.stream.JsonReader)1 JsonToken (com.google.gson.stream.JsonToken)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 InputStreamReader (java.io.InputStreamReader)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Locale (java.util.Locale)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Action (org.eclipse.smarthome.automation.Action)1 Condition (org.eclipse.smarthome.automation.Condition)1