View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.identity;
17  
18  import com.google.common.collect.Lists;
19  import org.kuali.rice.core.api.util.Truth;
20  import org.kuali.rice.kim.api.identity.name.EntityName;
21  import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
22  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
23  
24  import javax.persistence.EntityManager;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  public class IdentityServiceDaoJpa implements IdentityServiceDao {
30  
31      private static final int PARTITION_SIZE = 500;
32  
33      private EntityManager entityManager;
34  
35      @Override
36      public Map<String, EntityNamePrincipalName> getDefaultNamesByPrincipalIds(List<String> principalIds) {
37          Map<String, EntityNamePrincipalName> results = new HashMap<String, EntityNamePrincipalName>();
38  
39          // This partitioning is required because EclipseLink does not handle splitting up IN clauses with a large number of values into chunks
40          List<List<String>> partitionedPrincipalIds = Lists.partition(principalIds, PARTITION_SIZE);
41          for(List<String> partition : partitionedPrincipalIds) {
42              List<NameHolder> names = getEntityManager().createNamedQuery("EntityNameBo.findDefaultNamesForPrincipalIds",
43                      NameHolder.class).setParameter("principalIds", partition).getResultList();
44              for(NameHolder name : names) {
45                  EntityNamePrincipalName.Builder nameBuilder = EntityNamePrincipalName.Builder.create();
46                  EntityNameBo entityName = name.getEntityName();
47                  entityName.setSuppressName(name.isSuppressName());
48                  nameBuilder.setDefaultName(EntityName.Builder.create(entityName));
49                  nameBuilder.setPrincipalName(name.getPrincipalName());
50                  results.put(name.getPrincipalId(), nameBuilder.build());
51              }
52          }
53          return results;
54      }
55  
56      public EntityManager getEntityManager() {
57          return entityManager;
58      }
59  
60      public void setEntityManager(EntityManager entityManager) {
61          this.entityManager = entityManager;
62      }
63  
64      public static class NameHolder {
65  
66          private EntityNameBo entityName;
67          private String principalId;
68          private String principalName;
69          private boolean suppressName;
70  
71          public NameHolder() {}
72  
73          public NameHolder(EntityNameBo entityName, String principalId, String principalName, boolean suppressName) {
74              this.entityName = entityName;
75              this.principalId = principalId;
76              this.principalName = principalName;
77              this.suppressName = suppressName;
78          }
79  
80          public EntityNameBo getEntityName() {
81              return entityName;
82          }
83  
84          public void setEntityName(EntityNameBo entityName) {
85              this.entityName = entityName;
86          }
87  
88          public String getPrincipalId() {
89              return principalId;
90          }
91  
92          public void setPrincipalId(String principalId) {
93              this.principalId = principalId;
94          }
95  
96          public String getPrincipalName() {
97              return principalName;
98          }
99  
100         public void setPrincipalName(String principalName) {
101             this.principalName = principalName;
102         }
103 
104         public boolean isSuppressName() {
105             return suppressName;
106         }
107 
108         public void setSuppressName(boolean suppressName) {
109             this.suppressName = suppressName;
110         }
111     }
112 }