001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.identity;
017
018import com.google.common.collect.Lists;
019import org.kuali.rice.core.api.util.Truth;
020import org.kuali.rice.kim.api.identity.name.EntityName;
021import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
022import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
023
024import javax.persistence.EntityManager;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029public class IdentityServiceDaoJpa implements IdentityServiceDao {
030
031    private static final int PARTITION_SIZE = 500;
032
033    private EntityManager entityManager;
034
035    @Override
036    public Map<String, EntityNamePrincipalName> getDefaultNamesByPrincipalIds(List<String> principalIds) {
037        Map<String, EntityNamePrincipalName> results = new HashMap<String, EntityNamePrincipalName>();
038
039        // This partitioning is required because EclipseLink does not handle splitting up IN clauses with a large number of values into chunks
040        List<List<String>> partitionedPrincipalIds = Lists.partition(principalIds, PARTITION_SIZE);
041        for(List<String> partition : partitionedPrincipalIds) {
042            List<NameHolder> names = getEntityManager().createNamedQuery("EntityNameBo.findDefaultNamesForPrincipalIds",
043                    NameHolder.class).setParameter("principalIds", partition).getResultList();
044            for(NameHolder name : names) {
045                EntityNamePrincipalName.Builder nameBuilder = EntityNamePrincipalName.Builder.create();
046                EntityNameBo entityName = name.getEntityName();
047                entityName.setSuppressName(name.isSuppressName());
048                nameBuilder.setDefaultName(EntityName.Builder.create(entityName));
049                nameBuilder.setPrincipalName(name.getPrincipalName());
050                results.put(name.getPrincipalId(), nameBuilder.build());
051            }
052        }
053        return results;
054    }
055
056    public EntityManager getEntityManager() {
057        return entityManager;
058    }
059
060    public void setEntityManager(EntityManager entityManager) {
061        this.entityManager = entityManager;
062    }
063
064    public static class NameHolder {
065
066        private EntityNameBo entityName;
067        private String principalId;
068        private String principalName;
069        private boolean suppressName;
070
071        public NameHolder() {}
072
073        public NameHolder(EntityNameBo entityName, String principalId, String principalName, boolean suppressName) {
074            this.entityName = entityName;
075            this.principalId = principalId;
076            this.principalName = principalName;
077            this.suppressName = suppressName;
078        }
079
080        public EntityNameBo getEntityName() {
081            return entityName;
082        }
083
084        public void setEntityName(EntityNameBo entityName) {
085            this.entityName = entityName;
086        }
087
088        public String getPrincipalId() {
089            return principalId;
090        }
091
092        public void setPrincipalId(String principalId) {
093            this.principalId = principalId;
094        }
095
096        public String getPrincipalName() {
097            return principalName;
098        }
099
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}