View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.directories.service.entrytype;
35  
36  import fr.paris.lutece.plugins.directories.util.DirectoriesConstants;
37  import fr.paris.lutece.plugins.genericattributes.business.Entry;
38  import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeRadioButton;
39  import fr.paris.lutece.plugins.genericattributes.business.Field;
40  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
41  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
42  import fr.paris.lutece.plugins.genericattributes.business.Response;
43  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
44  import fr.paris.lutece.portal.service.i18n.I18nService;
45  import fr.paris.lutece.portal.service.message.AdminMessage;
46  import fr.paris.lutece.portal.service.message.AdminMessageService;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  
49  import org.apache.commons.lang.StringUtils;
50  
51  import java.util.List;
52  import java.util.Locale;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   *
58   * class EntryTypeRadioButton
59   * 
60   * @author
61   *
62   */
63  public final class EntryTypeRadioButton extends AbstractEntryTypeRadioButton
64  {
65      private static final String TEMPLATE_HTML_CODE = "skin/plugins/directories/entries/fill_entry_type_radio_button.html";
66      private static final String TEMPLATE_CREATE = "admin/plugins/directories/entries/create_entry_type_radio_button.html";
67      private static final String TEMPLATE_HTML_CODE_ADMIN = "admin/plugins/directories/entries/fill_entry_type_radio_button.html";
68      private static final String TEMPLATE_MODIFY = "admin/plugins/directories/entries/modify_entry_type_radio_button.html";
69      private static final String TEMPLATE_READONLY_FRONTOFFICE = "skin/plugins/directories/entries/readonly_entry_type_radio_button.html";
70      private static final String TEMPLATE_READONLY_BACKOFFICE = "admin/plugins/directories/entries/readonly_entry_type_radio_button.html";
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
77      {
78          return bDisplayFront ? TEMPLATE_HTML_CODE : TEMPLATE_HTML_CODE_ADMIN;
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public String getTemplateModify( Entry entry, boolean bDisplayFront )
86      {
87          return TEMPLATE_MODIFY;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getTemplateCreate( Entry entry, boolean bDisplayFront )
95      {
96          return TEMPLATE_CREATE;
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
104     {
105         initCommonRequestData( entry, request );
106         String strTitle = request.getParameter( PARAMETER_TITLE );
107         String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
108         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
109         String strComment = request.getParameter( PARAMETER_COMMENT );
110         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
111         String strFieldInLine = request.getParameter( PARAMETER_FIELD_IN_LINE );
112         String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
113         String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
114         String strEditableBack = request.getParameter( PARAMETER_EDITABLE_BACK );
115 
116         int nFieldInLine = -1;
117 
118         String strFieldError = StringUtils.EMPTY;
119 
120         if ( StringUtils.isBlank( strTitle ) )
121         {
122             strFieldError = ERROR_FIELD_TITLE;
123         }
124 
125         if ( StringUtils.isNotBlank( strFieldError ) )
126         {
127             Object [ ] tabRequiredFields = {
128                     I18nService.getLocalizedString( strFieldError, locale )
129             };
130 
131             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
132         }
133 
134         entry.setCode( strCode );
135         entry.setTitle( strTitle );
136         entry.setHelpMessage( strHelpMessage );
137         entry.setComment( strComment );
138         entry.setCSSClass( strCSSClass );
139 
140         entry.setMandatory( strMandatory != null );
141         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
142         entry.setEditableBack( strEditableBack != null );
143         GenericAttributesUtils.createOrUpdateField( entry, DirectoriesConstants.FIELD_FILTER, null,
144                 request.getParameter( DirectoriesConstants.PARAMETER_FILTER ) );
145 
146         try
147         {
148             nFieldInLine = Integer.parseInt( strFieldInLine );
149         }
150         catch( NumberFormatException ne )
151         {
152             AppLogService.error( ne.getMessage( ), ne );
153         }
154 
155         entry.setFieldInLine( nFieldInLine == 1 );
156 
157         return null;
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
165     {
166         String strIdField = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) );
167         int nIdField = -1;
168         Field field = null;
169         Response response = new Response( );
170         response.setEntry( entry );
171 
172         if ( strIdField != null )
173         {
174             try
175             {
176                 nIdField = Integer.parseInt( strIdField );
177             }
178             catch( NumberFormatException ne )
179             {
180                 AppLogService.error( ne.getMessage( ), ne );
181             }
182         }
183 
184         if ( nIdField != -1 )
185         {
186             field = GenericAttributesUtils.findFieldByIdInTheList( nIdField, entry.getFields( ) );
187         }
188 
189         if ( field != null )
190         {
191             response.setResponseValue( field.getValue( ) );
192             response.setField( field );
193         }
194 
195         response.setIterationNumber( getResponseIterationValue( request ) );
196 
197         listResponse.add( response );
198 
199         if ( entry.isMandatory( ) && ( ( field == null ) || StringUtils.isBlank( field.getValue( ) ) ) )
200         {
201             return new MandatoryError( entry, locale );
202         }
203 
204         String strFilter = entry.getFieldByCode( DirectoriesConstants.FIELD_FILTER ).getValue( );
205         response = new Response( );
206         response.setEntry( entry );
207         response.setResponseValue( strFilter );
208         response.setIterationNumber( getResponseIterationValue( request ) );
209         listResponse.add( response );
210 
211         return null;
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public String getTemplateEntryReadOnly( boolean bDisplayFront )
219     {
220         if ( bDisplayFront )
221         {
222             return TEMPLATE_READONLY_FRONTOFFICE;
223         }
224         return TEMPLATE_READONLY_BACKOFFICE;
225     }
226 }