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.library.business;
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.Collection;
41  
42  
43  public class LibraryMediaDAO implements ILibraryMediaDAO
44  {
45      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_media ) FROM library_media ";
46      private static final String SQL_QUERY_REMOVE_MEDIA = "DELETE FROM library_media WHERE id_media= ?";
47      private static final String SQL_QUERY_INSERT_MEDIA = "INSERT INTO library_media (id_media ,name ,description, is_multiple_media, stylesheet) VALUES ( ? , ? , ? , ? , ? )";
48      private static final String SQL_QUERY_SELECT_MEDIA = "SELECT id_media, name, description, is_multiple_media, stylesheet FROM library_media WHERE id_media= ?";
49      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_media, name, description, is_multiple_media FROM library_media";
50      private static final String SQL_QUERY_UPDATE_MEDIA = "UPDATE library_media SET name=?, description=?, is_multiple_media = ?, stylesheet=? WHERE id_media=?";
51  
52      public void delete( int nMediaId, Plugin plugin )
53      {
54          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REMOVE_MEDIA, plugin );
55          daoUtil.setInt( 1, nMediaId );
56          daoUtil.executeUpdate(  );
57          daoUtil.free(  );
58      }
59  
60      public void insert( LibraryMedia media, Plugin plugin )
61      {
62          media.setMediaId( newPrimaryKey( plugin ) );
63  
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_MEDIA, plugin );
65  
66          daoUtil.setInt( 1, media.getMediaId(  ) );
67          daoUtil.setString( 2, media.getName(  ) );
68          daoUtil.setString( 3, media.getDescription(  ) );
69          daoUtil.setBoolean( 4, media.getIsMultipleMedia(  ) );
70          daoUtil.setBytes( 5, media.getStyleSheet(  ).getSource(  ) );
71          daoUtil.executeUpdate(  );
72          daoUtil.free(  );
73      }
74  
75      public LibraryMedia load( int nMediaId, Plugin plugin )
76      {
77          LibraryMedia media = null;
78          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MEDIA, plugin );
79          daoUtil.setInt( 1, nMediaId );
80          daoUtil.executeQuery(  );
81  
82          if ( daoUtil.next(  ) )
83          {
84              media = new LibraryMedia(  );
85              media.setMediaId( daoUtil.getInt( 1 ) );
86              media.setName( daoUtil.getString( 2 ) );
87              media.setDescription( daoUtil.getString( 3 ) );
88              media.setIsMultipleMedia( daoUtil.getBoolean( 4 ) );
89              media.setStyleSheetBytes( daoUtil.getBytes( 5 ) );
90          }
91  
92          daoUtil.free(  );
93  
94          return media;
95      }
96  
97      public Collection<LibraryMedia> selectAll( Plugin plugin )
98      {
99          Collection<LibraryMedia> colMedia = new ArrayList<LibraryMedia>(  );
100         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
101         daoUtil.executeQuery(  );
102 
103         while ( daoUtil.next(  ) )
104         {
105             LibraryMediarary/business/LibraryMedia.html#LibraryMedia">LibraryMedia media = new LibraryMedia(  );
106             media.setMediaId( daoUtil.getInt( 1 ) );
107             media.setName( daoUtil.getString( 2 ) );
108             media.setDescription( daoUtil.getString( 3 ) );
109             media.setIsMultipleMedia( daoUtil.getBoolean( 4 ) );
110             colMedia.add( media );
111         }
112 
113         daoUtil.free(  );
114 
115         return colMedia;
116     }
117 
118     public void store( LibraryMedia media, Plugin plugin )
119     {
120         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_MEDIA, plugin );
121         daoUtil.setString( 1, media.getName(  ) );
122         daoUtil.setString( 2, media.getDescription(  ) );
123         daoUtil.setBoolean( 3, media.getIsMultipleMedia(  ) );
124         daoUtil.setBytes( 4, media.getStyleSheet(  ).getSource(  ) );
125         daoUtil.setInt( 5, media.getMediaId(  ) );
126         daoUtil.executeUpdate(  );
127         daoUtil.free(  );
128     }
129 
130     /**
131      * Generates a new primary key
132      * @param plugin The plugin
133      * @return The new primary key
134      */
135     public int newPrimaryKey( Plugin plugin )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
138         daoUtil.executeQuery(  );
139 
140         int nKey;
141 
142         if ( !daoUtil.next(  ) )
143         {
144             // if the table is empty
145             nKey = 1;
146         }
147 
148         nKey = daoUtil.getInt( 1 ) + 1;
149         daoUtil.free(  );
150 
151         return nKey;
152     }
153 }