1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
139
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
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 }