Added jpadevicedao implementations

This commit is contained in:
volodymyr-babak 2017-06-01 15:29:32 +03:00
parent 0ce99115cf
commit 03f0bf5865
6 changed files with 80 additions and 50 deletions

View File

@ -121,7 +121,7 @@ public class CassandraDeviceDao extends CassandraAbstractSearchTextDao<DeviceEnt
} }
@Override @Override
public Optional<Device> findDevicesByTenantIdAndName(UUID tenantId, String deviceName) { public Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String deviceName) {
Select select = select().from(DEVICE_BY_TENANT_AND_NAME_VIEW_NAME); Select select = select().from(DEVICE_BY_TENANT_AND_NAME_VIEW_NAME);
Select.Where query = select.where(); Select.Where query = select.where();
query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId)); query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));

View File

@ -107,7 +107,7 @@ public interface DeviceDao extends Dao<Device> {
* @param name the device name * @param name the device name
* @return the optional device object * @return the optional device object
*/ */
Optional<Device> findDevicesByTenantIdAndName(UUID tenantId, String name); Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String name);
/** /**
* Find tenants device types. * Find tenants device types.

View File

@ -37,7 +37,6 @@ import org.thingsboard.server.common.data.security.DeviceCredentialsType;
import org.thingsboard.server.dao.customer.CustomerDao; import org.thingsboard.server.dao.customer.CustomerDao;
import org.thingsboard.server.dao.entity.AbstractEntityService; import org.thingsboard.server.dao.entity.AbstractEntityService;
import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.model.TenantDeviceTypeEntity;
import org.thingsboard.server.dao.relation.EntitySearchDirection; import org.thingsboard.server.dao.relation.EntitySearchDirection;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import org.thingsboard.server.dao.service.PaginatedRemover; import org.thingsboard.server.dao.service.PaginatedRemover;
@ -88,7 +87,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
public Optional<Device> findDeviceByTenantIdAndName(TenantId tenantId, String name) { public Optional<Device> findDeviceByTenantIdAndName(TenantId tenantId, String name) {
log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name); log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name);
validateId(tenantId, "Incorrect tenantId " + tenantId); validateId(tenantId, "Incorrect tenantId " + tenantId);
Optional<Device> deviceOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name); Optional<Device> deviceOpt = deviceDao.findDeviceByTenantIdAndName(tenantId.getId(), name);
if (deviceOpt.isPresent()) { if (deviceOpt.isPresent()) {
return Optional.of(deviceOpt.get()); return Optional.of(deviceOpt.get());
} else { } else {
@ -261,7 +260,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
@Override @Override
protected void validateCreate(Device device) { protected void validateCreate(Device device) {
deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent( deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
d -> { d -> {
throw new DataValidationException("Device with such name already exists!"); throw new DataValidationException("Device with such name already exists!");
} }
@ -270,7 +269,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
@Override @Override
protected void validateUpdate(Device device) { protected void validateUpdate(Device device) {
deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent( deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
d -> { d -> {
if (!d.getUuidId().equals(device.getUuidId())) { if (!d.getUuidId().equals(device.getUuidId())) {
throw new DataValidationException("Device with such name already exists!"); throw new DataValidationException("Device with such name already exists!");

View File

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -18,8 +18,7 @@ package org.thingsboard.server.dao.sql.device;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.scheduling.annotation.Async; import org.springframework.data.repository.query.Param;
import org.springframework.util.concurrent.ListenableFuture;
import org.thingsboard.server.dao.model.sql.DeviceEntity; import org.thingsboard.server.dao.model.sql.DeviceEntity;
import java.util.List; import java.util.List;
@ -31,32 +30,52 @@ import java.util.UUID;
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) @ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false)
public interface DeviceRepository extends CrudRepository<DeviceEntity, UUID> { public interface DeviceRepository extends CrudRepository<DeviceEntity, UUID> {
@Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = ?2 " +
"AND CUSTOMER_ID = ?3 " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?4, '%')) " +
"ORDER BY ID LIMIT ?1")
List<DeviceEntity> findByTenantIdAndCustomerIdFirstPage(int limit, UUID tenantId, UUID customerId, String searchText);
@Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = ?2 " + @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
"AND CUSTOMER_ID = ?3 " + "AND CUSTOMER_ID = :customerId " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?4, '%')) " + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:searchText, '%')) " +
"AND ID > ?5 ORDER BY ID LIMIT ?1") "AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<DeviceEntity> findByTenantIdAndCustomerIdNextPage(int limit, UUID tenantId, UUID customerId, String searchText, UUID idOffset); List<DeviceEntity> findByTenantIdAndCustomerId(@Param("limit") int limit,
@Param("tenantId") UUID tenantId,
@Param("customerId") UUID customerId,
@Param("searchText") String searchText,
@Param("idOffset") UUID idOffset);
@Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = ?2 " + @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:textSearch, '%')) " +
"ORDER BY ID LIMIT ?1") "AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<DeviceEntity> findByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); List<DeviceEntity> findByTenantId(@Param("limit") int limit,
@Param("tenantId") UUID tenantId,
@Param("textSearch") String textSearch,
@Param("idOffset") UUID idOffset);
@Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = ?2 " + @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + "AND TYPE = :type " +
"AND ID > ?4 ORDER BY ID LIMIT ?1") "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:textSearch, '%')) " +
List<DeviceEntity> findByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); "AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<DeviceEntity> findByTenantIdAndType(@Param("limit") int limit,
@Param("tenantId") UUID tenantId,
@Param("type") String type,
@Param("textSearch") String textSearch,
@Param("idOffset") UUID idOffset);
@Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
"AND CUSTOMER_ID = :customerId " +
"AND TYPE = :type " +
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:textSearch, '%')) " +
"AND ID > :idOffset ORDER BY ID LIMIT :limit")
List<DeviceEntity> findByTenantIdAndCustomerIdAndType(@Param("limit") int limit,
@Param("tenantId") UUID tenantId,
@Param("customerId") UUID customerId,
@Param("type") String type,
@Param("textSearch") String textSearch,
@Param("idOffset") UUID idOffset);
DeviceEntity findByTenantIdAndName(UUID tenantId, String name); DeviceEntity findByTenantIdAndName(UUID tenantId, String name);
List<DeviceEntity> findDevicesByTenantIdAndCustomerIdAndIdIn(UUID tenantId, UUID customerId, List<UUID> deviceIds); List<DeviceEntity> findDevicesByTenantIdAndCustomerIdAndIdIn(UUID tenantId, UUID customerId, List<UUID> deviceIds);
List<DeviceEntity> findDevicesByTenantId(UUID tenantId); List<DeviceEntity> findDevicesByTenantId(UUID tenantId);
List<DeviceEntity> findDevicesByTenantIdAndIdIn(UUID tenantId, List<UUID> deviceIds); List<DeviceEntity> findDevicesByTenantIdAndIdIn(UUID tenantId, List<UUID> deviceIds);
} }

View File

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -35,7 +35,7 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static org.thingsboard.server.dao.model.ModelConstants.DEVICE_COLUMN_FAMILY_NAME; import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
/** /**
* Created by Valerii Sosliuk on 5/6/2017. * Created by Valerii Sosliuk on 5/6/2017.
@ -59,13 +59,13 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
@Override @Override
public List<Device> findDevicesByTenantId(UUID tenantId, TextPageLink pageLink) { public List<Device> findDevicesByTenantId(UUID tenantId, TextPageLink pageLink) {
if (pageLink.getIdOffset() == null) { return DaoUtil.convertDataList(
return DaoUtil.convertDataList(deviceRepository.findByTenantIdFirstPage( deviceRepository.findByTenantId(
pageLink.getLimit(), tenantId, pageLink.getTextSearch())); pageLink.getLimit(),
} else { tenantId,
return DaoUtil.convertDataList(deviceRepository.findByTenantIdNextPage( pageLink.getTextSearch(),
pageLink.getLimit(), tenantId, pageLink.getTextSearch(), pageLink.getIdOffset())); pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
} );
} }
@Override @Override
@ -77,13 +77,14 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
@Override @Override
public List<Device> findDevicesByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) { public List<Device> findDevicesByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) {
if (pageLink.getIdOffset() == null) { return DaoUtil.convertDataList(
return DaoUtil.convertDataList(deviceRepository.findByTenantIdAndCustomerIdFirstPage(pageLink.getLimit(), deviceRepository.findByTenantIdAndCustomerId(
tenantId, customerId, pageLink.getTextSearch())); pageLink.getLimit(),
} else { tenantId,
return DaoUtil.convertDataList(deviceRepository.findByTenantIdAndCustomerIdNextPage(pageLink.getLimit(), customerId,
tenantId, customerId, pageLink.getTextSearch(), pageLink.getIdOffset())); pageLink.getTextSearch(),
} pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
);
} }
@Override @Override
@ -94,22 +95,34 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
} }
@Override @Override
// Probably findDevice, not findDevices? public Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String name) {
public Optional<Device> findDevicesByTenantIdAndName(UUID tenantId, String name) {
Device device = DaoUtil.getData(deviceRepository.findByTenantIdAndName(tenantId, name)); Device device = DaoUtil.getData(deviceRepository.findByTenantIdAndName(tenantId, name));
return Optional.ofNullable(device); return Optional.ofNullable(device);
} }
@Override @Override
public List<Device> findDevicesByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) { public List<Device> findDevicesByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) {
//TODO return DaoUtil.convertDataList(
return null; deviceRepository.findByTenantIdAndType(
pageLink.getLimit(),
tenantId,
type,
pageLink.getTextSearch(),
pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
);
} }
@Override @Override
public List<Device> findDevicesByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) { public List<Device> findDevicesByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) {
//TODO return DaoUtil.convertDataList(
return null; deviceRepository.findByTenantIdAndCustomerIdAndType(
pageLink.getLimit(),
tenantId,
customerId,
type,
pageLink.getTextSearch(),
pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
);
} }
@Override @Override

View File

@ -56,7 +56,6 @@ public class JpaWidgetsBundleDao extends JpaAbstractSearchTextDao<WidgetsBundleE
return DaoUtil.getData(widgetsBundleRepository.findWidgetsBundleByTenantIdAndAlias(tenantId, alias)); return DaoUtil.getData(widgetsBundleRepository.findWidgetsBundleByTenantIdAndAlias(tenantId, alias));
} }
@Override @Override
public List<WidgetsBundle> findSystemWidgetsBundles(TextPageLink pageLink) { public List<WidgetsBundle> findSystemWidgetsBundles(TextPageLink pageLink) {
if (pageLink.getIdOffset() == null) { if (pageLink.getIdOffset() == null) {