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.dbpage.business;
35  
36  import fr.paris.lutece.plugins.dbpage.business.section.DbPageSection;
37  import fr.paris.lutece.portal.service.resource.Resource;
38  import fr.paris.lutece.portal.service.resource.ResourceLoader;
39  import fr.paris.lutece.portal.service.util.AppException;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.portal.service.util.AppPathService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
44  import fr.paris.lutece.util.filesystem.FileSystemUtil;
45  
46  import java.io.File;
47  import java.io.FileInputStream;
48  import java.io.IOException;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  import java.util.List;
53  import java.util.Properties;
54  
55  
56  /**
57   * This class provides the properties object loader
58   */
59  public class DbPageLoaderProperties implements ResourceLoader
60  {
61      private static final String PROPERTY_PAGE_TITLE = "page.title";
62      private static final String PROPERTY_PAGE_NAME = "page.paramname";
63      private static final String PROPERTY_PAGE_WORKGROUP = "page.workgroup";
64      private static final String PROPERTY_PAGE_NUMBER_SECTIONS = "page.nbsections";
65      private static final String PROPERTY_SECTION_CLASS = "dbpage.section.type.class.";
66      private static final String PROPERTY_SECTION = "section";
67      private static final String PROPERTY_SECTION_TYPE = ".type";
68      private static final String PROPERTY_SECTION_TEMPLATE = ".template";
69      private static final String PROPERTY_SECTION_COLUMN = ".column";
70      private static final String PROPERTY_SECTION_SQL = ".sql";
71      private static final String PROPERTY_SECTION_TITLE = ".title";
72      private static final String PROPERTY_SECTION_POOL = ".pool";
73      private static final String PROPERTY_SECTION_ROLE = ".role";
74      private static final String PROPERTY_DBPAGE_FILES_PATH = "dbpage.files.path";
75      private static final String EXT_DBPAGE_FILES = ".properties";
76      private String _strDbPageFilesPath;
77  
78      /**
79       * Constructor
80       */
81      public DbPageLoaderProperties(  )
82      {
83          super(  );
84          _strDbPageFilesPath = AppPropertiesService.getProperty( PROPERTY_DBPAGE_FILES_PATH );
85      }
86  
87      /**
88       * Gets all resource available for this Loader
89       * @return A collection of resources
90       */
91      public Collection<DbPage> getResources(  )
92      {
93          List<DbPage> listDbPage = new ArrayList<DbPage>(  );
94          String strRootDirectory = AppPathService.getWebAppPath(  );
95          List<File> listPages = null;
96  
97          try
98          {
99              listPages = FileSystemUtil.getFiles( strRootDirectory, _strDbPageFilesPath );
100 
101             for ( File file : listPages )
102             {
103                 if ( file.getName(  ).endsWith( EXT_DBPAGE_FILES ) )
104                 {
105                     DbPage dbPage = loadPage( file );
106                     listDbPage.add( dbPage );
107                 }
108             }
109         }
110         catch ( DirectoryNotFoundException e )
111         {
112             throw new AppException( e.getMessage(  ), e );
113         }
114 
115         return listDbPage;
116     }
117 
118     /**
119      * Get a resource by its Id
120      * @param strId The resource Id
121      * @return The resource
122      */
123     public Resource getResource( String strId )
124     {
125         Resource resource = null;
126         String strFilePath = AppPathService.getPath( PROPERTY_DBPAGE_FILES_PATH, strId + EXT_DBPAGE_FILES );
127         File file = new File( strFilePath );
128 
129         if ( file.exists(  ) )
130         {
131             resource = loadPage( file );
132         }
133 
134         return resource;
135     }
136 
137     /**
138      * Return the page
139      * @param file The File to load
140      * @return dbPage
141      */
142     private DbPage loadPage( File file )
143     {
144         DbPage dbPage = new DbPage(  );
145         Properties properties = new Properties(  );
146 
147         try
148         {
149             FileInputStream is = new FileInputStream( file );
150             properties.load( is );
151 
152             // read page data in the properties file
153             dbPage.setName( properties.getProperty( PROPERTY_PAGE_NAME ) );
154             dbPage.setTitle( properties.getProperty( PROPERTY_PAGE_TITLE ) );
155             dbPage.setWorkgroup( properties.getProperty( PROPERTY_PAGE_WORKGROUP ) );
156 
157             String strNbSection = properties.getProperty( PROPERTY_PAGE_NUMBER_SECTIONS );
158             int nSections = 0;
159             nSections = Integer.parseInt( strNbSection );
160             dbPage.setNbSection( nSections );
161 
162             List<DbPageSection> myCollection = loadListSection( properties, nSections );
163             dbPage.setListSection( myCollection );
164         }
165         catch ( IOException e )
166         {
167             AppLogService.error( e.getMessage(  ), e );
168         }
169 
170         return dbPage;
171     }
172 
173     /**
174      * Return a list of sections
175      * @param properties the list of properties
176      * @param nSections the section of the properties
177      * @return the collection of dbPageSection
178      */
179     public List<DbPageSection> loadListSection( Properties properties, int nSections )
180     {
181         List<DbPageSection> listSections = new ArrayList<DbPageSection>(  );
182 
183         for ( int i = 1; i <= nSections; i++ )
184         {
185             String strPrefixSection = PROPERTY_SECTION + i;
186             String strType = properties.getProperty( strPrefixSection + PROPERTY_SECTION_TYPE );
187             String strSectionClass = AppPropertiesService.getProperty( PROPERTY_SECTION_CLASS + strType );
188 
189             DbPageSection dbPageSection = null;
190 
191             try
192             {
193                 dbPageSection = (DbPageSection) Class.forName( strSectionClass ).newInstance(  );
194             }
195             catch ( ClassNotFoundException e )
196             {
197                 AppLogService.error( e.getMessage(  ), e );
198 
199                 return listSections;
200             }
201             catch ( IllegalAccessException e )
202             {
203                 AppLogService.error( e.getMessage(  ), e );
204 
205                 return listSections;
206             }
207             catch ( InstantiationException e )
208             {
209                 AppLogService.error( e.getMessage(  ), e );
210 
211                 return listSections;
212             }
213 
214             dbPageSection.setType( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TYPE ) );
215             dbPageSection.setTitle( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TITLE ) );
216             dbPageSection.setSql( properties.getProperty( strPrefixSection + PROPERTY_SECTION_SQL ) );
217             dbPageSection.setDbPool( properties.getProperty( strPrefixSection + PROPERTY_SECTION_POOL ) );
218             dbPageSection.setRole( properties.getProperty( strPrefixSection + PROPERTY_SECTION_ROLE ) );
219             dbPageSection.setTemplatePath( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TEMPLATE ) );
220 
221             String strTemplate = properties.getProperty( strPrefixSection + PROPERTY_SECTION_TEMPLATE );
222             dbPageSection.setTemplatePath( strTemplate );
223 
224             //Columns name loading
225             String strColumnsName = properties.getProperty( strPrefixSection + PROPERTY_SECTION_COLUMN );
226 
227             if ( strColumnsName != null )
228             {
229                 List<String> aCollumnsName = new ArrayList<String>(  );
230 
231                 while ( !strColumnsName.equals( "" ) )
232                 {
233                     int nPos = strColumnsName.indexOf( "," );
234 
235                     if ( nPos != -1 )
236                     {
237                         String strValue = strColumnsName.substring( 0, nPos );
238                         strValue = strValue.trim(  );
239                         strColumnsName = strColumnsName.substring( nPos + 1 );
240                         aCollumnsName.add( strValue );
241                     }
242                     else
243                     {
244                         aCollumnsName.add( strColumnsName.trim(  ) );
245                         strColumnsName = "";
246                     }
247 
248                     dbPageSection.setColumnNames( aCollumnsName );
249                 }
250             }
251 
252             listSections.add( dbPageSection );
253         }
254 
255         return listSections;
256     }
257 }