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.plugins.dbpage.business.section.DbSectionService;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.plugin.PluginService;
40  import fr.paris.lutece.portal.service.resource.Resource;
41  import fr.paris.lutece.portal.service.resource.ResourceLoader;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  import java.util.List;
46  
47  
48  /**
49   * This class provides the properties object loader
50   */
51  public class DbPageLoaderDatabase implements ResourceLoader
52  {
53      private Plugin _plugin;
54      private String _strPluginName = "dbpage";
55  
56      /**
57       * Gets all resource available for this Loader
58       * @return A collection of resources
59       */
60      public Collection<DbPage> getResources(  )
61      {
62          if ( _plugin == null )
63          {
64              _plugin = PluginService.getPlugin( _strPluginName );
65          }
66  
67          List<DbPage> listPages = new ArrayList<DbPage>(  );
68          List<DbPageDatabase> listDbPages = DbPageDatabaseHome.findDbPageDatabasesList( _plugin );
69  
70          for ( DbPageDatabase dbDatabasePage : listDbPages )
71          {
72              DbPage dbPage = loadPage( dbDatabasePage.getParamName(  ) );
73              listPages.add( dbPage );
74          }
75  
76          return listPages;
77      }
78  
79      /**
80       * Get a resource by its Id
81       * @return The resource
82       * @param strPageId a string representing the identifier of the Page
83       */
84      public Resource getResource( String strPageId )
85      {
86          Resource resource = null;
87  
88          resource = loadPage( strPageId );
89  
90          return resource;
91      }
92  
93      /**
94       * Return the page
95       * @return dbPage
96       * @param strPageName The name of the page
97       */
98      private DbPage loadPage( String strPageName )
99      {
100         if ( _plugin == null )
101         {
102             _plugin = PluginService.getPlugin( _strPluginName );
103         }
104 
105         DbPageDatabase dbPageDatabase = DbPageDatabaseHome.findByName( strPageName, _plugin );
106 
107         if ( dbPageDatabase == null )
108         {
109             return null;
110         }
111 
112         DbPage dbPage = new DbPage(  );
113         dbPage.setName( dbPageDatabase.getParamName(  ) );
114         dbPage.setTitle( dbPageDatabase.getTitle(  ) );
115         dbPage.setWorkgroup( dbPageDatabase.getWorkgroup(  ) );
116 
117         int nSections = DbPageDatabaseSectionHome.countNumberSections( dbPageDatabase.getId(  ), _plugin );
118         dbPage.setNbSection( nSections );
119 
120         List<DbPageSection> listSections = new ArrayList<DbPageSection>(  );
121         List<DbPageDatabaseSection> listSectionsDb = DbPageDatabaseSectionHome.findDbPageDatabaseSectionsList( _plugin );
122 
123         for ( DbPageDatabaseSection dbSection : listSectionsDb )
124         {
125             DbPageSection dbPageSection = null;
126 
127             if ( dbSection.getIdPage(  ) == dbPageDatabase.getId(  ) )
128             {
129                 String strIdType = dbSection.getIdType(  );
130 
131                 dbPageSection = DbSectionService.INSTANCE.findById( strIdType );
132                 //  dbPageSection.setType( type.getClassDesc(  ) );
133                 dbPageSection.setTitle( dbSection.getTitle(  ) );
134                 dbPageSection.setSql( dbSection.getSql(  ) );
135                 dbPageSection.setDbPool( dbSection.getPool(  ) );
136                 dbPageSection.setRole( dbSection.getRole(  ) );
137 
138                 String strTemplatePath = dbSection.getTemplatePath(  );
139 
140                 dbPageSection.setTemplatePath( strTemplatePath );
141 
142                 //Columns name loading
143                 String strColumnsName = dbSection.getColumn(  );
144 
145                 if ( strColumnsName != null )
146                 {
147                     List<String> aColumnsName = new ArrayList<String>(  );
148 
149                     while ( !strColumnsName.equals( "" ) )
150                     {
151                         int nPos = strColumnsName.indexOf( "," );
152 
153                         if ( nPos != -1 )
154                         {
155                             String strValue = strColumnsName.substring( 0, nPos );
156                             strValue = strValue.trim(  );
157                             strColumnsName = strColumnsName.substring( nPos + 1 );
158                             aColumnsName.add( strValue );
159                         }
160                         else
161                         {
162                             aColumnsName.add( strColumnsName.trim(  ) );
163                             strColumnsName = "";
164                         }
165 
166                         dbPageSection.setColumnNames( aColumnsName );
167                     }
168                 }
169             }
170 
171             if ( dbPageSection != null )
172             {
173                 listSections.add( dbPageSection );
174             }
175         }
176 
177         dbPage.setListSection( listSections );
178 
179         return dbPage;
180     }
181 }