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.phraseanet.business.template;
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 Account objects
45   */
46  public final class TemplateDAO implements ITemplateDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_template ) FROM phraseanet_template";
50      private static final String SQL_QUERY_SELECT = "SELECT id_template, name, default_template, media_type FROM phraseanet_template WHERE media_type = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO phraseanet_template ( id_template, name, default_template, media_type ) VALUES ( ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM phraseanet_template WHERE id_template = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE phraseanet_template SET name = ?, default_template = ? WHERE id_template = ?";
54      private static final String SQL_QUERY_SELECTALL = "SELECT id_template, name, default_template, media_type FROM phraseanet_template";
55  
56      /**
57       * Generates a new primary key
58       * @param plugin The Plugin
59       * @return The new primary key
60       */
61      public int newPrimaryKey( Plugin plugin )
62      {
63          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
64          daoUtil.executeQuery(  );
65  
66          int nKey;
67  
68          if ( !daoUtil.next(  ) )
69          {
70              // if the table is empty
71              nKey = 1;
72          }
73  
74          nKey = daoUtil.getInt( 1 ) + 1;
75          daoUtil.free(  );
76  
77          return nKey;
78      }
79  
80      /**
81       * Insert a new record in the table.
82       * @param template instance of the Template object to insert
83       * @param plugin The plugin
84       */
85      public void insert( Template template, Plugin plugin )
86      {
87          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
88  
89          template.setId( newPrimaryKey( plugin ) );
90  
91          daoUtil.setInt( 1, template.getId(  ) );
92          daoUtil.setString( 2, template.getName(  ) );
93          daoUtil.setString( 3, template.getTemplate(  ) );
94  
95          daoUtil.executeUpdate(  );
96          daoUtil.free(  );
97      }
98  
99      /**
100      * Load the data of the Template from the table
101      * @param strMediaType The media type
102      * @param plugin The plugin
103      * @return the instance of the Template
104      */
105     public Template load( String strMediaType, Plugin plugin )
106     {
107         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
108         daoUtil.setString( 1, strMediaType );
109         daoUtil.executeQuery(  );
110 
111         Template template = null;
112 
113         if ( daoUtil.next(  ) )
114         {
115             template = new Template(  );
116 
117             template.setId( daoUtil.getInt( 1 ) );
118             template.setName( daoUtil.getString( 2 ) );
119             template.setTemplate( daoUtil.getString( 3 ) );
120             template.setMediaType( daoUtil.getString( 4 ) );
121         }
122 
123         daoUtil.free(  );
124 
125         return template;
126     }
127 
128     /**
129      * Delete a record from the table
130      * @param nTemplateId The identifier of the Template
131      * @param plugin The plugin
132      */
133     public void delete( int nTemplateId, Plugin plugin )
134     {
135         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
136         daoUtil.setInt( 1, nTemplateId );
137         daoUtil.executeUpdate(  );
138         daoUtil.free(  );
139     }
140 
141     /**
142      * Update the record in the table
143      * @param template The reference of the Template
144      * @param plugin The plugin
145      */
146     public void store( Template template, Plugin plugin )
147     {
148         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
149 
150         daoUtil.setString( 1, template.getName(  ) );
151         daoUtil.setString( 2, template.getTemplate(  ) );
152         daoUtil.setInt( 3, template.getId(  ) );
153 
154         daoUtil.executeUpdate(  );
155         daoUtil.free(  );
156     }
157 
158     /**
159      * Load the data of all the Templates and returns them as a List
160      * @param plugin The plugin
161      * @return The List which contains the data of all the Templates
162      */
163     public List<Template> selectTemplatesList( Plugin plugin )
164     {
165         List<Template> templateList = new ArrayList<Template>(  );
166         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
167         daoUtil.executeQuery(  );
168 
169         while ( daoUtil.next(  ) )
170         {
171             Template template = new Template(  );
172 
173             template.setId( daoUtil.getInt( 1 ) );
174             template.setName( daoUtil.getString( 2 ) );
175             template.setTemplate( daoUtil.getString( 3 ) );
176 
177             templateList.add( template );
178         }
179 
180         daoUtil.free(  );
181 
182         return templateList;
183     }
184 }