View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.genericattributes.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpSession;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
47  import fr.paris.lutece.plugins.genericattributes.business.Response;
48  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  
53  /**
54   *
55   * Abstract entry type for sessions attributes This entry is used to fetch the value of a session's attribute. One example is when coupling form with crm, the
56   * module-crm-form will put in session the ID demand and the user GUID. This entry will be able to fetch the ID demand and user GUID when validating the form.
57   * Then, it is easier to export the value to directory with the module-form-exportdirectory.
58   *
59   */
60  public abstract class AbstractEntryTypeSession extends EntryTypeService
61  {
62      private static final String ERROR_FIELD_ATTRIBUTE_NAME = "genericattributes.createEntry.labelAttributeName";
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
69      {
70          initCommonRequestData( entry, request );
71          String strTitle = request.getParameter( PARAMETER_TITLE );
72          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
73          String strAttibuteName = request.getParameter( PARAMETER_VALUE );
74          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
75  
76          String strFieldError = StringUtils.EMPTY;
77  
78          if ( StringUtils.isBlank( strTitle ) )
79          {
80              strFieldError = ERROR_FIELD_TITLE;
81          }
82          else
83              if ( StringUtils.isBlank( strAttibuteName ) )
84              {
85                  strFieldError = ERROR_FIELD_ATTRIBUTE_NAME;
86              }
87  
88          if ( StringUtils.isNotBlank( strFieldError ) )
89          {
90              Object [ ] tabRequiredFields = {
91                      I18nService.getLocalizedString( strFieldError, locale )
92              };
93  
94              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
95          }
96  
97          entry.setCode( strCode );
98          entry.setTitle( strTitle );
99          entry.setHelpMessage( StringUtils.EMPTY );
100         entry.setComment( StringUtils.EMPTY );
101         entry.setMandatory( StringUtils.isNotEmpty( strMandatory ) );
102         entry.setUnique( false );
103 
104         GenericAttributesUtils.createOrUpdateField( entry, FIELD_ATTRIBUTE_NAME, strTitle, strAttibuteName );
105         return null;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
113     {
114         String strValueEntry = StringUtils.EMPTY;
115         HttpSession session = request.getSession( false );
116 
117         if ( session != null && CollectionUtils.isNotEmpty( entry.getFields( ) ) && entry.getFields( ).get( 0 ) != null )
118         {
119             String strAttributeName = entry.getFieldByCode( FIELD_ATTRIBUTE_NAME ).getValue( );
120             strValueEntry = (String) session.getAttribute( strAttributeName );
121         }
122 
123         if ( StringUtils.isNotBlank( strValueEntry ) )
124         {
125             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
126             response.setEntry( entry );
127             response.setResponseValue( strValueEntry );
128             response.setToStringValueResponse( StringUtils.EMPTY );
129             response.setIterationNumber( getResponseIterationValue( request ) );
130 
131             listResponse.add( response );
132         }
133 
134         return null;
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
142     {
143         return response.getResponseValue( );
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
151     {
152         return StringUtils.EMPTY;
153     }
154 }