瀏覽代碼

Replace UserPanel with UserPanelRelation

sunyj 8 年之前
父節點
當前提交
12486b594c

+ 2 - 2
kanban-console/src/main/java/com/uas/kanban/dao/PanelDao.java

@@ -23,7 +23,7 @@ import java.util.Map;
 public class PanelDao extends BaseDao<Panel> {
 
     @Autowired
-    private UserPanelDao userPanelDao;
+    private UserPanelRelationDao userPanelRelationDao;
 
     @Override
     protected Map<String, Object> globalFilter() {
@@ -32,7 +32,7 @@ public class PanelDao extends BaseDao<Panel> {
         if (user.getRole() == User.Role.Admin) {
             return null;
         }
-        List<String> panelCodes = userPanelDao.findAllPanelCodes();
+        List<String> panelCodes = userPanelRelationDao.findAllPanelCodes();
         if (panelCodes == null) {
             panelCodes = new ArrayList<>();
         }

+ 7 - 7
kanban-console/src/main/java/com/uas/kanban/dao/PanelInstanceDao.java

@@ -3,7 +3,7 @@ package com.uas.kanban.dao;
 import com.uas.kanban.annotation.NotEmpty;
 import com.uas.kanban.base.BaseDao;
 import com.uas.kanban.model.PanelInstance;
-import com.uas.kanban.model.UserPanel;
+import com.uas.kanban.model.UserPanelRelation;
 import com.uas.kanban.support.SystemSession;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
@@ -22,14 +22,14 @@ import java.util.Map;
 public class PanelInstanceDao extends BaseDao<PanelInstance> {
 
     @Autowired
-    private UserPanelDao userPanelDao;
+    private UserPanelRelationDao userPanelRelationDao;
 
     @Override
     protected Map<String, Object> globalFilter() {
-        List<String> userPanelCodes = userPanelDao.findAllCodes();
+        List<String> userPanelRelationCodes = userPanelRelationDao.findAllCodes();
         Map<String, Object> filters = new HashMap<>();
         // 所有用户只能操作自己的实例
-        filters.put("userPanelCode in", userPanelCodes);
+        filters.put("userPanelRelationCode in", userPanelRelationCodes);
         return filters;
     }
 
@@ -45,10 +45,10 @@ public class PanelInstanceDao extends BaseDao<PanelInstance> {
      * @return 面板实例
      */
     public PanelInstance findByPanelCode(@NotEmpty("panelCode") String panelCode) {
-        UserPanel userPanel = userPanelDao.findByUserCodeAndPanelCode(SystemSession.getUser().getCode(), panelCode);
-        if (userPanel == null) {
+        UserPanelRelation userPanelRelation = userPanelRelationDao.findByUserCodeAndPanelCode(SystemSession.getUser().getCode(), panelCode);
+        if (userPanelRelation == null) {
             return null;
         }
-        return findOneBy("userPanelCode", userPanel.getCode());
+        return findOneBy("userPanelRelationCode", userPanelRelation.getCode());
     }
 }

+ 18 - 18
kanban-console/src/main/java/com/uas/kanban/dao/UserPanelDao.java → kanban-console/src/main/java/com/uas/kanban/dao/UserPanelRelationDao.java

@@ -3,7 +3,7 @@ package com.uas.kanban.dao;
 import com.uas.kanban.annotation.NotEmpty;
 import com.uas.kanban.base.BaseDao;
 import com.uas.kanban.model.User;
-import com.uas.kanban.model.UserPanel;
+import com.uas.kanban.model.UserPanelRelation;
 import com.uas.kanban.support.SystemSession;
 import com.uas.kanban.util.CollectionUtils;
 import org.mongodb.morphia.query.Query;
@@ -14,13 +14,13 @@ import java.util.List;
 import java.util.Map;
 
 /**
- * 用户面板(存储用户可查看的面板)
+ * 用户面板关系(存储分配给用户的面板)
  *
  * @author sunyj
  * @since 2017/10/18 14:21
  */
 @Component
-public class UserPanelDao extends BaseDao<UserPanel> {
+public class UserPanelRelationDao extends BaseDao<UserPanelRelation> {
 
     @Override
     protected Map<String, Object> globalFilter() {
@@ -33,16 +33,16 @@ public class UserPanelDao extends BaseDao<UserPanel> {
 
     @Override
     protected String collectionSimpleName() {
-        return "用户面板";
+        return "用户面板关系";
     }
 
     /**
-     * 获取所有 code
+     * 获取所有用户面板关系 code
      *
-     * @return code
+     * @return 用户面板关系 code
      */
     public List<String> findAllCodes() {
-        Query<UserPanel> query = createQuery();
+        Query<UserPanelRelation> query = createQuery();
         try {
             return findField(query, "code", String.class);
         } catch (NoSuchFieldException e) {
@@ -51,12 +51,12 @@ public class UserPanelDao extends BaseDao<UserPanel> {
     }
 
     /**
-     * 获取用户所能查看的面板 code
+     * 获取分配给用户的面板 code
      *
      * @return 面板 code
      */
     public List<String> findAllPanelCodes() {
-        Query<UserPanel> query = createQuery();
+        Query<UserPanelRelation> query = createQuery();
         try {
             return findField(query, "panelCode", String.class);
         } catch (NoSuchFieldException e) {
@@ -65,29 +65,29 @@ public class UserPanelDao extends BaseDao<UserPanel> {
     }
 
     /**
-     * 根据指定的用户 code 和面板 code 查询数据
+     * 根据指定的用户 code 和面板 code 查询用户面板关系
      *
      * @param userCode  用户 code
      * @param panelCode 面板 code
-     * @return 查询结果
+     * @return 用户面板关系
      */
-    public UserPanel findByUserCodeAndPanelCode(@NotEmpty("userCode") String userCode, @NotEmpty("panelCode") String panelCode) {
-        Query<UserPanel> query = createQuery();
+    public UserPanelRelation findByUserCodeAndPanelCode(@NotEmpty("userCode") String userCode, @NotEmpty("panelCode") String panelCode) {
+        Query<UserPanelRelation> query = createQuery();
         Map<String, Object> filters = new HashMap<>();
         filters.put("userCode", userCode);
         filters.put("panelCode", panelCode);
         filter(query, filters);
-        List<UserPanel> userPanels = find(query);
-        if (CollectionUtils.isEmpty(userPanels)) {
+        List<UserPanelRelation> userPanelRelations = find(query);
+        if (CollectionUtils.isEmpty(userPanelRelations)) {
             return null;
-        } else if (userPanels.size() > 1) {
+        } else if (userPanelRelations.size() > 1) {
             throw new IllegalStateException("存在不止一条数据");
         }
-        return userPanels.get(0);
+        return userPanelRelations.get(0);
     }
 
     /**
-     * 删除某个用户的数据,指定的面板除外
+     * 删除某个用户的用户面板关系,指定的面板除外
      *
      * @param userCode   用户 code
      * @param panelCodes 面板 code

+ 8 - 8
kanban-console/src/main/java/com/uas/kanban/model/PanelInstance.java

@@ -21,7 +21,7 @@ import java.util.List;
  * @since 2017/10/18 15:58
  */
 @Entity
-@Indexes(@Index(value = "name, userPanelCode", unique = true))
+@Indexes(@Index(value = "name, userPanelRelationCode", unique = true))
 public class PanelInstance extends BaseEntity {
 
     /**
@@ -60,10 +60,10 @@ public class PanelInstance extends BaseEntity {
     private List<RelatedKanban> relatedKanbans;
 
     /**
-     * 用户面板 code {@link UserPanel}
+     * 用户面板关系 code {@link UserPanelRelation}
      */
     @FieldProperty(nullable = false)
-    private String userPanelCode;
+    private String userPanelRelationCode;
 
     @Override
     public void init() {
@@ -105,13 +105,13 @@ public class PanelInstance extends BaseEntity {
         this.relatedKanbans = relatedKanbans;
     }
 
-    public String getUserPanelCode() {
+    public String getUserPanelRelationCode() {
         // TODO sort by enabled state
-        return userPanelCode;
+        return userPanelRelationCode;
     }
 
-    public void setUserPanelCode(String userPanelCode) {
-        this.userPanelCode = userPanelCode;
+    public void setUserPanelRelationCode(String userPanelRelationCode) {
+        this.userPanelRelationCode = userPanelRelationCode;
     }
 
     @Override
@@ -121,7 +121,7 @@ public class PanelInstance extends BaseEntity {
                 ", refreshFrequency=" + refreshFrequency +
                 ", parameters=" + parameters +
                 ", relatedKanbans=" + relatedKanbans +
-                ", userPanelCode='" + userPanelCode + '\'' +
+                ", userPanelRelationCode='" + userPanelRelationCode + '\'' +
                 "} " + super.toString();
     }
 

+ 5 - 5
kanban-console/src/main/java/com/uas/kanban/model/UserPanel.java → kanban-console/src/main/java/com/uas/kanban/model/UserPanelRelation.java

@@ -7,14 +7,14 @@ import org.mongodb.morphia.annotations.Index;
 import org.mongodb.morphia.annotations.Indexes;
 
 /**
- * 中间表,用户面板(存储用户可查看的面板)
+ * 中间表,用户面板关系(存储分配给用户的面板)
  *
  * @author sunyj
  * @since 2017/10/20 11:12
  */
 @Entity
 @Indexes(@Index(value = "userCode, panelCode", unique = true))
-public class UserPanel extends BaseEntity {
+public class UserPanelRelation extends BaseEntity {
 
     private static final long serialVersionUID = 1L;
 
@@ -30,10 +30,10 @@ public class UserPanel extends BaseEntity {
     @FieldProperty(nullable = false)
     private String panelCode;
 
-    public UserPanel() {
+    public UserPanelRelation() {
     }
 
-    public UserPanel(String userCode, String panelCode) {
+    public UserPanelRelation(String userCode, String panelCode) {
         this.userCode = userCode;
         this.panelCode = panelCode;
     }
@@ -56,7 +56,7 @@ public class UserPanel extends BaseEntity {
 
     @Override
     public String toString() {
-        return "UserPanel{" +
+        return "UserPanelRelation{" +
                 "userCode='" + userCode + '\'' +
                 ", panelCode='" + panelCode + '\'' +
                 "} " + super.toString();

+ 2 - 2
kanban-console/src/main/java/com/uas/kanban/service/PanelService.java

@@ -1,7 +1,7 @@
 package com.uas.kanban.service;
 
 import com.uas.kanban.annotation.NotEmpty;
-import com.uas.kanban.model.UserPanel;
+import com.uas.kanban.model.UserPanelRelation;
 
 import java.util.List;
 
@@ -20,6 +20,6 @@ public interface PanelService {
      * @param panelCodes 面板 code
      * @return 面板实例
      */
-    List<UserPanel> assignPanel(@NotEmpty("userCode") String userCode, @NotEmpty("panelCodes") List<String> panelCodes);
+    List<UserPanelRelation> assignPanel(@NotEmpty("userCode") String userCode, @NotEmpty("panelCodes") List<String> panelCodes);
 
 }

+ 17 - 17
kanban-console/src/main/java/com/uas/kanban/service/impl/PanelInstanceServiceImpl.java

@@ -6,7 +6,7 @@ import com.uas.kanban.base.BaseService;
 import com.uas.kanban.dao.KanbanDao;
 import com.uas.kanban.dao.PanelDao;
 import com.uas.kanban.dao.PanelInstanceDao;
-import com.uas.kanban.dao.UserPanelDao;
+import com.uas.kanban.dao.UserPanelRelationDao;
 import com.uas.kanban.exception.OperationException;
 import com.uas.kanban.model.*;
 import com.uas.kanban.service.PanelInstanceService;
@@ -47,7 +47,7 @@ public class PanelInstanceServiceImpl extends BaseService<PanelInstance> impleme
     private KanbanDao kanbanDao;
 
     @Autowired
-    private UserPanelDao userPanelDao;
+    private UserPanelRelationDao userPanelRelationDao;
 
     @Autowired
     private ParameterService parameterService;
@@ -91,7 +91,7 @@ public class PanelInstanceServiceImpl extends BaseService<PanelInstance> impleme
      * @return 生成的参数
      */
     private List<Parameter> generateParameters(@NotEmpty("panelInstance") PanelInstance panelInstance) {
-        String panelCode = userPanelDao.checkExist(panelInstance.getUserPanelCode()).getPanelCode();
+        String panelCode = userPanelRelationDao.checkExist(panelInstance.getUserPanelRelationCode()).getPanelCode();
         Panel panel = panelDao.checkExist(panelCode);
         String dataSourceCode = panel.getDataSourceCode();
         List<Parameter> parameters = parameterService.getByPanelCode(panelCode);
@@ -165,7 +165,7 @@ public class PanelInstanceServiceImpl extends BaseService<PanelInstance> impleme
     @Override
     public Map<String, Object> parseData(@NotEmpty("code") String code, String kanbanCode) {
         PanelInstance panelInstance = panelInstanceDao.checkExist(code);
-        Panel panel = panelDao.checkExist(userPanelDao.checkExist(panelInstance.getUserPanelCode()).getPanelCode());
+        Panel panel = panelDao.checkExist(userPanelRelationDao.checkExist(panelInstance.getUserPanelRelationCode()).getPanelCode());
         List<PanelInstance.RelatedKanban> relatedKanbans = panelInstance.getRelatedKanbans();
         if (CollectionUtils.isEmpty(relatedKanbans)) {
             throw new IllegalStateException("面板下没有看板");
@@ -228,31 +228,31 @@ public class PanelInstanceServiceImpl extends BaseService<PanelInstance> impleme
         if (panelInstance == null) {
             User user = SystemSession.getUser();
             String userCode = user.getCode();
-            UserPanel userPanel = userPanelDao.findByUserCodeAndPanelCode(userCode, panelCode);
-            // 管理员可以查看所有面板,不必先分配,但是想要保存实例,就需要先建立关联
-            if (userPanel == null) {
+            UserPanelRelation userPanelRelation = userPanelRelationDao.findByUserCodeAndPanelCode(userCode, panelCode);
+            // 管理员可以查看所有面板,不必先分配,但是想要保存实例,就需要先建立用户面板关系
+            if (userPanelRelation == null) {
                 if (user.getRole() == User.Role.Admin) {
-                    userPanel = userPanelDao.save(new UserPanel(userCode, panelCode));
-                }else{
+                    userPanelRelation = userPanelRelationDao.save(new UserPanelRelation(userCode, panelCode));
+                } else {
                     throw new IllegalArgumentException("用户未分配该面板");
                 }
             }
-            panelInstance = generateInstance(userPanel);
+            panelInstance = generateInstance(userPanelRelation);
         }
         return panelInstance;
     }
 
     /**
-     * 根据指定的用户面板生成面板实例
+     * 根据用户面板关系生成面板实例
      *
-     * @param userPanel 所指定的用户面板
+     * @param userPanelRelation 用户面板关系
      * @return 面板实例
      */
-    private PanelInstance generateInstance(@NotEmpty("userPanel") UserPanel userPanel) {
-        String panelCode = userPanel.getPanelCode();
-        Panel panel = panelDao.checkExist(panelCode);
+    private PanelInstance generateInstance(@NotEmpty("userPanelRelation") UserPanelRelation userPanelRelation) {
+        String panelCode = userPanelRelation.getPanelCode();
+        panelDao.checkExist(panelCode);
         PanelInstance panelInstance = new PanelInstance();
-        panelInstance.setUserPanelCode(userPanel.getCode());
+        panelInstance.setUserPanelRelationCode(userPanelRelation.getCode());
         initSwitchFrequence(panelInstance);
         panelInstance.toParameters(generateParameters(panelInstance));
         List<Kanban> kanbans = kanbanDao.findByPanelCode(panelCode);
@@ -268,7 +268,7 @@ public class PanelInstanceServiceImpl extends BaseService<PanelInstance> impleme
      */
     private void initSwitchFrequence(@NotEmpty("panelInstance") PanelInstance panelInstance)
             throws IllegalArgumentException {
-        Panel panel = panelDao.checkExist(userPanelDao.checkExist(panelInstance.getUserPanelCode()).getPanelCode());
+        Panel panel = panelDao.checkExist(userPanelRelationDao.checkExist(panelInstance.getUserPanelRelationCode()).getPanelCode());
         switch (panel.getDisplay()) {
             case AutoSwitch:
                 if (panelInstance.getSwitchFrequency() == null) {

+ 13 - 17
kanban-console/src/main/java/com/uas/kanban/service/impl/PanelServiceImpl.java

@@ -4,11 +4,10 @@ import com.uas.kanban.annotation.NotEmpty;
 import com.uas.kanban.base.BaseService;
 import com.uas.kanban.dao.DataSourceDao;
 import com.uas.kanban.dao.PanelDao;
-import com.uas.kanban.dao.UserPanelDao;
+import com.uas.kanban.dao.UserPanelRelationDao;
 import com.uas.kanban.exception.OperationException;
 import com.uas.kanban.model.Panel;
-import com.uas.kanban.model.UserPanel;
-import com.uas.kanban.service.PanelInstanceService;
+import com.uas.kanban.model.UserPanelRelation;
 import com.uas.kanban.service.PanelService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -32,10 +31,7 @@ public class PanelServiceImpl extends BaseService<Panel> implements PanelService
     private DataSourceDao dataSourceDao;
 
     @Autowired
-    private UserPanelDao userPanelDao;
-
-    @Autowired
-    private PanelInstanceService panelInstanceService;
+    private UserPanelRelationDao userPanelRelationDao;
 
     @Override
     public Panel save(@NotEmpty("json") String json) {
@@ -88,19 +84,19 @@ public class PanelServiceImpl extends BaseService<Panel> implements PanelService
     }
 
     @Override
-    public List<UserPanel> assignPanel(@NotEmpty("userCode") String userCode, @NotEmpty("panelCodes") List<String> panelCodes) {
-        List<UserPanel> userPanels = new ArrayList<>();
+    public List<UserPanelRelation> assignPanel(@NotEmpty("userCode") String userCode, @NotEmpty("panelCodes") List<String> panelCodes) {
+        List<UserPanelRelation> userPanelRelations = new ArrayList<>();
         for (String panelCode : panelCodes) {
-            // 如果已存在用户看板,不必再进行关联
-            UserPanel userPanel = userPanelDao.findByUserCodeAndPanelCode(userCode, panelCode);
-            if (userPanel == null) {
-                userPanel = userPanelDao.save(new UserPanel(userCode, panelCode));
+            // 如果已存在用户面板关系,不必再进行关联
+            UserPanelRelation userPanelRelation = userPanelRelationDao.findByUserCodeAndPanelCode(userCode, panelCode);
+            if (userPanelRelation == null) {
+                userPanelRelation = userPanelRelationDao.save(new UserPanelRelation(userCode, panelCode));
             }
-            userPanels.add(userPanel);
+            userPanelRelations.add(userPanelRelation);
         }
-        // 重新分配面板时,删除旧的
-        userPanelDao.deleteExcept(userCode, panelCodes);
+        // 重新分配面板时,删除旧的关系
+        userPanelRelationDao.deleteExcept(userCode, panelCodes);
         // delete user's PanelInstance before generating new instances?
-        return userPanels;
+        return userPanelRelations;
     }
 }