From e2070b749479eb041a7a87d38ee91b0a3f87b2c7 Mon Sep 17 00:00:00 2001 From: suxinshuo <34018346+suxinshuo@users.noreply.github.com> Date: Sun, 12 May 2024 23:11:17 +0800 Subject: [PATCH] [Fix-3450][admin] Fix the issue of copying tasks with create time (#3472) Co-authored-by: suxinshuo --- .../main/java/org/dinky/data/model/Task.java | 1 + .../handler/DateMetaObjectHandler.java | 7 +- .../properties/MybatisPlusFillProperties.java | 2 + .../catalogue/factory/CatalogueFactory.java | 83 +++++++++++++++++++ .../catalogue/impl/CatalogueServiceImpl.java | 25 ++---- 5 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 dinky-admin/src/main/java/org/dinky/service/catalogue/factory/CatalogueFactory.java diff --git a/dinky-admin/src/main/java/org/dinky/data/model/Task.java b/dinky-admin/src/main/java/org/dinky/data/model/Task.java index ff897c88a7..d22dd461b6 100644 --- a/dinky-admin/src/main/java/org/dinky/data/model/Task.java +++ b/dinky-admin/src/main/java/org/dinky/data/model/Task.java @@ -55,6 +55,7 @@ public class Task extends SuperEntity { @ApiModelProperty(value = "Dialect", dataType = "String", notes = "Dialect for the task") private String dialect; + @TableField(fill = FieldFill.INSERT) @ApiModelProperty( value = "Tenant ID", dataType = "Integer", diff --git a/dinky-admin/src/main/java/org/dinky/mybatis/handler/DateMetaObjectHandler.java b/dinky-admin/src/main/java/org/dinky/mybatis/handler/DateMetaObjectHandler.java index b362005788..09a84aca81 100644 --- a/dinky-admin/src/main/java/org/dinky/mybatis/handler/DateMetaObjectHandler.java +++ b/dinky-admin/src/main/java/org/dinky/mybatis/handler/DateMetaObjectHandler.java @@ -19,6 +19,7 @@ package org.dinky.mybatis.handler; +import org.dinky.context.TenantContextHolder; import org.dinky.mybatis.properties.MybatisPlusFillProperties; import org.apache.ibatis.reflection.MetaObject; @@ -57,7 +58,6 @@ public boolean openUpdateFill() { @Override public void insertFill(MetaObject metaObject) { - Object createTime = getFieldValByName(mybatisPlusFillProperties.getCreateTimeField(), metaObject); Object updateTime = getFieldValByName(mybatisPlusFillProperties.getUpdateTimeField(), metaObject); if (createTime == null) { @@ -83,6 +83,7 @@ private void setFillFieldValue(MetaObject metaObject, int userId) { Object creator = getFieldValByName(mybatisPlusFillProperties.getCreatorField(), metaObject); Object updater = getFieldValByName(mybatisPlusFillProperties.getUpdaterField(), metaObject); Object operator = getFieldValByName(mybatisPlusFillProperties.getOperatorField(), metaObject); + Object tenantId = getFieldValByName(mybatisPlusFillProperties.getTenantIdField(), metaObject); if (creator == null) { setFieldValByName(mybatisPlusFillProperties.getCreatorField(), userId, metaObject); @@ -93,6 +94,10 @@ private void setFillFieldValue(MetaObject metaObject, int userId) { if (operator == null) { setFieldValByName(mybatisPlusFillProperties.getOperatorField(), userId, metaObject); } + if (tenantId == null) { + int loginTenantId = (Integer) TenantContextHolder.get(); + setFieldValByName(mybatisPlusFillProperties.getTenantIdField(), loginTenantId, metaObject); + } } @Override diff --git a/dinky-admin/src/main/java/org/dinky/mybatis/properties/MybatisPlusFillProperties.java b/dinky-admin/src/main/java/org/dinky/mybatis/properties/MybatisPlusFillProperties.java index 1a6d047062..91559f2985 100644 --- a/dinky-admin/src/main/java/org/dinky/mybatis/properties/MybatisPlusFillProperties.java +++ b/dinky-admin/src/main/java/org/dinky/mybatis/properties/MybatisPlusFillProperties.java @@ -49,4 +49,6 @@ public class MybatisPlusFillProperties { private String updaterField = "updater"; private String operatorField = "operator"; + + private String tenantIdField = "tenantId"; } diff --git a/dinky-admin/src/main/java/org/dinky/service/catalogue/factory/CatalogueFactory.java b/dinky-admin/src/main/java/org/dinky/service/catalogue/factory/CatalogueFactory.java new file mode 100644 index 0000000000..7036fe0f9b --- /dev/null +++ b/dinky-admin/src/main/java/org/dinky/service/catalogue/factory/CatalogueFactory.java @@ -0,0 +1,83 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.dinky.service.catalogue.factory; + +import org.dinky.data.enums.JobLifeCycle; +import org.dinky.data.model.Catalogue; +import org.dinky.data.model.Task; + +import java.util.Objects; + +import org.springframework.stereotype.Component; + +import cn.hutool.core.bean.BeanUtil; +import lombok.extern.slf4j.Slf4j; + +/** + * CatalogueFactory + * + * @since 2024/5/8 19:12 + */ +@Slf4j +@Component +public class CatalogueFactory { + + public Task getNewTask(Task oldTask, String newTaskName) { + if (Objects.isNull(oldTask)) { + return null; + } + Task newTask = new Task(); + BeanUtil.copyProperties(oldTask, newTask); + newTask.setId(null); + newTask.setJobInstanceId(null); + newTask.setName(newTaskName); + // set tasks to be in development status by default + newTask.setStep(JobLifeCycle.DEVELOP.getValue()); + newTask.setVersionId(null); + + // mybatis auto fill + newTask.setCreateTime(null); + newTask.setUpdateTime(null); + newTask.setCreator(null); + newTask.setUpdater(null); + newTask.setOperator(null); + newTask.setTenantId(null); + return newTask; + } + + public Catalogue getNewCatalogue(Catalogue paramCatalogue, Catalogue oldCatalogue, Task newTask) { + Catalogue newCatalogue = new Catalogue(); + BeanUtil.copyProperties(paramCatalogue, newCatalogue); + newCatalogue.setName(newTask.getName()); + newCatalogue.setIsLeaf(oldCatalogue.getIsLeaf()); + newCatalogue.setTaskId(newTask.getId()); + newCatalogue.setType(oldCatalogue.getType()); + newCatalogue.setParentId(oldCatalogue.getParentId()); + newCatalogue.setId(null); + + // mybatis auto fill + newCatalogue.setCreateTime(null); + newCatalogue.setUpdateTime(null); + newCatalogue.setCreator(null); + newCatalogue.setUpdater(null); + newCatalogue.setTenantId(null); + return newCatalogue; + } +} diff --git a/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java b/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java index 18ec19010a..d7e5878c99 100644 --- a/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java +++ b/dinky-admin/src/main/java/org/dinky/service/catalogue/impl/CatalogueServiceImpl.java @@ -49,6 +49,7 @@ import org.dinky.service.MonitorService; import org.dinky.service.TaskService; import org.dinky.service.catalogue.CatalogueService; +import org.dinky.service.catalogue.factory.CatalogueFactory; import org.dinky.service.catalogue.factory.CatalogueTreeSortFactory; import org.dinky.service.catalogue.strategy.CatalogueTreeSortStrategy; @@ -101,6 +102,8 @@ public class CatalogueServiceImpl extends SuperServiceImpl().eq(Catalogue::getTaskId, catalogue.getTaskId())); - - catalogue.setName(newTask.getName()); - catalogue.setIsLeaf(singleCatalogue.getIsLeaf()); - catalogue.setTaskId(newTask.getId()); - catalogue.setType(singleCatalogue.getType()); - catalogue.setParentId(singleCatalogue.getParentId()); - catalogue.setId(null); - - return this.save(catalogue); + Catalogue newCatalogue = catalogueFactory.getNewCatalogue(catalogue, singleCatalogue, newTask); + return this.save(newCatalogue); } @Override