use of com.rebuild.core.metadata.impl.Field2Schema in project rebuild by getrebuild.
the class MetaschemaImporter method performEntity.
/**
* @param schema
* @param mainEntity
* @return
* @throws MetadataModificationException
*/
private String performEntity(JSONObject schema, String mainEntity) throws MetadataModificationException {
final String entityName = schema.getString("entity");
final String entityLabel = schema.getString("entityLabel");
Entity2Schema entity2Schema = new Entity2Schema(this.getUser());
entity2Schema.createEntity(entityName, entityLabel, schema.getString("comments"), mainEntity, false);
Entity newEntity = MetadataHelper.getEntity(entityName);
this.setCompleted((int) (this.getCompleted() * 1.5));
JSONArray fields = schema.getJSONArray("fields");
try {
List<Field> fieldsList = new ArrayList<>();
for (Object field : fields) {
Field unsafe = performField((JSONObject) field, newEntity);
if (unsafe != null)
fieldsList.add(unsafe);
}
// 同步字段到数据库
new Field2Schema(UserService.ADMIN_USER).schema2Database(newEntity, fieldsList.toArray(new Field[0]));
} catch (Exception ex) {
entity2Schema.dropEntity(newEntity, true);
if (ex instanceof MetadataModificationException) {
throw ex;
} else {
throw new MetadataModificationException(ex);
}
}
Record needUpdate = EntityHelper.forUpdate(EasyMetaFactory.valueOf(newEntity).getMetaId(), this.getUser(), false);
String nameField = schema.getString("nameField");
if (nameField != null) {
needUpdate.setString("nameField", nameField);
}
String entityIcon = schema.getString("entityIcon");
if (entityIcon != null) {
needUpdate.setString("icon", entityIcon);
}
String quickFields = schema.getString("quickFields");
if (quickFields != null) {
needUpdate.setString("extConfig", JSONUtils.toJSONObject("quickFields", quickFields).toJSONString());
}
if (needUpdate.getAvailableFieldIterator().hasNext()) {
Application.getCommonsService().update(needUpdate);
}
// 刷新元数据
MetadataHelper.getMetadataFactory().refresh();
// 表单回填
JSONArray fillins = schema.getJSONArray(MetaSchemaGenerator.CFG_FILLINS);
if (fillins != null) {
for (Object o : fillins) {
performFillin(entityName, (JSONObject) o);
}
}
// 布局
JSONObject layouts = schema.getJSONObject(MetaSchemaGenerator.CFG_LAYOUTS);
if (layouts != null) {
for (Map.Entry<String, Object> e : layouts.entrySet()) {
performLayout(entityName, e.getKey(), (JSON) e.getValue());
}
}
// 高级查询
JSONArray filters = schema.getJSONArray(MetaSchemaGenerator.CFG_FILTERS);
if (filters != null) {
for (Object o : filters) {
performFilter(entityName, (JSONObject) o);
}
}
// 触发器
JSONArray triggers = schema.getJSONArray(MetaSchemaGenerator.CFG_TRIGGERS);
if (triggers != null) {
for (Object o : triggers) {
performTrigger(entityName, (JSONObject) o);
}
}
// 审批流程
JSONArray approvals = schema.getJSONArray(MetaSchemaGenerator.CFG_APPROVALS);
if (approvals != null) {
for (Object o : approvals) {
performApproval(entityName, (JSONObject) o);
}
}
// 记录转换
JSONArray transforms = schema.getJSONArray(MetaSchemaGenerator.CFG_TRANSFORMS);
if (transforms != null) {
for (Object o : transforms) {
performTransform(entityName, (JSONObject) o);
}
}
return entityName;
}
use of com.rebuild.core.metadata.impl.Field2Schema in project rebuild by getrebuild.
the class MetaschemaImporter method performField.
private Field performField(JSONObject schemaField, Entity belong) {
String fieldName = schemaField.getString("field");
String fieldLabel = schemaField.getString("fieldLabel");
String displayType = schemaField.getString("displayType");
JSON extConfig = schemaField.getJSONObject("extConfig");
DisplayType dt = DisplayType.valueOf(displayType);
if (dt == DisplayType.ID || MetadataHelper.isCommonsField(fieldName)) {
return null;
}
Field unsafeField = new Field2Schema(this.getUser()).createUnsafeField(belong, fieldName, fieldLabel, dt, schemaField.getBooleanValue("nullable"), true, schemaField.getBooleanValue("updatable"), !schemaField.containsKey("repeatable") || schemaField.getBooleanValue("repeatable"), !schemaField.containsKey("queryable") || schemaField.getBooleanValue("queryable"), schemaField.getString("comments"), schemaField.getString("refEntity"), null, extConfig, schemaField.getString("defaultValue"));
if (DisplayType.PICKLIST == dt || DisplayType.MULTISELECT == dt) {
picklistHolders.put(unsafeField, performPickList(schemaField.getJSONArray("items")));
}
return unsafeField;
}
use of com.rebuild.core.metadata.impl.Field2Schema in project rebuild by getrebuild.
the class MetaFieldController method fieldNew.
@PostMapping("field-new")
public RespBody fieldNew(HttpServletRequest request) {
JSONObject reqJson = (JSONObject) ServletUtils.getRequestJson(request);
String entityName = reqJson.getString("entity");
String label = reqJson.getString("label");
String type = reqJson.getString("type");
String comments = reqJson.getString("comments");
String refEntity = reqJson.getString("refEntity");
String refClassification = reqJson.getString("refClassification");
String stateClass = reqJson.getString("stateClass");
Entity entity = MetadataHelper.getEntity(entityName);
DisplayType dt = DisplayType.valueOf(type);
JSON extConfig = null;
if (dt == DisplayType.CLASSIFICATION) {
ID dataId = ID.valueOf(refClassification);
extConfig = JSONUtils.toJSONObject(EasyFieldConfigProps.CLASSIFICATION_USE, dataId);
} else if (dt == DisplayType.STATE) {
if (!StateHelper.isStateClass(stateClass)) {
return RespBody.errorl("无效状态类 (Enum)");
}
extConfig = JSONUtils.toJSONObject(EasyFieldConfigProps.STATE_CLASS, stateClass);
}
try {
String fieldName = new Field2Schema().createField(entity, label, dt, comments, refEntity, extConfig);
return RespBody.ok(fieldName);
} catch (Exception ex) {
return RespBody.error(ex.getLocalizedMessage());
}
}
use of com.rebuild.core.metadata.impl.Field2Schema in project rebuild by getrebuild.
the class MetaFieldController method fieldDrop.
@RequestMapping("field-drop")
public RespBody fieldDrop(@IdParam ID fieldId) {
Object[] fieldRecord = Application.createQueryNoFilter("select belongEntity,fieldName from MetaField where fieldId = ?").setParameter(1, fieldId).unique();
Field field = MetadataHelper.getEntity((String) fieldRecord[0]).getField((String) fieldRecord[1]);
boolean drop = new Field2Schema().dropField(field, false);
return drop ? RespBody.ok() : RespBody.error();
}
use of com.rebuild.core.metadata.impl.Field2Schema in project rebuild by getrebuild.
the class TestSupport method addTestEntities.
/**
* 添加测试用实体
*
* @param dropExists
* @return
* @throws Exception
*/
@SuppressWarnings("SameParameterValue")
protected static boolean addTestEntities(boolean dropExists) throws Exception {
boolean changed = false;
if (dropExists) {
if (MetadataHelper.containsEntity(TestAllFields)) {
LOG.warn("Dropping test entity : " + TestAllFields);
new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(TestAllFields), true);
}
if (MetadataHelper.containsEntity(SalesOrder)) {
LOG.warn("Dropping test entity : " + SalesOrder);
new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(SalesOrder), true);
}
if (MetadataHelper.containsEntity(Account)) {
LOG.warn("Dropping test entity : " + Account);
new Entity2Schema(UserService.ADMIN_USER).dropEntity(MetadataHelper.getEntity(Account), true);
}
}
if (!MetadataHelper.containsEntity(TestAllFields)) {
Entity2Schema entity2Schema = new Entity2Schema(UserService.ADMIN_USER);
String entityName = entity2Schema.createEntity(TestAllFields.toUpperCase(), null, null, true);
Entity testEntity = MetadataHelper.getEntity(entityName);
for (DisplayType dt : DisplayType.values()) {
if (dt == DisplayType.ID || dt == DisplayType.ANYREFERENCE) {
continue;
}
String fieldName = dt.name().toUpperCase();
if (BlockList.isBlock(fieldName))
fieldName += "1";
if (dt == DisplayType.REFERENCE || dt == DisplayType.N2NREFERENCE) {
new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, null);
} else if (dt == DisplayType.CLASSIFICATION) {
JSON extra = JSON.parseObject("{classification:'018-0000000000000001'}");
new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, extra);
} else if (dt == DisplayType.STATE) {
JSON extra = JSON.parseObject("{stateClass:'com.rebuild.core.support.state.HowtoState'}");
new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, entityName, extra);
} else {
new Field2Schema(UserService.ADMIN_USER).createField(testEntity, fieldName, dt, null, null, null);
}
}
changed = true;
}
if (!MetadataHelper.containsEntity(Account)) {
String metaschema = FileUtils.readFileToString(ResourceUtils.getFile("classpath:schema-Account999.json"));
MetaschemaImporter importer = new MetaschemaImporter(JSON.parseObject(metaschema));
TaskExecutors.run((HeavyTask<?>) importer.setUser(UserService.ADMIN_USER));
changed = true;
}
if (!MetadataHelper.containsEntity(SalesOrder)) {
String metaschema = FileUtils.readFileToString(ResourceUtils.getFile("classpath:schema-SalesOrder999.json"));
MetaschemaImporter importer = new MetaschemaImporter(JSON.parseObject(metaschema));
TaskExecutors.run((HeavyTask<?>) importer.setUser(UserService.ADMIN_USER));
changed = true;
}
return changed;
}
Aggregations