View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.flowable.support;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import org.apache.syncope.core.persistence.api.dao.GroupDAO;
24  import org.apache.syncope.core.persistence.api.dao.UserDAO;
25  import org.apache.syncope.core.persistence.api.entity.group.Group;
26  import org.apache.syncope.core.persistence.api.entity.user.UMembership;
27  import org.flowable.idm.api.User;
28  import org.flowable.idm.engine.impl.UserQueryImpl;
29  import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
30  import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  public class SyncopeUserQueryImpl extends UserQueryImpl {
35  
36      private static final long serialVersionUID = 4403344392227706318L;
37  
38      @Autowired
39      private UserDAO userDAO;
40  
41      @Autowired
42      private GroupDAO groupDAO;
43  
44      private List<User> result;
45  
46      private static User fromSyncopeUser(final org.apache.syncope.core.persistence.api.entity.user.User syncopeUser) {
47          UserEntity user = new UserEntityImpl();
48          user.setId(syncopeUser.getUsername());
49          return user;
50      }
51  
52      private void execute() {
53          if (id != null) {
54              org.apache.syncope.core.persistence.api.entity.user.User user = userDAO.findByUsername(id);
55              if (user == null) {
56                  result = List.of();
57              } else if (groupId == null || userDAO.findAllGroupNames(user).contains(groupId)) {
58                  result = List.of(fromSyncopeUser(user));
59              }
60          } else if (groupId != null) {
61              Group group = groupDAO.findByName(groupId);
62              if (group == null) {
63                  result = List.of();
64              } else {
65                  result = new ArrayList<>();
66                  List<UMembership> memberships = groupDAO.findUMemberships(group);
67                  memberships.stream().map(membership -> fromSyncopeUser(membership.getLeftEnd())).
68                          filter(user -> (!result.contains(user))).
69                          forEachOrdered(user -> result.add(user));
70              }
71          }
72      }
73  
74      @Transactional(readOnly = true)
75      @Override
76      public long count() {
77          checkQueryOk();
78  
79          this.resultType = ResultType.COUNT;
80          if (result == null) {
81              execute();
82          }
83          return result.size();
84      }
85  
86      @Transactional(readOnly = true)
87      @Override
88      public List<User> list() {
89          checkQueryOk();
90  
91          this.resultType = ResultType.LIST;
92          if (result == null) {
93              execute();
94          }
95          return result;
96      }
97  
98  }