001/**
002 * Copyright 2005-2015 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.ldap;
017
018import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
019import static org.apache.commons.lang.StringUtils.isBlank;
020import static org.kuali.rice.core.util.BufferedLogger.debug;
021
022import org.kuali.rice.kim.api.identity.CodedAttribute;
023import org.kuali.rice.kim.api.identity.phone.EntityPhone;
024import org.springframework.ldap.core.DirContextOperations;
025
026/**
027 * 
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class EntityPhoneMapper extends BaseMapper<EntityPhone> {
031
032        @Override
033    EntityPhone mapDtoFromContext(DirContextOperations context) {
034        return mapDtoFromContext(context, true);
035    }
036    
037    EntityPhone mapDtoFromContext(DirContextOperations context, boolean isdefault) {
038        EntityPhone.Builder builder = mapBuilderFromContext(context, isdefault);
039        return builder != null ? builder.build() : null;
040    }
041
042    EntityPhone.Builder mapBuilderFromContext(DirContextOperations context) {
043        return mapBuilderFromContext(context, true);
044    }
045
046    EntityPhone.Builder mapBuilderFromContext(DirContextOperations context, boolean isdefault) {        
047        final EntityPhone.Builder builder = EntityPhone.Builder.create();
048        debug("Looking up attribute from context ", getConstants().getEmployeePhoneLdapProperty());
049        final String pn = context.getStringAttribute(getConstants().getEmployeePhoneLdapProperty());
050        
051        if (isBlank(pn) || equalsIgnoreCase("NA", pn)) {
052            debug("Got nothing. Giving nothing back.");
053            return null;
054        }
055        
056        String phoneNumber = pn;
057        if (pn.length() >= 10) {
058            phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3, 6) + "-" + pn.substring(6);
059        } else if (pn.length() >= 6) {
060                    phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3);
061        }
062        final String countryCode = getConstants().getDefaultCountryCode();
063        
064        builder.setCountryCode(countryCode);
065        builder.setPhoneNumber(phoneNumber);
066        builder.setPhoneType(CodedAttribute.Builder.create("WORK"));
067        builder.setActive(true);
068        builder.setDefaultValue(isdefault);
069
070        return builder;
071    }
072
073}