17 changed files with 471 additions and 157 deletions
@ -0,0 +1,57 @@ |
|||||
|
package org.thingsboard.server.dao.sql.tenant; |
||||
|
|
||||
|
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.Tenant; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.model.sql.TenantEntity; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
||||
|
import org.thingsboard.server.dao.tenant.TenantDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.TENANT_COLUMN_FAMILY_NAME; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
@Component |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public class JpaTenantDao extends JpaAbstractDao<TenantEntity, Tenant> implements TenantDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private TenantRepository tenantRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<TenantEntity> getEntityClass() { |
||||
|
return TenantEntity.class; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected String getColumnFamilyName() { |
||||
|
return TENANT_COLUMN_FAMILY_NAME; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<TenantEntity, UUID> getCrudRepository() { |
||||
|
return tenantRepository; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected boolean isSearchTextDao() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<Tenant> findTenantsByRegion(String region, TextPageLink pageLink) { |
||||
|
if (pageLink.getIdOffset() == null) { |
||||
|
return DaoUtil.convertDataList(tenantRepository.findByRegionFirstPage(pageLink.getLimit(), region, pageLink.getTextSearch())); |
||||
|
} else { |
||||
|
return DaoUtil.convertDataList(tenantRepository.findByRegionNextPage(pageLink.getLimit(), region, pageLink.getTextSearch(), pageLink.getIdOffset())); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
package org.thingsboard.server.dao.sql.tenant; |
||||
|
|
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.thingsboard.server.dao.model.sql.TenantEntity; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public interface TenantRepository extends CrudRepository<TenantEntity, UUID> { |
||||
|
|
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM TENANT WHERE REGION = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"ORDER BY ID LIMIT ?1") |
||||
|
List<TenantEntity> findByRegionFirstPage(int limit, String region, String textSearch); |
||||
|
|
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM TENANT WHERE REGION = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
||||
|
List<TenantEntity> findByRegionNextPage(int limit, String region, String textSearch, UUID idOffset); |
||||
|
} |
||||
@ -0,0 +1,52 @@ |
|||||
|
package org.thingsboard.server.dao.sql.widget; |
||||
|
|
||||
|
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.widget.WidgetType; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.model.sql.WidgetTypeEntity; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
||||
|
import org.thingsboard.server.dao.widget.WidgetTypeDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.WIDGET_TYPE_COLUMN_FAMILY_NAME; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/29/2017. |
||||
|
*/ |
||||
|
@Component |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public class JpaWidgetTypeDao extends JpaAbstractDao<WidgetTypeEntity, WidgetType> implements WidgetTypeDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private WidgetTypeRepository widgetTypeRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<WidgetTypeEntity> getEntityClass() { |
||||
|
return WidgetTypeEntity.class; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected String getColumnFamilyName() { |
||||
|
return WIDGET_TYPE_COLUMN_FAMILY_NAME; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<WidgetTypeEntity, UUID> getCrudRepository() { |
||||
|
return widgetTypeRepository; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<WidgetType> findWidgetTypesByTenantIdAndBundleAlias(UUID tenantId, String bundleAlias) { |
||||
|
return DaoUtil.convertDataList(widgetTypeRepository.findByTenantIdAndBundleAlias(tenantId, bundleAlias)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public WidgetType findByTenantIdBundleAliasAndAlias(UUID tenantId, String bundleAlias, String alias) { |
||||
|
return DaoUtil.getData(widgetTypeRepository.findByTenantIdAndBundleAliasAndAlias(tenantId, bundleAlias, alias)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
package org.thingsboard.server.dao.sql.widget; |
||||
|
|
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetType; |
||||
|
import org.thingsboard.server.dao.model.sql.WidgetTypeEntity; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/29/2017. |
||||
|
*/ |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public interface WidgetTypeRepository extends CrudRepository<WidgetTypeEntity, UUID> { |
||||
|
|
||||
|
List<WidgetTypeEntity> findByTenantIdAndBundleAlias(UUID tenantId, String bundleAlias); |
||||
|
|
||||
|
WidgetTypeEntity findByTenantIdAndBundleAliasAndAlias(UUID tenantId, String bundleAlias, String alias); |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
package org.thingsboard.server.dao.sql.tenant; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import com.github.springtestdbunit.annotation.DatabaseSetup; |
||||
|
import org.junit.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.thingsboard.server.common.data.Tenant; |
||||
|
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.tenant.TenantDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
import static org.junit.Assert.*; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
public class JpaTenantDaoTest extends AbstractJpaDaoTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private TenantDao tenantDao; |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
||||
|
public void testFindTenantsByRegion() { |
||||
|
createTenants(); |
||||
|
assertEquals(60, tenantDao.find().size()); |
||||
|
List<Tenant> tenants1 = tenantDao.findTenantsByRegion("REGION_1", new TextPageLink(20,"title")); |
||||
|
assertEquals(20, tenants1.size()); |
||||
|
List<Tenant> tenants2 = tenantDao.findTenantsByRegion("REGION_1", |
||||
|
new TextPageLink(20,"title", tenants1.get(19).getId().getId(), null)); |
||||
|
assertEquals(10, tenants2.size()); |
||||
|
List<Tenant> tenants3 = tenantDao.findTenantsByRegion("REGION_1", |
||||
|
new TextPageLink(20,"title", tenants2.get(9).getId().getId(), null)); |
||||
|
assertEquals(0, tenants3.size()); |
||||
|
} |
||||
|
|
||||
|
private void createTenants() { |
||||
|
for (int i = 0; i < 30; i++) { |
||||
|
createTenant("REGION_1", "TITLE", i); |
||||
|
createTenant("REGION_2", "TITLE", i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void createTenant(String region, String title, int index) { |
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setId(new TenantId(UUIDs.timeBased())); |
||||
|
tenant.setRegion(region); |
||||
|
tenant.setTitle(title + "_" + index); |
||||
|
tenantDao.save(tenant); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
package org.thingsboard.server.dao.sql.widget; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import com.github.springtestdbunit.annotation.DatabaseSetup; |
||||
|
import org.junit.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetType; |
||||
|
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
||||
|
import org.thingsboard.server.dao.widget.WidgetTypeDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.junit.Assert.assertEquals; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
public class JpaWidgetTypeDaoTest extends AbstractJpaDaoTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private WidgetTypeDao widgetTypeDao; |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup(("classpath:dbunit/widget_type.xml")) |
||||
|
public void testFindByTenantIdAndBundleAlias() { |
||||
|
UUID tenantId = UUID.fromString("2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833"); |
||||
|
List<WidgetType> widgetTypes = widgetTypeDao.findWidgetTypesByTenantIdAndBundleAlias(tenantId, "BUNDLE_ALIAS_1"); |
||||
|
assertEquals(3, widgetTypes.size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup(("classpath:dbunit/widget_type.xml")) |
||||
|
public void testFindByTenantIdAndBundleAliasAndAlias() { |
||||
|
UUID tenantId = UUID.fromString("2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833"); |
||||
|
WidgetType widgetType = widgetTypeDao.findByTenantIdBundleAliasAndAlias(tenantId, "BUNDLE_ALIAS_1", "ALIAS3"); |
||||
|
UUID id = UUID.fromString("2b7e4c93-2dfe-11e7-94aa-f7f6dbfb4833"); |
||||
|
assertEquals(id, widgetType.getId().getId()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
<dataset> |
||||
|
<widget_type |
||||
|
id="uuid'2b7e4c91-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
bundle_alias="BUNDLE_ALIAS_1" |
||||
|
alias="ALIAS1" |
||||
|
name="ALIAS1" |
||||
|
/> |
||||
|
<widget_type |
||||
|
id="uuid'2b7e4c92-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
bundle_alias="BUNDLE_ALIAS_1" |
||||
|
alias="ALIAS2" |
||||
|
name="ALIAS2" |
||||
|
/> |
||||
|
<widget_type |
||||
|
id="uuid'2b7e4c93-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
bundle_alias="BUNDLE_ALIAS_1" |
||||
|
alias="ALIAS3" |
||||
|
name="ALIAS3" |
||||
|
/> |
||||
|
<widget_type |
||||
|
id="uuid'2b7e4c94-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'2b7e4c90-2dfe-11e7-94aa-f7f6dbfb4833'" |
||||
|
bundle_alias="BUNDLE_ALIAS_2" |
||||
|
alias="ALIAS4" |
||||
|
name="ALIAS4" |
||||
|
/> |
||||
|
|
||||
|
</dataset> |
||||
Loading…
Reference in new issue