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.folderlisting.business.portlet;
35
36 import fr.paris.lutece.plugins.folderlisting.business.FolderListingDirectory;
37 import fr.paris.lutece.plugins.folderlisting.business.FolderListingFile;
38 import fr.paris.lutece.plugins.folderlisting.business.FolderListingHome;
39 import fr.paris.lutece.plugins.folderlisting.service.Folder;
40 import fr.paris.lutece.plugins.folderlisting.service.FolderService;
41 import fr.paris.lutece.portal.business.portlet.Portlet;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPathService;
44 import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
45 import fr.paris.lutece.util.xml.XmlUtil;
46
47 import java.io.File;
48
49 import javax.servlet.http.HttpServletRequest;
50
51
52
53
54
55 public class FolderListingPortlet extends Portlet
56 {
57
58
59
60
61 private static final String TAG_ALIAS_ROOT_DIRECTORY = "directory-root-alias";
62 private static final String TAG_PORTLET_FOLDERLISTING = "folderlisting-portlet";
63 private static final String TAG_DIRECTORY_PATH = "directory-path";
64
65
66 private int _nFolderListingFileId;
67 private String _strFolderPath;
68 private String _strRootFolderId;
69
70
71
72
73 public FolderListingPortlet( )
74 {
75 setPortletTypeId( FolderListingPortletHome.getInstance( ).getPortletTypeId( ) );
76 }
77
78
79
80
81
82
83 public void setFolderListingFileId( int nFolderListingFileId )
84 {
85 _nFolderListingFileId = nFolderListingFileId;
86 }
87
88
89
90
91
92
93 public int getFolderListingFileId( )
94 {
95 return _nFolderListingFileId;
96 }
97
98
99
100
101
102
103 public String getFolderPath( )
104 {
105 return _strFolderPath;
106 }
107
108
109
110
111
112
113 public void setRootFolderId( String strRootFolderId )
114 {
115 _strRootFolderId = strRootFolderId;
116 }
117
118
119
120
121
122
123 public String getRootFolderId( )
124 {
125 return _strRootFolderId;
126 }
127
128
129
130
131
132
133 public void setFolderPath( String strFolderPath )
134 {
135 _strFolderPath = strFolderPath;
136 }
137
138
139
140
141
142 public String getRootFolderPath( )
143 {
144 String strFolderPath = "";
145
146 Folder folder = FolderService.getInstance( ).getFolder( getRootFolderId( ) );
147
148 if ( folder != null )
149 {
150 strFolderPath = folder.getPath( );
151 }
152 else
153 {
154 AppLogService.error( "Folderlisting : Invalid folder Id :" + getRootFolderId( ) );
155 }
156
157 return strFolderPath;
158 }
159
160
161
162
163
164 public String getRootFolderName( )
165 {
166 String strRootFolderName = "";
167
168 Folder folder = FolderService.getInstance( ).getFolder( getRootFolderId( ) );
169
170 if ( folder != null )
171 {
172 strRootFolderName = folder.getName( );
173 }
174 else
175 {
176 AppLogService.error( "Folderlisting : Invalid folder Id :" + getRootFolderId( ) );
177 }
178
179 return strRootFolderName;
180 }
181
182
183
184
185
186
187
188 public String getXml( HttpServletRequest request )
189 {
190
191 setFolderPathFromRequest( request );
192
193 StringBuffer sbXml = new StringBuffer( );
194
195 XmlUtil.beginElement( sbXml, TAG_PORTLET_FOLDERLISTING );
196
197 XmlUtil.addElement( sbXml, TAG_ALIAS_ROOT_DIRECTORY, getRootFolderName( ) );
198
199 XmlUtil.addElement( sbXml, TAG_DIRECTORY_PATH, getFolderPath( ) );
200
201 try
202 {
203 for ( FolderListingDirectory folderlistingDirectory : FolderListingHome.getSubDirectories( getRootFolderPath( ) +
204 getFolderPath( ) ) )
205 {
206 sbXml.append( folderlistingDirectory.getXml( request ) );
207 }
208
209 for ( FolderListingFile folderlistingFile : FolderListingHome.getFiles( getRootFolderPath( ) +
210 getFolderPath( ) ) )
211 {
212 sbXml.append( folderlistingFile.getXml( request ) );
213 }
214
215 XmlUtil.endElement( sbXml, TAG_PORTLET_FOLDERLISTING );
216 }
217 catch ( DirectoryNotFoundException e )
218 {
219 AppLogService.error( e.getMessage( ), e );
220
221 sbXml.delete( 0, sbXml.length( ) );
222 }
223
224 return addPortletTags( sbXml );
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238
239 private void setFolderPathFromRequest( HttpServletRequest request )
240 {
241 String strFolderPath = null;
242
243 if ( request != null )
244 {
245 strFolderPath = request.getParameter( "folder_" + getId( ) );
246 }
247
248 if ( ( strFolderPath == null ) || ( strFolderPath.trim( ).equals( "" ) ) )
249 {
250 setFolderPath( "" );
251 }
252 else
253 {
254
255 File fDirectory = new File( AppPathService.getAbsolutePathFromRelativePath( getRootFolderPath( ) +
256 strFolderPath ) );
257
258 if ( fDirectory.exists( ) )
259 {
260 setFolderPath( strFolderPath );
261 }
262 else
263 {
264 setFolderPath( "" );
265 }
266 }
267 }
268
269
270
271
272
273
274
275 public String getXmlDocument( HttpServletRequest request )
276 {
277 return XmlUtil.getXmlHeader( ) + getXml( request );
278 }
279
280
281
282
283 public void update( )
284 {
285 FolderListingPortletHome.getInstance( ).update( this );
286 }
287
288
289
290
291 public void remove( )
292 {
293 FolderListingPortletHome.getInstance( ).remove( this );
294 }
295 }