Skip to content
Permalink
Browse files

Added meta column(jsonb) to Rulesset

  • Loading branch information...
Miggets7 committed Jul 8, 2019
1 parent aa76a29 commit 682c5bf6bced32aa40e6a3bb8bbbce984415db53
@@ -73,7 +73,7 @@ public void stop(Container container) throws Exception {
public List<GlobalRuleset> findGlobalRulesets(boolean onlyEnabled, Ruleset.Lang language, boolean fullyPopulate) {

String query = "select new org.openremote.model.rules.GlobalRuleset(" +
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang";
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang, rs.meta";

query += fullyPopulate ? ", rs.rules" : ", cast(null as string)";

@@ -111,7 +111,7 @@ public void stop(Container container) throws Exception {
public List<TenantRuleset> findTenantRulesets(String realm, boolean onlyPublic, boolean onlyEnabled, Ruleset.Lang language, boolean fullyPopulate) {

String query = "select new org.openremote.model.rules.TenantRuleset(" +
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang";
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang, rs.meta";

query += fullyPopulate ? ", rs.rules" : ", cast(null as string)";

@@ -166,7 +166,7 @@ public void stop(Container container) throws Exception {
public List<AssetRuleset> findAssetRulesets(String realm, String assetId, boolean onlyPublic, boolean onlyEnabled, Ruleset.Lang language, boolean fullyPopulate) {

String query = "select new org.openremote.model.rules.AssetRuleset(" +
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang";
"rs.id, rs.version, rs.createdOn, rs.lastModified, rs.enabled, rs.name, rs.lang, rs.meta";

query += fullyPopulate ? ", rs.rules" : ", cast(null as string)";

@@ -0,0 +1,17 @@

/*
Adds meta column to rulesets
*/

-- UPDATE GLOBAL_RULESET
ALTER TABLE GLOBAL_RULESET
ADD COLUMN META jsonb;

-- UPDATE ASSET_RULESET
ALTER TABLE ASSET_RULESET
ADD COLUMN META jsonb;


-- UPDATE TENANT_RULESET
ALTER TABLE TENANT_RULESET
ADD COLUMN META jsonb;
@@ -19,6 +19,8 @@
*/
package org.openremote.model.rules;

import org.openremote.model.value.ObjectValue;

import javax.persistence.*;
import java.util.Date;

@@ -50,8 +52,14 @@ public AssetRuleset(String name, Lang lang, String rules, String assetId, boolea
this.accessPublicRead = accessPublicRead;
}

public AssetRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, String rules, String realm, String assetId, boolean accessPublicRead) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang);
public AssetRuleset(String name, Lang lang, ObjectValue meta, String rules, String assetId, boolean accessPublicRead) {
super(name, rules, lang, meta);
this.assetId = assetId;
this.accessPublicRead = accessPublicRead;
}

public AssetRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, ObjectValue meta, String rules, String realm, String assetId, boolean accessPublicRead) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang, meta);
this.assetId = assetId;
this.realm = realm;
this.accessPublicRead = accessPublicRead;
@@ -19,6 +19,8 @@
*/
package org.openremote.model.rules;

import org.openremote.model.value.ObjectValue;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
@@ -35,14 +37,18 @@
public GlobalRuleset() {
}

public GlobalRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, String rules) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang);
public GlobalRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, ObjectValue meta, String rules) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang, meta);
}

public GlobalRuleset(String name, Lang lang, String rules) {
super(name, rules, lang);
}

public GlobalRuleset(String name, Lang lang, String rules, ObjectValue meta) {
super(name, rules, lang, meta);
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
@@ -19,17 +19,17 @@
*/
package org.openremote.model.rules;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.value.ObjectValue;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import java.util.Date;
import java.util.Optional;

import static org.openremote.model.Constants.PERSISTENCE_JSON_OBJECT_TYPE;
import static org.openremote.model.Constants.PERSISTENCE_SEQUENCE_ID_GENERATOR;

/**
@@ -185,6 +185,10 @@ public String getEmptyRulesExample() {
@Column(name = "RULES_LANG", nullable = false)
protected Lang lang = Lang.GROOVY;

@Column(name = "META", columnDefinition = "jsonb")
@org.hibernate.annotations.Type(type = PERSISTENCE_JSON_OBJECT_TYPE)
protected ObjectValue meta;

@Transient
protected RulesetStatus status;

@@ -195,10 +199,10 @@ protected Ruleset() {
}

public Ruleset(long id, long version, Date createdOn, Date lastModified, String name, boolean enabled, Lang lang) {
this(id, version, createdOn, lastModified, name, enabled, null, lang);
this(id, version, createdOn, lastModified, name, enabled, null, lang, null);
}

public Ruleset(long id, long version, Date createdOn, Date lastModified, String name, boolean enabled, String rules, Lang lang) {
public Ruleset(long id, long version, Date createdOn, Date lastModified, String name, boolean enabled, String rules, Lang lang, ObjectValue meta) {
this.id = id;
this.version = version;
this.createdOn = createdOn;
@@ -207,6 +211,12 @@ public Ruleset(long id, long version, Date createdOn, Date lastModified, String
this.enabled = enabled;
this.rules = rules;
this.lang = lang;
this.meta = meta;
}

public Ruleset(String name, String rules, Lang lang, ObjectValue meta) {
this(name, rules, lang);
this.meta = meta;
}

public Ruleset(String name, String rules, Lang lang) {
@@ -277,6 +287,14 @@ public void setLang(Lang lang) {
this.lang = lang;
}

public ObjectValue getMeta() {
return meta;
}

public void setMeta(ObjectValue meta) {
this.meta = meta;
}

public RulesetStatus getStatus() {
return status;
}
@@ -19,6 +19,8 @@
*/
package org.openremote.model.rules;

import org.openremote.model.value.ObjectValue;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@@ -49,8 +51,14 @@ public TenantRuleset(String name, Lang lang, String rules, String realm, boolean
this.accessPublicRead = accessPublicRead;
}

public TenantRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, String rules, String realm, boolean accessPublicRead) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang);
public TenantRuleset(String name, Lang lang, ObjectValue meta, String rules, String realm, boolean accessPublicRead) {
super(name, rules, lang, meta);
this.realm = realm;
this.accessPublicRead = accessPublicRead;
}

public TenantRuleset(long id, long version, Date createdOn, Date lastModified, boolean enabled, String name, Lang lang, ObjectValue meta, String rules, String realm, boolean accessPublicRead) {
super(id, version, createdOn, lastModified, name, enabled, rules, lang, meta);
this.realm = realm;
this.accessPublicRead = accessPublicRead;
}
@@ -20,6 +20,7 @@
package org.openremote.model.rules.json;

import org.openremote.model.query.NewAssetQuery;
import org.openremote.model.value.ObjectValue;

/**
* A declarative rule definition a.k.a. JSON rules; consists of:
@@ -68,4 +69,5 @@
public RuleConditionReset reset;
public RuleAction[] onStart;
public RuleAction[] onStop;
public ObjectValue meta;
}
@@ -5,10 +5,14 @@ import org.openremote.manager.rules.RulesService
import org.openremote.manager.rules.RulesetStorageService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
import org.openremote.model.attribute.MetaItem
import org.openremote.model.attribute.MetaItemDescriptor
import org.openremote.model.attribute.MetaItemDescriptorImpl
import org.openremote.model.rules.AssetRuleset
import org.openremote.model.rules.GlobalRuleset
import org.openremote.model.rules.Ruleset
import org.openremote.model.rules.TenantRuleset
import org.openremote.model.value.Values

import static org.openremote.model.rules.RulesetStatus.DEPLOYED
import static org.openremote.model.rules.Ruleset.Lang.GROOVY
@@ -78,7 +82,8 @@ class BasicRulesImport {
apartment1RulesetId = rulesetStorageService.merge(ruleset).id

ruleset = new AssetRuleset(
"Some apartment 2 demo rules", GROOVY, getClass().getResource("/org/openremote/test/rules/BasicMatchAllAssetStates.groovy").text,
"Some apartment 2 demo rules", GROOVY, Values.createObject().put("visible", true),
getClass().getResource("/org/openremote/test/rules/BasicMatchAllAssetStates.groovy").text,
managerDemoSetup.apartment2Id, false
)
apartment2RulesetId = rulesetStorageService.merge(ruleset).id
@@ -9,6 +9,7 @@ import org.openremote.model.rules.AssetRuleset
import org.openremote.model.rules.GlobalRuleset
import org.openremote.model.rules.RulesResource
import org.openremote.model.rules.TenantRuleset
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -86,6 +87,7 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
ruleDefinitions.length == 1
ruleDefinitions[0].name == "Some apartment 2 demo rules"
ruleDefinitions[0].lang == GROOVY
ruleDefinitions[0].meta.getBoolean("visible").orElse(false)

/* ############################################## WRITE ####################################### */

@@ -793,7 +795,7 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
ex.response.status == 403

when: "an asset ruleset is created in the authenticated realm"
def assetRuleset = new AssetRuleset("Test asset definition", GROOVY, "SomeRulesCode", managerDemoSetup.apartment1Id, false)
def assetRuleset = new AssetRuleset("Test asset definition", GROOVY, Values.createObject().put("visible", true), "SomeRulesCode", managerDemoSetup.apartment1Id, false)
rulesetResource.createAssetRuleset(null, assetRuleset)
def rulesetId = rulesetResource.getAssetRulesets(null, managerDemoSetup.apartment1Id, null, false)[1].id
assetRuleset = rulesetResource.getAssetRuleset(null, rulesetId)
@@ -807,6 +809,7 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
assetRuleset.name == "Test asset definition"
assetRuleset.rules == "SomeRulesCode"
assetRuleset.assetId == managerDemoSetup.apartment1Id
assetRuleset.meta.getBoolean("visible").orElse(false)

when: "an asset ruleset is updated"
assetRuleset.name = "Renamed test asset definition"

0 comments on commit 682c5bf

Please sign in to comment.
You can’t perform that action at this time.