1 package fr.paris.lutece.plugins.directory.modules.exportfile.service;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.Vector;
6 import java.util.concurrent.ConcurrentHashMap;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpSession;
10
11 import fr.paris.lutece.plugins.directory.modules.exportfile.business.FileName;
12 import fr.paris.lutece.plugins.directory.modules.exportfile.business.FileNameHome;
13 import fr.paris.lutece.plugins.directory.modules.exportfile.business.MappingEntry;
14 import fr.paris.lutece.plugins.directory.modules.exportfile.business.MappingEntryHome;
15
16 public class FileNameService
17 {
18
19 private static Map<String, List<FileName>> _mapFileName = new ConcurrentHashMap<String, List<FileName>>( );
20 public static FileNameService _singleton;
21
22 public static FileNameService getService( )
23 {
24
25 if ( _singleton == null )
26 {
27
28 _singleton = new FileNameService( );
29 return _singleton;
30
31 }
32
33 return _singleton;
34 }
35
36 public void addFileNameToUploadedList( FileName fileName, HttpServletRequest request )
37 {
38 if ( fileName.getAttribute( ) != null )
39 {
40 initMap( request.getSession( ).getId( ) );
41 List<FileName> uploadedFilesName = getListUploadedFiles( request.getSession( ) );
42 fileName.setOrder( uploadedFilesName.size( ) + 1 );
43 uploadedFilesName.add( fileName );
44 }
45
46 }
47
48 private void initMap( String strSessionId )
49 {
50
51 List<FileName> mapFileNameSession = _mapFileName.get( strSessionId );
52
53
54 if ( mapFileNameSession == null )
55 {
56 synchronized( this )
57 {
58
59 mapFileNameSession = _mapFileName.get( strSessionId );
60
61 if ( mapFileNameSession == null )
62 {
63 mapFileNameSession = new Vector<FileName>( );
64 _mapFileName.put( strSessionId, mapFileNameSession );
65 }
66 }
67 }
68
69 }
70
71 public List<FileName> getListUploadedFiles( HttpSession session )
72 {
73
74 initMap( session.getId( ) );
75
76
77 List<FileName> mapFileNameSession = _mapFileName.get( session.getId( ) );
78
79 return mapFileNameSession;
80 }
81
82 public void removeFileName( HttpSession session, int nIndex )
83 {
84 List<FileName> uploadedFileName = getListUploadedFiles( session );
85
86 if ( ( uploadedFileName != null ) && !uploadedFileName.isEmpty( ) )
87 {
88 uploadedFileName.remove( nIndex );
89
90 }
91 }
92
93 public void removeFilesName( HttpSession session )
94 {
95 List<FileName> uploadedFileName = getListUploadedFiles( session );
96
97 if ( ( uploadedFileName != null ) && !uploadedFileName.isEmpty( ) )
98 {
99 uploadedFileName.clear( );
100
101 }
102 }
103
104 public void createEntry( MappingEntry mappingEntry, List<FileName> ListfileName )
105 {
106
107 MappingEntry mappingEnt = MappingEntryHome.create( mappingEntry );
108 for ( FileName fileName : ListfileName )
109 {
110 fileName.setMappingEntry( mappingEnt.getId( ) );
111 FileNameHome.create( fileName );
112 }
113
114 }
115
116 public void updateEntry( MappingEntry mappingEntry, List<FileName> ListfileName )
117 {
118
119 MappingEntry mappingEnt = MappingEntryHome.update( mappingEntry );
120 FileNameHome.remove( mappingEntry.getId( ) );
121 for ( FileName fileName : ListfileName )
122 {
123 fileName.setMappingEntry( mappingEnt.getId( ) );
124 FileNameHome.create( fileName );
125
126 }
127
128 }
129
130 public void removeEntry( int idEntry )
131 {
132
133 MappingEntryHome.remove( idEntry );
134 FileNameHome.remove( idEntry );
135
136 }
137
138 }