Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: trans #831

Merged
merged 10 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,7 @@ module.exports = {
$dropUpload: true,
$tagStyle2: true,
$clearSelection: true,
DlgTransform: true,
$select2OpenTemplateResult: true,
},
}
2 changes: 1 addition & 1 deletion @rbv
Submodule @rbv updated from e66c14 to 97e889
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public JSONArray getTransforms(String sourceEntity, ID user) {
JSONObject item = EasyMetaFactory.toJSON(targetEntity);
item.put("transid", cb.getID("id"));
item.put("transName", cb.getString("name"));
item.put("previewMode", config.getIntValue("transformMode") == 2);
data.add(item);
}
return data;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/rebuild/core/metadata/DeleteRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import cn.devezhao.persist4j.engine.ID;
import cn.devezhao.persist4j.engine.StandardRecord;
import lombok.Getter;

/**
* 删除专用
Expand All @@ -19,8 +20,16 @@
public class DeleteRecord extends StandardRecord {
private static final long serialVersionUID = -7098132591224439549L;

@Getter
private boolean quietly;

public DeleteRecord(ID primaryid, ID editor) {
this(primaryid, editor, false);
}

public DeleteRecord(ID primaryid, ID editor, boolean quietly) {
super(MetadataHelper.getEntity(primaryid.getEntityCode()), editor);
this.setID(getEntity().getPrimaryField().getName(), primaryid);
this.quietly = quietly;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/rebuild/core/metadata/EntityHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public static Record parse(JSONObject data, ID user, boolean safetyUrl, boolean
String id = metadata.getString("id");
return new DeleteRecord(ID.valueOf(id), user);
}
if (metadata.getBooleanValue("deleteQuietly")) {
String id = metadata.getString("id");
return new DeleteRecord(ID.valueOf(id), user, true);
}

Record record = new EntityRecordCreator(MetadataHelper.getEntity(entityName), data, user, safetyUrl)
.create(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ record = record.getPrimary() == null ? create(record) : update(record);
for (int i = 0; i < details.size(); i++) {
Record d = details.get(i);
if (d instanceof DeleteRecord) {
des.delete(d.getPrimary());
detaileds.put(i, d.getPrimary());
// 安静删除,不触发任何系统逻辑
if (((DeleteRecord) d).isQuietly()) {
Application.getCommonsService().delete(d.getPrimary(), false);
} else {
des.delete(d.getPrimary());
detaileds.put(i, d.getPrimary());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.rebuild.core.Application;
import com.rebuild.core.configuration.ConfigBean;
import com.rebuild.core.configuration.ConfigurationException;
import com.rebuild.core.configuration.general.AutoFillinManager;
import com.rebuild.core.configuration.general.TransformManager;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.metadata.EntityRecordCreator;
Expand Down Expand Up @@ -172,6 +173,11 @@ public ID transform(ID sourceRecordId, ID specMainId) {
return theNewId;
}

/**
* @param record
* @param detailsList
* @return
*/
protected ID saveRecord(Record record, List<Record> detailsList) {
if (this.skipGuard) GeneralEntityServiceContextHolder.setSkipGuard(EntityHelper.UNSAVED_ID);

Expand Down Expand Up @@ -242,22 +248,22 @@ protected Record transformRecord(
// v3.7 clean
fieldsMapping.remove("_");

Record target = EntityHelper.forNew(targetEntity.getEntityCode(), getUser());
Record targetRecord = EntityHelper.forNew(targetEntity.getEntityCode(), getUser());

if (defaultValue != null) {
for (Map.Entry<String, Object> e : defaultValue.entrySet()) {
target.setObjectValue(e.getKey(), e.getValue());
targetRecord.setObjectValue(e.getKey(), e.getValue());
}
}

List<String> validFields = checkAndWarnFields(sourceEntity, fieldsMapping.values());
if (validFields.isEmpty()) {
// Fixed https://github.com/getrebuild/rebuild/issues/633
// fix: https://github.com/getrebuild/rebuild/issues/633
log.warn("No fields (var) for transform : {}", fieldsMapping);
}

validFields.add(sourceEntity.getPrimaryField().getName());
Record source = Application.getQueryFactory().recordNoFilter(sourceRecordId, validFields.toArray(new String[0]));
Record sourceRecord = Application.getQueryFactory().recordNoFilter(sourceRecordId, validFields.toArray(new String[0]));

// 所属用户
ID specOwningUser = null;
Expand All @@ -266,7 +272,7 @@ protected Record transformRecord(
final String targetField = e.getKey();

if (e.getValue() == null) {
if (forceNullValue) target.setNull(targetField);
if (forceNullValue) targetRecord.setNull(targetField);
continue;
}

Expand All @@ -277,39 +283,40 @@ protected Record transformRecord(

if (sourceAny instanceof JSONArray) {
Object sourceValue = ((JSONArray) sourceAny).get(0);
RecordVisitor.setValueByLiteral(targetField, sourceValue.toString(), target);
RecordVisitor.setValueByLiteral(targetField, sourceValue.toString(), targetRecord);

} else {
String sourceField = (String) sourceAny;
Object sourceValue = source.getObjectValue(sourceField);
Object sourceValue = sourceRecord.getObjectValue(sourceField);

if (sourceValue != null) {
EasyField sourceFieldEasy = EasyMetaFactory.valueOf(
Objects.requireNonNull(MetadataHelper.getLastJoinField(sourceEntity, sourceField)));

Object targetValue = sourceFieldEasy.convertCompatibleValue(sourceValue, targetFieldEasy);
target.setObjectValue(targetField, targetValue);
targetRecord.setObjectValue(targetField, targetValue);

} else if (forceNullValue) {
target.setNull(targetField);
targetRecord.setNull(targetField);
}
}

if (EntityHelper.OwningUser.equals(targetField)) {
specOwningUser = target.getID(EntityHelper.OwningUser);
specOwningUser = targetRecord.getID(EntityHelper.OwningUser);
}
}

if (specOwningUser != null) {
target.setID(EntityHelper.OwningDept,
targetRecord.setID(EntityHelper.OwningDept,
(ID) Application.getUserStore().getUser(specOwningUser).getOwningDept().getIdentity());
}

if (checkNullable) {
new EntityRecordCreator(targetEntity, JSONUtils.EMPTY_OBJECT, getUser()).verify(target);
AutoFillinManager.instance.fillinRecord(targetRecord);
new EntityRecordCreator(targetEntity, JSONUtils.EMPTY_OBJECT, getUser()).verify(targetRecord);
}

return target;
return targetRecord;
}

private List<String> checkAndWarnFields(Entity entity, Collection<?> fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.rebuild.core.service.general.transform;

import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSONArray;
Expand Down Expand Up @@ -75,21 +74,23 @@ public ID transform(ID sourceRecordId, ID specMainId) {
if (filter != null) querySourceSql = querySourceSql.replace("(1=1)", filter);

Object[][] dArray = Application.createQueryNoFilter(querySourceSql).array();
Map<String, Object> dvMap4Details = Collections.singletonMap(
MetadataHelper.getDetailToMainField(dTargetEntity).getName(), EntityHelper.UNSAVED_ID);

for (Object[] d : dArray) {
detailsList.add(transformRecord(
dSourceEntity, dTargetEntity, fmd, (ID) d[0], null, false, false, checkNullable));
Record dRecord = transformRecord(
dSourceEntity, dTargetEntity, fmd, (ID) d[0], dvMap4Details, false, false, checkNullable);
detailsList.add(dRecord);
}
}

Map<String, Object> dvMap4Detail = null;
if (specMainId != null) {
Field dtf = MetadataHelper.getDetailToMainField(targetEntity);
dvMap4Detail = Collections.singletonMap(dtf.getName(), specMainId);
}
// 目标是明细
Map<String, Object> dvMap4Details = specMainId == null ? null : Collections.singletonMap(
MetadataHelper.getDetailToMainField(targetEntity).getName(), specMainId);

Entity sourceEntity = MetadataHelper.getEntity(sourceRecordId.getEntityCode());
Record targetRecord = transformRecord(
sourceEntity, targetEntity, fieldsMapping, sourceRecordId, dvMap4Detail, false, false, checkNullable);
sourceEntity, targetEntity, fieldsMapping, sourceRecordId, dvMap4Details, false, false, checkNullable);

// v3.5 需要先回填
// 因为可能以回填字段作为条件进行转换一次判断
Expand Down
Loading
Loading