Added jpadevicedao implementations
This commit is contained in:
		
							parent
							
								
									0ce99115cf
								
							
						
					
					
						commit
						03f0bf5865
					
				@ -121,7 +121,7 @@ public class CassandraDeviceDao extends CassandraAbstractSearchTextDao<DeviceEnt
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @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.Where query = select.where();
 | 
			
		||||
        query.and(eq(DEVICE_TENANT_ID_PROPERTY, tenantId));
 | 
			
		||||
 | 
			
		||||
@ -107,7 +107,7 @@ public interface DeviceDao extends Dao<Device> {
 | 
			
		||||
     * @param name the device name
 | 
			
		||||
     * @return the optional device object
 | 
			
		||||
     */
 | 
			
		||||
    Optional<Device> findDevicesByTenantIdAndName(UUID tenantId, String name);
 | 
			
		||||
    Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String name);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Find tenants device types.
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,6 @@ import org.thingsboard.server.common.data.security.DeviceCredentialsType;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerDao;
 | 
			
		||||
import org.thingsboard.server.dao.entity.AbstractEntityService;
 | 
			
		||||
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.service.DataValidator;
 | 
			
		||||
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) {
 | 
			
		||||
        log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name);
 | 
			
		||||
        validateId(tenantId, "Incorrect tenantId " + tenantId);
 | 
			
		||||
        Optional<Device> deviceOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name);
 | 
			
		||||
        Optional<Device> deviceOpt = deviceDao.findDeviceByTenantIdAndName(tenantId.getId(), name);
 | 
			
		||||
        if (deviceOpt.isPresent()) {
 | 
			
		||||
            return Optional.of(deviceOpt.get());
 | 
			
		||||
        } else {
 | 
			
		||||
@ -261,7 +260,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                protected void validateCreate(Device device) {
 | 
			
		||||
                    deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
 | 
			
		||||
                    deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
 | 
			
		||||
                            d -> {
 | 
			
		||||
                                throw new DataValidationException("Device with such name already exists!");
 | 
			
		||||
                            }
 | 
			
		||||
@ -270,7 +269,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                protected void validateUpdate(Device device) {
 | 
			
		||||
                    deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
 | 
			
		||||
                    deviceDao.findDeviceByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent(
 | 
			
		||||
                            d -> {
 | 
			
		||||
                                if (!d.getUuidId().equals(device.getUuidId())) {
 | 
			
		||||
                                    throw new DataValidationException("Device with such name already exists!");
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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,
 | 
			
		||||
@ -18,8 +18,7 @@ package org.thingsboard.server.dao.sql.device;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 | 
			
		||||
import org.springframework.data.jpa.repository.Query;
 | 
			
		||||
import org.springframework.data.repository.CrudRepository;
 | 
			
		||||
import org.springframework.scheduling.annotation.Async;
 | 
			
		||||
import org.springframework.util.concurrent.ListenableFuture;
 | 
			
		||||
import org.springframework.data.repository.query.Param;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.DeviceEntity;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -31,32 +30,52 @@ import java.util.UUID;
 | 
			
		||||
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false)
 | 
			
		||||
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 " +
 | 
			
		||||
            "AND CUSTOMER_ID = ?3 " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?4, '%')) " +
 | 
			
		||||
            "AND ID > ?5 ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<DeviceEntity> findByTenantIdAndCustomerIdNextPage(int limit, UUID tenantId, UUID customerId, String searchText, UUID idOffset);
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
 | 
			
		||||
            "AND CUSTOMER_ID = :customerId " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:searchText, '%')) " +
 | 
			
		||||
            "AND ID > :idOffset ORDER BY ID LIMIT :limit")
 | 
			
		||||
    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 " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<DeviceEntity> findByTenantIdFirstPage(int limit, UUID tenantId, String textSearch);
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:textSearch, '%')) " +
 | 
			
		||||
            "AND ID > :idOffset ORDER BY ID LIMIT :limit")
 | 
			
		||||
    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 " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " +
 | 
			
		||||
            "AND ID > ?4 ORDER BY ID LIMIT ?1")
 | 
			
		||||
    List<DeviceEntity> findByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset);
 | 
			
		||||
    @Query(nativeQuery = true, value = "SELECT * FROM DEVICE WHERE TENANT_ID = :tenantId " +
 | 
			
		||||
            "AND TYPE = :type " +
 | 
			
		||||
            "AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(:textSearch, '%')) " +
 | 
			
		||||
            "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);
 | 
			
		||||
 | 
			
		||||
    List<DeviceEntity> findDevicesByTenantIdAndCustomerIdAndIdIn(UUID tenantId, UUID customerId, List<UUID> deviceIds);
 | 
			
		||||
 | 
			
		||||
    List<DeviceEntity> findDevicesByTenantId(UUID tenantId);
 | 
			
		||||
 | 
			
		||||
    List<DeviceEntity> findDevicesByTenantIdAndIdIn(UUID tenantId, List<UUID> deviceIds);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
 * 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
 | 
			
		||||
 * 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,
 | 
			
		||||
@ -35,7 +35,7 @@ import java.util.Optional;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
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.
 | 
			
		||||
@ -59,13 +59,13 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Device> findDevicesByTenantId(UUID tenantId, TextPageLink pageLink) {
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
            return DaoUtil.convertDataList(deviceRepository.findByTenantIdFirstPage(
 | 
			
		||||
                    pageLink.getLimit(), tenantId, pageLink.getTextSearch()));
 | 
			
		||||
        } else {
 | 
			
		||||
            return DaoUtil.convertDataList(deviceRepository.findByTenantIdNextPage(
 | 
			
		||||
                    pageLink.getLimit(), tenantId, pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
        }
 | 
			
		||||
        return DaoUtil.convertDataList(
 | 
			
		||||
                deviceRepository.findByTenantId(
 | 
			
		||||
                        pageLink.getLimit(),
 | 
			
		||||
                        tenantId,
 | 
			
		||||
                        pageLink.getTextSearch(),
 | 
			
		||||
                        pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -77,13 +77,14 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Device> findDevicesByTenantIdAndCustomerId(UUID tenantId, UUID customerId, TextPageLink pageLink) {
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
            return DaoUtil.convertDataList(deviceRepository.findByTenantIdAndCustomerIdFirstPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, customerId, pageLink.getTextSearch()));
 | 
			
		||||
        } else {
 | 
			
		||||
            return DaoUtil.convertDataList(deviceRepository.findByTenantIdAndCustomerIdNextPage(pageLink.getLimit(),
 | 
			
		||||
                    tenantId, customerId, pageLink.getTextSearch(), pageLink.getIdOffset()));
 | 
			
		||||
        }
 | 
			
		||||
        return DaoUtil.convertDataList(
 | 
			
		||||
                deviceRepository.findByTenantIdAndCustomerId(
 | 
			
		||||
                        pageLink.getLimit(),
 | 
			
		||||
                        tenantId,
 | 
			
		||||
                        customerId,
 | 
			
		||||
                        pageLink.getTextSearch(),
 | 
			
		||||
                        pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -94,22 +95,34 @@ public class JpaDeviceDao extends JpaAbstractSearchTextDao<DeviceEntity, Device>
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    // Probably findDevice, not findDevices?
 | 
			
		||||
    public Optional<Device> findDevicesByTenantIdAndName(UUID tenantId, String name) {
 | 
			
		||||
    public Optional<Device> findDeviceByTenantIdAndName(UUID tenantId, String name) {
 | 
			
		||||
        Device device = DaoUtil.getData(deviceRepository.findByTenantIdAndName(tenantId, name));
 | 
			
		||||
        return Optional.ofNullable(device);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Device> findDevicesByTenantIdAndType(UUID tenantId, String type, TextPageLink pageLink) {
 | 
			
		||||
        //TODO
 | 
			
		||||
        return null;
 | 
			
		||||
        return DaoUtil.convertDataList(
 | 
			
		||||
                deviceRepository.findByTenantIdAndType(
 | 
			
		||||
                        pageLink.getLimit(),
 | 
			
		||||
                        tenantId,
 | 
			
		||||
                        type,
 | 
			
		||||
                        pageLink.getTextSearch(),
 | 
			
		||||
                        pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<Device> findDevicesByTenantIdAndCustomerIdAndType(UUID tenantId, UUID customerId, String type, TextPageLink pageLink) {
 | 
			
		||||
        //TODO
 | 
			
		||||
        return null;
 | 
			
		||||
        return DaoUtil.convertDataList(
 | 
			
		||||
                deviceRepository.findByTenantIdAndCustomerIdAndType(
 | 
			
		||||
                        pageLink.getLimit(),
 | 
			
		||||
                        tenantId,
 | 
			
		||||
                        customerId,
 | 
			
		||||
                        type,
 | 
			
		||||
                        pageLink.getTextSearch(),
 | 
			
		||||
                        pageLink.getIdOffset() == null ? NULL_UUID : pageLink.getIdOffset())
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -56,7 +56,6 @@ public class JpaWidgetsBundleDao extends JpaAbstractSearchTextDao<WidgetsBundleE
 | 
			
		||||
        return DaoUtil.getData(widgetsBundleRepository.findWidgetsBundleByTenantIdAndAlias(tenantId, alias));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<WidgetsBundle> findSystemWidgetsBundles(TextPageLink pageLink) {
 | 
			
		||||
        if (pageLink.getIdOffset() == null) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user