Coverage Report - fr.paris.lutece.plugins.releaser.business.ClusterDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
ClusterDAO
80 %
56/70
50 %
5/10
1,625
 
 1  
 /*
 2  
  * Copyright (c) 2002-2017, 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  
 
 35  
 package fr.paris.lutece.plugins.releaser.business;
 36  
 
 37  
 import fr.paris.lutece.portal.service.plugin.Plugin;
 38  
 import fr.paris.lutece.util.ReferenceList;
 39  
 import fr.paris.lutece.util.sql.DAOUtil;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.List;
 43  
 
 44  
 /**
 45  
  * This class provides Data Access methods for Cluster objects
 46  
  */
 47  1
 public final class ClusterDAO implements IClusterDAO
 48  
 {
 49  
     // Constants
 50  
     private static final String SQL_QUERY_NEW_PK = "SELECT max( id_cluster ) FROM releaser_cluster";
 51  
     private static final String SQL_QUERY_SELECT = "SELECT id_cluster, name, description FROM releaser_cluster WHERE id_cluster = ?";
 52  
     private static final String SQL_QUERY_INSERT = "INSERT INTO releaser_cluster ( id_cluster, name, description ) VALUES ( ?, ?, ? ) ";
 53  
     private static final String SQL_QUERY_DELETE = "DELETE FROM releaser_cluster WHERE id_cluster = ? ";
 54  
     private static final String SQL_QUERY_UPDATE = "UPDATE releaser_cluster SET id_cluster = ?, name = ?, description = ? WHERE id_cluster = ?";
 55  
     private static final String SQL_QUERY_SELECTALL = "SELECT id_cluster, name, description FROM releaser_cluster";
 56  
     private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_cluster FROM releaser_cluster";
 57  
 
 58  
     /**
 59  
      * Generates a new primary key
 60  
      * 
 61  
      * @param plugin
 62  
      *            The Plugin
 63  
      * @return The new primary key
 64  
      */
 65  
     public int newPrimaryKey( Plugin plugin )
 66  
     {
 67  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
 68  1
         daoUtil.executeQuery( );
 69  1
         int nKey = 1;
 70  
 
 71  1
         if ( daoUtil.next( ) )
 72  
         {
 73  1
             nKey = daoUtil.getInt( 1 ) + 1;
 74  
         }
 75  
 
 76  1
         daoUtil.free( );
 77  1
         return nKey;
 78  
     }
 79  
 
 80  
     /**
 81  
      * {@inheritDoc }
 82  
      */
 83  
     @Override
 84  
     public void insert( Cluster cluster, Plugin plugin )
 85  
     {
 86  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
 87  1
         cluster.setId( newPrimaryKey( plugin ) );
 88  1
         int nIndex = 1;
 89  
 
 90  1
         daoUtil.setInt( nIndex++, cluster.getId( ) );
 91  1
         daoUtil.setString( nIndex++, cluster.getName( ) );
 92  1
         daoUtil.setString( nIndex++, cluster.getDescription( ) );
 93  
 
 94  1
         daoUtil.executeUpdate( );
 95  1
         daoUtil.free( );
 96  1
     }
 97  
 
 98  
     /**
 99  
      * {@inheritDoc }
 100  
      */
 101  
     @Override
 102  
     public Cluster load( int nKey, Plugin plugin )
 103  
     {
 104  3
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
 105  3
         daoUtil.setInt( 1, nKey );
 106  3
         daoUtil.executeQuery( );
 107  3
         Cluster cluster = null;
 108  
 
 109  3
         if ( daoUtil.next( ) )
 110  
         {
 111  2
             cluster = new Cluster( );
 112  2
             int nIndex = 1;
 113  
 
 114  2
             cluster.setId( daoUtil.getInt( nIndex++ ) );
 115  2
             cluster.setName( daoUtil.getString( nIndex++ ) );
 116  2
             cluster.setDescription( daoUtil.getString( nIndex++ ) );
 117  
         }
 118  
 
 119  3
         daoUtil.free( );
 120  3
         return cluster;
 121  
     }
 122  
 
 123  
     /**
 124  
      * {@inheritDoc }
 125  
      */
 126  
     @Override
 127  
     public void delete( int nKey, Plugin plugin )
 128  
     {
 129  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
 130  1
         daoUtil.setInt( 1, nKey );
 131  1
         daoUtil.executeUpdate( );
 132  1
         daoUtil.free( );
 133  1
     }
 134  
 
 135  
     /**
 136  
      * {@inheritDoc }
 137  
      */
 138  
     @Override
 139  
     public void store( Cluster cluster, Plugin plugin )
 140  
     {
 141  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
 142  1
         int nIndex = 1;
 143  
 
 144  1
         daoUtil.setInt( nIndex++, cluster.getId( ) );
 145  1
         daoUtil.setString( nIndex++, cluster.getName( ) );
 146  1
         daoUtil.setString( nIndex++, cluster.getDescription( ) );
 147  1
         daoUtil.setInt( nIndex, cluster.getId( ) );
 148  
 
 149  1
         daoUtil.executeUpdate( );
 150  1
         daoUtil.free( );
 151  1
     }
 152  
 
 153  
     /**
 154  
      * {@inheritDoc }
 155  
      */
 156  
     @Override
 157  
     public List<Cluster> selectClustersList( Plugin plugin )
 158  
     {
 159  1
         List<Cluster> clusterList = new ArrayList<Cluster>( );
 160  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
 161  1
         daoUtil.executeQuery( );
 162  
 
 163  4
         while ( daoUtil.next( ) )
 164  
         {
 165  3
             Cluster cluster = new Cluster( );
 166  3
             int nIndex = 1;
 167  
 
 168  3
             cluster.setId( daoUtil.getInt( nIndex++ ) );
 169  3
             cluster.setName( daoUtil.getString( nIndex++ ) );
 170  3
             cluster.setDescription( daoUtil.getString( nIndex++ ) );
 171  
 
 172  3
             clusterList.add( cluster );
 173  3
         }
 174  
 
 175  1
         daoUtil.free( );
 176  1
         return clusterList;
 177  
     }
 178  
 
 179  
     /**
 180  
      * {@inheritDoc }
 181  
      */
 182  
     @Override
 183  
     public List<Integer> selectIdClustersList( Plugin plugin )
 184  
     {
 185  0
         List<Integer> clusterList = new ArrayList<Integer>( );
 186  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
 187  0
         daoUtil.executeQuery( );
 188  
 
 189  0
         while ( daoUtil.next( ) )
 190  
         {
 191  0
             clusterList.add( daoUtil.getInt( 1 ) );
 192  
         }
 193  
 
 194  0
         daoUtil.free( );
 195  0
         return clusterList;
 196  
     }
 197  
 
 198  
     /**
 199  
      * {@inheritDoc }
 200  
      */
 201  
     @Override
 202  
     public ReferenceList selectClustersReferenceList( Plugin plugin )
 203  
     {
 204  0
         ReferenceList clusterList = new ReferenceList( );
 205  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
 206  0
         daoUtil.executeQuery( );
 207  
 
 208  0
         while ( daoUtil.next( ) )
 209  
         {
 210  0
             clusterList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
 211  
         }
 212  
 
 213  0
         daoUtil.free( );
 214  0
         return clusterList;
 215  
     }
 216  
 }