use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class TenantProfileServiceImpl method findOrCreateDefaultTenantProfile.
@Override
public TenantProfile findOrCreateDefaultTenantProfile(TenantId tenantId) {
log.trace("Executing findOrCreateDefaultTenantProfile");
TenantProfile defaultTenantProfile = findDefaultTenantProfile(tenantId);
if (defaultTenantProfile == null) {
defaultTenantProfile = new TenantProfile();
defaultTenantProfile.setDefault(true);
defaultTenantProfile.setName("Default");
TenantProfileData profileData = new TenantProfileData();
profileData.setConfiguration(new DefaultTenantProfileConfiguration());
defaultTenantProfile.setProfileData(profileData);
defaultTenantProfile.setDescription("Default tenant profile");
defaultTenantProfile.setIsolatedTbCore(false);
defaultTenantProfile.setIsolatedTbRuleEngine(false);
defaultTenantProfile = saveTenantProfile(tenantId, defaultTenantProfile);
}
return defaultTenantProfile;
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class TenantProfileServiceImpl method saveTenantProfile.
@Override
public TenantProfile saveTenantProfile(TenantId tenantId, TenantProfile tenantProfile) {
log.trace("Executing saveTenantProfile [{}]", tenantProfile);
tenantProfileValidator.validate(tenantProfile, (tenantProfile1) -> TenantId.SYS_TENANT_ID);
TenantProfile savedTenantProfile;
try {
savedTenantProfile = tenantProfileDao.save(tenantId, tenantProfile);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("tenant_profile_name_unq_key")) {
throw new DataValidationException("Tenant profile with such name already exists!");
} else {
throw t;
}
}
Cache cache = cacheManager.getCache(TENANT_PROFILE_CACHE);
cache.evict(Collections.singletonList(savedTenantProfile.getId().getId()));
cache.evict(Arrays.asList("info", savedTenantProfile.getId().getId()));
if (savedTenantProfile.isDefault()) {
cache.evict(Collections.singletonList("default"));
cache.evict(Arrays.asList("default", "info"));
}
return savedTenantProfile;
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class TenantServiceImpl method saveTenant.
@Override
public Tenant saveTenant(Tenant tenant) {
log.trace("Executing saveTenant [{}]", tenant);
tenant.setRegion(DEFAULT_TENANT_REGION);
if (tenant.getTenantProfileId() == null) {
TenantProfile tenantProfile = this.tenantProfileService.findOrCreateDefaultTenantProfile(TenantId.SYS_TENANT_ID);
tenant.setTenantProfileId(tenantProfile.getId());
}
tenantValidator.validate(tenant, Tenant::getId);
Tenant savedTenant = tenantDao.save(tenant.getId(), tenant);
if (tenant.getId() == null) {
deviceProfileService.createDefaultDeviceProfile(savedTenant.getId());
apiUsageStateService.createDefaultApiUsageState(savedTenant.getId(), null);
}
return savedTenant;
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class DefaultSystemDataLoaderService method createDefaultTenantProfiles.
@Override
public void createDefaultTenantProfiles() throws Exception {
tenantProfileService.findOrCreateDefaultTenantProfile(TenantId.SYS_TENANT_ID);
TenantProfileData tenantProfileData = new TenantProfileData();
tenantProfileData.setConfiguration(new DefaultTenantProfileConfiguration());
TenantProfile isolatedTbCoreProfile = new TenantProfile();
isolatedTbCoreProfile.setDefault(false);
isolatedTbCoreProfile.setName("Isolated TB Core");
isolatedTbCoreProfile.setDescription("Isolated TB Core tenant profile");
isolatedTbCoreProfile.setIsolatedTbCore(true);
isolatedTbCoreProfile.setIsolatedTbRuleEngine(false);
isolatedTbCoreProfile.setProfileData(tenantProfileData);
try {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbCoreProfile);
} catch (DataValidationException e) {
log.warn(e.getMessage());
}
TenantProfile isolatedTbRuleEngineProfile = new TenantProfile();
isolatedTbRuleEngineProfile.setDefault(false);
isolatedTbRuleEngineProfile.setName("Isolated TB Rule Engine");
isolatedTbRuleEngineProfile.setDescription("Isolated TB Rule Engine tenant profile");
isolatedTbRuleEngineProfile.setIsolatedTbCore(false);
isolatedTbRuleEngineProfile.setIsolatedTbRuleEngine(true);
isolatedTbRuleEngineProfile.setProfileData(tenantProfileData);
try {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbRuleEngineProfile);
} catch (DataValidationException e) {
log.warn(e.getMessage());
}
TenantProfile isolatedTbCoreAndTbRuleEngineProfile = new TenantProfile();
isolatedTbCoreAndTbRuleEngineProfile.setDefault(false);
isolatedTbCoreAndTbRuleEngineProfile.setName("Isolated TB Core and TB Rule Engine");
isolatedTbCoreAndTbRuleEngineProfile.setDescription("Isolated TB Core and TB Rule Engine tenant profile");
isolatedTbCoreAndTbRuleEngineProfile.setIsolatedTbCore(true);
isolatedTbCoreAndTbRuleEngineProfile.setIsolatedTbRuleEngine(true);
isolatedTbCoreAndTbRuleEngineProfile.setProfileData(tenantProfileData);
try {
tenantProfileService.saveTenantProfile(TenantId.SYS_TENANT_ID, isolatedTbCoreAndTbRuleEngineProfile);
} catch (DataValidationException e) {
log.warn(e.getMessage());
}
}
use of org.thingsboard.server.common.data.TenantProfile in project thingsboard by thingsboard.
the class BaseTenantProfileControllerTest method testSaveTenantProfile.
@Test
public void testSaveTenantProfile() throws Exception {
loginSysAdmin();
TenantProfile tenantProfile = this.createTenantProfile("Tenant Profile");
TenantProfile savedTenantProfile = doPost("/api/tenantProfile", tenantProfile, TenantProfile.class);
Assert.assertNotNull(savedTenantProfile);
Assert.assertNotNull(savedTenantProfile.getId());
Assert.assertTrue(savedTenantProfile.getCreatedTime() > 0);
Assert.assertEquals(tenantProfile.getName(), savedTenantProfile.getName());
Assert.assertEquals(tenantProfile.getDescription(), savedTenantProfile.getDescription());
Assert.assertEquals(tenantProfile.getProfileData(), savedTenantProfile.getProfileData());
Assert.assertEquals(tenantProfile.isDefault(), savedTenantProfile.isDefault());
Assert.assertEquals(tenantProfile.isIsolatedTbCore(), savedTenantProfile.isIsolatedTbCore());
Assert.assertEquals(tenantProfile.isIsolatedTbRuleEngine(), savedTenantProfile.isIsolatedTbRuleEngine());
savedTenantProfile.setName("New tenant profile");
doPost("/api/tenantProfile", savedTenantProfile, TenantProfile.class);
TenantProfile foundTenantProfile = doGet("/api/tenantProfile/" + savedTenantProfile.getId().getId().toString(), TenantProfile.class);
Assert.assertEquals(foundTenantProfile.getName(), savedTenantProfile.getName());
}
Aggregations