diff --git a/src/main/java/com/rebuild/server/Application.java b/src/main/java/com/rebuild/server/Application.java index a521056bd..6377323e3 100644 --- a/src/main/java/com/rebuild/server/Application.java +++ b/src/main/java/com/rebuild/server/Application.java @@ -158,8 +158,8 @@ public final class Application { return getBean(OnlineSessionStore.class); } - public static ID currentCallerUser() { - return getSessionStore().getCurrentCaller(); + public static ID getCurrentUser() { + return getSessionStore().get(); } public static PersistManagerFactory getPersistManagerFactory() { diff --git a/src/main/java/com/rebuild/server/business/charts/ChartDataFactory.java b/src/main/java/com/rebuild/server/business/charts/ChartDataFactory.java index 7010e06c5..2474093d5 100644 --- a/src/main/java/com/rebuild/server/business/charts/ChartDataFactory.java +++ b/src/main/java/com/rebuild/server/business/charts/ChartDataFactory.java @@ -63,7 +63,7 @@ public class ChartDataFactory { throw new ChartsException("源实体 [" + e.toUpperCase() + "] 不存在"); } - ID user = Application.currentCallerUser(); + ID user = Application.getCurrentUser(); Entity entity = MetadataHelper.getEntity(e); if (!Application.getSecurityManager().allowedR(user, entity.getEntityCode())) { throw new ChartsException("没有读取 [" + EasyMeta.getLabel(entity) + "] 的权限"); diff --git a/src/main/java/com/rebuild/server/helper/manager/FormManager.java b/src/main/java/com/rebuild/server/helper/manager/FormsManager.java similarity index 97% rename from src/main/java/com/rebuild/server/helper/manager/FormManager.java rename to src/main/java/com/rebuild/server/helper/manager/FormsManager.java index 932353223..88c050609 100644 --- a/src/main/java/com/rebuild/server/helper/manager/FormManager.java +++ b/src/main/java/com/rebuild/server/helper/manager/FormsManager.java @@ -51,9 +51,9 @@ import cn.devezhao.persist4j.engine.ID; * @author zhaofang123@gmail.com * @since 08/30/2018 */ -public class FormManager extends LayoutManager { +public class FormsManager extends LayoutManager { - private static final Log LOG = LogFactory.getLog(FormManager.class); + private static final Log LOG = LogFactory.getLog(FormsManager.class); private static final ThreadLocal MASTERID4NEWSLAVE = new ThreadLocal<>(); @@ -162,7 +162,7 @@ public class FormManager extends LayoutManager { Record data = null; if (!elements.isEmpty() && record != null) { - data = queryRecord(record, elements); + data = record(record, user, elements); if (data == null) { return formatModelError("此记录已被删除,或你对此记录没有读取权限"); } @@ -291,7 +291,7 @@ public class FormManager extends LayoutManager { * @param elements * @return */ - protected static Record queryRecord(ID id, JSONArray elements) { + protected static Record record(ID id, ID user, JSONArray elements) { if (elements.isEmpty()) { return null; } @@ -323,7 +323,7 @@ public class FormManager extends LayoutManager { .append(" where ").append(entity.getPrimaryField().getName()) .append(" = '").append(id).append("'"); - return Application.getQueryFactory().record(ajql.toString()); + return Application.getQueryFactory().createQuery(ajql.toString(), user).record(); } /** diff --git a/src/main/java/com/rebuild/server/helper/task/BulkTask.java b/src/main/java/com/rebuild/server/helper/task/BulkTask.java index 9c0a6c0ff..7d1d31646 100644 --- a/src/main/java/com/rebuild/server/helper/task/BulkTask.java +++ b/src/main/java/com/rebuild/server/helper/task/BulkTask.java @@ -34,7 +34,10 @@ import cn.devezhao.commons.CalendarUtils; */ public abstract class BulkTask implements Runnable { - protected final Log LOG = LogFactory.getLog(getClass()); + protected static final Log LOG = LogFactory.getLog(BulkTask.class); + + volatile + private boolean interrupt = false; private int total = -1; private int complete = 0; @@ -147,10 +150,10 @@ public abstract class BulkTask implements Runnable { // -- for Thread public void interrupt() { - Thread.currentThread().interrupt(); + this.interrupt = true; } public boolean isInterrupted() { - return Thread.currentThread().isInterrupted(); + return interrupt; } } diff --git a/src/main/java/com/rebuild/server/helper/task/BulkTaskExecutor.java b/src/main/java/com/rebuild/server/helper/task/BulkTaskExecutor.java index 5d8bcce3a..10879a288 100644 --- a/src/main/java/com/rebuild/server/helper/task/BulkTaskExecutor.java +++ b/src/main/java/com/rebuild/server/helper/task/BulkTaskExecutor.java @@ -52,7 +52,9 @@ public class BulkTaskExecutor extends QuartzJobBean { */ public static String submit(BulkTask task) { ThreadPoolExecutor tpe = (ThreadPoolExecutor) EXECS; - if (tpe.getTaskCount() > EXECS_MAX * 5) { + int queueSize = tpe.getQueue().size(); + System.out.println(queueSize); + if (queueSize > EXECS_MAX * 5) { throw new RejectedExecutionException("Too many task : " + tpe.getTaskCount()); } diff --git a/src/main/java/com/rebuild/server/metadata/entityhub/PickListService.java b/src/main/java/com/rebuild/server/metadata/entityhub/PickListService.java index 9ced78130..ca3999ae4 100644 --- a/src/main/java/com/rebuild/server/metadata/entityhub/PickListService.java +++ b/src/main/java/com/rebuild/server/metadata/entityhub/PickListService.java @@ -53,7 +53,7 @@ public class PickListService extends BaseService { */ public void updateBatch(Field field, JSONObject config) { Assert.notNull(config, "无效配置"); - ID user = Application.currentCallerUser(); + ID user = Application.getCurrentUser(); JSONArray showItems = config.getJSONArray("show"); JSONArray hideItems = config.getJSONArray("hide"); diff --git a/src/main/java/com/rebuild/server/service/ObservableService.java b/src/main/java/com/rebuild/server/service/ObservableService.java index 6f9bb30a2..d004a9c1c 100644 --- a/src/main/java/com/rebuild/server/service/ObservableService.java +++ b/src/main/java/com/rebuild/server/service/ObservableService.java @@ -61,7 +61,7 @@ public abstract class ObservableService extends Observable implements IEntitySer if (countObservers() > 0) { setChanged(); - notifyObservers(OperatingContext.valueOf(Application.currentCallerUser(), BizzPermission.CREATE, null, record)); + notifyObservers(OperatingContext.valueOf(Application.getCurrentUser(), BizzPermission.CREATE, null, record)); } return record; } @@ -74,7 +74,7 @@ public abstract class ObservableService extends Observable implements IEntitySer if (countObservers() > 0) { setChanged(); - notifyObservers(OperatingContext.valueOf(Application.currentCallerUser(), BizzPermission.UPDATE, before, record)); + notifyObservers(OperatingContext.valueOf(Application.getCurrentUser(), BizzPermission.UPDATE, before, record)); } return record; } @@ -83,7 +83,7 @@ public abstract class ObservableService extends Observable implements IEntitySer public int delete(ID recordId) { Record deleted = null; if (countObservers() > 0) { - deleted = EntityHelper.forUpdate(recordId, Application.currentCallerUser()); + deleted = EntityHelper.forUpdate(recordId, Application.getCurrentUser()); deleted = getBeforeRecord(deleted); } @@ -91,7 +91,7 @@ public abstract class ObservableService extends Observable implements IEntitySer if (countObservers() > 0) { setChanged(); - notifyObservers(OperatingContext.valueOf(Application.currentCallerUser(), BizzPermission.DELETE, deleted, null)); + notifyObservers(OperatingContext.valueOf(Application.getCurrentUser(), BizzPermission.DELETE, deleted, null)); } return affected; } diff --git a/src/main/java/com/rebuild/server/service/base/GeneralEntityService.java b/src/main/java/com/rebuild/server/service/base/GeneralEntityService.java index 0b0eb72af..9eb3d815b 100644 --- a/src/main/java/com/rebuild/server/service/base/GeneralEntityService.java +++ b/src/main/java/com/rebuild/server/service/base/GeneralEntityService.java @@ -187,7 +187,7 @@ public class GeneralEntityService extends ObservableService { if (countObservers() > 0) { setChanged(); - notifyObservers(OperatingContext.valueOf(Application.currentCallerUser(), BizzPermission.ASSIGN, before, assigned)); + notifyObservers(OperatingContext.valueOf(Application.getCurrentUser(), BizzPermission.ASSIGN, before, assigned)); } return affected; } @@ -206,7 +206,7 @@ public class GeneralEntityService extends ObservableService { return 1; } - ID currentUser = Application.currentCallerUser(); + ID currentUser = Application.getCurrentUser(); Record shared = EntityHelper.forNew(EntityHelper.ShareAccess, currentUser); shared.setString("belongEntity", entityName); @@ -238,7 +238,7 @@ public class GeneralEntityService extends ObservableService { public int unshare(ID record, ID accessId) { // TODO 级联取消共享 ?? - ID currentUser = Application.currentCallerUser(); + ID currentUser = Application.getCurrentUser(); Record unshared = EntityHelper.forUpdate(accessId, currentUser); if (countObservers() > 0) { unshared.setNull("belongEntity"); @@ -298,7 +298,7 @@ public class GeneralEntityService extends ObservableService { sql = sql.substring(0, sql.length() - 4); // remove last ' or ' sql += " )"; - Filter filter = Application.getSecurityManager().createQueryFilter(Application.currentCallerUser(), action); + Filter filter = Application.getSecurityManager().createQueryFilter(Application.getCurrentUser(), action); Object[][] array = Application.getQueryFactory().createQuery(sql, filter).array(); Set records = new HashSet<>(); 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 6735c5020..a9f0f2ebc 100644 --- a/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java +++ b/src/main/java/com/rebuild/server/service/base/QuickCodeReindexTask.java @@ -21,8 +21,6 @@ package com.rebuild.server.service.base; import java.util.List; import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import com.github.stuxuhai.jpinyin.PinyinHelper; import com.rebuild.server.Application; @@ -39,16 +37,14 @@ import cn.devezhao.persist4j.Record; import cn.devezhao.persist4j.engine.ID; /** - * QuickCode 重建 + * QuickCode 字段值重建 * * @author devezhao * @since 12/28/2018 */ public class QuickCodeReindexTask extends BulkTask { - private static final Log LOG = LogFactory.getLog(QuickCodeReindexTask.class); - - private Entity entity; + final private Entity entity; /** * @param entity diff --git a/src/main/java/com/rebuild/server/service/bizz/ChangeOwningDeptTask.java b/src/main/java/com/rebuild/server/service/bizz/ChangeOwningDeptTask.java index a49aca963..5774d8eac 100644 --- a/src/main/java/com/rebuild/server/service/bizz/ChangeOwningDeptTask.java +++ b/src/main/java/com/rebuild/server/service/bizz/ChangeOwningDeptTask.java @@ -55,7 +55,7 @@ public class ChangeOwningDeptTask extends BulkTask { String updeptSql = "update `{0}` set OWNING_DEPT = ''%s'' where OWNING_USER = ''%s''"; updeptSql = String.format(updeptSql, deptNew.toLiteral(), user.toLiteral()); for (Entity e : MetadataHelper.getEntities()) { - if (isInterrupted()) { + if (this.isInterrupted()) { LOG.error("Task interrupted : " + user + " > " + deptNew); break; } diff --git a/src/main/java/com/rebuild/web/CurrentCaller.java b/src/main/java/com/rebuild/server/service/bizz/CurrentCaller.java similarity index 65% rename from src/main/java/com/rebuild/web/CurrentCaller.java rename to src/main/java/com/rebuild/server/service/bizz/CurrentCaller.java index 19329d2f3..140c482ea 100644 --- a/src/main/java/com/rebuild/web/CurrentCaller.java +++ b/src/main/java/com/rebuild/server/service/bizz/CurrentCaller.java @@ -16,8 +16,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package com.rebuild.web; +package com.rebuild.server.service.bizz; +import cn.devezhao.bizz.security.AccessDeniedException; import cn.devezhao.persist4j.engine.ID; /** @@ -31,35 +32,35 @@ public class CurrentCaller { private static final ThreadLocal CALLER = new ThreadLocal<>(); /** - * @param user + * @param caller */ - protected void setCurrentCaller(ID user) { - CALLER.set(user); + public void set(ID caller) { + CALLER.set(caller); } /** */ - protected void clearCurrentCaller() { + public void clean() { CALLER.remove(); } /** * @return - * @throws IllegalParameterException */ - public ID getCurrentCaller() throws IllegalParameterException { - return getCurrentCaller(false); + public ID get() { + return get(false); } /** + * @param allowedNull * @return - * @throws IllegalParameterException + * @throws AccessDeniedException If caller not found */ - public ID getCurrentCaller(boolean allowNull) throws IllegalParameterException { - ID user = CALLER.get(); - if (user == null && allowNull == false) { - throw new IllegalParameterException("无效请求用户", 403); + public ID get(boolean allowedNull) throws AccessDeniedException { + ID caller = CALLER.get(); + if (caller == null && allowedNull == false) { + throw new AccessDeniedException("无有效用户"); } - return user; + return caller; } } diff --git a/src/main/java/com/rebuild/server/service/bizz/DepartmentService.java b/src/main/java/com/rebuild/server/service/bizz/DepartmentService.java index 12b50f6f8..e57e2fd3d 100644 --- a/src/main/java/com/rebuild/server/service/bizz/DepartmentService.java +++ b/src/main/java/com/rebuild/server/service/bizz/DepartmentService.java @@ -50,8 +50,15 @@ public class DepartmentService extends BizzEntityService { } @Override - public Record createOrUpdate(Record record) { - record = super.createOrUpdate(record); + public Record create(Record record) { + record = super.create(record); + Application.getUserStore().refreshDepartment(record.getPrimary()); + return record; + } + + @Override + public Record update(Record record) { + record = super.update(record); Application.getUserStore().refreshDepartment(record.getPrimary()); return record; } diff --git a/src/main/java/com/rebuild/server/service/bizz/RoleService.java b/src/main/java/com/rebuild/server/service/bizz/RoleService.java index 699a92192..3db3dd25c 100644 --- a/src/main/java/com/rebuild/server/service/bizz/RoleService.java +++ b/src/main/java/com/rebuild/server/service/bizz/RoleService.java @@ -52,8 +52,15 @@ public class RoleService extends BizzEntityService { } @Override - public Record createOrUpdate(Record record) { - record = super.createOrUpdate(record); + public Record create(Record record) { + record = super.create(record); + Application.getUserStore().refreshRole(record.getPrimary(), false); + return record; + } + + @Override + public Record update(Record record) { + record = super.update(record); Application.getUserStore().refreshRole(record.getPrimary(), false); return record; } @@ -80,7 +87,7 @@ public class RoleService extends BizzEntityService { * @param definition */ public void updatePrivileges(ID roleId, JSONObject definition) { - final ID user = Application.currentCallerUser(); + final ID user = Application.getCurrentUser(); Object[][] array = Application.createQuery( "select privilegesId,definition,entity,zeroKey from RolePrivileges where roleId = ?") diff --git a/src/main/java/com/rebuild/server/service/bizz/UserService.java b/src/main/java/com/rebuild/server/service/bizz/UserService.java index fe03ae4fd..7dd4b60a1 100644 --- a/src/main/java/com/rebuild/server/service/bizz/UserService.java +++ b/src/main/java/com/rebuild/server/service/bizz/UserService.java @@ -108,7 +108,7 @@ public class UserService extends BizzEntityService { return; } - Record record = EntityHelper.forUpdate(user, Application.currentCallerUser()); + Record record = EntityHelper.forUpdate(user, Application.getCurrentUser()); record.setID("deptId", deptNew); super.update(record); Application.getUserStore().refreshUser(user); diff --git a/src/main/java/com/rebuild/server/service/bizz/privileges/PrivilegesGuardInterceptor.java b/src/main/java/com/rebuild/server/service/bizz/privileges/PrivilegesGuardInterceptor.java index b7756d201..94eaca232 100644 --- a/src/main/java/com/rebuild/server/service/bizz/privileges/PrivilegesGuardInterceptor.java +++ b/src/main/java/com/rebuild/server/service/bizz/privileges/PrivilegesGuardInterceptor.java @@ -72,7 +72,7 @@ public class PrivilegesGuardInterceptor implements MethodInterceptor, Guard { } BulkContext context = (BulkContext) first; - ID caller = Application.currentCallerUser(); + ID caller = Application.getCurrentUser(); Entity entity = context.getMainEntity(); if (!Application.getSecurityManager().allowed(caller, entity.getEntityCode(), context.getAction())) { @@ -97,7 +97,7 @@ public class PrivilegesGuardInterceptor implements MethodInterceptor, Guard { throw new IllegalArgumentException("First argument must be Record/ID!"); } - ID caller = Application.currentCallerUser(); + ID caller = Application.getCurrentUser(); Permission action = getPermissionByMethod(invocation.getMethod(), recordId == null); boolean isAllowed = false; diff --git a/src/main/java/com/rebuild/server/service/query/AdvFilterParser.java b/src/main/java/com/rebuild/server/service/query/AdvFilterParser.java index 82b079b1e..8c912899a 100644 --- a/src/main/java/com/rebuild/server/service/query/AdvFilterParser.java +++ b/src/main/java/com/rebuild/server/service/query/AdvFilterParser.java @@ -172,14 +172,14 @@ public class AdvFilterParser { } else if ("REM".equalsIgnoreCase(op)) { value = CalendarUtils.getUTCDateFormat().format(CalendarUtils.addMonth(-NumberUtils.toInt(value))) + fullTime; } else if ("SFU".equalsIgnoreCase(op)) { - value = Application.currentCallerUser().toLiteral(); + value = Application.getCurrentUser().toLiteral(); } else if ("SFB".equalsIgnoreCase(op)) { - Department dept = UserHelper.getDepartment(Application.currentCallerUser()); + Department dept = UserHelper.getDepartment(Application.getCurrentUser()); if (dept != null && fieldMeta.getReferenceEntities()[0].getEntityCode() == EntityHelper.User) { sb.insert(sb.indexOf(" "), "." + EntityHelper.OwningDept); } } else if ("SFD".equalsIgnoreCase(op)) { - Department dept = UserHelper.getDepartment(Application.currentCallerUser()); + Department dept = UserHelper.getDepartment(Application.getCurrentUser()); if (dept != null) { value = StringUtils.join(UserHelper.getAllChildrenId(dept), "|"); } diff --git a/src/main/java/com/rebuild/server/service/query/QueryFactory.java b/src/main/java/com/rebuild/server/service/query/QueryFactory.java index e7fca0263..929e2f4ca 100644 --- a/src/main/java/com/rebuild/server/service/query/QueryFactory.java +++ b/src/main/java/com/rebuild/server/service/query/QueryFactory.java @@ -53,7 +53,7 @@ public class QueryFactory { * @return */ public Query createQuery(String ajql) { - return createQuery(ajql, Application.currentCallerUser()); + return createQuery(ajql, Application.getCurrentUser()); } /** diff --git a/src/main/java/com/rebuild/web/OnlineSessionStore.java b/src/main/java/com/rebuild/web/OnlineSessionStore.java index 8b34ed1ba..1f59e421f 100644 --- a/src/main/java/com/rebuild/web/OnlineSessionStore.java +++ b/src/main/java/com/rebuild/web/OnlineSessionStore.java @@ -33,6 +33,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.util.Assert; +import com.rebuild.server.service.bizz.CurrentCaller; + import cn.devezhao.commons.CalendarUtils; import cn.devezhao.commons.web.WebUtils; import cn.devezhao.persist4j.engine.ID; diff --git a/src/main/java/com/rebuild/web/RequestWatchHandler.java b/src/main/java/com/rebuild/web/RequestWatchHandler.java index 15cd01682..9f9fc8ee7 100644 --- a/src/main/java/com/rebuild/web/RequestWatchHandler.java +++ b/src/main/java/com/rebuild/web/RequestWatchHandler.java @@ -70,9 +70,9 @@ public class RequestWatchHandler extends HandlerInterceptorAdapter { throws Exception { super.afterCompletion(request, response, handler, exception); - ID caller = Application.getSessionStore().getCurrentCaller(true); + ID caller = Application.getSessionStore().get(true); if (caller != null) { - Application.getSessionStore().clearCurrentCaller(); + Application.getSessionStore().clean(); } logProgressTime(request); @@ -141,7 +141,7 @@ public class RequestWatchHandler extends HandlerInterceptorAdapter { ID user = AppUtils.getRequestUser(request); if (user != null) { - Application.getSessionStore().setCurrentCaller(user); + Application.getSessionStore().set(user); // 管理后台访问 if (requestUrl.contains("/admin/") && !AdminEntryControll.isAdminVerified(request)) { diff --git a/src/main/java/com/rebuild/web/admin/entityhub/FormDesignControll.java b/src/main/java/com/rebuild/web/admin/entityhub/FormDesignControll.java index fe7b5417c..c04350df6 100644 --- a/src/main/java/com/rebuild/web/admin/entityhub/FormDesignControll.java +++ b/src/main/java/com/rebuild/web/admin/entityhub/FormDesignControll.java @@ -31,7 +31,7 @@ import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.rebuild.server.Application; -import com.rebuild.server.helper.manager.FormManager; +import com.rebuild.server.helper.manager.FormsManager; import com.rebuild.server.metadata.EntityHelper; import com.rebuild.web.BaseControll; import com.rebuild.web.LayoutConfig; @@ -52,7 +52,7 @@ public class FormDesignControll extends BaseControll implements LayoutConfig { public ModelAndView pageFormDesign(@PathVariable String entity, HttpServletRequest request) throws IOException { ModelAndView mv = createModelAndView("/admin/entity/form-design.jsp"); MetaEntityControll.setEntityBase(mv, entity); - JSON cfg = FormManager.getFormLayout(entity, getRequestUser(request)); + JSON cfg = FormsManager.getFormLayout(entity, getRequestUser(request)); if (cfg != null) { request.setAttribute("FormConfig", cfg); } @@ -69,7 +69,7 @@ public class FormDesignControll extends BaseControll implements LayoutConfig { JSON formJson = ServletUtils.getRequestJson(request); Record record = EntityHelper.parse((JSONObject) formJson, getRequestUser(request)); if (record.getPrimary() == null) { - record.setString("applyTo", FormManager.APPLY_ALL); + record.setString("applyTo", FormsManager.APPLY_ALL); } Application.getCommonService().createOrUpdate(record); diff --git a/src/main/java/com/rebuild/web/base/entity/GeneralEntityControll.java b/src/main/java/com/rebuild/web/base/entity/GeneralEntityControll.java index 48d2047ae..086c0ba2d 100644 --- a/src/main/java/com/rebuild/web/base/entity/GeneralEntityControll.java +++ b/src/main/java/com/rebuild/web/base/entity/GeneralEntityControll.java @@ -30,7 +30,7 @@ import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.rebuild.server.helper.manager.FormManager; +import com.rebuild.server.helper.manager.FormsManager; import com.rebuild.server.helper.manager.ViewAddonsManager; import com.rebuild.server.helper.manager.value.DefaultValueManager; import com.rebuild.server.metadata.MetadataHelper; @@ -86,12 +86,12 @@ public class GeneralEntityControll extends BaseControll { // 创建明细实体必须制定主实体,以便验证权限 String master = ((JSONObject) initialVal).getString(DefaultValueManager.DV_MASTER); if (ID.isId(master)) { - FormManager.setCurrentMasterId(ID.valueOf(master)); + FormsManager.setCurrentMasterId(ID.valueOf(master)); } } } - JSON model = FormManager.getFormModel(entity, user, record); + JSON model = FormsManager.getFormModel(entity, user, record); // 填充前端设定的初始值 if (record == null && initialVal != null) { DefaultValueManager.setFieldsValue(MetadataHelper.getEntity(entity), model, initialVal); @@ -104,7 +104,7 @@ public class GeneralEntityControll extends BaseControll { HttpServletRequest request, HttpServletResponse response) throws IOException { ID user = getRequestUser(request); ID record = getIdParameterNotNull(request, "id"); - JSON modal = FormManager.getViewModel(entity, user, record); + JSON modal = FormsManager.getViewModel(entity, user, record); writeSuccess(response, modal); } } diff --git a/src/test/java/com/rebuild/server/TestSupport.java b/src/test/java/com/rebuild/server/TestSupport.java index 03c7507c1..55711f474 100644 --- a/src/test/java/com/rebuild/server/TestSupport.java +++ b/src/test/java/com/rebuild/server/TestSupport.java @@ -40,6 +40,7 @@ public class TestSupport { @AfterClass public static void shutdown() { + Application.getSessionStore().clean(); LOG.warn("TESTING Shutdown ..."); } } diff --git a/src/test/java/com/rebuild/server/business/charts/ChartsTest.java b/src/test/java/com/rebuild/server/business/charts/ChartsTest.java new file mode 100644 index 000000000..c27f551fe --- /dev/null +++ b/src/test/java/com/rebuild/server/business/charts/ChartsTest.java @@ -0,0 +1,45 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.business.charts; + +import org.junit.Test; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.rebuild.server.Application; +import com.rebuild.server.TestSupport; +import com.rebuild.server.service.bizz.UserService; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class ChartsTest extends TestSupport { + + @Test + public void testIndex() throws Exception { + Application.getSessionStore().set(UserService.ADMIN_USER); + + JSONObject config = JSON.parseObject( + "{'entity':'User','title':'指标卡','type':'INDEX','axis':{'dimension':[],'numerical':[{'field':'userId','sort':'','calc':'COUNT'}]}}"); + ChartData index = ChartDataFactory.create(config); + System.out.println(index.build()); + } +} diff --git a/src/test/java/com/rebuild/server/helper/manager/FormsManagerTest.java b/src/test/java/com/rebuild/server/helper/manager/FormsManagerTest.java new file mode 100644 index 000000000..31c04916c --- /dev/null +++ b/src/test/java/com/rebuild/server/helper/manager/FormsManagerTest.java @@ -0,0 +1,48 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.helper.manager; + +import org.junit.Test; + +import com.alibaba.fastjson.JSON; +import com.rebuild.server.TestSupport; +import com.rebuild.server.service.bizz.UserService; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class FormsManagerTest extends TestSupport { + + @Test + public void testModel() throws Exception { + JSON newModel = FormsManager.getFormModel("User", UserService.ADMIN_USER); + System.out.println(newModel); + + JSON editModel = FormsManager.getFormModel("User", UserService.ADMIN_USER, UserService.SYSTEM_USER); + System.out.println(editModel); + } + + @Test + public void testViewModel() throws Exception { + JSON viewModel = FormsManager.getViewModel("User", UserService.ADMIN_USER, UserService.SYSTEM_USER); + System.out.println(viewModel); + } +} diff --git a/src/test/java/com/rebuild/server/helper/task/BulkTaskTest.java b/src/test/java/com/rebuild/server/helper/task/BulkTaskTest.java new file mode 100644 index 000000000..9c54d7e63 --- /dev/null +++ b/src/test/java/com/rebuild/server/helper/task/BulkTaskTest.java @@ -0,0 +1,89 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.helper.task; + +import java.util.concurrent.RejectedExecutionException; + +import org.junit.Test; + +import cn.devezhao.commons.ThreadPool; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class BulkTaskTest { + + @Test + public void testTask() throws Exception { + String taskid1 = BulkTaskExecutor.submit(new TestTask("testTask1", 5)); + System.out.println("Submit Task1 : " + taskid1); + + String taskid2 = BulkTaskExecutor.submit(new TestTask("testTask2",5)); + System.out.println("Submit Task2 : " + taskid2); + + ThreadPool.waitFor(2 * 1000); + } + + @Test(expected=RejectedExecutionException.class) + public void testRejected() throws Exception { + for (int i = 0; i < 30; i++) { + BulkTaskExecutor.submit(new TestTask("testRejected", 2)); + } + } + + @Test + public void testCancel() throws Exception { + ThreadPool.waitFor(1 * 1000); // Wait testRejected + + String taskid = BulkTaskExecutor.submit(new TestTask("testCancel", 100)); + System.out.println("Submit Task : " + taskid); + + ThreadPool.waitFor(1000 * 2); + boolean cancel = BulkTaskExecutor.cancel(taskid); + System.out.println("Cancel Task : " + taskid + " > " + cancel); + + ThreadPool.waitFor(1 * 1000); + } + + static class TestTask extends BulkTask { + private String name; + private int number; + protected TestTask(String name, int number) { + this.name = "[ " + name.toUpperCase() + " ] "; + this.number = number; + } + @Override + public void run() { + this.setTotal(this.number); + for (int i = 0; i < this.number; i++) { + if (this.isInterrupted()) { + System.err.println(this.name + "Interrupted : " + this.getComplete() + " / " + this.getTotal()); + break; + } + + ThreadPool.waitFor(200); // Mock time + System.out.println(this.name + "Mock ... " + i); + this.setCompleteOne(); + } + this.completedAfter(); + } + } +} diff --git a/src/test/java/com/rebuild/server/task/QuickCodeReindexTaskTest.java b/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java similarity index 77% rename from src/test/java/com/rebuild/server/task/QuickCodeReindexTaskTest.java rename to src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java index 081773484..60bb30545 100644 --- a/src/test/java/com/rebuild/server/task/QuickCodeReindexTaskTest.java +++ b/src/test/java/com/rebuild/server/helper/task/QuickCodeReindexTaskTest.java @@ -16,13 +16,21 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package com.rebuild.server.task; +package com.rebuild.server.helper.task; + +import org.junit.Test; + +import com.rebuild.server.TestSupport; /** * * @author devezhao * @since 01/04/2019 */ -public class QuickCodeReindexTaskTest { - +public class QuickCodeReindexTaskTest extends TestSupport { + + @Test + public void testReindex() throws Exception { + + } } diff --git a/src/test/java/com/rebuild/server/service/base/GeneralEntityServiceTest.java b/src/test/java/com/rebuild/server/service/base/GeneralEntityServiceTest.java new file mode 100644 index 000000000..71f3a2761 --- /dev/null +++ b/src/test/java/com/rebuild/server/service/base/GeneralEntityServiceTest.java @@ -0,0 +1,63 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.service.base; + +import org.junit.Assert; +import org.junit.Test; + +import com.rebuild.server.Application; +import com.rebuild.server.TestSupport; +import com.rebuild.server.metadata.EntityHelper; +import com.rebuild.server.service.IEntityService; +import com.rebuild.server.service.bizz.UserService; + +import cn.devezhao.persist4j.Record; +import cn.devezhao.persist4j.engine.ID; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class GeneralEntityServiceTest extends TestSupport { + + @Test + public void testGetEntityService() throws Exception { + IEntityService ies = Application.getEntityService(EntityHelper.User); + Assert.assertTrue(ies.getEntityCode() == EntityHelper.User); + } + + @Test + public void testCRUD() throws Exception { + Record record = EntityHelper.forNew(EntityHelper.Role, UserService.ADMIN_USER); + record.setString("name", "测试角色"); + record = Application.getEntityService(EntityHelper.Role).create(record); + + ID roleId = record.getPrimary(); + System.out.println(Application.getUserStore().getRole(roleId).getName()); + + record = EntityHelper.forUpdate(roleId, UserService.ADMIN_USER); + record.setString("name", "测试角色-2"); + record = Application.getEntityService(EntityHelper.Role).createOrUpdate(record); + + System.out.println(Application.getUserStore().getRole(roleId).getName()); + + Application.getEntityService(EntityHelper.Role).delete(roleId); + } +} diff --git a/src/test/java/com/rebuild/server/service/bizz/UserStoreTest.java b/src/test/java/com/rebuild/server/service/bizz/UserStoreTest.java new file mode 100644 index 000000000..fcc8513ef --- /dev/null +++ b/src/test/java/com/rebuild/server/service/bizz/UserStoreTest.java @@ -0,0 +1,45 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.service.bizz; + +import org.junit.Test; + +import com.rebuild.server.Application; +import com.rebuild.server.TestSupport; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class UserStoreTest extends TestSupport { + + @Test + public void testStore() throws Exception { + Application.getUserStore().getAllUsers(); + Application.getUserStore().getAllDepartments(); + } + + @Test + public void testRefresh() throws Exception { + Application.getUserStore().refreshUser(UserService.ADMIN_USER); + Application.getUserStore().refreshDepartment(DepartmentService.ROOT_DEPT); + Application.getUserStore().refreshRole(RoleService.ADMIN_ROLE, true); + } +} diff --git a/src/test/java/com/rebuild/server/service/query/AdvFilterParserTest.java b/src/test/java/com/rebuild/server/service/query/AdvFilterParserTest.java new file mode 100644 index 000000000..2cd1e1099 --- /dev/null +++ b/src/test/java/com/rebuild/server/service/query/AdvFilterParserTest.java @@ -0,0 +1,48 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.service.query; + +import org.junit.Test; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.rebuild.server.TestSupport; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class AdvFilterParserTest extends TestSupport { + + @Test + public void testBaseParse() throws Exception { + JSONObject filterExp = new JSONObject(); + filterExp.put("entity", "User"); + JSONArray items = new JSONArray(); + filterExp.put("items", items); + + // Filter entry + items.add(JSON.parseObject("{ op:'LK', field:'loginName', value:'admin' }")); + + String where = new AdvFilterParser(filterExp).toSqlWhere(); + System.out.println(where); + } +} diff --git a/src/test/java/com/rebuild/server/service/query/QueryFactoryTest.java b/src/test/java/com/rebuild/server/service/query/QueryFactoryTest.java new file mode 100644 index 000000000..6c864823a --- /dev/null +++ b/src/test/java/com/rebuild/server/service/query/QueryFactoryTest.java @@ -0,0 +1,49 @@ +/* +rebuild - Building your system freely. +Copyright (C) 2018 devezhao + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package com.rebuild.server.service.query; + +import org.junit.Assert; +import org.junit.Test; + +import com.rebuild.server.Application; +import com.rebuild.server.TestSupport; +import com.rebuild.server.service.bizz.UserService; + +import cn.devezhao.bizz.security.AccessDeniedException; +import cn.devezhao.persist4j.Filter; + +/** + * + * @author devezhao + * @since 01/04/2019 + */ +public class QueryFactoryTest extends TestSupport { + + @Test + public void testBaseQuery() throws Exception { + Filter filter = Application.getSecurityManager().createQueryFilter(UserService.SYSTEM_USER); + Object[][] array = Application.getQueryFactory().createQuery("select loginName from User", filter).array(); + Assert.assertTrue(array.length > 0); + } + + @Test(expected=AccessDeniedException.class) + public void testNoUser() throws Exception { + Application.getQueryFactory().createQuery("select loginName from User").array(); + } +}