View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.quicklinks.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  /**
40   *
41   * Class EntryListboxDAO
42   *
43   */
44  public class EntrySelectDAO implements IEntrySpecificDAO
45  {
46      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT title,target FROM quicklinks_entry_select WHERE id_entry = ? ";
47      private static final String SQL_QUERY_INSERT = " INSERT INTO quicklinks_entry_select ( id_entry,title,target ) VALUES ( ?, ?, ? )";
48      private static final String SQL_QUERY_DELETE = " DELETE FROM quicklinks_entry_select WHERE id_entry = ?";
49      private static final String SQL_QUERY_UPDATE = " UPDATE quicklinks_entry_select SET title = ?, target = ? WHERE id_entry = ?";
50  
51      /**
52       * Load the data of the entry type from the table
53       *
54       * @param entry  The empty entry object
55       * @param plugin the plugin
56       * @return the instance of the EntryType
57       */
58      public IEntryef="../../../../../../fr/paris/lutece/plugins/quicklinks/business/IEntry.html#IEntry">IEntry load( IEntry entry, Plugin plugin )
59      {
60          EntrySelect entrySelect = null;
61  
62          if ( entry == null )
63          {
64              return null;
65          }
66  
67          if ( entry instanceof EntrySelect )
68          {
69              entrySelect = (EntrySelect) entry;
70          }
71          else
72          {
73              return null;
74          }
75  
76          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
77          {
78              daoUtil.setInt( 1, entrySelect.getId( ) );
79              daoUtil.executeQuery( );
80  
81              if ( daoUtil.next( ) )
82              {
83                  entrySelect.setTitle( daoUtil.getString( 1 ) );
84                  entrySelect.setTarget( daoUtil.getString( 2 ) );
85              }
86  
87          }
88  
89          return entrySelect;
90      }
91  
92      /**
93       * Deletes the {@link Entry} whose identifier is specified in parameter
94       *
95       * @param nEntryId The identifier of the {@link Entry}
96       * @param plugin   The {@link Plugin}
97       */
98      public void delete( int nEntryId, Plugin plugin )
99      {
100         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
101         {
102             daoUtil.setInt( 1, nEntryId );
103 
104             daoUtil.executeUpdate( );
105         }
106     }
107 
108     /**
109      * Insert the Entry
110      *
111      * @param entry  The {@link Entry} object
112      * @param plugin The {@link Plugin}
113      * @return The {@link Entry}
114      */
115     public IEntry="../../../../../../fr/paris/lutece/plugins/quicklinks/business/IEntry.html#IEntry">IEntry insert( IEntry entry, Plugin plugin )
116     {
117         EntrySelect entrySelect = null;
118 
119         if ( entry == null )
120         {
121             return null;
122         }
123 
124         if ( entry instanceof EntrySelect )
125         {
126             entrySelect = (EntrySelect) entry;
127         }
128         else
129         {
130             return null;
131         }
132 
133         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ) )
134         {
135             int nParam = 1;
136             daoUtil.setInt( nParam++, entrySelect.getId( ) );
137             daoUtil.setString( nParam++, entrySelect.getTitle( ) );
138             daoUtil.setString( nParam++, entrySelect.getTarget( ) );
139 
140             daoUtil.executeUpdate( );
141         }
142 
143         return entry;
144     }
145 
146     /**
147      * Update the {@link Entry}
148      *
149      * @param entry  The {@link Entry} object
150      * @param plugin The {@link Plugin}
151      */
152     public void store( IEntry entry, Plugin plugin )
153     {
154         EntrySelect entrySelect = null;
155 
156         if ( entry == null )
157         {
158             return;
159         }
160 
161         if ( entry instanceof EntrySelect )
162         {
163             entrySelect = (EntrySelect) entry;
164         }
165         else
166         {
167             return;
168         }
169 
170         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
171         {
172 
173             int nParam = 1;
174             daoUtil.setString( nParam++, entrySelect.getTitle( ) );
175             daoUtil.setString( nParam++, entrySelect.getTarget( ) );
176             daoUtil.setInt( nParam++, entrySelect.getId( ) );
177             daoUtil.executeUpdate( );
178         }
179     }
180 }