2013-07-08 67 views
0

我是編程界的新手,我喜歡它。不同角色的Query.FilterOperator

我對Query.Filter運算符有一些問題,由不同的角色。

  • 管理 - 管理員可以查看所有留下的歷史
  • 主管 - 主管可以在他查看自己和全體員工/她
  • 員工 - 員工只能查看自己的休假歷史

從我做過的測試。管理員和員工工作正常,我面臨的問題是主管,主管能夠查看他/她下的員工,但無法查看自己的離開歷史

這裏是代碼的一部分,感謝任何人都可以幫助我。謝謝

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 
    log.debug(ViewHistory.class); 
    DataTableModel dataTableModel = DataTablesUtility.getParam(request); 
    String regionSelected = request.getParameter("cri_region"); 
    String sEcho = dataTableModel.sEcho; 
    int iTotalRecords = 0; // total number of records (unfiltered) 
    int iTotalDisplayRecords = 0; //value will be set when code filters companies by keyword 
    JsonArray data = new JsonArray(); //data that will be shown in the table 

    String emailAddress = (String)request.getSession().getAttribute("emailAdd"); 
    Boolean isAdmin = false; 
    Boolean isSupervisor = false; 

    AdministratorService as = new AdministratorService(); 
    for(Administrator admin : as.getAdministrators()){ 
     if(admin.getEmailAddress().equalsIgnoreCase(emailAddress)){ 
      isAdmin = true; 
     } 
    } 

    SupervisorService ss = new SupervisorService(); 
    for(Supervisor s : ss.getSupervisors()){ 
     if(s.getEmailAddress().equalsIgnoreCase(emailAddress)){ 
      isSupervisor = true; 
     } 
    } 

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
    Query q = new Query(History.class.getSimpleName()); 
      // only admin can view all leave history 
      if(!isAdmin && !isSupervisor){ 
       Filter emailFilter = new FilterPredicate("emailAdd", 
         FilterOperator.EQUAL, 
         emailAddress); 
       Filter regionFilter = new FilterPredicate("region", 
         FilterOperator.EQUAL, 
         StringUtils.defaultString(regionSelected, "Singapore")); 
       Filter filter = CompositeFilterOperator.and(emailFilter, regionFilter); 
       q.setFilter(filter); 
      } 
      else if(!isAdmin && isSupervisor){ 
       List<String> list = new ArrayList<String>(); 
       EmployeeService es = new EmployeeService(); 
       for(Employee emp : es.getEmployees()){ 
        if(emailAddress.equalsIgnoreCase(emp.getSupervisor())){ 
         list.add(emp.getEmailAddress()); 
        } 
       } 

       // if some one under this supervisor 
       if(list != null && !list.isEmpty()){ 
        Filter filter = new FilterPredicate("emailAdd", 
          FilterOperator.IN, 
          list); 
        q.setFilter(filter); 
       } 
       else{ 
        // if not one under this supervisor , put update it self to avoid illegal argument 
        Filter filter = new FilterPredicate("emailAdd", 
          FilterOperator.EQUAL, 
          emailAddress); 
        q.setFilter(filter); 
       } 

      } 

    List<History> historyList = new LinkedList<History>(); 
    List<History> entityList = new LinkedList<History>(); 

回答

0

編輯:更新的建議。

如果目前對於主管,您可以正確地獲得所有將他作爲主管的員工列表,那麼只需將主管的電子郵件也添加到您將用作查詢離開歷史記錄的過濾器的列表中。因此,尋找員工的電子郵件列表之後,還可以添加:

list.add(emailAddress); //since emailAddress will be the email of the supervisor 
+0

親愛的託尼,感謝您的建議,我已經測試了它,現在它成爲主管只能查看下他/她 – user1944535

+0

自己的休假歷史,但沒有員工你能否請你檢查一下你的測試用例,你是否正在測試一個主管下沒有人的情況 –