Search in sources :

Example 1 with Template

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

the class TemplateResource method getAll.

@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available templates.", response = Template.class, responseContainer = "Collection")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = Template.class, responseContainer = "Collection") })
public Response getAll(@HeaderParam("Accept-Language") @ApiParam(value = "language") String language) {
    Locale locale = LocaleUtil.getLocale(language);
    Collection<RuleTemplateDTO> result = templateRegistry.getAll(locale).stream().map(template -> RuleTemplateDTOMapper.map(template)).collect(Collectors.toList());
    return Response.ok(result).build();
}
Also used : Locale(java.util.Locale) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) ApiResponses(io.swagger.annotations.ApiResponses) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) RuleTemplateDTO(org.eclipse.smarthome.automation.dto.RuleTemplateDTO) Locale(java.util.Locale) RuleTemplateDTOMapper(org.eclipse.smarthome.automation.dto.RuleTemplateDTOMapper) HeaderParam(javax.ws.rs.HeaderParam) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) RESTResource(org.eclipse.smarthome.io.rest.RESTResource) Context(javax.ws.rs.core.Context) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template) TemplateRegistry(org.eclipse.smarthome.automation.template.TemplateRegistry) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) UriInfo(javax.ws.rs.core.UriInfo) LocaleUtil(org.eclipse.smarthome.io.rest.LocaleUtil) RuleTemplateDTO(org.eclipse.smarthome.automation.dto.RuleTemplateDTO) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with Template

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

the class TemplateGSONParser method parse.

@Override
public Set<Template> parse(InputStreamReader reader) throws ParsingException {
    JsonReader jr = new JsonReader(reader);
    try {
        if (jr.hasNext()) {
            JsonToken token = jr.peek();
            Set<Template> templates = new HashSet<>();
            if (JsonToken.BEGIN_ARRAY.equals(token)) {
                templates.addAll(gson.fromJson(jr, new TypeToken<List<RuleTemplate>>() {
                }.getType()));
            } else {
                Template template = gson.fromJson(jr, RuleTemplate.class);
                templates.add(template);
            }
            return templates;
        }
    } catch (Exception e) {
        throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e));
    } finally {
        try {
            jr.close();
        } catch (IOException e) {
        }
    }
    return Collections.emptySet();
}
Also used : ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) List(java.util.List) IOException(java.io.IOException) ParsingException(org.eclipse.smarthome.automation.parser.ParsingException) ParsingNestedException(org.eclipse.smarthome.automation.parser.ParsingNestedException) IOException(java.io.IOException) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template) HashSet(java.util.HashSet)

Example 3 with Template

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

the class AutomationCommandList method listTemplates.

/**
 * This method is responsible for execution of command {@link AutomationCommands#LIST_TEMPLATES}.
 *
 * @return a string representing understandable for the user message containing information on the outcome of the
 *         command {@link AutomationCommands#LIST_TEMPLATES}.
 */
private String listTemplates() {
    Collection<RuleTemplate> collection = autoCommands.getTemplates(locale);
    Map<String, Template> templates = new Hashtable<String, Template>();
    Map<String, String> listTemplates = null;
    if (collection != null && !collection.isEmpty()) {
        addCollection(collection, templates);
        String[] uids = new String[templates.size()];
        Utils.quickSort(templates.keySet().toArray(uids), 0, templates.size());
        listTemplates = Utils.putInHastable(uids);
    }
    if (listTemplates != null && !listTemplates.isEmpty()) {
        if (id != null) {
            collection = getTemplateByFilter(listTemplates);
            if (collection.size() == 1) {
                Template t = (Template) collection.toArray()[0];
                if (t != null) {
                    return Printer.printTemplate(t);
                } else {
                    return String.format("Nonexistent ID: %s", id);
                }
            } else if (collection.isEmpty()) {
                return String.format("Nonexistent ID: %s", id);
            } else {
                if (!templates.isEmpty()) {
                    templates.clear();
                }
                addCollection(collection, templates);
                listTemplates = Utils.filterList(templates, listTemplates);
            }
        }
        if (listTemplates != null && !listTemplates.isEmpty()) {
            return Printer.printTemplates(listTemplates);
        }
    }
    return "There are no Templates available!";
}
Also used : Hashtable(java.util.Hashtable) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) RuleTemplate(org.eclipse.smarthome.automation.template.RuleTemplate) Template(org.eclipse.smarthome.automation.template.Template)

Aggregations

RuleTemplate (org.eclipse.smarthome.automation.template.RuleTemplate)3 Template (org.eclipse.smarthome.automation.template.Template)3 JsonReader (com.google.gson.stream.JsonReader)1 JsonToken (com.google.gson.stream.JsonToken)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiParam (io.swagger.annotations.ApiParam)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Locale (java.util.Locale)1 Collectors (java.util.stream.Collectors)1 GET (javax.ws.rs.GET)1 HeaderParam (javax.ws.rs.HeaderParam)1 Path (javax.ws.rs.Path)1 PathParam (javax.ws.rs.PathParam)1