View Javadoc

1   /*
2    * Copyright 2010 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 org.springframework.ldap.core.DirContextOperations;
19  import org.springframework.ldap.core.support.AbstractContextMapper;
20  
21  import org.kuali.rice.kim.api.identity.Type;
22  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
23  import org.kuali.rice.kim.util.Constants;
24  
25  import static org.kuali.rice.core.util.BufferedLogger.*;
26  import static org.apache.commons.lang.StringUtils.isBlank;
27  import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;
28  
29  /**
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class EntityPhoneMapper extends AbstractContextMapper {
34      private Constants constants;
35      
36      public EntityPhone.Builder mapFromContext(DirContextOperations context, boolean isdefault) {
37          return (EntityPhone.Builder) doMapFromContext(context, isdefault);
38      }
39  
40      public EntityPhone.Builder mapFromContext(DirContextOperations context) {
41          return mapFromContext(context, true);
42      }
43  
44      public Object doMapFromContext(DirContextOperations context) {
45          return doMapFromContext(context, true);
46      }
47  
48      protected Object doMapFromContext(DirContextOperations context, boolean isdefault) {        
49          final EntityPhone.Builder builder = EntityPhone.Builder.create();
50          debug("Looking up attribute from context ", getConstants().getEmployeePhoneLdapProperty());
51          final String pn = context.getStringAttribute(getConstants().getEmployeePhoneLdapProperty());
52          
53          if (isBlank(pn) || equalsIgnoreCase("NA", pn)) {
54              debug("Got nothing. Giving nothing back.");
55              return null;
56          }
57          
58          String phoneNumber = pn;
59          if (pn.length() >= 10) {
60              phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3, 6) + "-" + pn.substring(6);
61          } else if (pn.length() >= 6) {
62                      phoneNumber = pn.substring(0, 3) + "-" + pn.substring(3);
63          }
64          final String countryCode = getConstants().getDefaultCountryCode();
65          
66          builder.setCountryCode(countryCode);
67          builder.setPhoneNumber(phoneNumber);
68          builder.setPhoneType(Type.Builder.create("WORK"));
69          builder.setActive(true);
70          builder.setDefaultValue(isdefault);
71  
72          return builder;
73      }
74      
75      /**
76       * Gets the value of constants
77       *
78       * @return the value of constants
79       */
80      public final Constants getConstants() {
81          return this.constants;
82      }
83  
84      /**
85       * Sets the value of constants
86       *
87       * @param argConstants Value to assign to this.constants
88       */
89      public final void setConstants(final Constants argConstants) {
90          this.constants = argConstants;
91      }
92  }