| 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.contextinclude.business; |
| 35 | |
|
| 36 | |
import fr.paris.lutece.plugins.contextinclude.util.OperatorEnum; |
| 37 | |
import fr.paris.lutece.portal.service.plugin.Plugin; |
| 38 | |
import fr.paris.lutece.util.sql.DAOUtil; |
| 39 | |
|
| 40 | |
import java.util.ArrayList; |
| 41 | |
import java.util.List; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | public class ContextDAO implements IContextDAO |
| 50 | |
{ |
| 51 | |
private static final String SQL_QUERY_NEW_PK = " SELECT max( id_context ) FROM contextinclude_context "; |
| 52 | |
private static final String SQL_QUERY_INSERT = " INSERT INTO contextinclude_context (id_context, html, nb_params, priority, strict, active) VALUES ( ?,?,?,?,?,? ) "; |
| 53 | |
private static final String SQL_QUERY_DELETE = " DELETE FROM contextinclude_context WHERE id_context = ? "; |
| 54 | |
private static final String SQL_QUERY_SELECT = " SELECT id_context, html, nb_params, priority, strict, active FROM contextinclude_context WHERE id_context = ? "; |
| 55 | |
private static final String SQL_QUERY_SELECT_ALL = " SELECT id_context, html, nb_params, priority, strict, active FROM contextinclude_context "; |
| 56 | |
private static final String SQL_QUERY_UPDATE = " UPDATE contextinclude_context SET html = ?, nb_params = ?, priority = ?, strict = ?, active = ? WHERE id_context = ? "; |
| 57 | |
private static final String SQL_WHERE = " WHERE "; |
| 58 | |
private static final String SQL_FILTER_PRIORITY = " priority "; |
| 59 | |
private static final String SQL_FILTER_ACTIVE = " active = ? "; |
| 60 | |
private static final String SQL_ORDER_BY_PRIORITY = " ORDER BY priority ASC "; |
| 61 | |
private static final String QUESTION_MARK = " ? "; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private int newPrimaryKey( Plugin plugin ) |
| 71 | |
{ |
| 72 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ); |
| 73 | 0 | daoUtil.executeQuery( ); |
| 74 | |
|
| 75 | 0 | int nKey = 1; |
| 76 | |
|
| 77 | 0 | if ( daoUtil.next( ) ) |
| 78 | |
{ |
| 79 | 0 | nKey = daoUtil.getInt( 1 ) + 1; |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | daoUtil.free( ); |
| 83 | |
|
| 84 | 0 | return nKey; |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public synchronized int insert( Context context, Plugin plugin ) |
| 92 | |
{ |
| 93 | 0 | int nIndex = 1; |
| 94 | 0 | int nIdContext = newPrimaryKey( plugin ); |
| 95 | 0 | context.setIdContext( nIdContext ); |
| 96 | |
|
| 97 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin ); |
| 98 | 0 | daoUtil.setInt( nIndex++, context.getIdContext( ) ); |
| 99 | 0 | daoUtil.setString( nIndex++, context.getHtml( ) ); |
| 100 | 0 | daoUtil.setInt( nIndex++, context.getNbParams( ) ); |
| 101 | 0 | daoUtil.setInt( nIndex++, context.getPriority( ) ); |
| 102 | 0 | daoUtil.setBoolean( nIndex++, context.isStrict( ) ); |
| 103 | 0 | daoUtil.setBoolean( nIndex++, context.isActive( ) ); |
| 104 | |
|
| 105 | 0 | daoUtil.executeUpdate( ); |
| 106 | 0 | daoUtil.free( ); |
| 107 | |
|
| 108 | 0 | return nIdContext; |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
@Override |
| 115 | |
public void store( Context context, Plugin plugin ) |
| 116 | |
{ |
| 117 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ); |
| 118 | 0 | int nIndex = 1; |
| 119 | |
|
| 120 | 0 | daoUtil.setString( nIndex++, context.getHtml( ) ); |
| 121 | 0 | daoUtil.setInt( nIndex++, context.getNbParams( ) ); |
| 122 | 0 | daoUtil.setInt( nIndex++, context.getPriority( ) ); |
| 123 | 0 | daoUtil.setBoolean( nIndex++, context.isStrict( ) ); |
| 124 | 0 | daoUtil.setBoolean( nIndex++, context.isActive( ) ); |
| 125 | |
|
| 126 | 0 | daoUtil.setInt( nIndex++, context.getIdContext( ) ); |
| 127 | |
|
| 128 | 0 | daoUtil.executeUpdate( ); |
| 129 | 0 | daoUtil.free( ); |
| 130 | 0 | } |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
@Override |
| 136 | |
public void delete( int nIdContext, Plugin plugin ) |
| 137 | |
{ |
| 138 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ); |
| 139 | 0 | daoUtil.setInt( 1, nIdContext ); |
| 140 | |
|
| 141 | 0 | daoUtil.executeUpdate( ); |
| 142 | 0 | daoUtil.free( ); |
| 143 | 0 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
@Override |
| 149 | |
public Context load( int nIdContext, Plugin plugin ) |
| 150 | |
{ |
| 151 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ); |
| 152 | 0 | daoUtil.setInt( 1, nIdContext ); |
| 153 | 0 | daoUtil.executeQuery( ); |
| 154 | |
|
| 155 | 0 | Context context = new Context( ); |
| 156 | |
|
| 157 | 0 | int nIndex = 1; |
| 158 | |
|
| 159 | 0 | if ( daoUtil.next( ) ) |
| 160 | |
{ |
| 161 | 0 | context.setIdContext( daoUtil.getInt( nIndex++ ) ); |
| 162 | 0 | context.setHtml( daoUtil.getString( nIndex++ ) ); |
| 163 | 0 | context.setNbParams( daoUtil.getInt( nIndex++ ) ); |
| 164 | 0 | context.setPriority( daoUtil.getInt( nIndex++ ) ); |
| 165 | 0 | context.setStrict( daoUtil.getBoolean( nIndex++ ) ); |
| 166 | 0 | context.setActive( daoUtil.getBoolean( nIndex++ ) ); |
| 167 | |
} |
| 168 | |
|
| 169 | 0 | daoUtil.free( ); |
| 170 | |
|
| 171 | 0 | return context; |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
@Override |
| 178 | |
public List<Context> findAll( Plugin plugin ) |
| 179 | |
{ |
| 180 | 0 | List<Context> listContexts = new ArrayList<Context>( ); |
| 181 | 0 | DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL + SQL_ORDER_BY_PRIORITY, plugin ); |
| 182 | 0 | daoUtil.executeQuery( ); |
| 183 | |
|
| 184 | 0 | while ( daoUtil.next( ) ) |
| 185 | |
{ |
| 186 | 0 | int nIndex = 1; |
| 187 | 0 | Context context = new Context( ); |
| 188 | 0 | context.setIdContext( daoUtil.getInt( nIndex++ ) ); |
| 189 | 0 | context.setHtml( daoUtil.getString( nIndex++ ) ); |
| 190 | 0 | context.setNbParams( daoUtil.getInt( nIndex++ ) ); |
| 191 | 0 | context.setPriority( daoUtil.getInt( nIndex++ ) ); |
| 192 | 0 | context.setStrict( daoUtil.getBoolean( nIndex++ ) ); |
| 193 | 0 | context.setActive( daoUtil.getBoolean( nIndex++ ) ); |
| 194 | |
|
| 195 | 0 | listContexts.add( context ); |
| 196 | 0 | } |
| 197 | |
|
| 198 | 0 | daoUtil.free( ); |
| 199 | |
|
| 200 | 0 | return listContexts; |
| 201 | |
} |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
@Override |
| 207 | |
public List<Context> findActiveContexts( Plugin plugin ) |
| 208 | |
{ |
| 209 | 0 | List<Context> listContexts = new ArrayList<Context>( ); |
| 210 | 0 | StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ALL ); |
| 211 | 0 | sbSQL.append( SQL_WHERE ).append( SQL_FILTER_ACTIVE ); |
| 212 | 0 | sbSQL.append( SQL_ORDER_BY_PRIORITY ); |
| 213 | |
|
| 214 | 0 | DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ); |
| 215 | 0 | daoUtil.setBoolean( 1, true ); |
| 216 | 0 | daoUtil.executeQuery( ); |
| 217 | |
|
| 218 | 0 | while ( daoUtil.next( ) ) |
| 219 | |
{ |
| 220 | 0 | int nIndex = 1; |
| 221 | 0 | Context context = new Context( ); |
| 222 | 0 | context.setIdContext( daoUtil.getInt( nIndex++ ) ); |
| 223 | 0 | context.setHtml( daoUtil.getString( nIndex++ ) ); |
| 224 | 0 | context.setNbParams( daoUtil.getInt( nIndex++ ) ); |
| 225 | 0 | context.setPriority( daoUtil.getInt( nIndex++ ) ); |
| 226 | 0 | context.setStrict( daoUtil.getBoolean( nIndex++ ) ); |
| 227 | 0 | context.setActive( daoUtil.getBoolean( nIndex++ ) ); |
| 228 | |
|
| 229 | 0 | listContexts.add( context ); |
| 230 | 0 | } |
| 231 | |
|
| 232 | 0 | daoUtil.free( ); |
| 233 | |
|
| 234 | 0 | return listContexts; |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
@Override |
| 241 | |
public List<Context> findByRelativePriority( OperatorEnum operator, int nPriority, Plugin plugin ) |
| 242 | |
{ |
| 243 | 0 | List<Context> listContexts = new ArrayList<Context>( ); |
| 244 | 0 | StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ALL ); |
| 245 | 0 | sbSQL.append( SQL_WHERE ).append( SQL_FILTER_PRIORITY ); |
| 246 | 0 | sbSQL.append( operator ).append( QUESTION_MARK ); |
| 247 | 0 | sbSQL.append( SQL_ORDER_BY_PRIORITY ); |
| 248 | |
|
| 249 | 0 | DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ); |
| 250 | 0 | daoUtil.setInt( 1, nPriority ); |
| 251 | 0 | daoUtil.executeQuery( ); |
| 252 | |
|
| 253 | 0 | while ( daoUtil.next( ) ) |
| 254 | |
{ |
| 255 | 0 | int nIndex = 1; |
| 256 | 0 | Context context = new Context( ); |
| 257 | 0 | context.setIdContext( daoUtil.getInt( nIndex++ ) ); |
| 258 | 0 | context.setHtml( daoUtil.getString( nIndex++ ) ); |
| 259 | 0 | context.setNbParams( daoUtil.getInt( nIndex++ ) ); |
| 260 | 0 | context.setPriority( daoUtil.getInt( nIndex++ ) ); |
| 261 | 0 | context.setStrict( daoUtil.getBoolean( nIndex++ ) ); |
| 262 | 0 | context.setActive( daoUtil.getBoolean( nIndex++ ) ); |
| 263 | |
|
| 264 | 0 | listContexts.add( context ); |
| 265 | 0 | } |
| 266 | |
|
| 267 | 0 | daoUtil.free( ); |
| 268 | |
|
| 269 | 0 | return listContexts; |
| 270 | |
} |
| 271 | |
} |