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;
}
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();
}
}
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;
}
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;
}
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);
}
}
Aggregations