View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.folderlisting.business;
35  
36  import fr.paris.lutece.portal.service.util.AppPathService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
39  import fr.paris.lutece.util.filesystem.FileSystemUtil;
40  
41  import java.io.File;
42  
43  import java.text.DateFormat;
44  import java.text.SimpleDateFormat;
45  
46  import java.util.ArrayList;
47  import java.util.Collection;
48  import java.util.Date;
49  
50  
51  /**
52   * This class provides instances management methods (accept, getDescription, getFiles) for FolderListingFile objects.
53   */
54  public class FolderListingHome
55  {
56      ///////////////////////////////////////////////////////////////////////////////////
57      // Constants
58      private static final String PROPERTY_DATE_FORMAT = "folderlisting.format.date";
59  
60      /**
61       * Return file informations
62       * @param strPath The relative path of the folder
63       * @return A collection of FolderListFiles
64       */
65      public static Collection<FolderListingFile> getFiles( String strPath )
66          throws DirectoryNotFoundException
67      {
68          Collection listFiles = new ArrayList(  );
69  
70          for ( File file : FileSystemUtil.getFiles( AppPathService.getWebAppPath(  ), strPath ) )
71          {
72              FolderListingFile f = new FolderListingFile(  );
73  
74              // Get the file extension by getting the position of the last dot
75              String strExtension = null;
76              int nPos = file.getName(  ).lastIndexOf( "." );
77  
78              if ( nPos != -1 )
79              {
80                  strExtension = file.getName(  ).substring( nPos + 1 );
81              }
82  
83              // Get the last modified date formatted
84              String strDateFormat = AppPropertiesService.getProperty( PROPERTY_DATE_FORMAT );
85              DateFormat formatter = new SimpleDateFormat( strDateFormat );
86              Date date = new Date( file.lastModified(  ) );
87              String strDate = formatter.format( date );
88  
89              // Sets attributes of the FolderListingFile
90              f.setName( file.getName(  ) );
91              f.setSize( "" + ( ( file.length(  ) / 1024L ) + 1 ) );
92              f.setPath( strPath );
93              f.setDate( strDate );
94              f.setExtension( strExtension );
95  
96              listFiles.add( f );
97          }
98  
99          return listFiles;
100     }
101 
102     /**
103      * Return subdirectories
104      * @param strPath The relative path of the folder
105      * @return A collection of FolderListFiles
106      */
107     public static Collection<FolderListingDirectory> getSubDirectories( String strPath )
108         throws DirectoryNotFoundException
109     {
110         Collection listDirs = new ArrayList(  );
111 
112         for ( File file : FileSystemUtil.getSubDirectories( AppPathService.getWebAppPath(  ), strPath ) )
113         {
114             FolderListingDirectory dir = new FolderListingDirectory(  );
115 
116             // Get the last modified date formatted
117             String strDateFormat = AppPropertiesService.getProperty( PROPERTY_DATE_FORMAT );
118             DateFormat formatter = new SimpleDateFormat( strDateFormat );
119             Date date = new Date( file.lastModified(  ) );
120             String strDate = formatter.format( date );
121 
122             // Sets attributes of the FolderListingFile
123             dir.setName( file.getName(  ) );
124             dir.setPath( strPath );
125             dir.setDate( strDate );
126 
127             listDirs.add( dir );
128         }
129 
130         return listDirs;
131     }
132 }