mirror of
https://github.com/getrebuild/rebuild.git
synced 2024-09-20 23:45:55 +08:00
details transform
This commit is contained in:
parent
fb6f757ef9
commit
3b71c12466
|
@ -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<Entity> {
|
|||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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<String, Object> 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<String> checkAndWarnFields(Entity entity, Collection<?> fieldsName) {
|
||||
List<String> valid = new ArrayList<>();
|
||||
for (Object field : fieldsName) {
|
||||
if (field == null) continue;
|
||||
|
||||
if (MetadataHelper.checkAndWarnField(entity, (String) field)) {
|
||||
valid.add((String) field);
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -139,3 +139,7 @@ class ConfigList extends React.Component {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
const ShowEnable = (enable) => {
|
||||
return enable ? <span className="badge badge-warning font-weight-light">{$L('否')}</span> : <span className="badge badge-success font-weight-light">{$L('是')}</span>
|
||||
}
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -4,10 +4,10 @@ Copyright (c) REBUILD <https://getrebuild.com/> 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(<TransformEdit />))
|
||||
$('.J_add').click(() => renderRbcomp(<TransformEditor />))
|
||||
renderRbcomp(<TransformList />, 'dataList')
|
||||
})
|
||||
|
||||
|
@ -29,13 +29,7 @@ class TransformList extends ConfigList {
|
|||
</td>
|
||||
<td>{item[2]}</td>
|
||||
<td>{item[4]}</td>
|
||||
<td>
|
||||
{item[7] ? (
|
||||
<span className="badge badge-warning font-weight-light">{$L('否')}</span>
|
||||
) : (
|
||||
<span className="badge badge-success font-weight-light">{$L('是')}</span>
|
||||
)}
|
||||
</td>
|
||||
<td>{ShowEnable(item[7])}</td>
|
||||
<td>
|
||||
<DateShow date={item[5]} />
|
||||
</td>
|
||||
|
@ -55,7 +49,7 @@ class TransformList extends ConfigList {
|
|||
}
|
||||
|
||||
handleEdit(item) {
|
||||
renderRbcomp(<TransformEdit id={item[0]} name={item[6]} isDisabled={item[7]} />)
|
||||
renderRbcomp(<TransformEditor id={item[0]} name={item[6]} isDisabled={item[7]} />)
|
||||
}
|
||||
|
||||
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 {
|
|||
<select className="form-control form-control-sm" ref={(c) => (this._source = c)}>
|
||||
{(this.state.entities || []).map((item) => {
|
||||
return (
|
||||
<option key={'e-' + item.name} value={item.name}>
|
||||
<option key={item.name} value={item.name}>
|
||||
{item.label}
|
||||
</option>
|
||||
)
|
||||
|
@ -101,8 +95,10 @@ class TransformEdit extends ConfigFormDlg {
|
|||
<div className="col-sm-7">
|
||||
<select className="form-control form-control-sm" ref={(c) => (this._target = c)}>
|
||||
{(this.state.entities || []).map((item) => {
|
||||
if (item.mainEntity) return null
|
||||
|
||||
return (
|
||||
<option key={'e-' + item.name} value={item.name}>
|
||||
<option key={item.name} value={item.name}>
|
||||
{item.label}
|
||||
</option>
|
||||
)
|
||||
|
@ -122,13 +118,7 @@ class TransformEdit extends ConfigFormDlg {
|
|||
<div className="form-group row">
|
||||
<div className="col-sm-7 offset-sm-3">
|
||||
<label className="custom-control custom-control-sm custom-checkbox custom-control-inline mb-0">
|
||||
<input
|
||||
className="custom-control-input"
|
||||
type="checkbox"
|
||||
checked={this.state.isDisabled === true}
|
||||
data-id="isDisabled"
|
||||
onChange={this.handleChange}
|
||||
/>
|
||||
<input className="custom-control-input" type="checkbox" checked={this.state.isDisabled === true} data-id="isDisabled" onChange={this.handleChange} />
|
||||
<span className="custom-control-label">{$L('是否禁用')}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
@ -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('选择实体'),
|
||||
|
|
|
@ -44,6 +44,13 @@
|
|||
<a class="dropdown-item J_print" target="_blank" th:href="|${baseUrl}/app/${entityName}/print?id=${id}|"><i class="icon zmdi zmdi-print"></i> [[${bundle.L('打印')}]]</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 col-lg-6 btn-group J_trans hide">
|
||||
<button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown"><i class="icon zmdi zmdi-transform"></i> [[${bundle.L('转换为')}]]</button>
|
||||
<div class="dropdown-menu dropdown-menu-right">
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item admin-show" target="_blank" th:href="${baseUrl} + '/admin/transforms'"><i class="icon zmdi zmdi-settings"></i> [[${bundle.L('配置记录转换映射')}]]</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="view-date">
|
||||
<div class="form-line">
|
||||
|
@ -75,6 +82,7 @@
|
|||
type: 'DetailView',
|
||||
entity: ['[[${entityName}]]', '[[${entityLabel}]]', '[[${entityIcon}]]'],
|
||||
privileges: [(${entityPrivileges})],
|
||||
transformTos: [(${TransformTos})],
|
||||
recordId: '[[${id}]]',
|
||||
}
|
||||
</script>
|
||||
|
|
Loading…
Reference in a new issue