use of com.rebuild.core.service.approval.ApprovalState in project rebuild by getrebuild.
the class FormsBuilder method buildModel.
/**
* @param entity
* @param user
* @param record
* @param viewMode 视图模式
* @return
*/
private JSON buildModel(String entity, ID user, ID record, boolean viewMode) {
Assert.notNull(entity, "[entity] cannot be null");
Assert.notNull(user, "[user] cannot be null");
final Entity entityMeta = MetadataHelper.getEntity(entity);
if (record != null) {
Assert.isTrue(entityMeta.getEntityCode().equals(record.getEntityCode()), "[entity] and [record] do not match");
}
// 如果明细实体
final Entity hasMainEntity = entityMeta.getMainEntity();
// 审批流程(状态)
ApprovalState approvalState;
// 新建
if (record == null) {
if (hasMainEntity != null) {
ID mainid = FormBuilderContextHolder.getMainIdOfDetail();
Assert.notNull(mainid, "Call `FormBuilderContextHolder#setMainIdOfDetail` first!");
approvalState = EntityHelper.isUnsavedId(mainid) ? null : getHadApproval(hasMainEntity, mainid);
if ((approvalState == ApprovalState.PROCESSING || approvalState == ApprovalState.APPROVED)) {
return formatModelError(approvalState == ApprovalState.APPROVED ? Language.L("主记录已完成审批,不能添加明细") : Language.L("主记录正在审批中,不能添加明细"));
}
// 明细无需审批
approvalState = null;
if (!EntityHelper.isUnsavedId(mainid) && !Application.getPrivilegesManager().allowUpdate(user, mainid)) {
return formatModelError(Language.L("你没有添加明细权限"));
}
} else if (!Application.getPrivilegesManager().allowCreate(user, entityMeta.getEntityCode())) {
return formatModelError(Language.L("你没有新建权限"));
} else {
approvalState = getHadApproval(entityMeta, null);
}
} else // 查看(视图)
if (viewMode) {
if (!Application.getPrivilegesManager().allowRead(user, record)) {
return formatModelError(Language.L("无权读取此记录或记录已被删除"));
}
approvalState = getHadApproval(entityMeta, record);
} else // 编辑
{
if (!Application.getPrivilegesManager().allowUpdate(user, record)) {
return formatModelError(Language.L("你没有修改此记录的权限"));
}
approvalState = getHadApproval(entityMeta, record);
if (approvalState != null) {
String recordType = hasMainEntity == null ? Language.L("记录") : Language.L("主记录");
if (approvalState == ApprovalState.APPROVED) {
return formatModelError(Language.L("%s已完成审批,禁止编辑", recordType));
} else if (approvalState == ApprovalState.PROCESSING) {
return formatModelError(Language.L("%s正在审批中,禁止编辑", recordType));
}
}
}
ConfigBean model = getFormLayout(entity, user);
JSONArray elements = (JSONArray) model.getJSON("elements");
if (elements == null || elements.isEmpty()) {
return formatModelError(Language.L("此表单布局尚未配置,请配置后使用"));
}
Record data = null;
if (record != null) {
data = findRecord(record, user, elements);
if (data == null) {
return formatModelError(Language.L("无权读取此记录或记录已被删除"));
}
}
// 自动只读
Set<String> roAutos = EasyMetaFactory.getAutoReadonlyFields(entity);
for (Object o : elements) {
JSONObject field = (JSONObject) o;
if (roAutos.contains(field.getString("field"))) {
field.put("readonly", true);
}
}
buildModelElements(elements, entityMeta, data, user, !viewMode);
if (elements.isEmpty()) {
return formatModelError(Language.L("此表单布局尚未配置,请配置后使用"));
}
// 主/明细实体处理
if (hasMainEntity != null) {
model.set("mainMeta", EasyMetaFactory.toJSON(hasMainEntity));
} else if (entityMeta.getDetailEntity() != null) {
model.set("detailMeta", EasyMetaFactory.toJSON(entityMeta.getDetailEntity()));
}
if (data != null && data.hasValue(EntityHelper.ModifiedOn)) {
model.set("lastModified", data.getDate(EntityHelper.ModifiedOn).getTime());
}
if (approvalState != null) {
model.set("hadApproval", approvalState.getState());
}
// Clean form's ID of config
model.set("id", null);
return model.toJSON();
}
use of com.rebuild.core.service.approval.ApprovalState in project rebuild by getrebuild.
the class GeneralEntityService method checkModifications.
/**
* 系统相关约束检查。此方法有 3 种结果:
* 1. true - 检查通过
* 2. false - 检查不通过,但可以忽略的错误(如删除一条不存在的记录)
* 3. 抛出异常 - 不可忽略的错误
*
* @param record
* @param action [CREATE|UPDATE|DELDETE]
* @return
* @throws DataSpecificationException
*/
protected boolean checkModifications(Record record, Permission action) throws DataSpecificationException {
final Entity entity = record.getEntity();
final Entity mainEntity = entity.getMainEntity();
if (action == BizzPermission.CREATE) {
// 仅验证新建明细(相当于更新主记录)
if (mainEntity != null && MetadataHelper.hasApprovalField(record.getEntity())) {
Field dtmField = MetadataHelper.getDetailToMainField(entity);
ApprovalState state = ApprovalHelper.getApprovalState(record.getID(dtmField.getName()));
if (state == ApprovalState.APPROVED || state == ApprovalState.PROCESSING) {
throw new DataSpecificationException(state == ApprovalState.APPROVED ? Language.L("主记录已完成审批,不能添加明细") : Language.L("主记录正在审批中,不能添加明细"));
}
}
} else {
final Entity checkEntity = mainEntity != null ? mainEntity : entity;
ID recordId = record.getPrimary();
if (checkEntity.containsField(EntityHelper.ApprovalId)) {
// 需要验证主记录
String recordType = Language.L("记录");
if (mainEntity != null) {
recordId = getMainId(entity, recordId);
recordType = Language.L("主记录");
}
ApprovalState currentState;
ApprovalState changeState = null;
try {
currentState = ApprovalHelper.getApprovalState(recordId);
if (record.hasValue(EntityHelper.ApprovalState)) {
changeState = (ApprovalState) ApprovalState.valueOf(record.getInt(EntityHelper.ApprovalState));
}
} catch (NoRecordFoundException ignored) {
log.warn("No record found for check : " + recordId);
return false;
}
boolean unallow = false;
if (action == BizzPermission.DELETE) {
unallow = currentState == ApprovalState.APPROVED || currentState == ApprovalState.PROCESSING;
} else if (action == BizzPermission.UPDATE) {
unallow = currentState == ApprovalState.APPROVED || currentState == ApprovalState.PROCESSING;
// 管理员撤销
if (unallow) {
boolean adminCancel = currentState == ApprovalState.APPROVED && changeState == ApprovalState.CANCELED;
if (adminCancel)
unallow = false;
}
// 审批时/已通过强制修改
if (unallow) {
boolean forceUpdate = GeneralEntityServiceContextHolder.isAllowForceUpdateOnce();
if (forceUpdate)
unallow = false;
}
}
if (unallow) {
if (RobotTriggerObserver.getTriggerSource() != null) {
recordType = Language.L("关联记录");
}
throw new DataSpecificationException(currentState == ApprovalState.APPROVED ? Language.L("%s已完成审批,禁止操作", recordType) : Language.L("%s正在审批中,禁止操作", recordType));
}
}
}
if (action == BizzPermission.CREATE || action == BizzPermission.UPDATE) {
// TODO 父级级联字段强校验,兼容问题???
}
return true;
}
use of com.rebuild.core.service.approval.ApprovalState in project rebuild by getrebuild.
the class NotificationController method listApprovals.
@GetMapping("/notification/approvals")
public Object[][] listApprovals(HttpServletRequest request) {
final ID user = getRequestUser(request);
int pn = getIntParameter(request, "pageNo", 1);
int ps = getIntParameter(request, "pageSize", 40);
Object[][] array = Application.createQueryNoFilter("select fromUser,message,createdOn,relatedRecord,messageId" + " from Notification where toUser = ? and type = 20 and relatedRecord is not null order by createdOn desc").setParameter(1, user).setLimit(ps, pn * ps - ps).array();
for (int i = 0; i < array.length; i++) {
Object[] m = array[i];
m[0] = new Object[] { m[0], UserHelper.getName((ID) m[0]) };
m[1] = MessageBuilder.formatMessage((String) m[1]);
m[2] = I18nUtils.formatDate((Date) m[2]);
// 审批状态
ID approvalStep = (ID) m[3];
Object[] stepState = Application.createQueryNoFilter("select isCanceled,state from RobotApprovalStep where stepId = ?").setParameter(1, approvalStep).unique();
if (stepState == null) {
m[3] = new Object[] { 0 };
} else {
ApprovalState state = (ApprovalState) ApprovalState.valueOf((Integer) stepState[1]);
if (state == ApprovalState.DRAFT) {
boolean canceled = (Boolean) stepState[0];
m[3] = canceled ? new Object[] { 2, Language.L("已处理") } : new Object[] { 1, Language.L("待处理") };
} else if (state == ApprovalState.APPROVED) {
m[3] = new Object[] { 10, Language.L("已同意") };
} else if (state == ApprovalState.REJECTED) {
m[3] = new Object[] { 11, Language.L("已驳回") };
}
}
array[i] = m;
}
return array;
}
use of com.rebuild.core.service.approval.ApprovalState in project rebuild by getrebuild.
the class I18nGettextParser method sysDefined.
// 系统定义的
static void sysDefined(JSONObject into) {
into.put("_", "中文");
for (DisplayType o : DisplayType.values()) into.put(o.getDisplayName(), o.getDisplayName());
for (ActionType o : ActionType.values()) into.put(o.getDisplayName(), o.getDisplayName());
for (ApprovalState s : ApprovalState.values()) into.put(s.getName(), s.getName());
for (Entity entity : Application.getPersistManagerFactory().getMetadataFactory().getEntities()) {
if (!EasyMetaFactory.valueOf(entity).isBuiltin())
continue;
sysDefinedMeta(entity, into);
for (Field field : entity.getFields()) {
if (!EasyMetaFactory.valueOf(field).isBuiltin())
continue;
sysDefinedMeta(field, into);
}
}
into.put("__", "__");
}
use of com.rebuild.core.service.approval.ApprovalState in project rebuild by getrebuild.
the class ApprovalList method build.
@Override
public JSON build() {
final int viewState = ObjectUtils.toInt(getExtraParams().get("state"), ApprovalState.DRAFT.getState());
final String baseWhere = "where isCanceled = 'F' and isWaiting = 'F' and approver = ?" + " and approvalId <> '' and recordId <> '' and ";
Object[][] array = Application.createQueryNoFilter("select createdBy,modifiedOn,recordId,approvalId from RobotApprovalStep " + baseWhere + " state = ? order by modifiedOn desc").setParameter(1, this.getUser()).setParameter(2, viewState).setLimit(// 最多显示
500).array();
List<Object> rearray = new ArrayList<>();
int deleted = 0;
for (Object[] o : array) {
final ID recordId = (ID) o[2];
String label;
try {
label = FieldValueHelper.getLabel(recordId);
} catch (NoRecordFoundException ignored) {
deleted++;
continue;
}
final ApprovalState currentState = ApprovalHelper.getApprovalState(recordId);
if (currentState == ApprovalState.CANCELED) {
deleted++;
continue;
}
Entity entity = MetadataHelper.getEntity(recordId.getEntityCode());
ID s = ApprovalHelper.getSubmitter(recordId, (ID) o[3]);
rearray.add(new Object[] { s, UserHelper.getName(s), I18nUtils.formatDate((Date) o[1]), o[2], label, o[3], EasyMetaFactory.getLabel(entity), entity.getName() });
}
Object[][] stats = Application.createQueryNoFilter("select state,count(state) from RobotApprovalStep " + baseWhere + " state < ? group by state").setParameter(1, this.getUser()).setParameter(2, ApprovalState.CANCELED.getState()).array();
// FIXME 排除删除的(可能导致不同状态下数据不一致)
if (deleted > 0) {
for (Object[] o : stats) {
if ((Integer) o[0] == viewState) {
o[1] = ObjectUtils.toInt(o[1]) - deleted;
if ((Integer) o[1] < 0) {
o[1] = 0;
}
}
}
}
Map<String, Object> ret = new HashMap<>();
ret.put("data", rearray);
ret.put("stats", stats);
return (JSON) JSON.toJSON(ret);
}
Aggregations