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.portal.business.theme;
35  
36  import fr.paris.lutece.portal.business.datastore.DataEntity;
37  import fr.paris.lutece.portal.business.datastore.DataEntityDAO;
38  import fr.paris.lutece.portal.business.datastore.DataEntityHome;
39  import fr.paris.lutece.portal.business.style.Theme;
40  import fr.paris.lutece.util.ReferenceList;
41  import fr.paris.lutece.util.sql.DAOUtil;
42  
43  import java.util.ArrayList;
44  import java.util.Collection;
45  
46  
47  /**
48   * This class provides Data Access methods for Theme objects
49   */
50  public final class ThemeDAO implements IThemeDAO
51  {
52      private static final String SQL_QUERY_SELECT = " SELECT code_theme, theme_description, path_images, path_css, theme_author, " +
53          " theme_author_url, theme_version, theme_licence, path_js FROM core_theme WHERE code_theme = ?";
54      private static final String SQL_QUERY_SELECTALL = " SELECT code_theme, theme_description, path_images, path_css, theme_author, " +
55          " theme_author_url, theme_version, theme_licence, path_js FROM core_theme ORDER BY code_theme";
56      private static final String SQL_QUERY_SELECT_THEME = " SELECT code_theme, theme_description FROM core_theme";
57      private static final String SQL_QUERY_SELECT_GLOBAL_THEME = " SELECT tt.code_theme, tt.theme_description, tt.path_images, tt.path_css, " +
58          " tt.theme_author, tt.theme_author_url, tt.theme_version, tt.theme_licence, tt.path_js " +
59          " FROM core_theme tt WHERE tt.code_theme = ?";
60      private static final String SQL_QUERY_UPDATE_GLOBAL_THEME = " UPDATE core_datastore SET entity_value=? WHERE entity_key = 'theme.globalThemeCode' ";
61      private static final String SQL_QUERY_UPDATE_GLOBAL_THEME_VERSION = " UPDATE core_datastore SET entity_value=? WHERE entity_key = 'theme.globalThemeVersion' ";
62  
63      /**
64       * {@inheritDoc}
65       */
66      public Theme load( String strCodeTheme )
67      {
68          Theme theme = null;
69          try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT ) ) {
70              daoUtil.setString( 1, strCodeTheme );
71  
72              daoUtil.executeQuery(  );
73  
74              if ( daoUtil.next(  ) )
75              {
76                  theme = new Theme(  );
77                  theme.setCodeTheme( daoUtil.getString( 1 ) );
78                  theme.setThemeDescription( daoUtil.getString( 2 ) );
79                  theme.setPathImages( daoUtil.getString( 3 ) );
80                  theme.setPathCss( daoUtil.getString( 4 ) );
81                  theme.setThemeAuthor( daoUtil.getString( 5 ) );
82                  theme.setThemeAuthorUrl( daoUtil.getString( 6 ) );
83                  theme.setThemeVersion( daoUtil.getString( 7 ) );
84                  theme.setThemeLicence( daoUtil.getString( 8 ) );
85                  theme.setPathJs( daoUtil.getString( 9 ) );
86              }
87          }
88          return theme;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      public Collection<Theme> selectThemesList( )
95      {
96          Collection<Theme> themeList = new ArrayList<Theme>(  );
97          try (DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL ) ) {
98              daoUtil.executeQuery(  );
99  
100             while ( daoUtil.next(  ) )
101             {
102                 Themeal/business/style/Theme.html#Theme">Theme theme = new Theme(  );
103 
104                 theme.setCodeTheme( daoUtil.getString( 1 ) );
105                 theme.setThemeDescription( daoUtil.getString( 2 ) );
106                 theme.setPathImages( daoUtil.getString( 3 ) );
107                 theme.setPathCss( daoUtil.getString( 4 ) );
108                 theme.setThemeAuthor( daoUtil.getString( 5 ) );
109                 theme.setThemeAuthorUrl( daoUtil.getString( 6 ) );
110                 theme.setThemeVersion( daoUtil.getString( 7 ) );
111                 theme.setThemeLicence( daoUtil.getString( 8 ) );
112                 theme.setPathJs( daoUtil.getString( 9 ) );
113 
114                 themeList.add( theme );
115             }
116         }
117         return themeList;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public ReferenceList getThemesList( )
124     {
125         ReferenceListt.html#ReferenceList">ReferenceList themesList = new ReferenceList(  );
126         try ( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_THEME ) ) {
127             daoUtil.executeQuery(  );
128 
129             while ( daoUtil.next(  ) )
130             {
131                 themesList.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
132             }
133 
134         }
135         return themesList;
136     }
137 
138     /**
139      * {@inheritDoc }
140      */
141     public void setGlobalTheme( String strGlobalTheme, String strGlobalThemeVersion )
142     {
143         try (DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_GLOBAL_THEME ) )
144         {
145             daoUtil.setString( 1, strGlobalTheme );
146             daoUtil.executeUpdate(  );
147         }
148 
149         try (DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_GLOBAL_THEME_VERSION ) )
150         {
151             daoUtil.setString( 1, strGlobalThemeVersion );
152             daoUtil.executeUpdate(  );
153         }
154     }
155 
156     /**
157      * {@inheritDoc }
158      */
159     public Theme getGlobalTheme( )
160     {
161         Theme theme = null;
162         DataEntity globalTheme = DataEntityHome.findByPrimaryKey("theme.globalThemeCode");
163         try( DAOUtil/DAOUtil.html#DAOUtil">DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_GLOBAL_THEME ) ) 
164         {
165             daoUtil.setString( 1, globalTheme.getValue() );
166             daoUtil.executeQuery(  );
167 
168             if ( daoUtil.next(  ) )
169             {
170                 theme = new Theme(  );
171                 theme.setCodeTheme( daoUtil.getString( 1 ) );
172                 theme.setThemeDescription( daoUtil.getString( 2 ) );
173                 theme.setPathImages( daoUtil.getString( 3 ) );
174                 theme.setPathCss( daoUtil.getString( 4 ) );
175                 theme.setThemeAuthor( daoUtil.getString( 5 ) );
176                 theme.setThemeAuthorUrl( daoUtil.getString( 6 ) );
177                 theme.setThemeVersion( daoUtil.getString( 7 ) );
178                 theme.setThemeLicence( daoUtil.getString( 8 ) );
179                 theme.setPathJs( daoUtil.getString( 9 ) );
180             }
181 
182         }
183 
184         return theme;
185     }
186 }