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 MediaAttributeDAO implements IMediaAttributeDAO
44  {
45      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_attribute ) FROM library_media_attribute ";
46      private static final String SQL_QUERY_REMOVE_ATTRIBUTE = "DELETE FROM library_media_attribute WHERE id_attribute= ?";
47      private static final String SQL_QUERY_INSERT_ATTRIBUTE = "INSERT INTO library_media_attribute (id_attribute, id_media ,code ,description, type, default_value) VALUES ( ?, ? , ? , ?, ?, ? )";
48      private static final String SQL_QUERY_SELECT_ATTRIBUTE = "SELECT * FROM library_media_attribute WHERE id_attribute= ?";
49      private static final String SQL_QUERY_SELECT_ALL_FOR_MEDIA = "SELECT * FROM library_media_attribute WHERE id_media = ?";
50      private static final String SQL_QUERY_UPDATE_ATTRIBUTE = "UPDATE library_media_attribute SET id_media=?, code=?, description=?, type=?, default_value=? WHERE id_attribute=?";
51      private static final String SQL_QUERY_REMOVE_ALL_ATTRIBUTE_FOR_MEDIA = "DELETE FROM library_media_attribute WHERE id_media = ?";
52  
53      public void delete( int nAttributeId, Plugin plugin )
54      {
55          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REMOVE_ATTRIBUTE, plugin );
56          daoUtil.setInt( 1, nAttributeId );
57          daoUtil.executeUpdate(  );
58          daoUtil.free(  );
59      }
60  
61      public void insert( MediaAttribute attribute, Plugin plugin )
62      {
63          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_ATTRIBUTE, plugin );
64  
65          daoUtil.setInt( 1, newPrimaryKey( plugin ) );
66          daoUtil.setInt( 2, attribute.getMediaId(  ) );
67          daoUtil.setString( 3, attribute.getCode(  ) );
68          daoUtil.setString( 4, attribute.getDescription(  ) );
69          daoUtil.setInt( 5, attribute.getTypeId(  ) );
70          daoUtil.setString( 6, attribute.getDefaultValue(  ) );
71          daoUtil.executeUpdate(  );
72          daoUtil.free(  );
73      }
74  
75      public MediaAttribute load( int nAttributeId, Plugin plugin )
76      {
77          MediaAttribute attribute = null;
78          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ATTRIBUTE, plugin );
79          daoUtil.setInt( 1, nAttributeId );
80          daoUtil.executeQuery(  );
81  
82          if ( daoUtil.next(  ) )
83          {
84              attribute = new MediaAttribute(  );
85              attribute.setAttributeId( daoUtil.getInt( 1 ) );
86              attribute.setMediaId( daoUtil.getInt( 2 ) );
87              attribute.setCode( daoUtil.getString( 3 ) );
88              attribute.setDescription( daoUtil.getString( 4 ) );
89              attribute.setTypeId( daoUtil.getInt( 5 ) );
90              attribute.setDefaultValue( daoUtil.getString( 6 ) );
91          }
92  
93          daoUtil.free(  );
94  
95          return attribute;
96      }
97  
98      public Collection<MediaAttribute> selectAllAttributesForMedia( int nIdMedia, Plugin plugin )
99      {
100         Collection<MediaAttribute> colAttributes = new ArrayList<MediaAttribute>(  );
101         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_FOR_MEDIA, plugin );
102         daoUtil.setInt( 1, nIdMedia );
103         daoUtil.executeQuery(  );
104 
105         while ( daoUtil.next(  ) )
106         {
107             MediaAttributeusiness/MediaAttribute.html#MediaAttribute">MediaAttribute attribute = new MediaAttribute(  );
108             attribute.setAttributeId( daoUtil.getInt( 1 ) );
109             attribute.setMediaId( daoUtil.getInt( 2 ) );
110             attribute.setCode( daoUtil.getString( 3 ) );
111             attribute.setDescription( daoUtil.getString( 4 ) );
112             attribute.setTypeId( daoUtil.getInt( 5 ) );
113             attribute.setDefaultValue( daoUtil.getString( 6 ) );
114             colAttributes.add( attribute );
115         }
116 
117         daoUtil.free(  );
118 
119         return colAttributes;
120     }
121 
122     public void store( MediaAttribute attribute, Plugin plugin )
123     {
124         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ATTRIBUTE, plugin );
125         daoUtil.setInt( 1, attribute.getMediaId(  ) );
126         daoUtil.setString( 2, attribute.getCode(  ) );
127         daoUtil.setString( 3, attribute.getDescription(  ) );
128         daoUtil.setInt( 4, attribute.getTypeId(  ) );
129         daoUtil.setString( 5, attribute.getDefaultValue(  ) );
130 
131         daoUtil.setInt( 6, attribute.getAttributeId(  ) );
132 
133         daoUtil.executeUpdate(  );
134         daoUtil.free(  );
135     }
136 
137     /**
138      * Generates a new primary key
139      * @return The new primary key
140      */
141     public int newPrimaryKey( Plugin plugin )
142     {
143         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
144         daoUtil.executeQuery(  );
145 
146         int nKey;
147 
148         if ( !daoUtil.next(  ) )
149         {
150             // if the table is empty
151             nKey = 1;
152         }
153 
154         nKey = daoUtil.getInt( 1 ) + 1;
155         daoUtil.free(  );
156 
157         return nKey;
158     }
159 
160     public void deleteAllForMedia( int nMediaId, Plugin plugin )
161     {
162         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REMOVE_ALL_ATTRIBUTE_FOR_MEDIA, plugin );
163         daoUtil.setInt( 1, nMediaId );
164         daoUtil.executeUpdate(  );
165         daoUtil.free(  );
166     }
167 }