Skip to content

Commit

Permalink
Fix 3.6 beta4 (#729)
Browse files Browse the repository at this point in the history
* fix: case TEXT to NTEXT

* fix: getReplacedUser

* be: view show

* fix: 字段类型转换属性冲突

* fix: *N


* CommonsUtils.DEVLOG

* feat: filter token: REP

* feat: advfilter valuesPlus

* lang

* fix: toJSONObject

* fix: DataList2Chart

* style

---------

Co-authored-by: devezhao <[email protected]>
  • Loading branch information
getrebuild and devezhao authored Mar 14, 2024
1 parent ea86c42 commit 8504946
Show file tree
Hide file tree
Showing 42 changed files with 336 additions and 141 deletions.
2 changes: 1 addition & 1 deletion @rbv
Submodule @rbv updated from a2b338 to c24b2e
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.rebuild</groupId>
<artifactId>rebuild</artifactId>
<version>3.6.0-beta3</version>
<version>3.6.0-beta4</version>
<name>rebuild</name>
<description>Building your business-systems freely!</description>
<url>https://getrebuild.com/</url>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/rebuild/core/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public class Application implements ApplicationListener<ApplicationStartedEvent>
/**
* Rebuild Version
*/
public static final String VER = "3.6.0-beta3";
public static final String VER = "3.6.0-beta4";
/**
* Rebuild Build [MAJOR]{1}[MINOR]{2}[PATCH]{2}[BUILD]{2}
*/
public static final int BUILD = 3060003;
public static final int BUILD = 3060004;

static {
// Driver for DB
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/com/rebuild/core/UserContextHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public static void set(ID user, String locale) {
}

/**
* 设置当前用户
*
* @param user
* @see #clearUser()
* @see #replaceUser(ID)
*/
public static void setUser(ID user) {
Assert.notNull(user, "[user] cannot be null");
Expand Down Expand Up @@ -119,20 +121,37 @@ public static void clearLocale() {
}

/**
* 设置当前用户,并保持原始用户(如有)
*
* @param user
* @see #getReplacedUser()
* @see #restoreUser()
* @see #setUser(ID)
*/
public static void replaceUser(ID user) {
Assert.notNull(user, "[user] cannot be null");

ID e = getUser(Boolean.TRUE);
// Keep origin
ID e = CALLER_PREV.get();
if (e == null) e = getUser(Boolean.TRUE);

if (e != null) CALLER_PREV.set(e);
else CALLER_PREV.remove();

CALLER.set(user);
}

/**
* 获取原始用户
*
* @return
* @see #replaceUser(ID)
*/
public static ID getReplacedUser() {
ID prev = CALLER_PREV.get();
if (prev != null) return prev;
return getUser();
}

/**
* @return
* @see #replaceUser(ID)
Expand All @@ -147,16 +166,6 @@ public static boolean restoreUser() {
return false;
}

/**
* @return
* @see #replaceUser(ID)
*/
public static ID getRestoreUser() {
ID prev = CALLER_PREV.get();
if (prev != null) return prev;
return getUser();
}

// --

/**
Expand Down
51 changes: 41 additions & 10 deletions src/main/java/com/rebuild/core/metadata/impl/Field2Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.dialect.Dialect;
import cn.devezhao.persist4j.dialect.FieldType;
import cn.devezhao.persist4j.engine.ID;
import cn.devezhao.persist4j.metadata.CascadeModel;
import cn.devezhao.persist4j.metadata.impl.AnyEntity;
import cn.devezhao.persist4j.metadata.impl.FieldImpl;
import cn.devezhao.persist4j.util.StringHelper;
import cn.devezhao.persist4j.util.support.Table;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hankcs.hanlp.HanLP;
import com.rebuild.core.Application;
import com.rebuild.core.metadata.EntityHelper;
Expand All @@ -38,12 +40,16 @@
import org.apache.commons.lang.CharSet;
import org.apache.commons.lang.StringUtils;

import java.sql.DataTruncation;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static com.rebuild.core.metadata.impl.EasyFieldConfigProps.NUMBER_CALCFORMULA;
import static com.rebuild.core.metadata.impl.EasyFieldConfigProps.NUMBER_NOTNEGATIVE;

/**
* 创建字段
*
Expand Down Expand Up @@ -421,9 +427,9 @@ protected String toPinyinName(final String text) {
* @return
*/
public boolean castType(Field field, DisplayType toType, boolean force) {
EasyField easyMeta = EasyMetaFactory.valueOf(field);
ID metaRecordId = easyMeta.getMetaId();
if (easyMeta.isBuiltin() || metaRecordId == null) {
EasyField fieldEasy = EasyMetaFactory.valueOf(field);
ID metaRecordId = fieldEasy.getMetaId();
if (fieldEasy.isBuiltin() || metaRecordId == null) {
throw new MetadataModificationException(Language.L("系统内置,不允许转换"));
}

Expand All @@ -434,9 +440,27 @@ public boolean castType(Field field, DisplayType toType, boolean force) {
}
}

Record meta = EntityHelper.forUpdate(metaRecordId, getUser(), false);
meta.setString("displayType", toType.name());
Application.getCommonsService().update(meta, false);
Record fieldMeta = EntityHelper.forUpdate(metaRecordId, getUser(), false);
fieldMeta.setString("displayType", toType.name());
// 长度
if (toType.getMaxLength() != FieldType.NO_NEED_LENGTH) {
fieldMeta.setInt("maxLength", toType.getMaxLength());
}
// 保留部分扩展配置,其余移除避免冲突
JSONObject extraAttrs = fieldEasy.getExtraAttrs();
if (!extraAttrs.isEmpty()) {
Object notNegative = extraAttrs.remove(NUMBER_NOTNEGATIVE);
Object calcFormula = extraAttrs.remove(NUMBER_CALCFORMULA);

extraAttrs.clear();
if (notNegative != null) extraAttrs.put(NUMBER_NOTNEGATIVE, notNegative);
if (calcFormula != null) extraAttrs.put(NUMBER_CALCFORMULA, calcFormula);

if (!extraAttrs.isEmpty()) {
fieldMeta.setString("extConfig", extraAttrs.toJSONString());
}
}
Application.getCommonsService().update(fieldMeta, false);

// 类型生效
DynamicMetadataContextHolder.setSkipLanguageRefresh();
Expand All @@ -452,18 +476,25 @@ public boolean castType(Field field, DisplayType toType, boolean force) {

alterTypeSql = String.format("alter table `%s` change column `%s` ",
field.getOwnEntity().getPhysicalName(), field.getPhysicalName());
alterTypeSql += ddl.toString().trim().replace(" ", "");
alterTypeSql += ddl.toString().trim().replace(" ", " ");

Application.getSqlExecutor().executeBatch(new String[]{alterTypeSql}, DDL_TIMEOUT);
log.info("Cast field type : {}", alterTypeSql);

} catch (Throwable ex) {
// 还原
meta.setString("displayType", EasyMetaFactory.getDisplayType(field).name());
Application.getCommonsService().update(meta, false);
fieldMeta.setString("displayType", EasyMetaFactory.getDisplayType(field).name());
Application.getCommonsService().update(fieldMeta, false);

log.error("DDL ERROR : \n" + alterTypeSql, ex);
throw new MetadataModificationException(ThrowableUtils.getRootCause(ex).getLocalizedMessage());

Throwable cause = ThrowableUtils.getRootCause(ex);
String causeMsg = cause.getLocalizedMessage();
if (cause instanceof DataTruncation) {
causeMsg = Language.L("已有数据内容长度超出限制,无法完成转换");
}
throw new MetadataModificationException(causeMsg);

} finally {
MetadataHelper.getMetadataFactory().refresh();
DynamicMetadataContextHolder.isSkipLanguageRefresh(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ protected FlowNode getNextNode(String currentNode) {

int bLength = nextNodes.size();
for (FlowNode node : nextNodes) {
// 匹配最后一个
// 匹配最后一个分支
if (--bLength == 0) {
return getNextNode(node.getNodeId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ public JSON build() {
}

int pageSize = config.getJSONObject("option").getIntValue("pageSize");
if (pageSize == 0) pageSize = 40;
if (pageSize <= 0) pageSize = 40;
if (pageSize >= 2000) pageSize = 2000;

JSONObject listConfig = new JSONObject();
listConfig.put("pageNo", 1);
listConfig.put("pageSize", Math.max(pageSize, 1));
listConfig.put("pageSize", pageSize);
listConfig.put("reload", false);
listConfig.put("statsField", false);
listConfig.put("entity", entity.getName());
listConfig.put("fields", fields);
listConfig.put("filter", config.getJSONObject("filter"));
if (sort != null) listConfig.put("sort", sort);

DataListBuilder builder = new DataListBuilderImpl(listConfig, getUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.core.Application;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.metadata.MetadataHelper;
import com.rebuild.core.privileges.UserService;
Expand Down Expand Up @@ -121,7 +120,7 @@ protected Set<ID> awareMentionCreate(Record record) {

String atAllKey = "@" + Language.L("所有人");
if (fakeContent.contains(atAllKey)
&& Application.getPrivilegesManager().allow(UserContextHolder.getUser(), ZeroEntry.AllowAtAllUsers)) {
&& Application.getPrivilegesManager().allow(getCurrentUser(), ZeroEntry.AllowAtAllUsers)) {
fakeContent = fakeContent.replace(atAllKey, "@" + UserService.ALLUSERS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.core.Application;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.privileges.OperationDeniedException;
import com.rebuild.core.privileges.UserHelper;
Expand Down Expand Up @@ -43,7 +42,7 @@ public int getEntityCode() {
public Record createOrUpdate(Record record) {
Integer type = record.getInt("type");
if (type != null && type == FeedsType.ANNOUNCEMENT.getMask()
&& !UserHelper.isAdmin(UserContextHolder.getUser())) {
&& !UserHelper.isAdmin(getCurrentUser())) {
throw new OperationDeniedException(Language.L("仅管理员可发布公告"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.core.Application;
import com.rebuild.core.RebuildException;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.DeleteRecord;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.metadata.MetadataHelper;
Expand Down Expand Up @@ -273,7 +272,7 @@ public int delete(ID recordId) {

@Override
public int delete(ID recordId, String[] cascades) {
final ID currentUser = UserContextHolder.getUser();
final ID currentUser = getCurrentUser();
final RecycleStore recycleBin = useRecycleStore(recordId);

int affected = this.deleteInternal(recordId);
Expand Down Expand Up @@ -317,7 +316,7 @@ public int delete(ID recordId, String[] cascades) {
* @throws DataSpecificationException
*/
protected int deleteInternal(ID recordId) throws DataSpecificationException {
Record delete = EntityHelper.forUpdate(recordId, UserContextHolder.getUser());
Record delete = EntityHelper.forUpdate(recordId, getCurrentUser());
if (!checkModifications(delete, BizzPermission.DELETE)) {
return 0;
}
Expand Down Expand Up @@ -374,14 +373,14 @@ public int assign(ID recordId, ID toUserId, String[] cascades) {
}

if (countObservers() > 0 && assignBefore != null) {
notifyObservers(OperatingContext.create(UserContextHolder.getUser(), BizzPermission.ASSIGN, assignBefore, assignAfter));
notifyObservers(OperatingContext.create(getCurrentUser(), BizzPermission.ASSIGN, assignBefore, assignAfter));
}
return affected;
}

@Override
public int share(ID recordId, ID toUserId, String[] cascades, int rights) {
final ID currentUser = UserContextHolder.getUser();
final ID currentUser = getCurrentUser();
final ID recordOrigin = recordId;
// v3.2.2 若为明细则转为主记录
if (MetadataHelper.getEntity(recordId.getEntityCode()).getMainEntity() != null) {
Expand Down Expand Up @@ -459,7 +458,7 @@ public int share(ID recordId, ID toUserId, String[] cascades, int rights) {

@Override
public int unshare(ID recordId, ID accessId) {
final ID currentUser = UserContextHolder.getUser();
final ID currentUser = getCurrentUser();

Record unsharedBefore = null;
if (countObservers() > 0) {
Expand Down Expand Up @@ -543,7 +542,7 @@ protected Map<String, Set<ID>> getCascadedRecords(ID mainRecordId, String[] casc
if (fromTriggerNoFilter) {
array = Application.createQueryNoFilter(sql).array();
} else {
Filter filter = Application.getPrivilegesManager().createQueryFilter(UserContextHolder.getUser(), action);
Filter filter = Application.getPrivilegesManager().createQueryFilter(getCurrentUser(), action);
array = Application.getQueryFactory().createQuery(sql, filter).array();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Record create(Record record) {
record = delegateService.create(record);

if (countObservers() > 0) {
notifyObservers(OperatingContext.create(UserContextHolder.getUser(), BizzPermission.CREATE, null, record));
notifyObservers(OperatingContext.create(getCurrentUser(), BizzPermission.CREATE, null, record));
}
return record;
}
Expand All @@ -76,15 +76,14 @@ public Record update(Record record) {
record = delegateService.update(record);

if (countObservers() > 0) {
notifyObservers(OperatingContext.create(UserContextHolder.getUser(), BizzPermission.UPDATE, before, record));
notifyObservers(OperatingContext.create(getCurrentUser(), BizzPermission.UPDATE, before, record));
}
return record;
}

@Override
public int delete(ID recordId) {
ID currentUser = UserContextHolder.getRestoreUser();
if (currentUser == null) currentUser = UserContextHolder.getUser();
final ID currentUser = getCurrentUser();

Record deleted = null;
if (countObservers() > 0) {
Expand Down Expand Up @@ -126,7 +125,7 @@ protected Record recordSnap(Record base, boolean allFields) {
* @return 返回 null 表示没开启
*/
protected RecycleStore useRecycleStore(ID recordId) {
final ID currentUser = UserContextHolder.getUser();
final ID currentUser = getCurrentUser();

RecycleStore recycleBin = null;
if (RecycleBinCleanerJob.isEnableRecycleBin()) {
Expand All @@ -141,4 +140,13 @@ protected RecycleStore useRecycleStore(ID recordId) {
return null;
}
}

/**
* 获取原始用户
*
* @return
*/
protected ID getCurrentUser() {
return UserContextHolder.getReplacedUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import cn.devezhao.persist4j.PersistManagerFactory;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.core.UserContextHolder;
import com.rebuild.core.configuration.ConfigBean;
import com.rebuild.core.metadata.EntityHelper;
import com.rebuild.core.service.DataSpecificationException;
Expand Down Expand Up @@ -37,7 +36,7 @@ protected BaseTaskService(PersistManagerFactory aPMFactory) {
* @return
*/
protected boolean checkModifications(ID user, ID taskOrProject) {
if (user == null) user = UserContextHolder.getUser();
if (user == null) user = getCurrentUser();
Assert.notNull(taskOrProject, "taskOrProject");

ConfigBean c = taskOrProject.getEntityCode() == EntityHelper.ProjectTask
Expand Down
Loading

0 comments on commit 8504946

Please sign in to comment.