use of com.rebuild.core.metadata.impl.Entity2Schema 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.Entity2Schema in project rebuild by getrebuild.
the class MetaschemaImporter method exec.
/**
* 执行导入
*
* @return
*/
@Override
protected String exec() {
setTotal(100);
if (!DynamicMetadataContextHolder.isSkipLanguageRefresh(false)) {
DynamicMetadataContextHolder.setSkipLanguageRefresh();
needClearContextHolder = true;
}
String entityName = performEntity(data, null);
Entity createdEntity = MetadataHelper.getEntity(entityName);
setCompleted(45);
JSONObject detailData = data.getJSONObject("detail");
if (detailData == null)
detailData = data.getJSONObject("slave");
if (detailData != null) {
try {
performEntity(detailData, createdEntity.getName());
setCompleted(90);
} catch (MetadataModificationException ex) {
// 出现异常,删除主实体
new Entity2Schema(this.getUser()).dropEntity(createdEntity, true);
throw ex;
}
}
final ID sessionUser = UserContextHolder.getUser(true);
if (sessionUser == null)
UserContextHolder.setUser(getUser());
// 字段选项
try {
for (Map.Entry<Field, JSONObject> e : picklistHolders.entrySet()) {
Field field = e.getKey();
Application.getBean(PickListService.class).updateBatch(MetadataHelper.getField(field.getOwnEntity().getName(), field.getName()), e.getValue());
}
} finally {
if (sessionUser == null)
UserContextHolder.clear();
}
setCompleted(100);
return entityName;
}
use of com.rebuild.core.metadata.impl.Entity2Schema in project rebuild by getrebuild.
the class MetaEntityController method entityDrop.
@RequestMapping("entity/entity-drop")
public RespBody entityDrop(HttpServletRequest request) {
final Entity entity = getEntityById(getIdParameterNotNull(request, "id"));
final boolean force = getBoolParameter(request, "force", false);
try {
boolean drop = new Entity2Schema().dropEntity(entity, force);
return drop ? RespBody.ok() : RespBody.error();
} catch (Exception ex) {
log.error("entity-drop", ex);
return RespBody.error(ex.getLocalizedMessage());
}
}
use of com.rebuild.core.metadata.impl.Entity2Schema in project rebuild by getrebuild.
the class MetaEntityController method entityNew.
@PostMapping("entity/entity-new")
public RespBody entityNew(HttpServletRequest request) {
final JSONObject reqJson = (JSONObject) ServletUtils.getRequestJson(request);
String label = reqJson.getString("label");
String comments = reqJson.getString("comments");
String mainEntity = reqJson.getString("mainEntity");
if (StringUtils.isNotBlank(mainEntity)) {
if (!MetadataHelper.containsEntity(mainEntity)) {
return RespBody.errorl("无效主实体 : %s", mainEntity);
}
Entity useMain = MetadataHelper.getEntity(mainEntity);
if (useMain.getMainEntity() != null) {
return RespBody.errorl("明细实体不能作为主实体");
} else if (useMain.getDetailEntity() != null) {
return RespBody.errorl("选择的主实体已被 [%s] 使用", useMain.getDetailEntity());
}
}
try {
String entityName = new Entity2Schema().createEntity(label, comments, mainEntity, getBoolParameter(request, "nameField"));
return RespBody.ok(entityName);
} catch (Exception ex) {
log.error("entity-new", ex);
return RespBody.error(ex.getLocalizedMessage());
}
}
use of com.rebuild.core.metadata.impl.Entity2Schema 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