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.service;
35  
36  import fr.paris.lutece.portal.service.resource.Resource;
37  import fr.paris.lutece.portal.service.resource.ResourceLoader;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.service.util.AppPathService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
42  import fr.paris.lutece.util.filesystem.FileSystemUtil;
43  
44  import java.io.File;
45  import java.io.FileInputStream;
46  import java.io.IOException;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.Properties;
51  
52  
53  public class FolderLoaderProperties implements ResourceLoader
54  {
55      /////////////////////////////////////////////////////////////////////
56      ////Properties
57      private static final String PROPERTY_FOLDER_FILES_PATH = "folderlisting.files.path";
58      private static final String PROPERTY_FOLDER_NAME = "folder.name";
59      private static final String PROPERTY_FOLDER_PATH = "folder.path";
60      private static final String PROPERTY_FOLDER_WORKGROUP = "folder.workgroup";
61      private static final String EXT_FOLDER_FILES = ".properties";
62      private String _strFolderFilesPath;
63  
64      /**
65       * Constructor
66       */
67      public FolderLoaderProperties(  )
68      {
69          super(  );
70          _strFolderFilesPath = AppPropertiesService.getProperty( PROPERTY_FOLDER_FILES_PATH );
71      }
72  
73      /**
74       * Gets all resource available for this Loader
75       * @return A collection of resources
76       */
77      public Collection getResources(  )
78      {
79          ArrayList listFolders = new ArrayList(  );
80  
81          try
82          {
83              String strRootDirectory = AppPathService.getWebAppPath(  );
84  
85              for ( File file : FileSystemUtil.getFiles( strRootDirectory, _strFolderFilesPath ) )
86              {
87                  String fileName = file.getName(  );
88  
89                  if ( fileName.endsWith( EXT_FOLDER_FILES ) )
90                  {
91                      Folder folder = loadFolder( file );
92                      listFolders.add( folder );
93                  }
94              }
95          }
96          catch ( DirectoryNotFoundException e )
97          {
98              AppLogService.error( e.getMessage(  ), e );
99          }
100 
101         return listFolders;
102     }
103 
104     /**
105      * Get a resource by its Id
106      * @param strId The resource Id
107      * @return The resource
108      */
109     public Resource getResource( String strId )
110     {
111         Resource resource = null;
112         String strFilePath = AppPathService.getPath( PROPERTY_FOLDER_FILES_PATH, strId + EXT_FOLDER_FILES );
113         File file = new File( strFilePath );
114 
115         if ( file.exists(  ) )
116         {
117             resource = loadFolder( file );
118         }
119 
120         return resource;
121     }
122 
123     /**
124      * Return the page
125      * @param file The File to load
126      * @return folder
127      */
128     private Folder loadFolder( File file )
129     {
130         Folder folder = new Folder(  );
131         Properties properties = new Properties(  );
132 
133         try
134         {
135             FileInputStream is = new FileInputStream( file );
136             properties.load( is );
137             folder.setName( properties.getProperty( PROPERTY_FOLDER_NAME ) );
138             folder.setPath( properties.getProperty( PROPERTY_FOLDER_PATH ) );
139             folder.setWorkgroup( properties.getProperty( PROPERTY_FOLDER_WORKGROUP ) );
140 
141             String strFolderId = file.getName(  ).substring( 0, file.getName(  ).lastIndexOf( "." ) );
142             folder.setId( strFolderId );
143         }
144         catch ( IOException e )
145         {
146             AppLogService.error( e.getMessage(  ), e );
147         }
148 
149         return folder;
150     }
151 }