refactoring: 11_WidgetsBundleController
This commit is contained in:
parent
38e6286430
commit
b129b96a8c
@ -17,6 +17,7 @@ package org.thingsboard.server.controller;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@ -27,7 +28,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.id.WidgetsBundleId;
|
||||
@ -36,6 +36,7 @@ import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.common.data.security.Authority;
|
||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
|
||||
import org.thingsboard.server.queue.util.TbCoreComponent;
|
||||
import org.thingsboard.server.service.entitiy.widgetsBundle.TbWidgetsBundleService;
|
||||
import org.thingsboard.server.service.security.permission.Operation;
|
||||
import org.thingsboard.server.service.security.permission.Resource;
|
||||
|
||||
@ -57,8 +58,11 @@ import static org.thingsboard.server.controller.ControllerConstants.WIDGET_BUNDL
|
||||
@RestController
|
||||
@TbCoreComponent
|
||||
@RequestMapping("/api")
|
||||
@RequiredArgsConstructor
|
||||
public class WidgetsBundleController extends BaseController {
|
||||
|
||||
private final TbWidgetsBundleService tbWidgetsBundleService;
|
||||
|
||||
private static final String WIDGET_BUNDLE_DESCRIPTION = "Widget Bundle represents a group(bundle) of widgets. Widgets are grouped into bundle by type or use case. ";
|
||||
|
||||
@ApiOperation(value = "Get Widget Bundle (getWidgetsBundleById)",
|
||||
@ -93,7 +97,7 @@ public class WidgetsBundleController extends BaseController {
|
||||
public WidgetsBundle saveWidgetsBundle(
|
||||
@ApiParam(value = "A JSON value representing the Widget Bundle.", required = true)
|
||||
@RequestBody WidgetsBundle widgetsBundle) throws ThingsboardException {
|
||||
try {
|
||||
|
||||
if (Authority.SYS_ADMIN.equals(getCurrentUser().getAuthority())) {
|
||||
widgetsBundle.setTenantId(TenantId.SYS_TENANT_ID);
|
||||
} else {
|
||||
@ -101,15 +105,8 @@ public class WidgetsBundleController extends BaseController {
|
||||
}
|
||||
|
||||
checkEntity(widgetsBundle.getId(), widgetsBundle, Resource.WIDGETS_BUNDLE);
|
||||
WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
|
||||
|
||||
sendEntityNotificationMsg(getTenantId(), savedWidgetsBundle.getId(),
|
||||
widgetsBundle.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
|
||||
|
||||
return checkNotNull(savedWidgetsBundle);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
return tbWidgetsBundleService.save(widgetsBundle, getCurrentUser());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Delete widgets bundle (deleteWidgetsBundle)",
|
||||
@ -121,16 +118,9 @@ public class WidgetsBundleController extends BaseController {
|
||||
@ApiParam(value = WIDGET_BUNDLE_ID_PARAM_DESCRIPTION, required = true)
|
||||
@PathVariable("widgetsBundleId") String strWidgetsBundleId) throws ThingsboardException {
|
||||
checkParameter("widgetsBundleId", strWidgetsBundleId);
|
||||
try {
|
||||
WidgetsBundleId widgetsBundleId = new WidgetsBundleId(toUUID(strWidgetsBundleId));
|
||||
checkWidgetsBundleId(widgetsBundleId, Operation.DELETE);
|
||||
widgetsBundleService.deleteWidgetsBundle(getTenantId(), widgetsBundleId);
|
||||
|
||||
sendEntityNotificationMsg(getTenantId(), widgetsBundleId, EdgeEventActionType.DELETED);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
WidgetsBundle widgetsBundle = checkWidgetsBundleId(widgetsBundleId, Operation.DELETE);
|
||||
tbWidgetsBundleService.delete(widgetsBundle, getCurrentUser());
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Get Widget Bundles (getWidgetsBundles)",
|
||||
|
||||
@ -60,6 +60,7 @@ import org.thingsboard.server.dao.rule.RuleChainService;
|
||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
|
||||
import org.thingsboard.server.dao.tenant.TenantService;
|
||||
import org.thingsboard.server.dao.user.UserService;
|
||||
import org.thingsboard.server.dao.widget.WidgetsBundleService;
|
||||
import org.thingsboard.server.service.action.EntityActionService;
|
||||
import org.thingsboard.server.service.edge.EdgeNotificationService;
|
||||
import org.thingsboard.server.service.executors.DbCallbackExecutorService;
|
||||
@ -147,6 +148,8 @@ public abstract class AbstractTbEntityService {
|
||||
protected UserService userService;
|
||||
@Autowired
|
||||
protected TbResourceService resourceService;
|
||||
@Autowired
|
||||
protected WidgetsBundleService widgetsBundleService;
|
||||
|
||||
protected ListenableFuture<Void> removeAlarmsByEntityId(TenantId tenantId, EntityId entityId) {
|
||||
ListenableFuture<PageData<AlarmInfo>> alarmsFuture =
|
||||
|
||||
@ -100,8 +100,8 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifySendMsgToEdgeService(TenantId tenantId, RuleChain ruleChain, EdgeEventActionType edgeEventActionType) {
|
||||
sendEntityNotificationMsg(tenantId, ruleChain.getId(), edgeEventActionType);
|
||||
public <I extends EntityId> void notifySendMsgToEdgeService(TenantId tenantId, I entityId, EdgeEventActionType edgeEventActionType) {
|
||||
sendEntityNotificationMsg(tenantId, entityId, edgeEventActionType);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -57,7 +57,7 @@ public interface TbNotificationEntityService {
|
||||
void notifyDeleteRuleChain(TenantId tenantId, RuleChain ruleChain,
|
||||
List<EdgeId> relatedEdgeIds, SecurityUser user);
|
||||
|
||||
void notifySendMsgToEdgeService(TenantId tenantId, RuleChain ruleChain, EdgeEventActionType edgeEventActionType);
|
||||
<I extends EntityId> void notifySendMsgToEdgeService(TenantId tenantId, I entityId, EdgeEventActionType edgeEventActionType);
|
||||
|
||||
<E extends HasName, I extends EntityId> void notifyAssignOrUnassignEntityToCustomer(TenantId tenantId, I entityId,
|
||||
CustomerId customerId, E entity,
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright © 2016-2022 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* 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
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.service.entitiy.widgetsBundle;
|
||||
|
||||
import org.thingsboard.server.common.data.edge.EdgeEventActionType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
|
||||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
|
||||
import org.thingsboard.server.service.security.model.SecurityUser;
|
||||
|
||||
public class DefaultWidgetsBundleService extends AbstractTbEntityService implements TbWidgetsBundleService{
|
||||
@Override
|
||||
public WidgetsBundle save(WidgetsBundle widgetsBundle, SecurityUser user) throws ThingsboardException {
|
||||
try {
|
||||
WidgetsBundle savedWidgetsBundle = checkNotNull(widgetsBundleService.saveWidgetsBundle(widgetsBundle));
|
||||
notificationEntityService.notifySendMsgToEdgeService(widgetsBundle.getTenantId(), savedWidgetsBundle.getId(),
|
||||
widgetsBundle.getId() == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED);
|
||||
return savedWidgetsBundle;
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(WidgetsBundle widgetsBundle, SecurityUser user) throws ThingsboardException {
|
||||
try {
|
||||
widgetsBundleService.deleteWidgetsBundle(widgetsBundle.getTenantId(), widgetsBundle.getId());
|
||||
notificationEntityService.notifySendMsgToEdgeService(widgetsBundle.getTenantId(), widgetsBundle.getId(),
|
||||
EdgeEventActionType.DELETED);
|
||||
} catch (Exception e) {
|
||||
throw handleException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright © 2016-2022 The Thingsboard Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* 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
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.thingsboard.server.service.entitiy.widgetsBundle;
|
||||
|
||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
|
||||
import org.thingsboard.server.service.entitiy.SimpleTbEntityService;
|
||||
|
||||
public interface TbWidgetsBundleService extends SimpleTbEntityService<WidgetsBundle> {
|
||||
}
|
||||
@ -294,12 +294,12 @@ public class DefaultTbRuleChainService extends AbstractTbEntityService implement
|
||||
ruleChain, user, ActionType.UPDATED, false, null, ruleChainMetaData);
|
||||
|
||||
if (RuleChainType.EDGE.equals(ruleChain.getType())) {
|
||||
notificationEntityService.notifySendMsgToEdgeService(tenantId, ruleChain, EdgeEventActionType.UPDATED);
|
||||
notificationEntityService.notifySendMsgToEdgeService(tenantId, ruleChain.getId(), EdgeEventActionType.UPDATED);
|
||||
}
|
||||
|
||||
updatedRuleChains.forEach(updatedRuleChain -> {
|
||||
if (RuleChainType.EDGE.equals(ruleChain.getType())) {
|
||||
notificationEntityService.notifySendMsgToEdgeService(tenantId, updatedRuleChain, EdgeEventActionType.UPDATED);
|
||||
notificationEntityService.notifySendMsgToEdgeService(tenantId, updatedRuleChain.getId(), EdgeEventActionType.UPDATED);
|
||||
} else {
|
||||
try {
|
||||
RuleChainMetaData updatedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, updatedRuleChain.getId()));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user