16 changed files with 258 additions and 58 deletions
@ -0,0 +1,60 @@ |
|||||
|
/** |
||||
|
* 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.sync.ie.exporting.impl; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.id.WidgetsBundleId; |
||||
|
import org.thingsboard.server.common.data.sync.ie.EntityExportSettings; |
||||
|
import org.thingsboard.server.common.data.sync.ie.WidgetsBundleExportData; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetsBundle; |
||||
|
import org.thingsboard.server.dao.widget.WidgetTypeService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Set; |
||||
|
|
||||
|
@Service |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
public class WidgetsBundleExportService extends BaseEntityExportService<WidgetsBundleId, WidgetsBundle, WidgetsBundleExportData> { |
||||
|
|
||||
|
private final WidgetTypeService widgetTypeService; |
||||
|
|
||||
|
@Override |
||||
|
protected void setRelatedEntities(TenantId tenantId, WidgetsBundle widgetsBundle, WidgetsBundleExportData exportData, EntityExportSettings settings) { |
||||
|
if (widgetsBundle.getTenantId() == null || widgetsBundle.getTenantId().isNullUid()) { |
||||
|
throw new IllegalArgumentException("Export of system Widget Bundles is not allowed"); |
||||
|
} |
||||
|
|
||||
|
List<WidgetTypeDetails> widgets = widgetTypeService.findWidgetTypesDetailsByTenantIdAndBundleAlias(tenantId, widgetsBundle.getAlias()); |
||||
|
exportData.setWidgets(widgets); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected WidgetsBundleExportData newExportData() { |
||||
|
return new WidgetsBundleExportData(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Set<EntityType> getSupportedEntityTypes() { |
||||
|
return Set.of(EntityType.WIDGETS_BUNDLE); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,96 @@ |
|||||
|
/** |
||||
|
* 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.sync.ie.importing.impl; |
||||
|
|
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
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; |
||||
|
import org.thingsboard.server.common.data.sync.ie.EntityImportSettings; |
||||
|
import org.thingsboard.server.common.data.sync.ie.WidgetsBundleExportData; |
||||
|
import org.thingsboard.server.common.data.widget.BaseWidgetType; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeInfo; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetsBundle; |
||||
|
import org.thingsboard.server.dao.widget.WidgetTypeService; |
||||
|
import org.thingsboard.server.dao.widget.WidgetsBundleService; |
||||
|
import org.thingsboard.server.queue.util.TbCoreComponent; |
||||
|
import org.thingsboard.server.service.security.model.SecurityUser; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@Service |
||||
|
@TbCoreComponent |
||||
|
@RequiredArgsConstructor |
||||
|
public class WidgetsBundleImportService extends BaseEntityImportService<WidgetsBundleId, WidgetsBundle, WidgetsBundleExportData> { |
||||
|
|
||||
|
private final WidgetsBundleService widgetsBundleService; |
||||
|
private final WidgetTypeService widgetTypeService; |
||||
|
|
||||
|
@Override |
||||
|
protected void setOwner(TenantId tenantId, WidgetsBundle widgetsBundle, IdProvider idProvider) { |
||||
|
widgetsBundle.setTenantId(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected WidgetsBundle prepareAndSave(TenantId tenantId, WidgetsBundle widgetsBundle, WidgetsBundleExportData exportData, IdProvider idProvider, EntityImportSettings importSettings) { |
||||
|
WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle); |
||||
|
if (widgetsBundle.getId() == null) { |
||||
|
for (WidgetTypeDetails widget : exportData.getWidgets()) { |
||||
|
widget.setId(null); |
||||
|
widget.setTenantId(tenantId); |
||||
|
widget.setBundleAlias(savedWidgetsBundle.getAlias()); |
||||
|
widgetTypeService.saveWidgetType(widget); |
||||
|
} |
||||
|
} else { |
||||
|
Map<String, WidgetTypeInfo> existingWidgets = widgetTypeService.findWidgetTypesInfosByTenantIdAndBundleAlias(tenantId, savedWidgetsBundle.getAlias()).stream() |
||||
|
.collect(Collectors.toMap(BaseWidgetType::getAlias, w -> w)); |
||||
|
for (WidgetTypeDetails widget : exportData.getWidgets()) { |
||||
|
WidgetTypeInfo existingWidget; |
||||
|
if ((existingWidget = existingWidgets.remove(widget.getAlias())) != null) { |
||||
|
widget.setId(existingWidget.getId()); |
||||
|
widget.setCreatedTime(existingWidget.getCreatedTime()); |
||||
|
} else { |
||||
|
widget.setId(null); |
||||
|
} |
||||
|
widget.setTenantId(tenantId); |
||||
|
widget.setBundleAlias(savedWidgetsBundle.getAlias()); |
||||
|
widgetTypeService.saveWidgetType(widget); |
||||
|
} |
||||
|
existingWidgets.values().stream() |
||||
|
.map(BaseWidgetType::getId) |
||||
|
.forEach(widgetTypeId -> widgetTypeService.deleteWidgetType(tenantId, widgetTypeId)); |
||||
|
} |
||||
|
return savedWidgetsBundle; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void onEntitySaved(SecurityUser user, WidgetsBundle savedWidgetsBundle, WidgetsBundle oldWidgetsBundle) throws ThingsboardException { |
||||
|
super.onEntitySaved(user, savedWidgetsBundle, oldWidgetsBundle); |
||||
|
entityNotificationService.notifySendMsgToEdgeService(user.getTenantId(), savedWidgetsBundle.getId(), |
||||
|
oldWidgetsBundle == null ? EdgeEventActionType.ADDED : EdgeEventActionType.UPDATED); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public EntityType getEntityType() { |
||||
|
return EntityType.WIDGETS_BUNDLE; |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
/** |
||||
|
* 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.common.data.sync.ie; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
|
import lombok.Data; |
||||
|
import lombok.EqualsAndHashCode; |
||||
|
import org.thingsboard.server.common.data.widget.BaseWidgetType; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails; |
||||
|
import org.thingsboard.server.common.data.widget.WidgetsBundle; |
||||
|
|
||||
|
import java.util.Comparator; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
@EqualsAndHashCode(callSuper = true) |
||||
|
public class WidgetsBundleExportData extends EntityExportData<WidgetsBundle> { |
||||
|
|
||||
|
@JsonProperty(index = 3) |
||||
|
private List<WidgetTypeDetails> widgets; |
||||
|
|
||||
|
@Override |
||||
|
public EntityExportData<WidgetsBundle> sort() { |
||||
|
super.sort(); |
||||
|
widgets.sort(Comparator.comparing(BaseWidgetType::getAlias)); |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue