use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.
the class RulesetResourceImpl method getAssetRulesets.
@Override
public AssetRuleset[] getAssetRulesets(@BeanParam RequestParams requestParams, String assetId) {
Asset asset = assetStorageService.find(assetId, false);
if (asset == null)
return new AssetRuleset[0];
if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
List<AssetRuleset> result = rulesetStorageService.findAssetRulesets(asset.getRealmId(), assetId);
return result.toArray(new AssetRuleset[result.size()]);
}
use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.
the class RulesetResourceImpl method deleteAssetRuleset.
@Override
public void deleteAssetRuleset(@BeanParam RequestParams requestParams, Long id) {
AssetRuleset ruleset = rulesetStorageService.findById(AssetRuleset.class, id);
if (ruleset == null) {
return;
}
Asset asset = assetStorageService.find(ruleset.getAssetId(), false);
if (asset == null) {
throw new WebApplicationException(NOT_FOUND);
}
if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
rulesetStorageService.delete(AssetRuleset.class, id);
}
use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.
the class RulesetResourceImpl method getAssetRuleset.
@Override
public AssetRuleset getAssetRuleset(@BeanParam RequestParams requestParams, Long id) {
AssetRuleset ruleset = rulesetStorageService.findById(AssetRuleset.class, id);
if (ruleset == null) {
throw new WebApplicationException(NOT_FOUND);
}
Asset asset = assetStorageService.find(ruleset.getAssetId(), false);
if (asset == null) {
throw new WebApplicationException(NOT_FOUND);
}
if (!isRealmAccessibleByUser(asset.getTenantRealm())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), asset.getId())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
return ruleset;
}
use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.
the class RulesDemoSetup method onStart.
@Override
public void onStart() throws Exception {
KeycloakDemoSetup keycloakDemoSetup = setupService.getTaskOfType(KeycloakDemoSetup.class);
ManagerDemoSetup managerDemoSetup = setupService.getTaskOfType(ManagerDemoSetup.class);
LOG.info("Importing demo rulesets");
// Apartment 1
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidencePresenceDetection.groovy")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - Presence Detection with motion and CO2 sensors", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceVacationMode.groovy")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - Vacation Mode", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceAutoVentilation.groovy")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - Auto Ventilation", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceNotifyAlarmTrigger.groovy")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - Notify Alarm Trigger", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceSmartSwitch.groovy")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - Smart Start Switch", managerDemoSetup.apartment1Id, rules, Ruleset.Lang.GROOVY);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
// Apartment 2
try (InputStream inputStream = RulesDemoSetup.class.getResourceAsStream("/demo/rules/DemoResidenceAllLightsOff.js")) {
String rules = IOUtils.toString(inputStream, Charset.forName("utf-8"));
Ruleset ruleset = new AssetRuleset("Demo Residence - All Lights Off", managerDemoSetup.apartment2Id, rules, Ruleset.Lang.JAVASCRIPT);
apartmentActionsRulesetId = rulesetStorageService.merge(ruleset).getId();
}
}
use of org.openremote.model.rules.AssetRuleset in project openremote by openremote.
the class RulesetStorageService method mapResultTuple.
@SuppressWarnings("unchecked")
protected <T extends Ruleset> T mapResultTuple(Class<T> rulesetType, RulesetQuery query, ResultSet rs) throws SQLException {
T ruleset;
if (rulesetType == GlobalRuleset.class) {
ruleset = (T) new GlobalRuleset();
} else if (rulesetType == TenantRuleset.class) {
TenantRuleset tenantRuleset = new TenantRuleset();
tenantRuleset.setRealm(rs.getString("REALM"));
tenantRuleset.setAccessPublicRead(rs.getBoolean("ACCESS_PUBLIC_READ"));
ruleset = (T) tenantRuleset;
} else if (rulesetType == AssetRuleset.class) {
AssetRuleset assetRuleset = new AssetRuleset();
assetRuleset.setAssetId(rs.getString("ASSET_ID"));
assetRuleset.setRealm(rs.getString("REALM"));
assetRuleset.setAccessPublicRead(rs.getBoolean("ACCESS_PUBLIC_READ"));
ruleset = (T) assetRuleset;
} else {
throw new UnsupportedOperationException("Ruleset type not supported: " + rulesetType);
}
ruleset.setName(rs.getString("NAME"));
ruleset.setId(rs.getLong("ID"));
ruleset.setVersion(rs.getLong("VERSION"));
ruleset.setLang(Ruleset.Lang.valueOf(rs.getString("RULES_LANG")));
ruleset.setEnabled(rs.getBoolean("ENABLED"));
ruleset.setLastModified(rs.getTimestamp("LAST_MODIFIED"));
ruleset.setCreatedOn(rs.getTimestamp("CREATED_ON"));
if (rs.getString("META") != null) {
ruleset.setMeta(ValueUtil.parse(rs.getString("META"), MetaMap.class).orElse(null));
}
if (query.fullyPopulate) {
ruleset.setRules(rs.getString("RULES"));
}
return ruleset;
}
Aggregations