1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
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
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
83
84 @Override
85 public String getTemplateModify( Entry entry, boolean bDisplayFront )
86 {
87 return TEMPLATE_MODIFY;
88 }
89
90
91
92
93 @Override
94 public String getTemplateCreate( Entry entry, boolean bDisplayFront )
95 {
96 return TEMPLATE_CREATE;
97 }
98
99
100
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
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
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 }