diff --git a/src/main/java/com/rebuild/core/metadata/easymeta/EasyEntity.java b/src/main/java/com/rebuild/core/metadata/easymeta/EasyEntity.java index e28c85e98..543497845 100644 --- a/src/main/java/com/rebuild/core/metadata/easymeta/EasyEntity.java +++ b/src/main/java/com/rebuild/core/metadata/easymeta/EasyEntity.java @@ -9,6 +9,7 @@ package com.rebuild.core.metadata.easymeta; import cn.devezhao.persist4j.Entity; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.rebuild.utils.JSONUtils; import org.apache.commons.lang.StringUtils; @@ -45,8 +46,13 @@ public class EasyEntity extends BaseEasyMeta { @Override public JSON toJSON() { - return JSONUtils.toJSONObject( + JSONObject o = JSONUtils.toJSONObject( new String[] { "entity", "entityLabel", "entityCode", "icon" }, new Object[] { getName(), getLabel(), getRawMeta().getEntityCode(), getIcon() }); + + if (getRawMeta().getMainEntity() != null) o.put("mainEntity", getRawMeta().getMainEntity().getName()); + if (getRawMeta().getDetailEntity() != null) o.put("detailEntity", getRawMeta().getDetailEntity().getName()); + + return o; } } diff --git a/src/main/java/com/rebuild/core/service/general/transform/RecordTransfomer.java b/src/main/java/com/rebuild/core/service/general/transform/RecordTransfomer.java index 5006955f9..56735d7e5 100644 --- a/src/main/java/com/rebuild/core/service/general/transform/RecordTransfomer.java +++ b/src/main/java/com/rebuild/core/service/general/transform/RecordTransfomer.java @@ -23,6 +23,7 @@ import com.rebuild.core.service.TransactionManual; import com.rebuild.core.service.query.FilterRecordChecker; import com.rebuild.core.support.SetUser; import com.rebuild.core.support.general.N2NReferenceSupport; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.transaction.TransactionStatus; @@ -30,10 +31,14 @@ import java.util.*; /** * 转换记录 + * 1. 转换主记录 + * 2. 转换主记录+(多条)明细记录 + * 3. 转换明细记录 > 主实体 * * @author devezhao * @since 2020/10/27 */ +@Slf4j public class RecordTransfomer extends SetUser { final private Entity targetEntity; @@ -76,6 +81,9 @@ public class RecordTransfomer extends SetUser { final Entity sourceEntity = MetadataHelper.getEntity(sourceRecordId.getEntityCode()); final ID newId = transformRecord(sourceEntity, targetEntity, fieldsMapping, sourceRecordId, null); + if (newId == null) { + throw new ConfigurationException("Cannot transform record of main : " + transConfig); + } // 明细 @@ -132,6 +140,11 @@ public class RecordTransfomer extends SetUser { } List validFields = checkAndWarnFields(sourceEntity, fieldsMapping.values()); + if (validFields.isEmpty()) { + log.warn("No fields for transform"); + return null; + } + String querySource = String.format( "select %s from %s where %s = '%s'", StringUtils.join(validFields, ","), sourceEntity.getName(), @@ -139,20 +152,22 @@ public class RecordTransfomer extends SetUser { Record source = Application.createQueryNoFilter(querySource).record(); for (Map.Entry e : fieldsMapping.entrySet()) { + if (e.getValue() == null) continue; + String targetField = e.getKey(); String sourceField = (String) e.getValue(); - Object value = source.getObjectValue(sourceField); - if (value != null) { + Object sourceValue = source.getObjectValue(sourceField); + if (sourceValue != null) { EasyField targetFieldEasy = EasyMetaFactory.valueOf(targetEntity.getField(targetField)); EasyField sourceFieldEasy = EasyMetaFactory.valueOf(sourceEntity.getField(sourceField)); if (targetFieldEasy.getDisplayType() == DisplayType.N2NREFERENCE) { - value = N2NReferenceSupport.items(sourceFieldEasy.getRawMeta(), sourceRecordId); + sourceValue = N2NReferenceSupport.items(sourceFieldEasy.getRawMeta(), sourceRecordId); } - Object compatibleValue = sourceFieldEasy.convertCompatibleValue(value, targetFieldEasy); - target.setObjectValue(targetField, compatibleValue); + Object targetValue = sourceFieldEasy.convertCompatibleValue(sourceValue, targetFieldEasy); + target.setObjectValue(targetField, targetValue); } } @@ -163,6 +178,8 @@ public class RecordTransfomer extends SetUser { private List checkAndWarnFields(Entity entity, Collection fieldsName) { List valid = new ArrayList<>(); for (Object field : fieldsName) { + if (field == null) continue; + if (MetadataHelper.checkAndWarnField(entity, (String) field)) { valid.add((String) field); } diff --git a/src/main/java/com/rebuild/web/general/ModelExtrasController.java b/src/main/java/com/rebuild/web/general/ModelExtrasController.java index e401b7099..8740b05ec 100644 --- a/src/main/java/com/rebuild/web/general/ModelExtrasController.java +++ b/src/main/java/com/rebuild/web/general/ModelExtrasController.java @@ -7,7 +7,6 @@ See LICENSE and COMMERCIAL in the project root for license information. package com.rebuild.web.general; -import cn.devezhao.bizz.privileges.Permission; import cn.devezhao.bizz.privileges.impl.BizzPermission; import cn.devezhao.persist4j.Entity; import cn.devezhao.persist4j.Field; @@ -33,13 +32,15 @@ import com.rebuild.utils.JSONUtils; import com.rebuild.web.BaseController; import com.rebuild.web.EntityParam; import com.rebuild.web.IdParam; -import org.apache.commons.lang.StringUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; /** * 表单/视图 功能扩展 @@ -47,6 +48,7 @@ import java.util.*; * @author devezhao zhaofang123@gmail.com * @since 2019/05/20 */ +@Slf4j @RestController @RequestMapping("/app/entity/extras/") public class ModelExtrasController extends BaseController { @@ -75,8 +77,13 @@ public class ModelExtrasController extends BaseController { return RespBody.error(Language.L("当前记录不符合转换条件"), 400); } - ID newId = transfomer.transform(sourceRecord); - return RespBody.ok(newId); + try { + ID newId = transfomer.transform(sourceRecord); + return RespBody.ok(newId); + } catch (Exception ex) { + log.warn(">>>>> {}", ex.getLocalizedMessage()); + return RespBody.errorl("记录转换失败,请检查记录转换映射配置"); + } } @GetMapping("record-last-modified") diff --git a/src/main/resources/web/assets/js/admin/config-comps.js b/src/main/resources/web/assets/js/admin/config-comps.js index ed50113d9..e0000332e 100644 --- a/src/main/resources/web/assets/js/admin/config-comps.js +++ b/src/main/resources/web/assets/js/admin/config-comps.js @@ -139,3 +139,7 @@ class ConfigList extends React.Component { }) } } + +const ShowEnable = (enable) => { + return enable ? {$L('否')} : {$L('是')} +} diff --git a/src/main/resources/web/assets/js/admin/transform-editor.js b/src/main/resources/web/assets/js/admin/transform-editor.js index df4f13090..b092637fa 100644 --- a/src/main/resources/web/assets/js/admin/transform-editor.js +++ b/src/main/resources/web/assets/js/admin/transform-editor.js @@ -61,7 +61,7 @@ $(document).ready(() => { const tips = [] const fmd = fieldsMappingDetail ? fieldsMappingDetail.buildMapping() : null - if (!fmd) tips.push($L('明细实体未配置字段映射,因此明细记录不会转换')) + if (fieldsMappingDetail && !fmd) tips.push($L('明细实体未配置字段映射,因此明细记录不会转换')) function _save() { const config = { diff --git a/src/main/resources/web/assets/js/admin/transform-list.js b/src/main/resources/web/assets/js/admin/transform-list.js index ac46c68de..8cf2773fc 100644 --- a/src/main/resources/web/assets/js/admin/transform-list.js +++ b/src/main/resources/web/assets/js/admin/transform-list.js @@ -4,10 +4,10 @@ Copyright (c) REBUILD and/or its owners. All rights re rebuild is dual-licensed under commercial and open source licenses (GPLv3). See LICENSE and COMMERCIAL in the project root for license information. */ -/* global dlgActionAfter */ +/* global dlgActionAfter, ShowEnable */ $(document).ready(function () { - $('.J_add').click(() => renderRbcomp()) + $('.J_add').click(() => renderRbcomp()) renderRbcomp(, 'dataList') }) @@ -29,13 +29,7 @@ class TransformList extends ConfigList { {item[2]} {item[4]} - - {item[7] ? ( - {$L('否')} - ) : ( - {$L('是')} - )} - + {ShowEnable(item[7])} @@ -55,7 +49,7 @@ class TransformList extends ConfigList { } handleEdit(item) { - renderRbcomp() + renderRbcomp() } handleDelete(id) { @@ -71,7 +65,7 @@ class TransformList extends ConfigList { } } -class TransformEdit extends ConfigFormDlg { +class TransformEditor extends ConfigFormDlg { constructor(props) { super(props) this.subtitle = $L('记录转换映射') @@ -88,7 +82,7 @@ class TransformEdit extends ConfigFormDlg { (this._target = c)}> {(this.state.entities || []).map((item) => { + if (item.mainEntity) return null + return ( - ) @@ -122,13 +118,7 @@ class TransformEdit extends ConfigFormDlg {
@@ -139,7 +129,7 @@ class TransformEdit extends ConfigFormDlg { } componentDidMount() { - $.get('/commons/metadata/entities', (res) => { + $.get('/commons/metadata/entities?detail=true', (res) => { this.setState({ entities: res.data }, () => { this._$source = $(this._source).select2({ placeholder: $L('选择实体'), diff --git a/src/main/resources/web/general/detail-view.html b/src/main/resources/web/general/detail-view.html index 69b880aad..1681a7545 100644 --- a/src/main/resources/web/general/detail-view.html +++ b/src/main/resources/web/general/detail-view.html @@ -44,6 +44,13 @@ [[${bundle.L('打印')}]]
+
+ + +
@@ -75,6 +82,7 @@ type: 'DetailView', entity: ['[[${entityName}]]', '[[${entityLabel}]]', '[[${entityIcon}]]'], privileges: [(${entityPrivileges})], + transformTos: [(${TransformTos})], recordId: '[[${id}]]', }