1 package fr.paris.lutece.pligins.documentimport.service;
2
3 import java.sql.Timestamp;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Locale;
10
11 import javax.servlet.http.HttpServletRequest;
12
13 import org.apache.commons.lang.StringUtils;
14
15 import fr.paris.lutece.plugins.document.business.Document;
16 import fr.paris.lutece.plugins.document.business.DocumentHome;
17 import fr.paris.lutece.plugins.document.business.DocumentType;
18 import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
19 import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
20 import fr.paris.lutece.plugins.document.business.category.Category;
21 import fr.paris.lutece.plugins.document.business.category.CategoryHome;
22 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
23 import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
24 import fr.paris.lutece.plugins.document.service.AttributeManager;
25 import fr.paris.lutece.plugins.document.service.AttributeService;
26 import fr.paris.lutece.plugins.document.service.DocumentException;
27 import fr.paris.lutece.plugins.document.service.DocumentService;
28 import fr.paris.lutece.plugins.document.service.metadata.MetadataHandler;
29 import fr.paris.lutece.plugins.document.service.spaces.SpaceResourceIdService;
30 import fr.paris.lutece.plugins.document.utils.IntegerUtils;
31 import fr.paris.lutece.plugins.documentimport.util.DocumentImport;
32 import fr.paris.lutece.plugins.documentimport.util.DocumentimporError;
33 import fr.paris.lutece.portal.business.user.AdminUser;
34 import fr.paris.lutece.portal.service.i18n.I18nService;
35 import fr.paris.lutece.portal.service.rbac.RBACService;
36 import fr.paris.lutece.util.ReferenceItem;
37 import fr.paris.lutece.util.ReferenceList;
38 import fr.paris.lutece.util.date.DateUtil;
39 import fr.paris.lutece.util.string.StringUtil;
40
41 public class DocumentimportService {
42
43
44
45
46 private static final String PROPERTY_WARNING_LINE = "documentimport.import_document.warning.line";
47 private static final String PROPERTY_ERROR_LINE = "documentimport.import_document.error.line";
48
49 private static final String MESSAGE_ERROR_CSV_MANDATORY_FIELD= "documentimport.message.mandatory.required";
50 private static final String MESSAGE_INVALID_DATEBEGIN = "documentimport.invalid.date.begin";
51 private static final String MESSAGE_INVALID_DATEEND = "documentimport.invalid.date.end";
52 private static final String MESSAGE_INVALID_DATE_BEFORE_70 = "documentimport.invalid.date.before";
53 private static final String MESSAGE_ERROR_DATEEND_BEFORE_DATEBEGIN= "documentimport.invalid.date.end.befor.begin";
54 private static final String MESSAGE_WARNING_CSV_ILLEGAL_CHARACTER= "documentimport.warning.illegal.char";
55
56
57 private static final String ERROR= "error";
58 private static DocumentimportService _singleton = new DocumentimportService();
59
60
61
62 private DocumentimportService( )
63 {
64
65 }
66
67
68
69
70
71
72
73
74 public String getDocumentData(HttpServletRequest mRequest, Document document, HashMap<String,String> valueAttribute, DocumentimporError _docError,Locale locale, AdminUser user )
75 {
76 String strDocumentTitle = valueAttribute.get(DocumentImport.PARAMETER_TITLE);
77 String strDocumentSummary = valueAttribute.get(DocumentImport.PARAMETER_SUMMARY);
78 String strDocumentComment = valueAttribute.get(DocumentImport.PARAMAETER_COMMENT);
79 String strDateValidityBegin = valueAttribute.get(DocumentImport.PARAMAETER_DATE_VALID_BEGIN);
80 String strDateValidityEnd = valueAttribute.get(DocumentImport.PARAMETER_DATE_VALID_END);
81 String strMailingListId = valueAttribute.get(DocumentImport.PARAMETER_DATE_MAILING_LIST);
82 int nMailingListId = IntegerUtils.convert( strMailingListId, 0 );
83 String strPageTemplateDocumentId = valueAttribute.get(DocumentImport.PARAMETER_TEMPLATE_DOCUMENT_ID);
84 int nPageTemplateDocumentId = IntegerUtils.convert( strPageTemplateDocumentId, 0 );
85
86 String[] arrayCategory = null;
87
88
89 if ( StringUtils.isBlank( strDocumentSummary ) )
90 {
91 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
92 _docError.getWarning( ).append( _docError.getCountLine( ) );
93 _docError.getWarning( ).append( " > " );
94 _docError.getWarning( )
95 .append( DocumentImport.PARAMETER_TITLE +" :"+ I18nService.getLocalizedString(MESSAGE_ERROR_CSV_MANDATORY_FIELD ,
96 locale) );
97 _docError.getWarning( ).append( "<br/>" );
98 _docError.setCountWarning( _docError.getCountWarning() + 1 );
99
100
101 }
102 if (StringUtils.isBlank( strDocumentSummary ) )
103 {
104 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
105 _docError.getWarning( ).append( _docError.getCountLine( ) );
106 _docError.getWarning( ).append( " > " );
107 _docError.getWarning( )
108 .append( DocumentImport.PARAMETER_SUMMARY + " :" + I18nService.getLocalizedString(MESSAGE_ERROR_CSV_MANDATORY_FIELD ,
109 locale ));
110 _docError.getWarning( ).append( "<br/>" );
111 _docError.setCountWarning( _docError.getCountWarning() + 1 );
112
113
114 }
115
116
117 if ( StringUtil.containsHtmlSpecialCharacters( strDocumentTitle ) )
118 {
119 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
120 _docError.getWarning( ).append( _docError.getCountLine( ) );
121 _docError.getWarning( ).append( " > " );
122 _docError.getWarning( )
123 .append( DocumentImport.PARAMETER_TITLE + " :" + I18nService.getLocalizedString(MESSAGE_WARNING_CSV_ILLEGAL_CHARACTER ,
124 locale) );
125 _docError.getWarning( ).append( "<br/>" );
126 _docError.setCountWarning( _docError.getCountWarning() + 1 );
127
128 }
129
130 if ( StringUtil.containsHtmlSpecialCharacters( strDocumentSummary ) )
131 {
132 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
133 _docError.getWarning( ).append( _docError.getCountLine( ) );
134 _docError.getWarning( ).append( " > " );
135 _docError.getWarning( )
136 .append( DocumentImport.PARAMETER_SUMMARY + " :" + I18nService.getLocalizedString( MESSAGE_WARNING_CSV_ILLEGAL_CHARACTER ,
137 locale) );
138 _docError.getWarning( ).append( "<br/>" );
139 _docError.setCountWarning( _docError.getCountWarning() + 1 );
140 }
141
142 DocumentType documentType = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType( ) );
143 List<DocumentAttribute> listAttributes = documentType.getAttributes( );
144
145 for ( DocumentAttribute attribute : listAttributes )
146 {
147 String strAdminMessage = setAttribute( attribute, document, valueAttribute,_docError,locale );
148
149 if ( strAdminMessage != null )
150 {
151 return ERROR;
152 }
153 }
154
155 Timestamp dateValidityBegin = null;
156 Timestamp dateValidityEnd = null;
157
158 if ( ( strDateValidityBegin != null ) && !strDateValidityBegin.equals( "" ) )
159 {
160 Date dateBegin = DateUtil.formatDateLongYear( strDateValidityBegin, locale );
161
162 if ( ( dateBegin == null ) )
163 {
164 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
165 _docError.getWarning( ).append( _docError.getCountLine( ) );
166 _docError.getWarning( ).append( " > " );
167 _docError.getWarning( )
168 .append( DocumentImport.PARAMAETER_DATE_VALID_BEGIN + " :" + I18nService.getLocalizedString( MESSAGE_INVALID_DATEBEGIN ,
169 locale) );
170 _docError.getWarning( ).append( "<br/>" );
171 _docError.setCountWarning( _docError.getCountWarning() + 1 );
172
173 }
174
175 dateValidityBegin = new Timestamp( dateBegin.getTime( ) );
176
177 if ( dateValidityBegin.before( new Timestamp( 0 ) ) )
178 {
179 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
180 _docError.getWarning( ).append( _docError.getCountLine( ) );
181 _docError.getWarning( ).append( " > " );
182 _docError.getWarning( )
183 .append( DocumentImport.PARAMAETER_DATE_VALID_BEGIN + " :" + I18nService.getLocalizedString( MESSAGE_INVALID_DATE_BEFORE_70 ,
184 locale) );
185 _docError.getWarning( ).append( "<br/>" );
186 _docError.setCountWarning( _docError.getCountWarning() + 1 );
187
188 }
189 }
190
191 if ( ( strDateValidityEnd != null ) && !strDateValidityEnd.equals( "" ) )
192 {
193 Date dateEnd = DateUtil.formatDateLongYear( strDateValidityEnd, locale );
194
195 if ( ( dateEnd == null ) )
196 {
197 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
198 _docError.getWarning( ).append( _docError.getCountLine( ) );
199 _docError.getWarning( ).append( " > " );
200 _docError.getWarning( )
201 .append( DocumentImport.PARAMETER_DATE_VALID_END+ " :" + I18nService.getLocalizedString( MESSAGE_INVALID_DATEEND ,
202 locale) );
203 _docError.getWarning( ).append( "<br/>" );
204 _docError.setCountWarning( _docError.getCountWarning() + 1 );
205
206 }
207
208 dateValidityEnd = new Timestamp( dateEnd.getTime( ) );
209
210 if ( dateValidityEnd.before( new Timestamp( 0 ) ) )
211 {
212 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
213 _docError.getWarning( ).append( _docError.getCountLine( ) );
214 _docError.getWarning( ).append( " > " );
215 _docError.getWarning( )
216 .append( DocumentImport.PARAMETER_DATE_VALID_END + " :" + I18nService.getLocalizedString( MESSAGE_INVALID_DATE_BEFORE_70 ,
217 locale) );
218 _docError.getWarning( ).append( "<br/>" );
219 _docError.setCountWarning( _docError.getCountWarning() + 1 );
220
221 }
222 }
223
224
225 if ( ( dateValidityBegin != null ) && ( dateValidityEnd != null ) )
226 {
227 if ( dateValidityEnd.before( dateValidityBegin ) )
228 {
229 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
230 _docError.getWarning( ).append( _docError.getCountLine( ) );
231 _docError.getWarning( ).append( " > " );
232 _docError.getWarning( )
233 .append( DocumentImport.PARAMETER_DATE_VALID_END + ", "+ DocumentImport.PARAMAETER_DATE_VALID_BEGIN + " :" + I18nService.getLocalizedString(
234 MESSAGE_ERROR_DATEEND_BEFORE_DATEBEGIN ,
235 locale) );
236 _docError.getWarning( ).append( "<br/>" );
237 _docError.setCountWarning( _docError.getCountWarning() + 1 );
238
239 }
240 }
241
242 document.setTitle( strDocumentTitle );
243 document.setSummary( strDocumentSummary );
244 document.setComment( strDocumentComment );
245 document.setDateValidityBegin( dateValidityBegin );
246 document.setDateValidityEnd( dateValidityEnd );
247 document.setMailingListId( nMailingListId );
248 document.setPageTemplateDocumentId( nPageTemplateDocumentId );
249
250 MetadataHandler hMetadata = documentType.metadataHandler( );
251
252 if ( hMetadata != null )
253 {
254 document.setXmlMetadata( hMetadata.getXmlMetadata( mRequest.getParameterMap( ) ) );
255 }
256
257 document.setAttributes( listAttributes );
258
259
260 List<Category> listCategories = new ArrayList<Category>( );
261
262 if ( arrayCategory != null )
263 {
264 for ( String strIdCategory : arrayCategory )
265 {
266 listCategories.add( CategoryHome.find( IntegerUtils.convert( strIdCategory ) ) );
267 }
268 }
269
270 document.setCategories( listCategories );
271
272 try {
273 this.createDocument(document, user);
274
275 } catch (DocumentException e) {
276
277 e.printStackTrace();
278 }
279
280 return null;
281 }
282
283
284
285
286
287
288
289
290
291
292 private String setAttribute( DocumentAttribute attribute, Document document, HashMap<String,String> valueAttribute , DocumentimporError _docError,
293 Locale locale )
294 {
295 String strParameterStringValue = StringUtils.isEmpty(valueAttribute.get(attribute.getName())) ? "" : valueAttribute.get(attribute.getName());
296
297
298
299
300
301
302 if ( !StringUtils.isEmpty(strParameterStringValue) )
303 {
304
305 if ( attribute.isRequired( ) && strParameterStringValue.trim( ).equals( "" ) )
306 {
307
308 _docError.getWarning( ).append( I18nService.getLocalizedString( PROPERTY_WARNING_LINE, locale ) );
309 _docError.getWarning( ).append( _docError.getCountLine( ) );
310 _docError.getWarning( ).append( " > " );
311 _docError.getWarning( )
312 .append( attribute.getName( ) + " :" + I18nService.getLocalizedString( MESSAGE_ERROR_CSV_MANDATORY_FIELD ,
313 locale));
314 _docError.getWarning( ).append( "<br/>" );
315 _docError.setCountWarning(_docError.getCountWarning( ) + 1 );
316 return null;
317 }
318
319
320 AttributeManager manager = AttributeService.getInstance( ).getManager( attribute.getCodeAttributeType( ) );
321 if ( attribute.getCodeAttributeType().equalsIgnoreCase("numerictext") && strParameterStringValue.trim( ).equals( "" ) )
322 {
323 strParameterStringValue= "0";
324 }
325 String strValidationErrorMessage = manager.validateValue( attribute.getId( ), strParameterStringValue,
326 locale );
327
328 if ( strValidationErrorMessage != null )
329 {
330
331 _docError.getError().append( I18nService.getLocalizedString( PROPERTY_ERROR_LINE, locale ) );
332 _docError.getError( ).append( _docError.getCountLine( ) );
333 _docError.getError( ).append( " > " );
334 _docError.getError( ).append( attribute.getName( )+": " + strValidationErrorMessage );
335 _docError.getError( ).append( "<br/>" );
336
337 _docError.setCountLineFailure( _docError.getCountLineFailure( ) + 1 );
338
339 return ERROR;
340
341 }
342 }
343 attribute.setTextValue( strParameterStringValue );
344 return null;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441 }
442
443
444
445
446
447
448
449
450
451 public void createDocument( Document document, AdminUser user)
452 throws DocumentException
453 {
454 DocumentService.getInstance().createDocument(document, user);
455
456
457
458
459
460 }
461
462
463
464
465
466
467 public static DocumentimportService getInstance( )
468 {
469 return _singleton;
470 }
471
472
473
474
475
476
477 public Collection<DocumentSpace> getUserSpaces( AdminUser user )
478 {
479 Collection<DocumentSpace> listSpaces = DocumentSpaceHome.findAll( );
480 listSpaces = RBACService.getAuthorizedCollection( listSpaces, SpaceResourceIdService.PERMISSION_VIEW, user );
481
482 return listSpaces;
483 }
484
485
486
487
488
489
490
491
492 public int getIdSpace(String nameSpaceParent, String nameSpaceFils){
493
494
495 if(nameSpaceParent != null || nameSpaceFils!= null ){
496 String ident= getIdSpaceParent(nameSpaceParent);
497 if(ident == null){
498 return -1;
499 }
500 List<DocumentSpace> docSpace= DocumentSpaceHome.findChilds(Integer.parseInt(ident));
501
502 for(DocumentSpace item: docSpace ){
503
504 if(item.getName().equals(nameSpaceFils)) return item.getId();
505 }
506 }
507 return -1;
508 }
509
510
511
512
513
514
515 private String getIdSpaceParent(String nameSpace){
516
517 ReferenceList listSpace= DocumentSpaceHome.getDocumentSpaceList( );
518
519 for(ReferenceItem item: listSpace ){
520
521 if(item.getName().equals(nameSpace)) return item.getCode();
522 }
523
524 return null;
525 }
526 }