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.digglike.business;
35
36 import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
37 import fr.paris.lutece.portal.service.fileupload.FileUploadService;
38 import fr.paris.lutece.portal.service.i18n.I18nService;
39 import fr.paris.lutece.portal.service.image.ImageResource;
40 import fr.paris.lutece.portal.service.message.AdminMessage;
41 import fr.paris.lutece.portal.service.message.AdminMessageService;
42 import fr.paris.lutece.portal.service.plugin.Plugin;
43 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
44
45 import org.apache.commons.fileupload.FileItem;
46
47 import java.util.List;
48 import java.util.Locale;
49
50 import javax.servlet.http.HttpServletRequest;
51
52
53
54
55
56
57
58 public class EntryTypeImage extends Entry
59 {
60 private static final String PARAMETER_WIDTH = "width";
61 private static final String PARAMETER_HEIGHT = "height";
62 private static final String MESSAGE_IMAGE_TOO_HEAVY = "digglike.message.imageTooHeavy";
63 private final String _template_create = "admin/plugins/digglike/create_entry_type_image.html";
64 private final String _template_modify = "admin/plugins/digglike/modify_entry_type_image.html";
65 private final String _template_html_code_form = "admin/plugins/digglike/html_code_form_entry_type_image.html";
66 private final String _template_html_code_response = "admin/plugins/digglike/html_code_response_entry_type_image.html";
67
68
69
70
71
72
73 public String getTemplateHtmlCodeForm( )
74 {
75 return _template_html_code_form;
76 }
77
78
79
80
81
82
83
84 public String getRequestData( HttpServletRequest request, Locale locale )
85 {
86 String strTitle = request.getParameter( PARAMETER_TITLE );
87 String strHelpMessage = request.getParameter( PARAMETER_HELP_MESSAGE );
88 String strComment = request.getParameter( PARAMETER_COMMENT );
89 String strValue = request.getParameter( PARAMETER_VALUE );
90 String strMandatory = request.getParameter( PARAMETER_MANDATORY );
91 String strWidth = request.getParameter( PARAMETER_WIDTH );
92 String strHeight = request.getParameter( PARAMETER_HEIGHT );
93 String strShowInDiggSubmitList = request.getParameter( PARAMETER_SHOW_IN_DIGG_SUBMIT_LIST );
94
95 int nWidth = -1;
96 int nHeight = -1;
97
98 String strFieldError = EMPTY_STRING;
99
100 if ( ( strTitle == null ) || strTitle.trim( ).equals( EMPTY_STRING ) )
101 {
102 strFieldError = FIELD_TITLE;
103 }
104
105 if ( !strFieldError.equals( EMPTY_STRING ) )
106 {
107 Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
108
109 return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
110 AdminMessage.TYPE_STOP );
111 }
112
113 try
114 {
115 if ( !( ( strHeight == null ) || strHeight.trim( ).equals( EMPTY_STRING ) ) )
116 {
117 nHeight = Integer.parseInt( strHeight );
118 }
119 }
120 catch ( NumberFormatException ne )
121 {
122 strFieldError = FIELD_HEIGHT;
123 }
124
125 try
126 {
127 if ( !( ( strWidth == null ) || strWidth.trim( ).equals( EMPTY_STRING ) ) )
128 {
129 nWidth = Integer.parseInt( strWidth );
130 }
131 }
132 catch ( NumberFormatException ne )
133 {
134 strFieldError = FIELD_WIDTH;
135 }
136
137 if ( !strFieldError.equals( EMPTY_STRING ) )
138 {
139 Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
140
141 return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields,
142 AdminMessage.TYPE_STOP );
143 }
144
145 this.setTitle( strTitle );
146 this.setHelpMessage( DiggUtils.trim( strHelpMessage ) );
147 this.setComment( strComment );
148
149 this.setDefaultValue( strValue );
150 this.setWidth( nWidth );
151 this.setHeight( nHeight );
152
153 if ( strMandatory != null )
154 {
155 this.setMandatory( true );
156 }
157 else
158 {
159 this.setMandatory( false );
160 }
161
162 if ( strShowInDiggSubmitList != null )
163 {
164 this.setShowInDiggSubmitList( true );
165 }
166 else
167 {
168 this.setShowInDiggSubmitList( false );
169 }
170
171 return null;
172 }
173
174
175
176
177
178 public String getTemplateCreate( )
179 {
180 return _template_create;
181 }
182
183
184
185
186
187 public String getTemplateModify( )
188 {
189 return _template_modify;
190 }
191
192
193
194
195
196
197
198
199
200
201 public FormError getResponseData( int nIdDiggSubmit, HttpServletRequest request, List<Response> listResponse,
202 Locale locale, Plugin plugin )
203 {
204 MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
205 FileItem item = mRequest.getFile( DiggUtils.EMPTY_STRING + this.getIdEntry( ) );
206
207 byte[] bytes = item.get( );
208
209 Response response = new Response( );
210 response.setEntry( this );
211
212 if ( bytes != null )
213 {
214 ImageResource image = new ImageResource( );
215 image.setImage( bytes );
216 image.setMimeType( item.getContentType( ) );
217 response.setImage( image );
218 response.setValueResponse( FileUploadService.getFileNameOnly( item ) );
219 }
220 else
221 {
222 if ( this.isMandatory( ) )
223 {
224 FormError formError = new FormError( );
225 formError.setMandatoryError( true );
226 formError.setTitleQuestion( this.getTitle( ) );
227
228 return formError;
229 }
230 }
231
232 listResponse.add( response );
233
234 return null;
235 }
236
237
238
239
240
241 public String getTemplateHtmlCodeResponse( )
242 {
243 return _template_html_code_response;
244 }
245 }