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.mydashboard.modules.identity.util;
35  
36  import fr.paris.lutece.plugins.identitystore.web.rs.dto.AttributeDto;
37  import fr.paris.lutece.plugins.identitystore.web.rs.dto.CertificateDto;
38  import fr.paris.lutece.plugins.identitystore.web.rs.dto.IdentityDto;
39  import fr.paris.lutece.plugins.mydashboard.modules.identity.business.DashboardAttribute;
40  import fr.paris.lutece.plugins.mydashboard.modules.identity.business.DashboardIdentity;
41  
42  import org.apache.commons.lang.StringUtils;
43  
44  import java.util.HashMap;
45  import java.util.Map;
46  import javax.servlet.http.HttpServletRequest;
47  
48  
49  /**
50   *
51   * class to help managing identity feature
52   *
53   */
54  public class DashboardIdentityUtils
55  {
56      private static DashboardIdentityUtils _instance;   
57      
58      //For matching on DBAttributes and Identity store attributes
59      private static Map<String,String> _mapAttributeKeyMatch;
60      
61      /**
62       * private constructor for singleton
63       */
64      private DashboardIdentityUtils(  )
65      {
66          _mapAttributeKeyMatch = new HashMap<String,String>( );
67          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_LAST_NAME, Constants.PROPERTY_KEY_NAME );
68          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_PREFERRED_USER_NAME, Constants.PROPERTY_KEY_PREFERREDUSERNAME );
69          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_FIRSTNAME, Constants.PROPERTY_KEY_FIRSTNAME );
70          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_GENDER, Constants.PROPERTY_KEY_GENDER );
71          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BIRTHDATE, Constants.PROPERTY_KEY_BIRTHDATE );
72          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BIRTHPLACE, Constants.PROPERTY_KEY_BIRTHPLACE );
73          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BIRTHCOUNTRY, Constants.PROPERTY_KEY_BIRTHCOUNTRY );
74          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ADDRESS, Constants.PROPERTY_KEY_ADDRESS );
75          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ADDRESS_DETAIL, Constants.PROPERTY_KEY_ADDRESSDETAIL );
76          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ADDRESS_POSTAL_CODE, Constants.PROPERTY_KEY_ADDRESS_POSTAL_CODE );
77          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ADDRESS_CITY, Constants.PROPERTY_KEY_ADDRESS_CITY );
78          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BILLING_ADDRESS, Constants.PROPERTY_KEY_BILLING_ADDRESS );
79          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BILLING_ADDRESS_DETAIL, Constants.PROPERTY_KEY_BILLING_ADDRESSDETAIL );
80          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BILLING_ADDRESS_POSTAL_CODE, Constants.PROPERTY_KEY_BILLING_ADDRESS_POSTAL_CODE );
81          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_BILLING_ADDRESS_CITY, Constants.PROPERTY_KEY_BILLING_ADDRESS_CITY );
82          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_EMAIL, Constants.PROPERTY_KEY_EMAIL );
83          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_PHONE, Constants.PROPERTY_KEY_PHONE );
84          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_PREFERRED_CONTACT_MODE, Constants.PROPERTY_KEY_PREFERRED_CONTACT );
85          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_MOBILE_PHONE, Constants.PROPERTY_KEY_MOBILE_PHONE );
86          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ACCEPT_NEWS, Constants.PROPERTY_KEY_ACCEPT_NEWS );
87          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ACCEPT_SURVEY, Constants.PROPERTY_KEY_ACCEPT_SURVEY );
88          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_DIRECTION, Constants.PROPERTY_KEY_DIRECTION );
89          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_SOI, Constants.PROPERTY_KEY_SOI );
90          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_NUM_BUREAU, Constants.PROPERTY_KEY_NUM_BUREAU);
91          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_TEL_PRO, Constants.PROPERTY_KEY_TEL_PRO );
92          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_CATEGORIE, Constants.PROPERTY_KEY_CATEGORIE );
93          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_CORPS, Constants.PROPERTY_KEY_CORPS);
94          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_GRADE, Constants.PROPERTY_KEY_GRADE );
95          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_POSITION_ADMINISTRATIVE, Constants.PROPERTY_KEY_POSITION_ADMINISTRATIVE );
96          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_AFFECTION_DATE, Constants.PROPERTY_KEY_AFFECTECTATION_DATE );
97          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ENTITE_JURIDIQUE, Constants.PROPERTY_KEY_ENTITE_JURIDIQUE);
98          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_SERVICE_CENTRAUX, Constants.PROPERTY_KEY_SERVICE_CENTRAUX);
99          _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_NIVEAU, Constants.PROPERTY_KEY_NIVEAU );
100         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_ENCADRANT, Constants.PROPERTY_KEY_ENCADRANT );
101         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_RH_NOM_USAGE, Constants.PROPERTY_KEY_RH_NOM_USAGE);
102         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_RH_PRENOM, Constants.PROPERTY_KEY_RH_PRENOM );
103         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_RH_TEL, Constants.PROPERTY_KEY_RH_TEL );
104         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_UGD_SOI, Constants.PROPERTY_KEY_UGD_SOI);
105         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_UGD_UNITE, Constants.PROPERTY_KEY_UGD_UNITE);
106         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_UGD_UNITE_SOI, Constants.ATTRIBUTE_DB_IDENTITY_UGD_UNITE_SOI );
107         _mapAttributeKeyMatch.put(Constants.ATTRIBUTE_DB_IDENTITY_IS_ENCADRANT, Constants.PROPERTY_KEY_IS_ENCADRANT );
108       }
109     
110     public static DashboardIdentityUtils getInstance( )
111     {
112         if ( _instance == null )
113         {
114             _instance = new DashboardIdentityUtils( );
115         }
116             return _instance;
117     }
118     
119 
120     /**
121      * return an dashboardIdentity from a identityDto
122      *
123      * @param identity
124      *          identityDto to convert
125      * @return dashboardIdentity initialized from provided identityDto
126      */
127     public DashboardIdentity convertToDashboardIdentity( IdentityDto identity )
128     {
129         DashboardIdentity dashboardIdentity = new DashboardIdentity(  );
130         
131         dashboardIdentity.setConnectionId( new DashboardAttribute( 
132                 Constants.ATTRIBUTE_DB_IDENTITY_CONNECTION_ID, 
133                 identity.getConnectionId( ) ) );
134         
135         
136         dashboardIdentity.setCustomerId( new DashboardAttribute(
137                 Constants.ATTRIBUTE_DB_IDENTITY_CUSTOMER_ID,
138                 identity.getCustomerId(  ) ) );
139         
140         for ( Map.Entry<String,String> attributeMatch : _mapAttributeKeyMatch.entrySet( ) )
141         {
142             dashboardIdentity.setAttribute( 
143                     attributeMatch.getKey( ), 
144                     getDashboardAttributeFromAttributeDtoKey (
145                         identity, 
146                         attributeMatch.getValue( ), 
147                         attributeMatch.getKey( )
148                     ) );
149         }
150 
151         return dashboardIdentity;
152     }
153 
154     /**
155      * return an identityDto from a DashboardIdentity
156      *
157      * @param dashboardIdentity
158      *          dashboardIdentity to convert
159      * @return identityDto initialized from provided dashboardIdentity
160      */
161     public IdentityDto convertToIdentityDto( DashboardIdentity dashboardIdentity )
162     {
163         IdentityDto identityDto = new IdentityDto(  );
164         identityDto.setConnectionId( dashboardIdentity.getConnectionId(  ).getValue( ) );
165         identityDto.setCustomerId( dashboardIdentity.getCustomerId(  ).getValue( ) );
166 
167         Map<String, AttributeDto> mapAttributes = new HashMap<String, AttributeDto>(  );
168         
169         for ( Map.Entry<String,String> attributeMatch : _mapAttributeKeyMatch.entrySet( ) )
170         {
171             DashboardAttribute dashboardAttribute = dashboardIdentity.getAttribute( attributeMatch.getKey( ) );
172             AttributeDto attribute = new AttributeDto(  );
173             attribute.setKey( attributeMatch.getValue( ) );
174             attribute.setValue( dashboardAttribute.getValue( ) );
175             attribute.setCertified( dashboardAttribute.getCertifierCode( ) != null );
176             
177             if ( attribute.getCertified( ) )
178             {
179                 CertificateDto certificate = new CertificateDto( );
180                 certificate.setCertificateExpirationDate( dashboardAttribute.getExpirationDate( ) );
181                 certificate.setCertifierCode( dashboardAttribute.getCertifierCode( ) );
182                 certificate.setCertifierLevel( dashboardAttribute.getCertifierLevel( ) );
183                 certificate.setCertifierName( dashboardAttribute.getCertifierName( ) );
184                 attribute.setCertificate( certificate );
185             }
186             mapAttributes.put( attribute.getKey(  ), attribute );
187         }
188         
189         identityDto.setAttributes( mapAttributes );
190 
191         return identityDto;
192     }
193     
194     /**
195      * Get DashboardAttribute From AttributeDto
196      * @param identity the IdentityDto
197      * @param identityDtoAttributeKey the identityDto attribute key
198      * @param dashboardAttributeKey the Dashboard attribute key
199      * @return 
200      */
201     private DashboardAttribute getDashboardAttributeFromAttributeDtoKey ( IdentityDto identity, String identityDtoAttributeKey, String dashboardAttributeKey )
202     {
203         AttributeDto attribute = identity.getAttributes( ).get( identityDtoAttributeKey );
204         if ( attribute != null )
205         {
206             if ( attribute.getCertificate( ) != null )
207             {
208                 return new DashboardAttribute(
209                     dashboardAttributeKey,
210                     attribute.getValue( ),
211                     attribute.getCertificate( ).getCertifierCode( ),
212                     attribute.getCertificate( ).getCertifierName( ),
213                     attribute.getCertificate( ).getCertifierLevel( ),
214                     attribute.getCertificate( ).getCertificateExpirationDate( ) );
215             }
216             else
217             {
218                 return new DashboardAttribute(
219                     dashboardAttributeKey,
220                     attribute.getValue( ) );
221             }
222             
223         }
224         return new DashboardAttribute(
225             dashboardAttributeKey,
226             StringUtils.EMPTY
227             );
228     }
229     
230     /**
231      * populaite DashboardIdentity from the request
232      * @param identity the DashboardIdentity
233      * @param request the HttpServletRequest
234      */
235     public void populateDashboardIdentity ( DashboardIdentity identity, HttpServletRequest request )
236     {
237         String connectionId = request.getParameter( Constants.ATTRIBUTE_DB_IDENTITY_CONNECTION_ID );
238         String customerId = request.getParameter( Constants.ATTRIBUTE_DB_IDENTITY_CUSTOMER_ID );
239         
240         if ( connectionId != null )
241         {
242             identity.setAttributeValue( Constants.ATTRIBUTE_DB_IDENTITY_CONNECTION_ID, connectionId );
243         }
244         if ( customerId != null )
245         {
246             identity.setAttributeValue( Constants.ATTRIBUTE_DB_IDENTITY_CUSTOMER_ID, customerId );
247         }
248         
249         for ( String strAttributeKey : _mapAttributeKeyMatch.keySet( ) )
250         {
251             String attributeValue = request.getParameter( strAttributeKey );
252             if ( attributeValue != null )
253             {
254                 identity.setAttributeValue( strAttributeKey, attributeValue );
255             }
256         }
257     }
258 }