Browse Source

Refactoring for mobile settings api

pull/10591/head
ViacheslavKlimov 2 years ago
parent
commit
0d7d9b481b
  1. 5
      application/src/main/data/upgrade/3.6.4/schema_update.sql
  2. 7
      application/src/main/java/org/thingsboard/server/controller/MobileApplicationController.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileAppSecretService.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileAppSecretServiceImpl.java
  5. 2
      application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretCaffeineCache.java
  6. 2
      application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretEvictEvent.java
  7. 2
      application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretRedisCache.java
  8. 20
      dao/src/main/java/org/thingsboard/server/dao/mobile/BaseMobileAppSettingsService.java
  9. 2
      dao/src/main/java/org/thingsboard/server/dao/mobile/MobileAppSettingsRedisCache.java
  10. 9
      dao/src/main/java/org/thingsboard/server/dao/model/sql/MobileAppSettingsEntity.java
  11. 7
      dao/src/main/resources/sql/schema-entities.sql

5
application/src/main/data/upgrade/3.6.4/schema_update.sql

@ -157,11 +157,12 @@ DELETE FROM asset_profile WHERE name ='TbServiceQueue';
CREATE TABLE IF NOT EXISTS mobile_app_settings ( CREATE TABLE IF NOT EXISTS mobile_app_settings (
id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY,
created_time bigint NOT NULL, created_time bigint NOT NULL,
tenant_id uuid UNIQUE NOT NULL, tenant_id uuid NOT NULL,
use_default_app boolean, use_default_app boolean,
android_config VARCHAR(1000), android_config VARCHAR(1000),
ios_config VARCHAR(1000), ios_config VARCHAR(1000),
qr_code_config VARCHAR(100000) qr_code_config VARCHAR(100000),
CONSTRAINT mobile_app_settings_tenant_id_unq_key UNIQUE (tenant_id)
); );
-- MOBILE APP SETTINGS TABLE CREATE END -- MOBILE APP SETTINGS TABLE CREATE END

7
application/src/main/java/org/thingsboard/server/controller/MobileApplicationController.java

@ -39,7 +39,7 @@ import org.thingsboard.server.common.data.security.model.JwtPair;
import org.thingsboard.server.config.annotations.ApiOperation; import org.thingsboard.server.config.annotations.ApiOperation;
import org.thingsboard.server.dao.mobile.MobileAppSettingsService; import org.thingsboard.server.dao.mobile.MobileAppSettingsService;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
import org.thingsboard.server.service.qr.MobileAppSecretService; import org.thingsboard.server.service.mobile.secret.MobileAppSecretService;
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Operation;
import org.thingsboard.server.service.security.permission.Resource; import org.thingsboard.server.service.security.permission.Resource;
@ -84,7 +84,7 @@ public class MobileApplicationController extends BaseController {
public static final String ANDROID_APPLICATION_STORE_LINK = "https://play.google.com/store/apps/details?id=org.thingsboard.demo.app"; public static final String ANDROID_APPLICATION_STORE_LINK = "https://play.google.com/store/apps/details?id=org.thingsboard.demo.app";
public static final String APPLE_APPLICATION_STORE_LINK = "https://apps.apple.com/us/app/thingsboard-live/id1594355695"; public static final String APPLE_APPLICATION_STORE_LINK = "https://apps.apple.com/us/app/thingsboard-live/id1594355695";
public static final String SECRET = "secret"; public static final String SECRET = "secret";
public static final String SECRET_PARAM_DESCRIPTION = "A string value representing short-live secret key"; public static final String SECRET_PARAM_DESCRIPTION = "A string value representing short-lived secret key";
public static final String DEFAULT_APP_DOMAIN = "demo.thingsboard.io"; public static final String DEFAULT_APP_DOMAIN = "demo.thingsboard.io";
public static final String DEEP_LINK_PATTERN = "https://%s/api/noauth/qr?secret=%s&ttl=%s"; public static final String DEEP_LINK_PATTERN = "https://%s/api/noauth/qr?secret=%s&ttl=%s";
@ -164,7 +164,7 @@ public class MobileApplicationController extends BaseController {
notes = "Returns the token of the User based on the provided secret key.") notes = "Returns the token of the User based on the provided secret key.")
@GetMapping(value = "/api/noauth/qr/{secret}") @GetMapping(value = "/api/noauth/qr/{secret}")
public JwtPair getUserTokenByMobileSecret(@Parameter(description = SECRET_PARAM_DESCRIPTION) public JwtPair getUserTokenByMobileSecret(@Parameter(description = SECRET_PARAM_DESCRIPTION)
@PathVariable(SECRET) String secret) throws ThingsboardException { @PathVariable(SECRET) String secret) throws ThingsboardException {
checkParameter(SECRET, secret); checkParameter(SECRET, secret);
return mobileAppSecretService.getJwtPair(secret); return mobileAppSecretService.getJwtPair(secret);
} }
@ -184,4 +184,5 @@ public class MobileApplicationController extends BaseController {
.build(); .build();
} }
} }
} }

2
application/src/main/java/org/thingsboard/server/service/qr/MobileAppSecretService.java → application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileAppSecretService.java

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.service.qr; package org.thingsboard.server.service.mobile.secret;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.security.model.JwtPair; import org.thingsboard.server.common.data.security.model.JwtPair;

2
application/src/main/java/org/thingsboard/server/service/qr/MobileAppSecretServiceImpl.java → application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileAppSecretServiceImpl.java

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.service.qr; package org.thingsboard.server.service.mobile.secret;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
application/src/main/java/org/thingsboard/server/service/qr/MobileSecretCaffeineCache.java → application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretCaffeineCache.java

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.service.qr; package org.thingsboard.server.service.mobile.secret;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;

2
application/src/main/java/org/thingsboard/server/service/qr/MobileSecretEvictEvent.java → application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretEvictEvent.java

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.service.qr; package org.thingsboard.server.service.mobile.secret;
import lombok.Data; import lombok.Data;

2
application/src/main/java/org/thingsboard/server/service/qr/MobileSecretRedisCache.java → application/src/main/java/org/thingsboard/server/service/mobile/secret/MobileSecretRedisCache.java

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.thingsboard.server.service.qr; package org.thingsboard.server.service.mobile.secret;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;

20
dao/src/main/java/org/thingsboard/server/dao/mobile/BaseMobileAppSettingsService.java

@ -17,7 +17,6 @@ package org.thingsboard.server.dao.mobile;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionalEventListener; import org.springframework.transaction.event.TransactionalEventListener;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -28,9 +27,10 @@ import org.thingsboard.server.common.data.mobile.IosConfig;
import org.thingsboard.server.common.data.mobile.MobileAppSettings; import org.thingsboard.server.common.data.mobile.MobileAppSettings;
import org.thingsboard.server.common.data.mobile.QRCodeConfig; import org.thingsboard.server.common.data.mobile.QRCodeConfig;
import org.thingsboard.server.dao.entity.AbstractCachedEntityService; import org.thingsboard.server.dao.entity.AbstractCachedEntityService;
import org.thingsboard.server.dao.exception.DataValidationException;
import org.thingsboard.server.dao.service.DataValidator; import org.thingsboard.server.dao.service.DataValidator;
import java.util.Map;
import static org.thingsboard.server.dao.service.Validator.validateId; import static org.thingsboard.server.dao.service.Validator.validateId;
@Service @Service
@ -51,16 +51,12 @@ public class BaseMobileAppSettingsService extends AbstractCachedEntityService<Te
MobileAppSettings savedMobileAppSettings = mobileAppSettingsDao.save(tenantId, mobileAppSettings); MobileAppSettings savedMobileAppSettings = mobileAppSettingsDao.save(tenantId, mobileAppSettings);
publishEvictEvent(new MobileAppSettingsEvictEvent(tenantId)); publishEvictEvent(new MobileAppSettingsEvictEvent(tenantId));
return savedMobileAppSettings; return savedMobileAppSettings;
} catch (Exception exception) { } catch (Exception e) {
if (mobileAppSettings != null) { handleEvictEvent(new MobileAppSettingsEvictEvent(tenantId));
handleEvictEvent(new MobileAppSettingsEvictEvent(tenantId)); checkConstraintViolation(e, Map.of(
} "mobile_app_settings_tenant_id_unq_key", "Mobile application for specified tenant already exists!"
ConstraintViolationException e = extractConstraintViolationException(exception).orElse(null); ));
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("mobile_app_settings_tenant_id_key")) { throw e;
throw new DataValidationException("Mobile application for specified tenant already exists!");
} else {
throw exception;
}
} }
} }

2
dao/src/main/java/org/thingsboard/server/dao/mobile/MobileAppSettingsRedisCache.java

@ -1,4 +1,4 @@
/** /**
* Copyright © 2016-2024 The Thingsboard Authors * Copyright © 2016-2024 The Thingsboard Authors
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");

9
dao/src/main/java/org/thingsboard/server/dao/model/sql/MobileAppSettingsEntity.java

@ -21,6 +21,7 @@ import jakarta.persistence.Convert;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.thingsboard.server.common.data.id.MobileAppSettingsId; import org.thingsboard.server.common.data.id.MobileAppSettingsId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
@ -35,6 +36,7 @@ import org.thingsboard.server.dao.util.mapping.JsonConverter;
import java.util.UUID; import java.util.UUID;
@Data @Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor @NoArgsConstructor
@Entity @Entity
@Table(name = ModelConstants.MOBILE_APP_SETTINGS_TABLE_NAME) @Table(name = ModelConstants.MOBILE_APP_SETTINGS_TABLE_NAME)
@ -59,16 +61,14 @@ public class MobileAppSettingsEntity extends BaseSqlEntity<MobileAppSettings> {
private JsonNode qrCodeConfig; private JsonNode qrCodeConfig;
public MobileAppSettingsEntity(MobileAppSettings mobileAppSettings) { public MobileAppSettingsEntity(MobileAppSettings mobileAppSettings) {
if (mobileAppSettings.getId() != null) { this.setId(mobileAppSettings.getUuidId());
this.setId(mobileAppSettings.getId().getId());
}
this.setCreatedTime(mobileAppSettings.getCreatedTime()); this.setCreatedTime(mobileAppSettings.getCreatedTime());
this.tenantId = mobileAppSettings.getTenantId().getId(); this.tenantId = mobileAppSettings.getTenantId().getId();
this.useDefaultApp = mobileAppSettings.isUseDefaultApp(); this.useDefaultApp = mobileAppSettings.isUseDefaultApp();
this.androidConfig = toJson(mobileAppSettings.getAndroidConfig()); this.androidConfig = toJson(mobileAppSettings.getAndroidConfig());
this.iosConfig = toJson(mobileAppSettings.getIosConfig()); this.iosConfig = toJson(mobileAppSettings.getIosConfig());
this.qrCodeConfig = toJson(mobileAppSettings.getQrCodeConfig()); this.qrCodeConfig = toJson(mobileAppSettings.getQrCodeConfig());
} }
@Override @Override
public MobileAppSettings toData() { public MobileAppSettings toData() {
@ -81,4 +81,5 @@ public class MobileAppSettingsEntity extends BaseSqlEntity<MobileAppSettings> {
mobileAppSettings.setQrCodeConfig(fromJson(qrCodeConfig, QRCodeConfig.class)); mobileAppSettings.setQrCodeConfig(fromJson(qrCodeConfig, QRCodeConfig.class));
return mobileAppSettings; return mobileAppSettings;
} }
} }

7
dao/src/main/resources/sql/schema-entities.sql

@ -900,9 +900,10 @@ CREATE TABLE IF NOT EXISTS queue_stats (
CREATE TABLE IF NOT EXISTS mobile_app_settings ( CREATE TABLE IF NOT EXISTS mobile_app_settings (
id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY, id uuid NOT NULL CONSTRAINT mobile_app_settings_pkey PRIMARY KEY,
created_time bigint NOT NULL, created_time bigint NOT NULL,
tenant_id uuid UNIQUE NOT NULL, tenant_id uuid NOT NULL,
use_default_app boolean, use_default_app boolean,
android_config VARCHAR(1000), android_config VARCHAR(1000),
ios_config VARCHAR(1000), ios_config VARCHAR(1000),
qr_code_config VARCHAR(100000) qr_code_config VARCHAR(100000),
); CONSTRAINT mobile_app_settings_tenant_id_unq_key UNIQUE (tenant_id)
);

Loading…
Cancel
Save