View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.blog.business.rss;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   *
44   * class BlogResourceRssConfigDAO
45   *
46   */
47  public class BlogResourceRssConfigDAO implements IBlogResourceRssConfigDAO
48  {
49      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT id_rss,id_portlet " + "FROM blog_rss_cf  WHERE id_rss=?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO blog_rss_cf( " + "id_rss, id_portlet )" + "VALUES (?, ?)";
51      private static final String SQL_QUERY_UPDATE = "UPDATE blog_rss_cf " + "SET id_rss=?, id_portlet=?" + " WHERE id_rss=?";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM blog_rss_cf WHERE id_rss=? ";
53      private static final String SQL_QUERY_FIND_ALL = "SELECT id_rss, id_portlet " + "FROM blog_rss_cf";
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public synchronized void insert( BlogResourceRssConfig config, Plugin plugin )
60      {
61          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
62          {
63              int nPos = 0;
64  
65              daoUtil.setInt( ++nPos, config.getIdRss( ) );
66              daoUtil.setInt( ++nPos, config.getIdPortlet( ) );
67  
68              daoUtil.executeUpdate( );
69          }
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void store( BlogResourceRssConfig config, Plugin plugin )
77      {
78          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
79          {
80              int nPos = 0;
81  
82              daoUtil.setInt( ++nPos, config.getIdRss( ) );
83              daoUtil.setInt( ++nPos, config.getIdPortlet( ) );
84  
85              daoUtil.setInt( ++nPos, config.getIdRss( ) );
86              daoUtil.executeUpdate( );
87          }
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public BlogResourceRssConfig load( int nIdRss, Plugin plugin )
95      {
96          BlogResourceRssConfig config = null;
97          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
98          {
99              daoUtil.setInt( 1, nIdRss );
100 
101             daoUtil.executeQuery( );
102 
103             int nPos = 0;
104 
105             if ( daoUtil.next( ) )
106             {
107                 config = new BlogResourceRssConfig( );
108                 config.setIdRss( daoUtil.getInt( ++nPos ) );
109                 config.setIdPortlet( daoUtil.getInt( ++nPos ) );
110             }
111         }
112         return config;
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public void delete( int nIdRss, Plugin plugin )
120     {
121         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
122         {
123             daoUtil.setInt( 1, nIdRss );
124             daoUtil.executeUpdate( );
125         }
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public List<BlogResourceRssConfig> loadAll( Plugin plugin )
133     {
134         List<BlogResourceRssConfig> configList = new ArrayList<>( );
135         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_ALL, plugin ) )
136         {
137             daoUtil.executeQuery( );
138 
139             int nPos = 0;
140             if ( daoUtil.next( ) )
141             {
142                 BlogResourceRssConfigess/rss/BlogResourceRssConfig.html#BlogResourceRssConfig">BlogResourceRssConfig config = new BlogResourceRssConfig( );
143                 config.setIdRss( daoUtil.getInt( ++nPos ) );
144                 config.setIdPortlet( daoUtil.getInt( ++nPos ) );
145 
146                 configList.add( config );
147             }
148         }
149         return configList;
150     }
151 }