From 4261c79a6bbac1d0ae7d2161d39812974eb21b15 Mon Sep 17 00:00:00 2001 From: FangfangZhao Date: Fri, 4 Jan 2019 23:12:59 +0800 Subject: [PATCH] bizz entity QuickCode --- .../metadata/entityhub/DisplayType.java | 20 ++++---- .../metadata/entityhub/Field2Schema.java | 9 +++- .../service/base/QuickCodeReindexTask.java | 46 ++++++++++++++++--- .../web/base/entity/ReferenceSearch.java | 7 ++- src/main/resources/metadata-conf.xml | 12 +++-- src/main/webapp/assets/js/assign-share.jsx | 2 +- .../helper/task/QuickCodeReindexTaskTest.java | 20 ++++++++ 7 files changed, 90 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/rebuild/server/metadata/entityhub/DisplayType.java b/src/main/java/com/rebuild/server/metadata/entityhub/DisplayType.java index 192d2363b..8ddd23a60 100644 --- a/src/main/java/com/rebuild/server/metadata/entityhub/DisplayType.java +++ b/src/main/java/com/rebuild/server/metadata/entityhub/DisplayType.java @@ -27,10 +27,10 @@ import cn.devezhao.persist4j.dialect.Type; */ public enum DisplayType { - NUMBER("数字", FieldType.LONG, 0, "##,###"), - DECIMAL("货币", FieldType.DECIMAL, 0, "##,##0.00"), - DATE("日期", FieldType.DATE, 0, "yyyy-MM-dd"), - DATETIME("日期时间", FieldType.TIMESTAMP, 0, "yyyy-MM-dd HH:mm:ss"), + NUMBER("数字", FieldType.LONG, -1, "##,###"), + DECIMAL("货币", FieldType.DECIMAL, -1, "##,##0.00"), + DATE("日期", FieldType.DATE, -1, "yyyy-MM-dd"), + DATETIME("日期时间", FieldType.TIMESTAMP, -1, "yyyy-MM-dd HH:mm:ss"), TEXT("文本", FieldType.STRING, 300, null), NTEXT("大文本", FieldType.TEXT, 6000, null), EMAIL("邮箱", FieldType.STRING, 100, null), @@ -39,14 +39,14 @@ public enum DisplayType { SERIES("自动编号", FieldType.STRING, 40, "{YYYYMMDD}-{0000}"), IMAGE("图片", FieldType.STRING, 700, null), FILE("附件", FieldType.STRING, 700, null), - PICKLIST("列表", FieldType.REFERENCE), - REFERENCE("引用", FieldType.REFERENCE), + PICKLIST("列表", FieldType.REFERENCE, -1, null), + REFERENCE("引用", FieldType.REFERENCE, -1, null), // 待启用/内部用 - ID("主键", FieldType.PRIMARY), - ANYREFERENCE("任意引用", FieldType.ANY_REFERENCE), - BOOL("布尔", FieldType.BOOL), - LOCATION("位置", FieldType.STRING, 50, null), + ID("主键", FieldType.PRIMARY, -1, null), + ANYREFERENCE("任意引用", FieldType.ANY_REFERENCE, -1, null), + BOOL("布尔", FieldType.BOOL, -1, null), + LOCATION("位置", FieldType.STRING, 70, null), ; diff --git a/src/main/java/com/rebuild/server/metadata/entityhub/Field2Schema.java b/src/main/java/com/rebuild/server/metadata/entityhub/Field2Schema.java index f6ef91380..b5e58a60c 100644 --- a/src/main/java/com/rebuild/server/metadata/entityhub/Field2Schema.java +++ b/src/main/java/com/rebuild/server/metadata/entityhub/Field2Schema.java @@ -196,6 +196,7 @@ public class Field2Schema { if (StringUtils.isNotBlank(comments)) { record.setString("comments", comments); } + if (displayType == DisplayType.PICKLIST) { refEntity = "PickList"; } @@ -207,6 +208,12 @@ public class Field2Schema { } } + int maxLength = displayType.getMaxLength(); + if (EntityHelper.QuickCode.equals(fieldName)) { + maxLength = 70; + } + record.setInt("maxLength", maxLength); + if (displayType == DisplayType.REFERENCE && StringUtils.isBlank(refEntity)) { throw new ModificationMetadataException("引用字段必须指定引用实体"); } @@ -218,7 +225,7 @@ public class Field2Schema { String defaultValue = EntityHelper.IsDeleted.equals(fieldName) ? "F" : null; Field unsafeField = new FieldImpl( - fieldName, physicalName, fieldLabel, entity, displayType.getFieldType(), CascadeModel.Ignore, displayType.getMaxLength(), + fieldName, physicalName, fieldLabel, entity, displayType.getFieldType(), CascadeModel.Ignore, maxLength, dbNullable, creatable, updatable, true, 6, defaultValue, autoValue); if (entity instanceof UnsafeEntity) { ((UnsafeEntity) entity).addField(unsafeField); diff --git a/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java b/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java index a9f0f2ebc..69910d64c 100644 --- a/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java +++ b/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java @@ -21,6 +21,7 @@ package com.rebuild.server.service.base; import java.util.List; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; import com.github.stuxuhai.jpinyin.PinyinHelper; import com.rebuild.server.Application; @@ -71,6 +72,9 @@ public class QuickCodeReindexTask extends BulkTask { this.setTotal(records.size() + this.getTotal() + 1); for (Record o : records) { + if (this.isInterrupted()) { + break; + } this.setCompleteOne(); String quickCodeNew = generateQuickCode(o); @@ -82,13 +86,17 @@ public class QuickCodeReindexTask extends BulkTask { } Record record = EntityHelper.forUpdate(o.getPrimary(), UserService.SYSTEM_USER); - record.setString("quickCode", quickCodeNew); + if (StringUtils.isBlank(quickCodeNew)) { + record.setNull(EntityHelper.QuickCode); + } else { + record.setString(EntityHelper.QuickCode, quickCodeNew); + } record.removeValue(EntityHelper.ModifiedBy); record.removeValue(EntityHelper.ModifiedOn); Application.getCommonService().update(record); } - if (records.size() < 1000) { + if (records.size() < 1000 || this.isInterrupted()) { break; } } @@ -131,6 +139,11 @@ public class QuickCodeReindexTask extends BulkTask { } /** + * 你好世界 - NHSJ + * HelloWorld - HW + * hello world - HW + * 43284327432 - "" + * * @param nameVal * @return */ @@ -139,12 +152,31 @@ public class QuickCodeReindexTask extends BulkTask { return StringUtils.EMPTY; } + // 提取 0-9+a-z+A-Z+中文+空格 + nameVal = nameVal.replaceAll("[^a-zA-Z0-9\\s\u4e00-\u9fa5]", ""); + String quickCode = StringUtils.EMPTY; - try { - quickCode = PinyinHelper.getShortPinyin(nameVal).toUpperCase(); - } catch (Exception e) { - LOG.error("QuickCode shorting error : " + nameVal, e); + if (nameVal.matches("[a-zA-Z0-9\\s]+")) { + String aplit[] = nameVal.split("(?=[A-Z\\s])"); + StringBuffer sb = new StringBuffer(); + for (String a : aplit) { + if (a.trim().length() > 0) { + sb.append(a.trim().substring(0, 1)); + } + } + quickCode = sb.toString(); + } else { + nameVal = nameVal.replaceAll(" ", ""); + try { + quickCode = PinyinHelper.getShortPinyin(nameVal); + } catch (Exception e) { + LOG.error("QuickCode shorting error : " + nameVal, e); + } } - return quickCode; + + if (NumberUtils.isDigits(quickCode)) { + quickCode = StringUtils.EMPTY; + } + return quickCode.toUpperCase(); } } diff --git a/src/main/java/com/rebuild/web/base/entity/ReferenceSearch.java b/src/main/java/com/rebuild/web/base/entity/ReferenceSearch.java index b2de3ecd6..179e2b834 100644 --- a/src/main/java/com/rebuild/web/base/entity/ReferenceSearch.java +++ b/src/main/java/com/rebuild/web/base/entity/ReferenceSearch.java @@ -65,6 +65,7 @@ public class ReferenceSearch extends BaseControll { writeSuccess(response, ArrayUtils.EMPTY_STRING_ARRAY); return; } + q = StringEscapeUtils.escapeSql(q); Entity metaEntity = MetadataHelper.getEntity(entity); Field nameField = MetadataHelper.getNameField(metaEntity); @@ -74,14 +75,16 @@ public class ReferenceSearch extends BaseControll { } if (StringUtils.isBlank(qFields)) { qFields = nameField.getName(); + if (metaEntity.containsField(EntityHelper.QuickCode)) { + qFields += "," + EntityHelper.QuickCode; + } } - q = StringEscapeUtils.escapeSql(q); List or = new ArrayList<>(); for (String field : qFields.split(",")) { if (!metaEntity.containsField(field)) { LOG.warn("No field for search : " + field); - } else if (StringUtils.isNotBlank(q)) { + } else { or.add(field + " like '%" + q + "%'"); } } diff --git a/src/main/resources/metadata-conf.xml b/src/main/resources/metadata-conf.xml index 107ff8722..e657e7c64 100644 --- a/src/main/resources/metadata-conf.xml +++ b/src/main/resources/metadata-conf.xml @@ -21,7 +21,7 @@ - + @@ -31,12 +31,14 @@ + + @@ -80,12 +82,12 @@ - - + + - - + + diff --git a/src/main/webapp/assets/js/assign-share.jsx b/src/main/webapp/assets/js/assign-share.jsx index 73c1f6554..193c1350b 100644 --- a/src/main/webapp/assets/js/assign-share.jsx +++ b/src/main/webapp/assets/js/assign-share.jsx @@ -60,7 +60,7 @@ class DlgAssign extends RbModalHandler { data: function(params) { let query = { entity: 'User', - qfields: 'loginName,fullName,email', + qfields: 'loginName,fullName,email,quickCode', q: params.term } return query diff --git a/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java b/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java index 60bb30545..bfcc691c4 100644 --- a/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java +++ b/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java @@ -18,9 +18,12 @@ along with this program. If not, see . package com.rebuild.server.helper.task; +import org.junit.Assert; import org.junit.Test; import com.rebuild.server.TestSupport; +import com.rebuild.server.metadata.MetadataHelper; +import com.rebuild.server.service.base.QuickCodeReindexTask; /** * @@ -29,8 +32,25 @@ import com.rebuild.server.TestSupport; */ public class QuickCodeReindexTaskTest extends TestSupport { + @Test + public void testGenerateQuickCode() throws Exception { + Assert.assertFalse("NHHSJ".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("你 好 hello 世 界"))); + Assert.assertTrue("HW".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("hello world ........"))); + Assert.assertTrue("HW".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("HelloWorld!"))); + Assert.assertTrue("NHSJ".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("你好世界"))); + Assert.assertTrue("NHSJ".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("你 好 世 界"))); + Assert.assertTrue("".equalsIgnoreCase(QuickCodeReindexTask.generateQuickCode("54325432543"))); + } + @Test public void testReindex() throws Exception { + new QuickCodeReindexTask(MetadataHelper.getEntity("User")).run(); + new QuickCodeReindexTask(MetadataHelper.getEntity("Role")).run(); + new QuickCodeReindexTask(MetadataHelper.getEntity("Department")).run(); + String entity = "h45hy54hy"; + if (MetadataHelper.containsEntity(entity)) { + new QuickCodeReindexTask(MetadataHelper.getEntity(entity)).run(); + } } }