001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kim.ldap;
017    
018    import static org.apache.commons.lang.StringUtils.contains;
019    import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
020    import static org.kuali.rice.core.util.BufferedLogger.debug;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliation;
027    import org.kuali.rice.kim.api.identity.affiliation.EntityAffiliationType;
028    import org.springframework.ldap.core.DirContextOperations;
029    
030    /**
031     * Maps LDAP Information to KIM Entity Affiliation
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class EntityAffiliationMapper extends BaseMapper<List<EntityAffiliation>> {
036    
037            @Override
038        List<EntityAffiliation> mapDtoFromContext(DirContextOperations context) {
039            List<EntityAffiliation.Builder> builders = mapBuilderFromContext(context);
040            List<EntityAffiliation> affiliations = new ArrayList<EntityAffiliation>();
041            if (builders != null) {
042                    for (EntityAffiliation.Builder builder : builders) {
043                            affiliations.add(builder.build());
044                    }
045            }
046            return affiliations;
047        }
048        
049        List<EntityAffiliation.Builder> mapBuilderFromContext(DirContextOperations context) {
050            List<EntityAffiliation.Builder> retval = new ArrayList<EntityAffiliation.Builder>();
051            final String primaryAffiliationProperty = getConstants().getPrimaryAffiliationLdapProperty();
052            final String affiliationProperty = getConstants().getAffiliationLdapProperty();
053            debug("Got affiliation ", context.getStringAttribute(primaryAffiliationProperty));
054            debug("Got affiliation ", context.getStringAttribute(affiliationProperty));
055            
056            String primaryAffiliation = context.getStringAttribute(primaryAffiliationProperty);
057            
058            int affiliationId = 1;
059            String affiliationCode = getAffiliationTypeCodeForName(primaryAffiliation);
060    
061            final EntityAffiliation.Builder aff1 = EntityAffiliation.Builder.create();
062            aff1.setAffiliationType(EntityAffiliationType.Builder.create(affiliationCode == null ? "AFLT" : affiliationCode));
063            aff1.setCampusCode(getConstants().getDefaultCampusCode());
064            aff1.setId("" + affiliationId++);
065            aff1.setDefaultValue(true);
066            aff1.setActive(true);
067            retval.add(aff1);
068            
069            String[] affiliations = context.getStringAttributes(affiliationProperty);
070            // Create an empty array to prevent NPE
071            if (affiliations == null) {
072                affiliations = new String[] {};
073            }
074    
075            for (String affiliation : affiliations) {
076                if (!StringUtils.equals(affiliation, primaryAffiliation)) {
077                    affiliationCode = getAffiliationTypeCodeForName(affiliation);
078                    if (affiliationCode != null && !hasAffiliation(retval, affiliationCode)) {
079                        final EntityAffiliation.Builder aff = EntityAffiliation.Builder.create();
080                        aff.setAffiliationType(EntityAffiliationType.Builder.create(affiliationCode));
081                        aff.setCampusCode(getConstants().getDefaultCampusCode());
082                        aff.setId("" + affiliationId++);
083                        aff.setDefaultValue(false);
084                        aff.setActive(true);
085                        retval.add(aff);
086                    }
087                }
088            }
089            
090            return retval;
091        }
092        
093        /**
094         *
095         * Returns the affiliation type code for the given affiliation name. Returns null if the affiliation is not found
096         * @param affiliationName
097         * @return null if no matching affiliation is found
098         */
099        protected String getAffiliationTypeCodeForName(String affiliationName) {
100            String[] mappings = getConstants().getAffiliationMappings().split(",");
101            for (String affilMap : mappings) {
102                if (contains(affilMap, affiliationName)) {
103                    return affilMap.split("=")[1];
104                }
105            }
106            return null;
107        }
108    
109        protected boolean hasAffiliation(List<EntityAffiliation.Builder> affiliations, String affiliationCode) {
110            for (EntityAffiliation.Builder affiliation : affiliations) {
111                if (equalsIgnoreCase(affiliation.getAffiliationType().getCode(), affiliationCode)) {
112                    return true;
113                }
114            }
115            return false;
116        }
117    
118    }