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.formengine.business.mail;
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   * This class provides Data Access methods for MailConfiguration objects
45   */
46  public final class MailConfigurationDAO implements IMailConfigurationDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT form, senderName, senderMail, object FROM formengine_mail_configuration WHERE form = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO formengine_mail_configuration ( form, senderName, senderMail, object ) VALUES ( ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM formengine_mail_configuration WHERE form = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE formengine_mail_configuration SET form = ?, senderName = ?, senderMail = ?, object = ? WHERE form = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT form, senderName, senderMail, object FROM formengine_mail_configuration";
54  
55      /**
56       * Insert a new record in the table.
57       * @param mailConfiguration instance of the MailConfiguration object to insert
58       * @param plugin The plugin
59       */
60      public void insert( MailConfiguration mailConfiguration, Plugin plugin )
61      {
62          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
63  
64          daoUtil.setString( 1, mailConfiguration.getForm(  ) );
65          daoUtil.setString( 2, mailConfiguration.getSenderName(  ) );
66          daoUtil.setString( 3, mailConfiguration.getSenderMail(  ) );
67          daoUtil.setString( 4, mailConfiguration.getObject(  ) );
68  
69          daoUtil.executeUpdate(  );
70          daoUtil.free(  );
71      }
72  
73      /**
74       * Load the data of the mailConfiguration from the table
75       * @param strId The identifier of the mailConfiguration
76       * @param plugin The plugin
77       * @return the instance of the MailConfiguration
78       */
79      public MailConfiguration load( String strId, Plugin plugin )
80      {
81          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
82          daoUtil.setString( 1, strId );
83          daoUtil.executeQuery(  );
84  
85          MailConfiguration mailConfiguration = null;
86  
87          if ( daoUtil.next(  ) )
88          {
89              mailConfiguration = new MailConfiguration(  );
90  
91              mailConfiguration.setForm( daoUtil.getString( 1 ) );
92              mailConfiguration.setSenderName( daoUtil.getString( 2 ) );
93              mailConfiguration.setSenderMail( daoUtil.getString( 3 ) );
94              mailConfiguration.setObject( daoUtil.getString( 4 ) );
95          }
96  
97          daoUtil.free(  );
98  
99          return mailConfiguration;
100     }
101 
102     /**
103      * Delete a record from the table
104      * @param strForm The identifier of the mailConfiguration
105      * @param plugin The plugin
106      */
107     public void delete( String strForm, Plugin plugin )
108     {
109         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
110         daoUtil.setString( 1, strForm );
111         daoUtil.executeUpdate(  );
112         daoUtil.free(  );
113     }
114 
115     /**
116      * Update the record in the table
117      * @param mailConfiguration The reference of the mailConfiguration
118      * @param plugin The plugin
119      */
120     public void store( MailConfiguration mailConfiguration, Plugin plugin )
121     {
122         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
123 
124         daoUtil.setString( 1, mailConfiguration.getForm(  ) );
125         daoUtil.setString( 2, mailConfiguration.getSenderName(  ) );
126         daoUtil.setString( 3, mailConfiguration.getSenderMail(  ) );
127         daoUtil.setString( 4, mailConfiguration.getObject(  ) );
128         daoUtil.setString( 5, mailConfiguration.getForm(  ) );
129 
130         daoUtil.executeUpdate(  );
131         daoUtil.free(  );
132     }
133 
134     /**
135      * Load the data of all the mailConfigurations and returns them as a List
136      * @param plugin The plugin
137      * @return The List which contains the data of all the mailConfigurations
138      */
139     public List<MailConfiguration> selectMailConfigurationsList( Plugin plugin )
140     {
141         List<MailConfiguration> mailConfigurationList = new ArrayList<MailConfiguration>(  );
142         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
143         daoUtil.executeQuery(  );
144 
145         while ( daoUtil.next(  ) )
146         {
147             MailConfiguration mailConfiguration = new MailConfiguration(  );
148 
149             mailConfiguration.setForm( daoUtil.getString( 1 ) );
150             mailConfiguration.setSenderName( daoUtil.getString( 2 ) );
151             mailConfiguration.setSenderMail( daoUtil.getString( 3 ) );
152             mailConfiguration.setObject( daoUtil.getString( 4 ) );
153 
154             mailConfigurationList.add( mailConfiguration );
155         }
156 
157         daoUtil.free(  );
158 
159         return mailConfigurationList;
160     }
161 }