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.newsletter.business.topic;
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   * DAO implementation for {@link FreeHtmlTopic}
44   */
45  public class FreeHtmlTopicDao implements IFreeHtmlTopicDAO
46  {
47      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = " SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic = ? ";
48      private static final String SQL_QUERY_UPDATE = " UPDATE newsletter_topic_free_html SET html_content = ? WHERE id_topic = ? ";
49      private static final String SQL_QUERY_INSERT = " INSERT INTO newsletter_topic_free_html (id_topic, html_content) VALUES (?,?) ";
50      private static final String SQL_QUERY_DELETE = " DELETE FROM newsletter_topic_free_html WHERE id_topic = ? ";
51      private static final String SQL_QUERY_FIND_BY_ID_LIST = "  SELECT id_topic, html_content FROM newsletter_topic_free_html WHERE id_topic IN ( ";
52  
53      private static final String CONSTANT_COMMA = ",";
54      private static final String CONSTANT_CLOSE_PARENTHESIS = ")";
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      public FreeHtmlTopic findByPrimaryKey( int nId, Plugin plugin )
61      {
62          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
63          daoUtil.setInt( 1, nId );
64          FreeHtmlTopic topic = null;
65  
66          daoUtil.executeQuery( );
67          if ( daoUtil.next( ) )
68          {
69              topic = new FreeHtmlTopic( );
70              topic.setId( daoUtil.getInt( 1 ) );
71              topic.setHtmlContent( daoUtil.getString( 2 ) );
72          }
73          daoUtil.free( );
74          return topic;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void insert( FreeHtmlTopic freeHtmlTopic, Plugin plugin )
82      {
83          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
84          daoUtil.setInt( 1, freeHtmlTopic.getId( ) );
85          daoUtil.setString( 2, freeHtmlTopic.getHtmlContent( ) );
86          daoUtil.executeUpdate( );
87          daoUtil.free( );
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public void update( FreeHtmlTopic freeHtmlTopic, Plugin plugin )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
97          daoUtil.setString( 1, freeHtmlTopic.getHtmlContent( ) );
98          daoUtil.setInt( 2, freeHtmlTopic.getId( ) );
99          daoUtil.executeUpdate( );
100         daoUtil.free( );
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public void remove( int nId, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
110         daoUtil.setInt( 1, nId );
111         daoUtil.executeUpdate( );
112         daoUtil.free( );
113     }
114 
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public List<FreeHtmlTopic> findCollection( List<Integer> listIds, Plugin plugin )
120     {
121         List<FreeHtmlTopic> listTopic = new ArrayList<FreeHtmlTopic>( );
122         if ( listIds != null )
123         {
124             List<Integer> listPrivIds = new ArrayList<Integer>( listIds );
125             StringBuilder sbSql = new StringBuilder( SQL_QUERY_FIND_BY_ID_LIST );
126             if ( listIds.size( ) > 0 )
127             {
128                 sbSql.append( listPrivIds.get( 0 ) );
129                 listPrivIds.remove( 0 );
130             }
131             for ( int nId : listPrivIds )
132             {
133                 sbSql.append( CONSTANT_COMMA );
134                 sbSql.append( nId );
135             }
136             sbSql.append( CONSTANT_CLOSE_PARENTHESIS );
137             DAOUtil daoUtil = new DAOUtil( sbSql.toString( ), plugin );
138             int nIndex = 1;
139             for ( int nId : listIds )
140             {
141                 daoUtil.setInt( nIndex++, nId );
142             }
143 
144             daoUtil.executeQuery( );
145             while ( daoUtil.next( ) )
146             {
147                 FreeHtmlTopicewsletter/business/topic/FreeHtmlTopic.html#FreeHtmlTopic">FreeHtmlTopic topic = new FreeHtmlTopic( );
148                 topic.setId( daoUtil.getInt( 1 ) );
149                 topic.setHtmlContent( daoUtil.getString( 2 ) );
150                 listTopic.add( topic );
151             }
152             daoUtil.free( );
153         }
154         return listTopic;
155     }
156 }