View Javadoc
1   /*
2    * Copyright (c) 2002-2016, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.customerprovisioning.modules.identitystore.services;
35  
36  import fr.paris.lutece.plugins.customerprovisioning.services.ICustomerInfoService;
37  import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
38  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityNotFoundException;
39  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AttributeDto;
40  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AuthorDto;
41  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityChangeDto;
42  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
43  import fr.paris.lutece.plugins.identitystore.web.service.IdentityService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import javax.inject.Inject;
52  import javax.inject.Named;
53  
54  
55  /**
56   * This class manages customer via the identity store
57   *
58   */
59  public class IdentityStoreCustomerInfoService implements ICustomerInfoService
60  {
61      private static final String ATTRIBUTE_USER_NAME_GIVEN = "customerprovisioning.identity.attribute.user.name.given";
62      private static final String ATTRIBUTE_USER_NAME_FAMILLY = "customerprovisioning.identity.attribute.user.name.family";
63      private static final String ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL = "customerprovisioning.identity.attribute.user.home-info.online.email";
64      private static final String ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER = "customerprovisioning.identity.attribute.user.home-info.telecom.telephone.number";
65      private static final String ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER = "customerprovisioning.identity.attribute.user.home-info.telecom.mobile.number";
66      private static final String ATTRIBUTE_IDENTITY_NAME_GIVEN = AppPropertiesService.getProperty( ATTRIBUTE_USER_NAME_GIVEN );
67      private static final String ATTRIBUTE_IDENTITY_NAME_FAMILLY = AppPropertiesService.getProperty( ATTRIBUTE_USER_NAME_FAMILLY );
68      private static final String ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_ONLINE_EMAIL );
69      private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_TELECOM_TELEPHONE_NUMBER );
70      private static final String ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER = AppPropertiesService.getProperty( ATTRIBUTE_USER_HOMEINFO_TELECOM_MOBILE_NUMBER );
71  
72      // FIXME : the application code must be provided by the caller
73      private static final String APPLICATION_CODE = "CustomerProvisioning";
74  
75      //Service identityStore
76      private static final String BEAN_IDENTITYSTORE_SERVICE = "customerprovisioning.identitystore.service";
77      @Inject
78      @Named( BEAN_IDENTITYSTORE_SERVICE )
79      private IdentityService _identityService;
80  
81      /**
82       * default constructor
83       */
84      public IdentityStoreCustomerInfoService(  )
85      {
86          super(  );
87      }
88  
89      @Override
90      public Customer getCustomerByGuid( String strGuid )
91      {
92          Customer customer = null;
93  
94          try
95          {
96              IdentityDto identityDto = _identityService.getIdentityByConnectionId( strGuid, APPLICATION_CODE );
97              customer = identityDtoToCustomer( identityDto );
98          }
99          catch ( IdentityNotFoundException e )
100         {
101             // The customer is not in the identity store yet : nothing to do
102         }
103 
104         return customer;
105     }
106 
107     @Override
108     public Customer getCustomerByCid( String strCustomerId )
109     {
110         Customer customer = null;
111 
112         try
113         {
114             // FIXME : the hash must be provided
115             IdentityDto identityDto = _identityService.getIdentityByCustomerId( strCustomerId, APPLICATION_CODE );
116             customer = identityDtoToCustomer( identityDto );
117         }
118         catch ( NumberFormatException e )
119         {
120             throw new IllegalArgumentException( e );
121         }
122         catch ( IdentityNotFoundException e )
123         {
124             // The customer is not in the identity store yet : nothing to do
125         }
126 
127         return customer;
128     }
129 
130     @Override
131     public Customer createCustomer( Customer customer )
132     {
133         IdentityChangeDto identityChangeDto = new IdentityChangeDto(  );
134         IdentityDto identityDto = customerToIdentityDto( customer );
135 
136         identityChangeDto.setIdentity( identityDto );
137 
138         AuthorDto authorDto = new AuthorDto(  );
139         authorDto.setApplicationCode( APPLICATION_CODE );
140 
141         identityChangeDto.setAuthor( authorDto );
142 
143         identityDto = _identityService.createIdentity( identityChangeDto );
144 
145         customer.setId( identityDto.getCustomerId(  ) );
146 
147         return customer;
148     }
149 
150     /**
151      * Converts a Customer object to an IdentityDto object
152      * @param customer the Customer object
153      * @return the IdentityDto object
154      */
155     private IdentityDto customerToIdentityDto( Customer customer )
156     {
157         IdentityDto identityDto = new IdentityDto(  );
158         Map<String, AttributeDto> mapAttributes = new HashMap<String, AttributeDto>(  );
159 
160         identityDto.setConnectionId( customer.getConnectionId(  ) );
161         identityDto.setCustomerId( customer.getId(  ) );
162         identityDto.setAttributes( mapAttributes );
163 
164         setAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_GIVEN, customer.getFirstname(  ) );
165         setAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_FAMILLY, customer.getLastname(  ) );
166         setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL, customer.getEmail(  ) );
167         setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER,
168             customer.getFixedPhoneNumber(  ) );
169         setAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER, customer.getMobilePhone(  ) );
170 
171         return identityDto;
172     }
173 
174     /**
175      * Converts an IdentityDto object to a Customer object
176      * @param identityDto the IdentityDto object
177      * @return the Customer object
178      */
179     private Customer identityDtoToCustomer( IdentityDto identityDto )
180     {
181         Customer customer = new Customer(  );
182 
183         customer.setConnectionId( identityDto.getConnectionId(  ) );
184         customer.setId( identityDto.getCustomerId(  ) );
185         customer.setFirstname( getAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_GIVEN ) );
186         customer.setLastname( getAttribute( identityDto, ATTRIBUTE_IDENTITY_NAME_FAMILLY ) );
187         customer.setEmail( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_ONLINE_EMAIL ) );
188         customer.setFixedPhoneNumber( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_TELEPHONE_NUMBER ) );
189         customer.setMobilePhone( getAttribute( identityDto, ATTRIBUTE_IDENTITY_HOMEINFO_TELECOM_MOBILE_NUMBER ) );
190 
191         return customer;
192     }
193 
194     /**
195      * Sets an attribute into the specified identity
196      * @param identityDto the identity
197      * @param strCode the attribute code
198      * @param strValue the attribute value
199      */
200     private void setAttribute( IdentityDto identityDto, String strCode, String strValue )
201     {
202         AttributeDto attributeDto = new AttributeDto(  );
203         attributeDto.setKey( strCode );
204         attributeDto.setValue( strValue );
205 
206         identityDto.getAttributes(  ).put( attributeDto.getKey(  ), attributeDto );
207     }
208 
209     /**
210      * Gets the attribute value from the specified identity
211      * @param identityDto the identity
212      * @param strCode the attribute code
213      * @return {@code null} if the attribute does not exist, the attribute value otherwise
214      */
215     private String getAttribute( IdentityDto identityDto, String strCode )
216     {
217         AttributeDto attribute = identityDto.getAttributes(  ).get( strCode );
218 
219         return ( attribute == null ) ? null : attribute.getValue(  );
220     }
221 }