View Javadoc

1   /**
2    * Copyright 2005-2013 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.ldap;
17  
18  import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
19  import static org.apache.commons.lang.StringUtils.isBlank;
20  import static org.kuali.rice.core.util.BufferedLogger.debug;
21  
22  import org.kuali.rice.kim.api.identity.CodedAttribute;
23  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
24  import org.springframework.ldap.core.DirContextOperations;
25  
26  /**
27   * 
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class EntityPhoneMapper extends BaseMapper<EntityPhone> {
31  
32  	@Override
33      EntityPhone mapDtoFromContext(DirContextOperations context) {
34          return mapDtoFromContext(context, true);
35      }
36      
37      EntityPhone mapDtoFromContext(DirContextOperations context, boolean isdefault) {
38      	EntityPhone.Builder builder = mapBuilderFromContext(context, isdefault);
39          return builder != null ? builder.build() : null;
40      }
41  
42      EntityPhone.Builder mapBuilderFromContext(DirContextOperations context) {
43          return mapBuilderFromContext(context, true);
44      }
45  
46      EntityPhone.Builder mapBuilderFromContext(DirContextOperations context, boolean isdefault) {        
47          final EntityPhone.Builder builder = EntityPhone.Builder.create();
48          debug("Looking up attribute from context ", getConstants().getEmployeePhoneLdapProperty());
49          final String pn = context.getStringAttribute(getConstants().getEmployeePhoneLdapProperty());
50          
51          if (isBlank(pn) || equalsIgnoreCase("NA", pn)) {
52              debug("Got nothing. Giving nothing back.");
53              return null;
54          }
55          
56          String phoneNumber = pn;
57          if (pn.length() >= 10) {
58              phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3, 6) + "-" + pn.substring(6);
59          } else if (pn.length() >= 6) {
60                      phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3);
61          }
62          final String countryCode = getConstants().getDefaultCountryCode();
63          
64          builder.setCountryCode(countryCode);
65          builder.setPhoneNumber(phoneNumber);
66          builder.setPhoneType(CodedAttribute.Builder.create("WORK"));
67          builder.setActive(true);
68          builder.setDefaultValue(isdefault);
69  
70          return builder;
71      }
72  
73  }