37 changed files with 834 additions and 235 deletions
@ -1,42 +0,0 @@ |
|||||
-- |
|
||||
-- Copyright © 2016-2020 The Thingsboard Authors |
|
||||
-- |
|
||||
-- Licensed under the Apache License, Version 2.0 (the "License"); |
|
||||
-- you may not use this file except in compliance with the License. |
|
||||
-- You may obtain a copy of the License at |
|
||||
-- |
|
||||
-- http://www.apache.org/licenses/LICENSE-2.0 |
|
||||
-- |
|
||||
-- Unless required by applicable law or agreed to in writing, software |
|
||||
-- distributed under the License is distributed on an "AS IS" BASIS, |
|
||||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
||||
-- See the License for the specific language governing permissions and |
|
||||
-- limitations under the License. |
|
||||
-- |
|
||||
|
|
||||
ALTER TABLE device ADD COLUMN device_profile_id uuid; |
|
||||
ALTER TABLE tenant ADD COLUMN tenant_profile_id uuid; |
|
||||
|
|
||||
CREATE TABLE IF NOT EXISTS device_profile ( |
|
||||
id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY, |
|
||||
created_time bigint NOT NULL, |
|
||||
name varchar(255), |
|
||||
profile_data varchar, |
|
||||
description varchar, |
|
||||
search_text varchar(255), |
|
||||
default boolean, |
|
||||
tenant_id uuid, |
|
||||
default_rule_chain_id uuid |
|
||||
); |
|
||||
|
|
||||
CREATE TABLE IF NOT EXISTS tenant_profile ( |
|
||||
id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY, |
|
||||
created_time bigint NOT NULL, |
|
||||
name varchar(255), |
|
||||
profile_data varchar, |
|
||||
description varchar, |
|
||||
search_text varchar(255), |
|
||||
default boolean, |
|
||||
isolated_tb_core boolean, |
|
||||
isolated_tb_rule_engine boolean |
|
||||
); |
|
||||
@ -0,0 +1,28 @@ |
|||||
|
-- |
||||
|
-- Copyright © 2016-2020 The Thingsboard Authors |
||||
|
-- |
||||
|
-- Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
-- you may not use this file except in compliance with the License. |
||||
|
-- You may obtain a copy of the License at |
||||
|
-- |
||||
|
-- http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
-- |
||||
|
-- Unless required by applicable law or agreed to in writing, software |
||||
|
-- distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
-- See the License for the specific language governing permissions and |
||||
|
-- limitations under the License. |
||||
|
-- |
||||
|
|
||||
|
DROP PROCEDURE IF EXISTS update_tenant_profiles; |
||||
|
DROP PROCEDURE IF EXISTS update_device_profiles; |
||||
|
|
||||
|
ALTER TABLE tenant ALTER COLUMN tenant_profile_id SET NOT NULL; |
||||
|
ALTER TABLE tenant DROP CONSTRAINT IF EXISTS fk_tenant_profile; |
||||
|
ALTER TABLE tenant ADD CONSTRAINT fk_tenant_profile FOREIGN KEY (tenant_profile_id) REFERENCES tenant_profile(id); |
||||
|
ALTER TABLE tenant DROP COLUMN IF EXISTS isolated_tb_core; |
||||
|
ALTER TABLE tenant DROP COLUMN IF EXISTS isolated_tb_rule_engine; |
||||
|
|
||||
|
ALTER TABLE device ALTER COLUMN device_profile_id SET NOT NULL; |
||||
|
ALTER TABLE device DROP CONSTRAINT IF EXISTS fk_device_profile; |
||||
|
ALTER TABLE device ADD CONSTRAINT fk_device_profile FOREIGN KEY (device_profile_id) REFERENCES device_profile(id); |
||||
@ -0,0 +1,79 @@ |
|||||
|
-- |
||||
|
-- Copyright © 2016-2020 The Thingsboard Authors |
||||
|
-- |
||||
|
-- Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
-- you may not use this file except in compliance with the License. |
||||
|
-- You may obtain a copy of the License at |
||||
|
-- |
||||
|
-- http://www.apache.org/licenses/LICENSE-2.0 |
||||
|
-- |
||||
|
-- Unless required by applicable law or agreed to in writing, software |
||||
|
-- distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
-- See the License for the specific language governing permissions and |
||||
|
-- limitations under the License. |
||||
|
-- |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS device_profile ( |
||||
|
id uuid NOT NULL CONSTRAINT device_profile_pkey PRIMARY KEY, |
||||
|
created_time bigint NOT NULL, |
||||
|
name varchar(255), |
||||
|
type varchar(255), |
||||
|
profile_data varchar, |
||||
|
description varchar, |
||||
|
search_text varchar(255), |
||||
|
is_default boolean, |
||||
|
tenant_id uuid, |
||||
|
default_rule_chain_id uuid, |
||||
|
CONSTRAINT device_profile_name_unq_key UNIQUE (tenant_id, name) |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE IF NOT EXISTS tenant_profile ( |
||||
|
id uuid NOT NULL CONSTRAINT tenant_profile_pkey PRIMARY KEY, |
||||
|
created_time bigint NOT NULL, |
||||
|
name varchar(255), |
||||
|
profile_data varchar, |
||||
|
description varchar, |
||||
|
search_text varchar(255), |
||||
|
is_default boolean, |
||||
|
isolated_tb_core boolean, |
||||
|
isolated_tb_rule_engine boolean, |
||||
|
CONSTRAINT tenant_profile_name_unq_key UNIQUE (name) |
||||
|
); |
||||
|
|
||||
|
CREATE OR REPLACE PROCEDURE update_tenant_profiles() |
||||
|
LANGUAGE plpgsql AS |
||||
|
$$ |
||||
|
BEGIN |
||||
|
UPDATE tenant as t SET tenant_profile_id = p.id |
||||
|
FROM |
||||
|
(SELECT id from tenant_profile WHERE isolated_tb_core = false AND isolated_tb_rule_engine = false) as p |
||||
|
WHERE t.tenant_profile_id IS NULL AND t.isolated_tb_core = false AND t.isolated_tb_rule_engine = false; |
||||
|
|
||||
|
UPDATE tenant as t SET tenant_profile_id = p.id |
||||
|
FROM |
||||
|
(SELECT id from tenant_profile WHERE isolated_tb_core = true AND isolated_tb_rule_engine = false) as p |
||||
|
WHERE t.tenant_profile_id IS NULL AND t.isolated_tb_core = true AND t.isolated_tb_rule_engine = false; |
||||
|
|
||||
|
UPDATE tenant as t SET tenant_profile_id = p.id |
||||
|
FROM |
||||
|
(SELECT id from tenant_profile WHERE isolated_tb_core = false AND isolated_tb_rule_engine = true) as p |
||||
|
WHERE t.tenant_profile_id IS NULL AND t.isolated_tb_core = false AND t.isolated_tb_rule_engine = true; |
||||
|
|
||||
|
UPDATE tenant as t SET tenant_profile_id = p.id |
||||
|
FROM |
||||
|
(SELECT id from tenant_profile WHERE isolated_tb_core = true AND isolated_tb_rule_engine = true) as p |
||||
|
WHERE t.tenant_profile_id IS NULL AND t.isolated_tb_core = true AND t.isolated_tb_rule_engine = true; |
||||
|
END; |
||||
|
$$; |
||||
|
|
||||
|
CREATE OR REPLACE PROCEDURE update_device_profiles() |
||||
|
LANGUAGE plpgsql AS |
||||
|
$$ |
||||
|
BEGIN |
||||
|
UPDATE device as d SET device_profile_id = p.id, device_data = '{"configuration":{"type":"DEFAULT"}}' |
||||
|
FROM |
||||
|
(SELECT id, tenant_id from device_profile WHERE is_default = true) as p |
||||
|
WHERE d.device_profile_id IS NULL AND p.tenant_id = d.tenant_id; |
||||
|
END; |
||||
|
$$; |
||||
@ -0,0 +1,48 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonCreator; |
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import lombok.ToString; |
||||
|
import lombok.Value; |
||||
|
import org.thingsboard.server.common.data.id.EntityId; |
||||
|
import org.thingsboard.server.common.data.id.EntityIdFactory; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Value |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@ToString(callSuper = true) |
||||
|
public class DeviceProfileInfo extends EntityInfo { |
||||
|
|
||||
|
private final DeviceProfileType type; |
||||
|
|
||||
|
@JsonCreator |
||||
|
public DeviceProfileInfo(@JsonProperty("id") EntityId id, |
||||
|
@JsonProperty("name") String name, |
||||
|
@JsonProperty("type") DeviceProfileType type) { |
||||
|
super(id, name); |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public DeviceProfileInfo(UUID uuid, String name, DeviceProfileType type) { |
||||
|
super(EntityIdFactory.getByTypeAndUuid(EntityType.DEVICE_PROFILE, uuid), name); |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,39 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
@Data |
||||
|
public class TenantInfo extends Tenant { |
||||
|
|
||||
|
private String tenantProfileName; |
||||
|
|
||||
|
public TenantInfo() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantInfo(TenantId tenantId) { |
||||
|
super(tenantId); |
||||
|
} |
||||
|
|
||||
|
public TenantInfo(Tenant tenant, String tenantProfileName) { |
||||
|
super(tenant); |
||||
|
this.tenantProfileName = tenantProfileName; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,161 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.model.sql; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.hibernate.annotations.Type; |
||||
|
import org.hibernate.annotations.TypeDef; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
import org.thingsboard.server.common.data.id.CustomerId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.id.TenantProfileId; |
||||
|
import org.thingsboard.server.dao.model.BaseSqlEntity; |
||||
|
import org.thingsboard.server.dao.model.ModelConstants; |
||||
|
import org.thingsboard.server.dao.model.SearchTextEntity; |
||||
|
import org.thingsboard.server.dao.util.mapping.JsonStringType; |
||||
|
|
||||
|
import javax.persistence.Column; |
||||
|
import javax.persistence.Entity; |
||||
|
import javax.persistence.MappedSuperclass; |
||||
|
import javax.persistence.Table; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
@TypeDef(name = "json", typeClass = JsonStringType.class) |
||||
|
@MappedSuperclass |
||||
|
public abstract class AbstractTenantEntity<T extends Tenant> extends BaseSqlEntity<T> implements SearchTextEntity<T> { |
||||
|
|
||||
|
@Column(name = ModelConstants.TENANT_TITLE_PROPERTY) |
||||
|
private String title; |
||||
|
|
||||
|
@Column(name = ModelConstants.SEARCH_TEXT_PROPERTY) |
||||
|
private String searchText; |
||||
|
|
||||
|
@Column(name = ModelConstants.TENANT_REGION_PROPERTY) |
||||
|
private String region; |
||||
|
|
||||
|
@Column(name = ModelConstants.COUNTRY_PROPERTY) |
||||
|
private String country; |
||||
|
|
||||
|
@Column(name = ModelConstants.STATE_PROPERTY) |
||||
|
private String state; |
||||
|
|
||||
|
@Column(name = ModelConstants.CITY_PROPERTY) |
||||
|
private String city; |
||||
|
|
||||
|
@Column(name = ModelConstants.ADDRESS_PROPERTY) |
||||
|
private String address; |
||||
|
|
||||
|
@Column(name = ModelConstants.ADDRESS2_PROPERTY) |
||||
|
private String address2; |
||||
|
|
||||
|
@Column(name = ModelConstants.ZIP_PROPERTY) |
||||
|
private String zip; |
||||
|
|
||||
|
@Column(name = ModelConstants.PHONE_PROPERTY) |
||||
|
private String phone; |
||||
|
|
||||
|
@Column(name = ModelConstants.EMAIL_PROPERTY) |
||||
|
private String email; |
||||
|
|
||||
|
@Type(type = "json") |
||||
|
@Column(name = ModelConstants.TENANT_ADDITIONAL_INFO_PROPERTY) |
||||
|
private JsonNode additionalInfo; |
||||
|
|
||||
|
@Column(name = ModelConstants.TENANT_TENANT_PROFILE_ID_PROPERTY, columnDefinition = "uuid") |
||||
|
private UUID tenantProfileId; |
||||
|
|
||||
|
public AbstractTenantEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public AbstractTenantEntity(Tenant tenant) { |
||||
|
if (tenant.getId() != null) { |
||||
|
this.setUuid(tenant.getId().getId()); |
||||
|
} |
||||
|
this.setCreatedTime(tenant.getCreatedTime()); |
||||
|
this.title = tenant.getTitle(); |
||||
|
this.region = tenant.getRegion(); |
||||
|
this.country = tenant.getCountry(); |
||||
|
this.state = tenant.getState(); |
||||
|
this.city = tenant.getCity(); |
||||
|
this.address = tenant.getAddress(); |
||||
|
this.address2 = tenant.getAddress2(); |
||||
|
this.zip = tenant.getZip(); |
||||
|
this.phone = tenant.getPhone(); |
||||
|
this.email = tenant.getEmail(); |
||||
|
this.additionalInfo = tenant.getAdditionalInfo(); |
||||
|
if (tenant.getTenantProfileId() != null) { |
||||
|
this.tenantProfileId = tenant.getTenantProfileId().getId(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public AbstractTenantEntity(TenantEntity tenantEntity) { |
||||
|
this.setId(tenantEntity.getId()); |
||||
|
this.setCreatedTime(tenantEntity.getCreatedTime()); |
||||
|
this.title = tenantEntity.getTitle(); |
||||
|
this.region = tenantEntity.getRegion(); |
||||
|
this.country = tenantEntity.getCountry(); |
||||
|
this.state = tenantEntity.getState(); |
||||
|
this.city = tenantEntity.getCity(); |
||||
|
this.address = tenantEntity.getAddress(); |
||||
|
this.address2 = tenantEntity.getAddress2(); |
||||
|
this.zip = tenantEntity.getZip(); |
||||
|
this.phone = tenantEntity.getPhone(); |
||||
|
this.email = tenantEntity.getEmail(); |
||||
|
this.additionalInfo = tenantEntity.getAdditionalInfo(); |
||||
|
this.tenantProfileId = tenantEntity.getTenantProfileId(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String getSearchTextSource() { |
||||
|
return title; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setSearchText(String searchText) { |
||||
|
this.searchText = searchText; |
||||
|
} |
||||
|
|
||||
|
public String getSearchText() { |
||||
|
return searchText; |
||||
|
} |
||||
|
|
||||
|
protected Tenant toTenant() { |
||||
|
Tenant tenant = new Tenant(new TenantId(this.getUuid())); |
||||
|
tenant.setCreatedTime(createdTime); |
||||
|
tenant.setTitle(title); |
||||
|
tenant.setRegion(region); |
||||
|
tenant.setCountry(country); |
||||
|
tenant.setState(state); |
||||
|
tenant.setCity(city); |
||||
|
tenant.setAddress(address); |
||||
|
tenant.setAddress2(address2); |
||||
|
tenant.setZip(zip); |
||||
|
tenant.setPhone(phone); |
||||
|
tenant.setEmail(email); |
||||
|
tenant.setAdditionalInfo(additionalInfo); |
||||
|
if (tenantProfileId != null) { |
||||
|
tenant.setTenantProfileId(new TenantProfileId(tenantProfileId)); |
||||
|
} |
||||
|
return tenant; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.model.sql; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.TenantInfo; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class TenantInfoEntity extends AbstractTenantEntity<TenantInfo> { |
||||
|
|
||||
|
public static final Map<String,String> tenantInfoColumnMap = new HashMap<>(); |
||||
|
static { |
||||
|
tenantInfoColumnMap.put("tenantProfileName", "p.name"); |
||||
|
} |
||||
|
|
||||
|
private String tenantProfileName; |
||||
|
|
||||
|
public TenantInfoEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantInfoEntity(TenantEntity tenantEntity, String tenantProfileName) { |
||||
|
super(tenantEntity); |
||||
|
this.tenantProfileName = tenantProfileName; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public TenantInfo toData() { |
||||
|
return new TenantInfo(super.toTenant(), this.tenantProfileName); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue