28 changed files with 878 additions and 53 deletions
@ -0,0 +1,131 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.datastax.driver.core.utils.UUIDs; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import org.hibernate.annotations.Type; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.alarm.Alarm; |
|||
import org.thingsboard.server.common.data.alarm.AlarmId; |
|||
import org.thingsboard.server.common.data.alarm.AlarmSeverity; |
|||
import org.thingsboard.server.common.data.alarm.AlarmStatus; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.model.BaseEntity; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.UUID; |
|||
|
|||
import static org.thingsboard.server.dao.model.ModelConstants.*; |
|||
|
|||
@Data |
|||
@Entity |
|||
@Table(name = ALARM_COLUMN_FAMILY_NAME) |
|||
public final class AlarmEntity implements BaseEntity<Alarm> { |
|||
|
|||
@Transient |
|||
private static final long serialVersionUID = -339979717281685984L; |
|||
|
|||
@Id |
|||
@Column(name = ID_PROPERTY) |
|||
private UUID id; |
|||
|
|||
@Column(name = ALARM_TENANT_ID_PROPERTY) |
|||
private UUID tenantId; |
|||
|
|||
@Column(name = ALARM_ORIGINATOR_ID_PROPERTY) |
|||
private UUID originatorId; |
|||
|
|||
@Column(name = ALARM_ORIGINATOR_TYPE_PROPERTY) |
|||
private EntityType originatorType; |
|||
|
|||
@Column(name = ALARM_TYPE_PROPERTY) |
|||
private String type; |
|||
|
|||
@Column(name = ALARM_SEVERITY_PROPERTY) |
|||
private AlarmSeverity severity; |
|||
|
|||
@Column(name = ALARM_STATUS_PROPERTY) |
|||
private AlarmStatus status; |
|||
|
|||
@Column(name = ALARM_START_TS_PROPERTY) |
|||
private Long startTs; |
|||
|
|||
@Column(name = ALARM_END_TS_PROPERTY) |
|||
private Long endTs; |
|||
|
|||
@Column(name = ALARM_ACK_TS_PROPERTY) |
|||
private Long ackTs; |
|||
|
|||
@Column(name = ALARM_CLEAR_TS_PROPERTY) |
|||
private Long clearTs; |
|||
|
|||
@Type(type = "jsonb") |
|||
@Column(name = ModelConstants.ASSET_ADDITIONAL_INFO_PROPERTY, columnDefinition = "jsonb") |
|||
private JsonNode details; |
|||
|
|||
@Column(name = ALARM_PROPAGATE_PROPERTY) |
|||
private Boolean propagate; |
|||
|
|||
public AlarmEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public AlarmEntity(Alarm alarm) { |
|||
if (alarm.getId() != null) { |
|||
this.id = alarm.getId().getId(); |
|||
} |
|||
if (alarm.getTenantId() != null) { |
|||
this.tenantId = alarm.getTenantId().getId(); |
|||
} |
|||
this.type = alarm.getType(); |
|||
this.originatorId = alarm.getOriginator().getId(); |
|||
this.originatorType = alarm.getOriginator().getEntityType(); |
|||
this.type = alarm.getType(); |
|||
this.severity = alarm.getSeverity(); |
|||
this.status = alarm.getStatus(); |
|||
this.propagate = alarm.isPropagate(); |
|||
this.startTs = alarm.getStartTs(); |
|||
this.endTs = alarm.getEndTs(); |
|||
this.ackTs = alarm.getAckTs(); |
|||
this.clearTs = alarm.getClearTs(); |
|||
this.details = alarm.getDetails(); |
|||
} |
|||
|
|||
@Override |
|||
public Alarm toData() { |
|||
Alarm alarm = new Alarm(new AlarmId(id)); |
|||
alarm.setCreatedTime(UUIDs.unixTimestamp(id)); |
|||
if (tenantId != null) { |
|||
alarm.setTenantId(new TenantId(tenantId)); |
|||
} |
|||
alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId)); |
|||
alarm.setType(type); |
|||
alarm.setSeverity(severity); |
|||
alarm.setStatus(status); |
|||
alarm.setPropagate(propagate); |
|||
alarm.setStartTs(startTs); |
|||
alarm.setEndTs(endTs); |
|||
alarm.setAckTs(ackTs); |
|||
alarm.setClearTs(clearTs); |
|||
alarm.setDetails(details); |
|||
return alarm; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,188 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.datastax.driver.core.utils.UUIDs; |
|||
import com.fasterxml.jackson.databind.JsonNode; |
|||
import lombok.Data; |
|||
import org.hibernate.annotations.Type; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.model.ModelConstants; |
|||
import org.thingsboard.server.dao.model.SearchTextEntity; |
|||
|
|||
import javax.persistence.*; |
|||
import java.util.UUID; |
|||
|
|||
import static org.thingsboard.server.dao.model.ModelConstants.*; |
|||
|
|||
@Data |
|||
@Entity |
|||
@Table(name = ASSET_COLUMN_FAMILY_NAME) |
|||
public final class AssetEntity implements SearchTextEntity<Asset> { |
|||
|
|||
@Transient |
|||
private static final long serialVersionUID = -4089175869616037592L; |
|||
|
|||
@Id |
|||
@Column(name = ID_PROPERTY) |
|||
private UUID id; |
|||
|
|||
@Column(name = ASSET_TENANT_ID_PROPERTY) |
|||
private UUID tenantId; |
|||
|
|||
@Column(name = ASSET_CUSTOMER_ID_PROPERTY) |
|||
private UUID customerId; |
|||
|
|||
@Column(name = ASSET_NAME_PROPERTY) |
|||
private String name; |
|||
|
|||
@Column(name = ASSET_TYPE_PROPERTY) |
|||
private String type; |
|||
|
|||
@Column(name = SEARCH_TEXT_PROPERTY) |
|||
private String searchText; |
|||
|
|||
@Type(type = "jsonb") |
|||
@Column(name = ModelConstants.ASSET_ADDITIONAL_INFO_PROPERTY, columnDefinition = "jsonb") |
|||
private JsonNode additionalInfo; |
|||
|
|||
public AssetEntity() { |
|||
super(); |
|||
} |
|||
|
|||
public AssetEntity(Asset asset) { |
|||
if (asset.getId() != null) { |
|||
this.id = asset.getId().getId(); |
|||
} |
|||
if (asset.getTenantId() != null) { |
|||
this.tenantId = asset.getTenantId().getId(); |
|||
} |
|||
if (asset.getCustomerId() != null) { |
|||
this.customerId = asset.getCustomerId().getId(); |
|||
} |
|||
this.name = asset.getName(); |
|||
this.type = asset.getType(); |
|||
this.additionalInfo = asset.getAdditionalInfo(); |
|||
} |
|||
|
|||
@Override |
|||
public String getSearchTextSource() { |
|||
return name; |
|||
} |
|||
|
|||
@Override |
|||
public void setSearchText(String searchText) { |
|||
this.searchText = searchText; |
|||
} |
|||
|
|||
public String getSearchText() { |
|||
return searchText; |
|||
} |
|||
|
|||
@Override |
|||
public int hashCode() { |
|||
final int prime = 31; |
|||
int result = 1; |
|||
result = prime * result + ((additionalInfo == null) ? 0 : additionalInfo.hashCode()); |
|||
result = prime * result + ((customerId == null) ? 0 : customerId.hashCode()); |
|||
result = prime * result + ((id == null) ? 0 : id.hashCode()); |
|||
result = prime * result + ((name == null) ? 0 : name.hashCode()); |
|||
result = prime * result + ((type == null) ? 0 : type.hashCode()); |
|||
result = prime * result + ((tenantId == null) ? 0 : tenantId.hashCode()); |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object obj) { |
|||
if (this == obj) |
|||
return true; |
|||
if (obj == null) |
|||
return false; |
|||
if (getClass() != obj.getClass()) |
|||
return false; |
|||
AssetEntity other = (AssetEntity) obj; |
|||
if (additionalInfo == null) { |
|||
if (other.additionalInfo != null) |
|||
return false; |
|||
} else if (!additionalInfo.equals(other.additionalInfo)) |
|||
return false; |
|||
if (customerId == null) { |
|||
if (other.customerId != null) |
|||
return false; |
|||
} else if (!customerId.equals(other.customerId)) |
|||
return false; |
|||
if (id == null) { |
|||
if (other.id != null) |
|||
return false; |
|||
} else if (!id.equals(other.id)) |
|||
return false; |
|||
if (name == null) { |
|||
if (other.name != null) |
|||
return false; |
|||
} else if (!name.equals(other.name)) |
|||
return false; |
|||
if (type == null) { |
|||
if (other.type != null) |
|||
return false; |
|||
} else if (!type.equals(other.type)) |
|||
return false; |
|||
if (tenantId == null) { |
|||
if (other.tenantId != null) |
|||
return false; |
|||
} else if (!tenantId.equals(other.tenantId)) |
|||
return false; |
|||
return true; |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
StringBuilder builder = new StringBuilder(); |
|||
builder.append("AssetEntity [id="); |
|||
builder.append(id); |
|||
builder.append(", tenantId="); |
|||
builder.append(tenantId); |
|||
builder.append(", customerId="); |
|||
builder.append(customerId); |
|||
builder.append(", name="); |
|||
builder.append(name); |
|||
builder.append(", type="); |
|||
builder.append(type); |
|||
builder.append(", additionalInfo="); |
|||
builder.append(additionalInfo); |
|||
builder.append("]"); |
|||
return builder.toString(); |
|||
} |
|||
|
|||
@Override |
|||
public Asset toData() { |
|||
Asset asset = new Asset(new AssetId(id)); |
|||
asset.setCreatedTime(UUIDs.unixTimestamp(id)); |
|||
if (tenantId != null) { |
|||
asset.setTenantId(new TenantId(tenantId)); |
|||
} |
|||
if (customerId != null) { |
|||
asset.setCustomerId(new CustomerId(customerId)); |
|||
} |
|||
asset.setName(name); |
|||
asset.setType(type); |
|||
asset.setAdditionalInfo(additionalInfo); |
|||
return asset; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.alarm; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.jpa.repository.Query; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.thingsboard.server.common.data.alarm.Alarm; |
|||
import org.thingsboard.server.dao.model.sql.AlarmEntity; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/21/2017. |
|||
*/ |
|||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
|||
public interface AlarmRepository extends CrudRepository<AlarmEntity, Alarm> { |
|||
|
|||
@Query(nativeQuery = true, value = "SELECT * FROM ALARM WHERE TENANT_ID = ?1 AND ORIGINATOR_ID = ?2 " + |
|||
"AND ?3 = ?3 AND TYPE = ?4 ORDER BY ID DESC LIMIT 1") |
|||
AlarmEntity findLatestByOriginatorAndType(UUID tenantId, UUID originatorId, int entityType, String alarmType); |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.alarm; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.ListeningExecutorService; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.transaction.annotation.Transactional; |
|||
import org.thingsboard.server.common.data.alarm.Alarm; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.DaoUtil; |
|||
import org.thingsboard.server.dao.alarm.AlarmDao; |
|||
import org.thingsboard.server.dao.model.sql.AlarmEntity; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
|||
|
|||
import java.util.concurrent.Executors; |
|||
|
|||
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/19/2017. |
|||
*/ |
|||
@Component |
|||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
|||
public class JpaAlarmDao extends JpaAbstractDao<AlarmEntity, Alarm> implements AlarmDao { |
|||
|
|||
@Autowired |
|||
private AlarmRepository alarmRepository; |
|||
|
|||
@Override |
|||
protected Class getEntityClass() { |
|||
return AlarmEntity.class; |
|||
} |
|||
|
|||
@Override |
|||
protected CrudRepository getCrudRepository() { |
|||
return alarmRepository; |
|||
} |
|||
|
|||
@Override |
|||
@Transactional(propagation = REQUIRES_NEW) |
|||
public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) { |
|||
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); |
|||
ListenableFuture<Alarm> listenableFuture = service.submit(() -> DaoUtil.getData( |
|||
alarmRepository.findLatestByOriginatorAndType(tenantId.getId(), originator.getId(), |
|||
originator.getEntityType().ordinal(), type))); |
|||
return listenableFuture; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.asset; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.jpa.repository.Query; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.dao.model.sql.AssetEntity; |
|||
|
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/21/2017. |
|||
*/ |
|||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
|||
public interface AssetRepository extends CrudRepository<AssetEntity, UUID> { |
|||
//
|
|||
@Query(nativeQuery = true, value = "SELECT * FROM ASSET WHERE TENANT_ID = ?2 " + |
|||
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
|||
"ORDER BY ID LIMIT ?1") |
|||
List<AssetEntity> findByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); |
|||
|
|||
@Query(nativeQuery = true, value = "SELECT * FROM ASSET WHERE TENANT_ID = ?2 " + |
|||
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
|||
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
|||
List<AssetEntity> findByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); |
|||
|
|||
@Query(nativeQuery = true, value = "SELECT * FROM ASSET WHERE TENANT_ID = ?2 " + |
|||
"AND CUSTOMER_ID = ?3 " + |
|||
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?4, '%')) " + |
|||
"ORDER BY ID LIMIT ?1") |
|||
List<AssetEntity> findByTenantIdAndCustomerIdFirstPage(int limit, UUID tenantId, UUID customerId, String textSearch); |
|||
|
|||
@Query(nativeQuery = true, value = "SELECT * FROM ASSET WHERE TENANT_ID = ?2 " + |
|||
"AND CUSTOMER_ID = ?3 " + |
|||
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?4, '%')) " + |
|||
"AND ID > ?5 ORDER BY ID LIMIT ?1") |
|||
List<AssetEntity> findByTenantIdAndCustomerIdNextPage(int limit, UUID tenantId, UUID customerId, String textSearch, UUID idOffset); |
|||
|
|||
List<AssetEntity> findByTenantIdAndIdIn(UUID tenantId, List<UUID> assetIds); |
|||
|
|||
List<AssetEntity> findByTenantIdAndCustomerIdAndIdIn(UUID tenantId, UUID customerId, List<UUID> assetIds); |
|||
|
|||
AssetEntity findByTenantIdAndName(UUID tenantId, String name); |
|||
} |
|||
@ -0,0 +1,98 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.asset; |
|||
|
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import com.google.common.util.concurrent.ListeningExecutorService; |
|||
import com.google.common.util.concurrent.MoreExecutors; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.repository.CrudRepository; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.page.TextPageLink; |
|||
import org.thingsboard.server.dao.DaoUtil; |
|||
import org.thingsboard.server.dao.asset.AssetDao; |
|||
import org.thingsboard.server.dao.model.sql.AssetEntity; |
|||
import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; |
|||
|
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.Executors; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/19/2017. |
|||
*/ |
|||
@Component |
|||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
|||
public class JpaAssetDao extends JpaAbstractSearchTextDao<AssetEntity, Asset> implements AssetDao { |
|||
|
|||
@Autowired |
|||
private AssetRepository assetRepository; |
|||
|
|||
@Override |
|||
protected Class getEntityClass() { |
|||
return AssetEntity.class; |
|||
} |
|||
|
|||
@Override |
|||
protected CrudRepository getCrudRepository() { |
|||
return assetRepository; |
|||
} |
|||
|
|||
@Override |
|||
public List<Asset> findAssetsByTenantId(UUID tenantId, TextPageLink pageLink) { |
|||
if (pageLink.getIdOffset() == null) { |
|||
return DaoUtil.convertDataList(assetRepository.findByTenantIdFirstPage(pageLink.getLimit(), tenantId, |
|||
pageLink.getTextSearch())); |
|||
} else { |
|||
return DaoUtil.convertDataList(assetRepository.findByTenantIdNextPage(pageLink.getLimit(), tenantId, |
|||
pageLink.getTextSearch(), pageLink.getIdOffset())); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<List<Asset>> findAssetsByTenantIdAndIdsAsync(UUID tenantId, List<UUID> assetIds) { |
|||
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); |
|||
return service.submit(() -> |
|||
DaoUtil.convertDataList(assetRepository.findByTenantIdAndIdIn(tenantId, assetIds))); |
|||
} |
|||
|
|||
@Override |
|||
public List<Asset> findAssetsByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) { |
|||
if (pageLink.getIdOffset() == null) { |
|||
return DaoUtil.convertDataList(assetRepository.findByTenantIdAndCustomerIdFirstPage(pageLink.getLimit(), tenantId, |
|||
customerId, pageLink.getTextSearch())); |
|||
} else { |
|||
return DaoUtil.convertDataList(assetRepository.findByTenantIdAndCustomerIdNextPage(pageLink.getLimit(), tenantId, |
|||
customerId, pageLink.getTextSearch(), pageLink.getIdOffset())); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public ListenableFuture<List<Asset>> findAssetsByTenantIdAndCustomerIdAndIdsAsync(UUID tenantId, UUID customerId, List<UUID> assetIds) { |
|||
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); |
|||
return service.submit(() -> |
|||
DaoUtil.convertDataList( assetRepository.findByTenantIdAndCustomerIdAndIdIn(tenantId, customerId, assetIds))); |
|||
} |
|||
|
|||
@Override |
|||
public Optional<Asset> findAssetsByTenantIdAndName(UUID tenantId, String name) { |
|||
Asset asset = DaoUtil.getData(assetRepository.findByTenantIdAndName(tenantId, name)); |
|||
return Optional.ofNullable(asset); |
|||
} |
|||
} |
|||
@ -0,0 +1,86 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.alarm; |
|||
|
|||
import com.datastax.driver.core.utils.UUIDs; |
|||
import com.github.springtestdbunit.annotation.DatabaseSetup; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.alarm.Alarm; |
|||
import org.thingsboard.server.common.data.alarm.AlarmId; |
|||
import org.thingsboard.server.common.data.alarm.AlarmStatus; |
|||
import org.thingsboard.server.common.data.id.DeviceId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
|||
import org.thingsboard.server.dao.alarm.AlarmDao; |
|||
import org.thingsboard.server.dao.model.sql.AlarmEntity; |
|||
|
|||
import java.util.UUID; |
|||
import java.util.concurrent.ExecutionException; |
|||
|
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNotNull; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/21/2017. |
|||
*/ |
|||
public class JpaAlarmDaoTest extends AbstractJpaDaoTest { |
|||
|
|||
@Autowired |
|||
private AlarmDao alarmDao; |
|||
|
|||
@Autowired |
|||
private AlarmRepository alarmRepository; |
|||
|
|||
@Test |
|||
public void testFindLatestByOriginatorAndType() throws ExecutionException, InterruptedException { |
|||
System.out.println(System.currentTimeMillis()); |
|||
UUID tenantId = UUID.fromString("d4b68f40-3e96-11e7-a884-898080180d6b"); |
|||
UUID originator1Id = UUID.fromString("d4b68f41-3e96-11e7-a884-898080180d6b"); |
|||
UUID originator2Id = UUID.fromString("d4b68f42-3e96-11e7-a884-898080180d6b"); |
|||
UUID alarm1Id = UUID.fromString("d4b68f43-3e96-11e7-a884-898080180d6b"); |
|||
UUID alarm2Id = UUID.fromString("d4b68f44-3e96-11e7-a884-898080180d6b"); |
|||
UUID alarm3Id = UUID.fromString("d4b68f45-3e96-11e7-a884-898080180d6b"); |
|||
saveAlarm(alarm1Id, tenantId, originator1Id, "TEST_ALARM"); |
|||
saveAlarm(alarm2Id, tenantId, originator1Id, "TEST_ALARM"); |
|||
saveAlarm(alarm3Id, tenantId, originator2Id, "TEST_ALARM"); |
|||
assertEquals(3, alarmDao.find().size()); |
|||
AlarmEntity alarmEntity = alarmRepository.findLatestByOriginatorAndType( |
|||
tenantId, originator1Id, EntityType.DEVICE.ordinal(), "TEST_ALARM"); |
|||
assertNotNull(alarmEntity); |
|||
ListenableFuture<Alarm> future = alarmDao |
|||
.findLatestByOriginatorAndType(new TenantId(tenantId), new DeviceId(originator1Id), "TEST_ALARM"); |
|||
Alarm alarm = future.get(); |
|||
assertNotNull(alarm); |
|||
assertEquals(alarm2Id, alarm.getId().getId()); |
|||
} |
|||
|
|||
private void saveAlarm(UUID id, UUID tenantId, UUID deviceId, String type) { |
|||
Alarm alarm = new Alarm(); |
|||
alarm.setId(new AlarmId(id)); |
|||
alarm.setTenantId(new TenantId(tenantId)); |
|||
alarm.setOriginator(new DeviceId(deviceId)); |
|||
alarm.setType(type); |
|||
alarm.setPropagate(true); |
|||
alarm.setStartTs(System.currentTimeMillis()); |
|||
alarm.setEndTs(System.currentTimeMillis()); |
|||
alarm.setStatus(AlarmStatus.ACTIVE_UNACK); |
|||
alarmDao.save(alarm); |
|||
} |
|||
} |
|||
@ -0,0 +1,173 @@ |
|||
/** |
|||
* Copyright © 2016-2017 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.sql.asset; |
|||
|
|||
import com.datastax.driver.core.utils.UUIDs; |
|||
import com.google.common.util.concurrent.ListenableFuture; |
|||
import org.junit.Test; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.thingsboard.server.common.data.asset.Asset; |
|||
import org.thingsboard.server.common.data.id.AssetId; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.page.TextPageLink; |
|||
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
|||
import org.thingsboard.server.dao.asset.AssetDao; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Optional; |
|||
import java.util.UUID; |
|||
import java.util.concurrent.ExecutionException; |
|||
|
|||
import static junit.framework.TestCase.assertFalse; |
|||
import static org.junit.Assert.assertEquals; |
|||
import static org.junit.Assert.assertNotNull; |
|||
import static org.junit.Assert.assertTrue; |
|||
|
|||
/** |
|||
* Created by Valerii Sosliuk on 5/21/2017. |
|||
*/ |
|||
public class JpaAssetDaoTest extends AbstractJpaDaoTest { |
|||
|
|||
@Autowired |
|||
private AssetDao assetDao; |
|||
|
|||
@Test |
|||
public void testFindAssetsByTenantId() { |
|||
UUID tenantId1 = UUIDs.timeBased(); |
|||
UUID tenantId2 = UUIDs.timeBased(); |
|||
UUID customerId1 = UUIDs.timeBased(); |
|||
UUID customerId2 = UUIDs.timeBased(); |
|||
for (int i = 0; i < 60; i++) { |
|||
UUID assetId = UUIDs.timeBased(); |
|||
UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2; |
|||
UUID customerId = i % 2 == 0 ? customerId1 : customerId2; |
|||
saveAsset(assetId, tenantId, customerId, "ASSET_" + i); |
|||
} |
|||
assertEquals(60, assetDao.find().size()); |
|||
|
|||
TextPageLink pageLink1 = new TextPageLink(20, "ASSET_"); |
|||
List<Asset> assets1 = assetDao.findAssetsByTenantId(tenantId1, pageLink1); |
|||
assertEquals(20, assets1.size()); |
|||
|
|||
TextPageLink pageLink2 = new TextPageLink(20, "ASSET_", assets1.get(19).getId().getId(), null); |
|||
List<Asset> assets2 = assetDao.findAssetsByTenantId(tenantId1, pageLink2); |
|||
assertEquals(10, assets2.size()); |
|||
|
|||
TextPageLink pageLink3 = new TextPageLink(20, "ASSET_", assets2.get(9).getId().getId(), null); |
|||
List<Asset> assets3 = assetDao.findAssetsByTenantId(tenantId1, pageLink3); |
|||
assertEquals(0, assets3.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testFindAssetsByTenantIdAndCustomerId() { |
|||
UUID tenantId1 = UUIDs.timeBased(); |
|||
UUID tenantId2 = UUIDs.timeBased(); |
|||
UUID customerId1 = UUIDs.timeBased(); |
|||
UUID customerId2 = UUIDs.timeBased(); |
|||
for (int i = 0; i < 60; i++) { |
|||
UUID assetId = UUIDs.timeBased(); |
|||
UUID tenantId = i % 2 == 0 ? tenantId1 : tenantId2; |
|||
UUID customerId = i % 2 == 0 ? customerId1 : customerId2; |
|||
saveAsset(assetId, tenantId, customerId, "ASSET_" + i); |
|||
} |
|||
|
|||
TextPageLink pageLink1 = new TextPageLink(20, "ASSET_"); |
|||
List<Asset> assets1 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink1); |
|||
assertEquals(20, assets1.size()); |
|||
|
|||
TextPageLink pageLink2 = new TextPageLink(20, "ASSET_", assets1.get(19).getId().getId(), null); |
|||
List<Asset> assets2 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink2); |
|||
assertEquals(10, assets2.size()); |
|||
|
|||
TextPageLink pageLink3 = new TextPageLink(20, "ASSET_", assets2.get(9).getId().getId(), null); |
|||
List<Asset> assets3 = assetDao.findAssetsByTenantIdAndCustomerId(tenantId1, customerId1, pageLink3); |
|||
assertEquals(0, assets3.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testFindAssetsByTenantIdAndIdsAsync() throws ExecutionException, InterruptedException { |
|||
UUID tenantId = UUIDs.timeBased(); |
|||
UUID customerId = UUIDs.timeBased(); |
|||
List<UUID> searchIds = new ArrayList<>(); |
|||
for (int i = 0; i < 30; i++) { |
|||
UUID assetId = UUIDs.timeBased(); |
|||
saveAsset(assetId, tenantId, customerId, "ASSET_" + i); |
|||
if (i % 3 == 0) { |
|||
searchIds.add(assetId); |
|||
} |
|||
} |
|||
|
|||
ListenableFuture<List<Asset>> assetsFuture = assetDao |
|||
.findAssetsByTenantIdAndIdsAsync(tenantId, searchIds); |
|||
List<Asset> assets = assetsFuture.get(); |
|||
assertNotNull(assets); |
|||
assertEquals(10, assets.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testFindAssetsByTenantIdCustomerIdAndIdsAsync() throws ExecutionException, InterruptedException { |
|||
UUID tenantId = UUIDs.timeBased(); |
|||
UUID customerId1 = UUIDs.timeBased(); |
|||
UUID customerId2 = UUIDs.timeBased(); |
|||
List<UUID> searchIds = new ArrayList<>(); |
|||
for (int i = 0; i < 30; i++) { |
|||
UUID assetId = UUIDs.timeBased(); |
|||
UUID customerId = i%2 == 0 ? customerId1 : customerId2; |
|||
saveAsset(assetId, tenantId, customerId, "ASSET_" + i); |
|||
if (i % 3 == 0) { |
|||
searchIds.add(assetId); |
|||
} |
|||
} |
|||
|
|||
ListenableFuture<List<Asset>> assetsFuture = assetDao |
|||
.findAssetsByTenantIdAndCustomerIdAndIdsAsync(tenantId, customerId1, searchIds); |
|||
List<Asset> assets = assetsFuture.get(); |
|||
assertNotNull(assets); |
|||
assertEquals(5, assets.size()); |
|||
} |
|||
|
|||
@Test |
|||
public void testFindAssetsByTenantIdAndName() { |
|||
UUID assetId1 = UUIDs.timeBased(); |
|||
UUID assetId2 = UUIDs.timeBased(); |
|||
UUID tenantId1 = UUIDs.timeBased(); |
|||
UUID tenantId2 = UUIDs.timeBased(); |
|||
UUID customerId1 = UUIDs.timeBased(); |
|||
UUID customerId2 = UUIDs.timeBased(); |
|||
String name = "TEST_ASSET"; |
|||
saveAsset(assetId1, tenantId1, customerId1, name); |
|||
saveAsset(assetId2, tenantId2, customerId2, name); |
|||
|
|||
Optional<Asset> assetOpt1 = assetDao.findAssetsByTenantIdAndName(tenantId2, name); |
|||
assertTrue("Optional expected to be non-empty", assetOpt1.isPresent()); |
|||
assertEquals(assetId2, assetOpt1.get().getId().getId()); |
|||
|
|||
Optional<Asset> assetOpt2 = assetDao.findAssetsByTenantIdAndName(tenantId2, "NON_EXISTENT_NAME"); |
|||
assertFalse("Optional expected to be empty", assetOpt2.isPresent()); |
|||
} |
|||
|
|||
private void saveAsset(UUID id, UUID tenantId, UUID customerId, String name) { |
|||
Asset asset = new Asset(); |
|||
asset.setId(new AssetId(id)); |
|||
asset.setTenantId(new TenantId(tenantId)); |
|||
asset.setCustomerId(new CustomerId(customerId)); |
|||
asset.setName(name); |
|||
|
|||
assetDao.save(asset); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue