Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Response get(@Context SecurityContext securityContext, @Context Request r
.map(rule -> EnrichedRuleDTOMapper.map(rule, ruleManager, managedRuleProvider)); // map matching rules
if (summary != null && summary) {
rules = dtoMapper.limitToFields(rules,
"uid,templateUID,templateState,name,visibility,description,status,tags,editable");
"uid,templateUID,templateState,name,visibility,description,status,tags,configuration,editable");
}

return Response.ok(new Stream2JSONInputStream(rules)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.dto.RuleTemplateDTO;
import org.openhab.core.automation.dto.RuleTemplateDTOMapper;
import org.openhab.core.automation.template.RuleTemplate;
import org.openhab.core.automation.template.Template;
import org.openhab.core.automation.template.TemplateRegistry;
import org.openhab.core.io.rest.LocaleService;
import org.openhab.core.io.rest.RESTConstants;
Expand Down Expand Up @@ -73,20 +71,20 @@ public class TemplateResource implements RESTResource {
public static final String PATH_TEMPLATES = "templates";

private final LocaleService localeService;
private final TemplateRegistry<@NonNull RuleTemplate> templateRegistry;
private final TemplateRegistry<RuleTemplate> templateRegistry;

@Activate
public TemplateResource( //
final @Reference LocaleService localeService,
final @Reference TemplateRegistry<@NonNull RuleTemplate> templateRegistry) {
final @Reference TemplateRegistry<RuleTemplate> templateRegistry) {
this.localeService = localeService;
this.templateRegistry = templateRegistry;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getTemplates", summary = "Get all available templates.", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Template.class)))) })
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = RuleTemplateDTO.class)))) })
public Response getAll(
@HeaderParam("Accept-Language") @Parameter(description = "language") @Nullable String language) {
Locale locale = localeService.getLocale(language);
Expand All @@ -99,7 +97,7 @@ public Response getAll(
@Path("/{templateUID}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getTemplateById", summary = "Gets a template corresponding to the given UID.", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = Template.class))),
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = RuleTemplateDTO.class))),
@ApiResponse(responseCode = "404", description = "Template corresponding to the given UID does not found.") })
public Response getByUID(
@HeaderParam("Accept-Language") @Parameter(description = "language") @Nullable String language,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@ public interface Module {
* @return the current configuration values of the {@link Module}.
*/
Configuration getConfiguration();

/**
* The "magic key" used for {@link Module}s that depend on shared context that isn't part of the {@link Module}
* object itself, for example the {@link Action}s of DSL rules with definitions outside the {@code rule} section.
* The information is stored in the {@link Module}'s {@link Configuration}. The payload is a {@link Boolean}.
*/
static String SHARED_CONTEXT = "sharedContext";
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ default TemplateState getTemplateState() {
return null;
}

/**
* The "magic key" used for the source code string itself when storing {@link Rule}'s source code in its
* {@link Configuration}. A {@link Rule} can only have a source code if it is created by a script. The payload
* is a string containing the whole source code.
*/
static String SOURCE = "source";

/**
* The "magic key" used for the source code type when storing {@link Rule}'s source code in its
* {@link Configuration}. A {@link Rule} can only have a source code if it is created by a script. The payload
* is a string containing OH's "quasi MIME-type" for the scripting language of the source code.
*/
static String SOURCE_TYPE = "sourceType";

/**
* This enum represent the different states a rule can have in respect to rule templates.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.converter;

import java.util.Collection;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Rule;
import org.openhab.core.converter.ObjectParser;

/**
* {@link RuleParser} is the interface to implement by any file parser for {@link Rule} object.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface RuleParser extends ObjectParser<Rule> {

/**
* Parse the provided {@code syntax} string without impacting the rule registry.
*
* @param syntax the syntax in format.
* @param errors the {@link List} to use to report errors.
* @param warnings the {@link List} to be used to report warnings.
* @return The model name used for parsing if the parsing succeeded without errors; {@code null} otherwise.
*/
@Override
@Nullable
String startParsingFormat(String syntax, List<String> errors, List<String> warnings);

/**
* Get the {@link Rule} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link Rule}s.
*/
@Override
Collection<Rule> getParsedObjects(String modelName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.converter;

import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Rule;
import org.openhab.core.converter.ObjectSerializer;
import org.openhab.core.converter.SerializabilityResult;
import org.openhab.core.io.dto.SerializationException;

/**
* {@link RuleSerializer} is the interface to implement by any file generator for {@link Rule} object.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface RuleSerializer extends ObjectSerializer<Rule> {

/**
* Checks if the specified rules are serializable with this {@link RuleSerializer}. Returned results are in the same
* order as the specified rules.
*
* @param rules the {@link List} of {@link Rule}s to check.
* @return The resulting {@link List} of {@link SerializabilityResult}s.
*/
List<SerializabilityResult<String>> checkSerializability(Collection<Rule> rules);

/**
* Specify the {@link List} of {@link Rule}s to be serialized and associate them with an identifier.
*
* @param id the identifier of the {@link Rule} format generation.
* @param rules the {@link List} of {@link Rule}s to serialize.
* @param option the option that determines how to serialize the {@link Rule}s.
* @throws SerializationException If one or more of the rules can't be serialized.
*/
void setRulesToBeSerialized(String id, List<Rule> rules, RuleSerializationOption option)
throws SerializationException;

/**
* An enum representing the different rule serialization options.
*/
public enum RuleSerializationOption {

/** Empty collections and normally irrelevant fields are hidden */
NORMAL("Normal"),

/** Everything is serialized, including empty collections */
INCLUDE_ALL("Include all"),

/** Only the fields required in a rule stub to be used with a template is serialized */
STUB_ONLY("Stub only"),

/** Template information is stripped, otherwise like {@link #NORMAL} */
STRIP_TEMPLATE("Strip template");

private final String friendlyName;

private RuleSerializationOption(String friendlyName) {
this.friendlyName = friendlyName;
}

@Override
public String toString() {
return friendlyName;
}

public static @Nullable RuleSerializationOption fromString(@Nullable String id) {
if (id == null || id.isBlank()) {
return null;
}
String upId = id.toUpperCase(Locale.ROOT).trim();
for (RuleSerializationOption option : values()) {
if (upId.equals(option.name()) || upId.equalsIgnoreCase(option.friendlyName)
|| upId.equalsIgnoreCase(option.friendlyName.replace(" ", ""))
|| upId.equalsIgnoreCase(option.friendlyName.replace(" ", "-"))) {
return option;
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.converter;

import java.util.Collection;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.template.RuleTemplate;
import org.openhab.core.converter.ObjectParser;

/**
* {@link RuleTemplateParser} is the interface to implement by any file parser for {@link RuleTemplate} object.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface RuleTemplateParser extends ObjectParser<RuleTemplate> {

/**
* Parse the provided {@code syntax} string without impacting the rule template registry.
*
* @param syntax the syntax in format.
* @param errors the {@link List} to use to report errors.
* @param warnings the {@link List} to be used to report warnings.
* @return The model name used for parsing if the parsing succeeded without errors; {@code null} otherwise.
*/
@Override
@Nullable
String startParsingFormat(String syntax, List<String> errors, List<String> warnings);

/**
* Get the {@link RuleTemplate} objects found when parsing the format.
*
* @param modelName the model name used when parsing.
* @return The {@link Collection} of {@link RuleTemplate}s.
*/
@Override
Collection<RuleTemplate> getParsedObjects(String modelName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2010-2026 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.automation.converter;

import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.converter.RuleSerializer.RuleSerializationOption;
import org.openhab.core.automation.template.RuleTemplate;
import org.openhab.core.converter.ObjectSerializer;
import org.openhab.core.converter.SerializabilityResult;
import org.openhab.core.io.dto.SerializationException;

/**
* {@link RuleTemplateSerializer} is the interface to implement by any file generator for {@link RuleTemplate} object.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public interface RuleTemplateSerializer extends ObjectSerializer<RuleTemplate> {

/**
* Checks if the specified rule templates are serializable with this {@link RuleTemplateSerializer}. Returned
* results are in the same order as the specified rule templates.
*
* @param templates the {@link List} of {@link RuleTemplate}s to check.
* @return The resulting {@link List} of {@link SerializabilityResult}s.
*/
List<SerializabilityResult<String>> checkSerializability(Collection<RuleTemplate> templates);

/**
* Specify the {@link List} of {@link RuleTemplate}s to be serialized and associate them with an identifier.
*
* @param id the identifier of the {@link RuleTemplate} format generation.
* @param templates the {@link List} of {@link RuleTemplate}s to serialize.
* @param option the option that determines how to serialize the {@link RuleTemplate}s.
* @throws SerializationException If one or more of the rule templates can't be serialized.
*/
void setTemplatesToBeSerialized(String id, List<RuleTemplate> templates, RuleTemplateSerializationOption option)
throws SerializationException;

/**
* An enum representing the different rule template serialization options.
*/
public enum RuleTemplateSerializationOption {

/** Empty collections and normally irrelevant fields are hidden */
NORMAL("Normal"),

/** Everything is serialized, including empty collections */
INCLUDE_ALL("Include all");

private final String friendlyName;

private RuleTemplateSerializationOption(String friendlyName) {
this.friendlyName = friendlyName;
}

public RuleSerializationOption toRuleSerializationOption() {
switch (this) {
case INCLUDE_ALL:
return RuleSerializationOption.INCLUDE_ALL;
case NORMAL:
return RuleSerializationOption.NORMAL;
default:
throw new UnsupportedOperationException(
"Missing toRuleSerializationOption() implementation for " + name());
}
}

@Override
public String toString() {
return friendlyName;
}

public static @Nullable RuleTemplateSerializationOption fromString(@Nullable String id) {
if (id == null || id.isBlank()) {
return null;
}
String upId = id.toUpperCase(Locale.ROOT).trim();
for (RuleTemplateSerializationOption option : values()) {
if (upId.equals(option.name()) || upId.equalsIgnoreCase(option.friendlyName)
|| upId.equalsIgnoreCase(option.friendlyName.replace(" ", ""))
|| upId.equalsIgnoreCase(option.friendlyName.replace(" ", "-"))) {
return option;
}
}
return null;
}
}
}
Loading
Loading