fix: Name field show bad value

This commit is contained in:
devezhao-mbp 2019-12-12 23:18:57 +08:00
parent e6cfea114f
commit e88f6061e8
4 changed files with 54 additions and 19 deletions

View file

@ -317,10 +317,11 @@ public class FieldValueWrapper {
* 获取记录的 NAME/LABEL 字段值
*
* @param id
* @param defaultValue
* @return
* @throws NoRecordFoundException If no record found
*/
public static String getLabel(ID id) throws NoRecordFoundException {
public static String getLabel(ID id, String defaultValue) throws NoRecordFoundException {
Entity entity = MetadataHelper.getEntity(id.getEntityCode());
Field nameField = MetadataHelper.getNameField(entity);
@ -331,11 +332,23 @@ public class FieldValueWrapper {
Object nameLabel = instance.wrapFieldValue(nameValue[0], nameField, true);
if (nameLabel == null || StringUtils.isBlank(nameLabel.toString())) {
return NO_LABEL_PREFIX + id.toLiteral().toUpperCase();
if (defaultValue == null) {
defaultValue = NO_LABEL_PREFIX + id.toLiteral().toUpperCase();
}
return defaultValue;
}
return nameLabel.toString();
}
/**
* @param id
* @return
* @throws NoRecordFoundException
*/
public static String getLabel(ID id) throws NoRecordFoundException {
return getLabel(id, null);
}
/**
* @param id
* @return
@ -354,7 +367,7 @@ public class FieldValueWrapper {
* @param text
* @return Returns `{ id:xxx, text:xxx, entity:xxx }`
*/
public static JSON wrapMixValue(ID id, String text) {
public static JSONObject wrapMixValue(ID id, String text) {
if (id != null && StringUtils.isBlank(text)) {
text = id.getLabel();
}

View file

@ -20,6 +20,7 @@ package com.rebuild.server.helper.datalist;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.dialect.FieldType;
import cn.devezhao.persist4j.engine.ID;
import cn.devezhao.persist4j.query.compiler.SelectItem;
import com.alibaba.fastjson.JSON;
@ -28,6 +29,7 @@ import com.rebuild.server.configuration.portals.FieldValueWrapper;
import com.rebuild.server.metadata.MetadataHelper;
import com.rebuild.server.metadata.entity.DisplayType;
import com.rebuild.server.metadata.entity.EasyMeta;
import com.rebuild.server.service.bizz.UserHelper;
import com.rebuild.utils.JSONUtils;
import org.apache.commons.lang.StringUtils;
@ -70,6 +72,8 @@ public class DataListWrapper {
}
/**
* 设置权限过滤针对引用字段
*
* @param user
* @param joinFields
*/
@ -98,26 +102,41 @@ public class DataListWrapper {
data[rowIndex] = row;
}
Object nameValue = StringUtils.EMPTY;
Object nameValue = null;
for (int colIndex = 0; colIndex < selectFieldsLen; colIndex++) {
if (!checkHasJoinFieldPrivileges(selectFields[colIndex], original)) {
row[colIndex] = NO_READ_PRIVILEGES;
continue;
}
if (row[colIndex] == null) {
final Object value = row[colIndex];
if (value == null) {
row[colIndex] = StringUtils.EMPTY;
continue;
}
Field field = selectFields[colIndex].getField();
if (field.equals(nameFiled)) {
nameValue = row[colIndex];
SelectItem fieldItem = selectFields[colIndex];
Field field = fieldItem.getField();
if (field.equals(nameFiled) && !fieldItem.getFieldPath().contains(".")) {
nameValue = value;
if (nameValue == null) {
nameValue = StringUtils.EMPTY;
}
}
// 如果最终没能取得名称字段则补充
if (field.getType() == FieldType.PRIMARY) {
if (nameValue == null) {
nameValue = FieldValueWrapper.getLabel((ID) value, StringUtils.EMPTY);
} else {
nameValue = FieldValueWrapper.instance.wrapFieldValue(nameValue, nameFiled, true);
if (nameValue == null) {
nameValue = StringUtils.EMPTY;
}
}
((ID) value).setLabel(nameValue.toString());
}
row[colIndex] = wrapFieldValue(row[colIndex], nameValue, field);
row[colIndex] = wrapFieldValue(value, field);
}
}
@ -127,16 +146,14 @@ public class DataListWrapper {
}
/**
* see FormsBuilder#wrapFieldValue(Record, EasyMeta)
* @param value
* @param nameValue
* @param field
* @return
*/
protected Object wrapFieldValue(Object value, Object nameValue, Field field) {
protected Object wrapFieldValue(Object value, Field field) {
EasyMeta fieldEasy = EasyMeta.valueOf(field);
if (fieldEasy.getDisplayType() == DisplayType.ID) {
return FieldValueWrapper.wrapMixValue((ID) value, (String) nameValue);
return FieldValueWrapper.wrapMixValue((ID) value, null);
} else if (fieldEasy.getDisplayType() == DisplayType.CLASSIFICATION) {
return FieldValueWrapper.instance.wrapFieldValue(value, fieldEasy, true);
} else {
@ -152,10 +169,10 @@ public class DataListWrapper {
* @return
*/
private boolean checkHasJoinFieldPrivileges(SelectItem field, Object[] original) {
if (this.queryJoinFields == null) {
if (this.queryJoinFields == null || UserHelper.isAdmin(user)) {
return true;
}
String[] fieldPath = field.getFieldPath().split("\\.");
if (fieldPath.length == 1) {
return true;

View file

@ -146,12 +146,17 @@ public class FeedsListControll extends BasePageControll {
item.put("type", o[8]);
item.put("numComments", FeedsHelper.getNumOfComment((ID) o[0]));
// 相关记录
ID related = (ID) o[9];
if (related != null && MetadataHelper.containsEntity(related.getEntityCode())) {
EasyMeta entity = EasyMeta.valueOf(related.getEntityCode());
String recordLabel = FieldValueWrapper.getLabelNotry(related);
item.put("related", new Object[] { related, recordLabel, entity.getLabel(), entity.getIcon(), entity.getName() });
String nameValue = FieldValueWrapper.getLabelNotry(related);
JSONObject mixValue = FieldValueWrapper.wrapMixValue(related, nameValue);
mixValue.put("icon", entity.getIcon());
mixValue.put("entityLabel", entity.getLabel());
item.put("related", mixValue);
}
list.add(item);
}
writeSuccess(response,

View file

@ -377,8 +377,8 @@ function __renderRichContent(e) {
dangerouslySetInnerHTML={{ __html: contentHtml }}
/>
{e.related && <div className="related">
<span className="text-muted"><i className={`icon zmdi zmdi-${e.related[3]}`}></i> {e.related[2]} - </span>
<a target="_blank" href={`${rb.baseUrl}/app/list-and-view?id=${e.related[0]}`}>{e.related[1]}</a>
<span className="text-muted"><i className={`icon zmdi zmdi-${e.related.icon}`} /> {e.related.entityLabel} - </span>
<a target="_blank" href={`${rb.baseUrl}/app/list-and-view?id=${e.related.id}`}>{e.related.text}</a>
</div>
}
{(e.images || []).length > 0 && <div className="img-field">