View Javadoc
1   /*
2    * Copyright (c) 2002-2015, 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.services;
35  
36  import fr.paris.lutece.plugins.customerprovisioning.business.UserDTO;
37  import fr.paris.lutece.plugins.grubusiness.business.customer.Customer;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  
43  /**
44   * The Class ProvisioningService.
45   */
46  public final class ProvisioningService
47  {
48      /**
49       * Instantiates a new provisioning service.
50       */
51      private ProvisioningService(  )
52      {
53      }
54  
55      /**
56       * Process guid cuid.
57       *
58       * @param strGuid
59       *            the str guid
60       * @param strCuid
61       *            the str cuid
62       * @param userDto
63       *            the user dto
64       * @return the customer
65       */
66      public static Customer processGuidCuid( String strGuid, String strCuid, UserDTO userDto )
67      {
68          if ( AppLogService.isDebugEnabled(  ) )
69          {
70              AppLogService.debug( "\n" + "Provisionning - Info : GUID : " + strGuid + "\n" );
71              AppLogService.debug( "Provisionning - Info : CID : " + strCuid + "\n" );
72              AppLogService.debug( "Provisionning - Info : user : " + userDto + "\n" );
73          }
74  
75          Customer gruCustomer = null;
76  
77          // CASE 1 NOT CID
78          if ( ( strCuid == null ) || StringUtils.isEmpty( strCuid ) || !StringUtils.isNumeric( strCuid ) )
79          {
80              // CASE 1.1 : no cid and no guid: break the flux and wait for a new
81              // flux with one of them
82              if ( ( ( strCuid == null ) || !StringUtils.isNumeric( strCuid ) ) &&
83                      ( ( strGuid == null ) || StringUtils.isEmpty( strGuid ) ) && ( userDto == null ) )
84              {
85                  AppLogService.error( "Provisionning - Error : JSON doesnot contains any GUID nor Customer ID : " +
86                      strCuid );
87              } // CASE 1.2 : no cid and guid: look for a mapping beween an
88                // existing guid
89  
90              else if ( ( strGuid != null ) && !StringUtils.isEmpty( strGuid ) )
91              {
92                  gruCustomer = getCustomerByGuid( strGuid );
93  
94                  if ( gruCustomer == null )
95                  {
96                      gruCustomer = createCustomerByGuid( strGuid );
97  
98                      if ( AppLogService.isDebugEnabled(  ) )
99                      {
100                         AppLogService.debug( "Provisionning - New user created into the GRU for the guid : " + strGuid +
101                             " its customer id is : " + gruCustomer.getId(  ) );
102                     }
103                 }
104             }
105             else if ( userDto != null )
106             {
107                 gruCustomer = createCustomerByGuid( userDto, strGuid );
108             }
109         } // CASE 2 : cid and (guid or no guid): find customer info in GRU
110           // database
111 
112         else if ( StringUtils.isNumeric( strCuid ) )
113         {
114             // MUST CONTROL IF COSTUMER CUID IS NUMBER FORMAT, ELSE :
115             // java.lang.NumberFormatException: For input string:
116             gruCustomer = getCustomerByCuid( strCuid );
117 
118             if ( gruCustomer == null )
119             {
120                 AppLogService.error( "Provisionning - Error : No user found with the customer ID : " + strCuid );
121             }
122         }
123 
124         return gruCustomer;
125     }
126 
127     /**
128      * Gets the customer by guid.
129      *
130      * @param strGuid
131      *            the str guid
132      * @return the customer by guid
133      */
134     public static Customer getCustomerByGuid( String strGuid )
135     {
136         Customer grusupplyCustomer = CustomerService.instance(  ).getCustomerByGuid( strGuid );
137 
138         return grusupplyCustomer;
139     }
140 
141     /**
142      * Creates the customer by guid.
143      *
144      * @param strGuid
145      *            the str guid
146      * @return the customer
147      */
148     public static Customer createCustomerByGuid( String strGuid )
149     {
150         UserDTO user = UserInfoService.instance(  ).getUserInfo( strGuid );
151         Customer costumer = CustomerService.instance(  ).createCustomer( buildCustomer( user, strGuid ) );
152 
153         return costumer;
154     }
155 
156     /**
157      * Creates the customer by guid.
158      *
159      * @param userDto
160      *            the user dto
161      * @param strGuidFromTicket
162      *            the str guid from ticket
163      * @return the customer
164      */
165     public static Customer createCustomerByGuid( UserDTO userDto, String strGuidFromTicket )
166     {
167         Customer costumer = CustomerService.instance(  ).createCustomer( buildCustomer( userDto, strGuidFromTicket ) );
168 
169         return costumer;
170     }
171 
172     /**
173      * Gets the customer by cuid.
174      *
175      * @param strCuid
176      *            the str cuid
177      * @return the customer by cuid
178      */
179     public static Customer getCustomerByCuid( String strCuid )
180     {
181         Customer grusupplyCustomer = CustomerService.instance(  ).getCustomerByCid( strCuid );
182 
183         return grusupplyCustomer;
184     }
185 
186     /**
187      * Builds the customer.
188      *
189      * @param user
190      *            the user
191      * @param strUserId
192      *            the str user id
193      * @return the customer
194      */
195     private static Customer buildCustomer( UserDTO user, String strUserId )
196     {
197         Customer gruCustomer = new Customer(  );
198         gruCustomer.setFirstname( setEmptyValueWhenNullValue( user.getFirstname(  ) ) );
199         gruCustomer.setLastname( setEmptyValueWhenNullValue( user.getLastname(  ) ) );
200         gruCustomer.setEmail( setEmptyValueWhenNullValue( user.getEmail(  ) ) );
201         gruCustomer.setAccountGuid( setEmptyValueWhenNullValue( strUserId ) );
202         gruCustomer.setAccountLogin( setEmptyValueWhenNullValue( user.getEmail(  ) ) );
203         gruCustomer.setMobilePhone( setEmptyValueWhenNullValue( user.getTelephoneNumber(  ) ) );
204         gruCustomer.setFixedPhoneNumber( setEmptyValueWhenNullValue( user.getFixedPhoneNumber(  ) ) );
205         gruCustomer.setExtrasAttributes( "NON RENSEIGNE" );
206 
207         return gruCustomer;
208     }
209 
210     /**
211      * Sets the empty value when null value.
212      *
213      * @param value
214      *            the value
215      * @return the string
216      */
217     private static String setEmptyValueWhenNullValue( String value )
218     {
219         return ( StringUtils.isEmpty( value ) ) ? "" : value;
220     }
221 }