Coverage Report - fr.paris.lutece.plugins.releaser.business.SiteDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteDAO
87 %
98/112
58 %
7/12
1,667
 
 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 Site objects
 46  
  */
 47  1
 public final class SiteDAO implements ISiteDAO
 48  
 {
 49  
     // Constants
 50  
     private static final String SQL_QUERY_NEW_PK = "SELECT max( id_site ) FROM releaser_site";
 51  
     private static final String SQL_QUERY_SELECT = "SELECT a.id_site, a.name, a.description, a.artifact_id, a.id_cluster, b.name, a.scm_url, a.jira_key,a.is_theme "
 52  
             + " FROM releaser_site a , releaser_cluster b  WHERE a.id_site = ? AND a.id_cluster = b.id_cluster";
 53  
     private static final String SQL_QUERY_INSERT = "INSERT INTO releaser_site ( id_site, artifact_id, id_cluster, scm_url, name, description, jira_key,is_theme ) VALUES ( ?, ?, ?, ?, ?, ?, ? , ?) ";
 54  
     private static final String SQL_QUERY_DELETE = "DELETE FROM releaser_site WHERE id_site = ? ";
 55  
     private static final String SQL_QUERY_UPDATE = "UPDATE releaser_site SET id_site = ?, artifact_id = ?, id_cluster = ?, scm_url = ?, name = ?, description = ?, jira_key = ?,is_theme= ?  WHERE id_site = ?";
 56  
     private static final String SQL_QUERY_SELECTALL = "SELECT a.id_site, a.name, a.description, a.artifact_id, a.id_cluster, b.name, a.scm_url, a.jira_key,a.is_theme "
 57  
             + " FROM releaser_site a , releaser_cluster b  WHERE a.id_cluster = b.id_cluster";
 58  
     private static final String SQL_QUERY_SELECT_BY_CLUSTER = "SELECT a.id_site, a.name, a.description, a.artifact_id, a.id_cluster, b.name, a.scm_url, a.jira_key,a.is_theme "
 59  
             + " FROM releaser_site a , releaser_cluster b  WHERE a.id_cluster = b.id_cluster AND a.id_cluster = ?";
 60  
     private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_site FROM releaser_site";
 61  
 
 62  
     /**
 63  
      * Generates a new primary key
 64  
      * 
 65  
      * @param plugin
 66  
      *            The Plugin
 67  
      * @return The new primary key
 68  
      */
 69  
     public int newPrimaryKey( Plugin plugin )
 70  
     {
 71  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
 72  1
         daoUtil.executeQuery( );
 73  1
         int nKey = 1;
 74  
 
 75  1
         if ( daoUtil.next( ) )
 76  
         {
 77  1
             nKey = daoUtil.getInt( 1 ) + 1;
 78  
         }
 79  
 
 80  1
         daoUtil.free( );
 81  1
         return nKey;
 82  
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc }
 86  
      */
 87  
     @Override
 88  
     public void insert( Site site, Plugin plugin )
 89  
     {
 90  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
 91  1
         site.setId( newPrimaryKey( plugin ) );
 92  1
         int nIndex = 1;
 93  
 
 94  1
         daoUtil.setInt( nIndex++, site.getId( ) );
 95  1
         daoUtil.setString( nIndex++, site.getArtifactId( ) );
 96  1
         daoUtil.setInt( nIndex++, site.getIdCluster( ) );
 97  1
         daoUtil.setString( nIndex++, site.getScmUrl( ) );
 98  1
         daoUtil.setString( nIndex++, site.getName( ) );
 99  1
         daoUtil.setString( nIndex++, site.getDescription( ) );
 100  1
         daoUtil.setString( nIndex++, site.getJiraKey( ) );
 101  1
         daoUtil.setBoolean( nIndex++, site.isTheme( ) );
 102  
 
 103  1
         daoUtil.executeUpdate( );
 104  1
         daoUtil.free( );
 105  1
     }
 106  
 
 107  
     /**
 108  
      * {@inheritDoc }
 109  
      */
 110  
     @Override
 111  
     public Site load( int nKey, Plugin plugin )
 112  
     {
 113  3
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
 114  3
         daoUtil.setInt( 1, nKey );
 115  3
         daoUtil.executeQuery( );
 116  3
         Site site = null;
 117  
 
 118  3
         if ( daoUtil.next( ) )
 119  
         {
 120  2
             site = new Site( );
 121  2
             int nIndex = 1;
 122  
 
 123  2
             site.setId( daoUtil.getInt( nIndex++ ) );
 124  2
             site.setName( daoUtil.getString( nIndex++ ) );
 125  2
             site.setDescription( daoUtil.getString( nIndex++ ) );
 126  2
             site.setArtifactId( daoUtil.getString( nIndex++ ) );
 127  2
             site.setIdCluster( daoUtil.getInt( nIndex++ ) );
 128  2
             site.setCluster( daoUtil.getString( nIndex++ ) );
 129  2
             site.setScmUrl( daoUtil.getString( nIndex++ ) );
 130  2
             site.setJiraKey( daoUtil.getString( nIndex++ ) );
 131  2
             site.setTheme( daoUtil.getBoolean( nIndex++  ) );
 132  
         }
 133  
 
 134  3
         daoUtil.free( );
 135  3
         return site;
 136  
 
 137  
     }
 138  
 
 139  
     /**
 140  
      * {@inheritDoc }
 141  
      */
 142  
     @Override
 143  
     public void delete( int nKey, Plugin plugin )
 144  
     {
 145  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
 146  1
         daoUtil.setInt( 1, nKey );
 147  1
         daoUtil.executeUpdate( );
 148  1
         daoUtil.free( );
 149  1
     }
 150  
 
 151  
     /**
 152  
      * {@inheritDoc }
 153  
      */
 154  
     @Override
 155  
     public void store( Site site, Plugin plugin )
 156  
     {
 157  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
 158  1
         int nIndex = 1;
 159  
 
 160  1
         daoUtil.setInt( nIndex++, site.getId( ) );
 161  1
         daoUtil.setString( nIndex++, site.getArtifactId( ) );
 162  1
         daoUtil.setInt( nIndex++, site.getIdCluster( ) );
 163  1
         daoUtil.setString( nIndex++, site.getScmUrl( ) );
 164  1
         daoUtil.setString( nIndex++, site.getName( ) );
 165  1
         daoUtil.setString( nIndex++, site.getDescription( ) );
 166  1
         daoUtil.setString( nIndex++, site.getJiraKey( ) );
 167  1
         daoUtil.setBoolean( nIndex++, site.isTheme( ) );
 168  
         
 169  1
         daoUtil.setInt( nIndex, site.getId( ) );
 170  
 
 171  1
         daoUtil.executeUpdate( );
 172  1
         daoUtil.free( );
 173  1
     }
 174  
 
 175  
     /**
 176  
      * {@inheritDoc }
 177  
      */
 178  
     @Override
 179  
     public List<Site> selectSitesList( Plugin plugin )
 180  
     {
 181  1
         List<Site> siteList = new ArrayList<Site>( );
 182  1
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
 183  1
         daoUtil.executeQuery( );
 184  
 
 185  8
         while ( daoUtil.next( ) )
 186  
         {
 187  7
             Site site = new Site( );
 188  7
             int nIndex = 1;
 189  
 
 190  7
             site.setId( daoUtil.getInt( nIndex++ ) );
 191  7
             site.setName( daoUtil.getString( nIndex++ ) );
 192  7
             site.setDescription( daoUtil.getString( nIndex++ ) );
 193  7
             site.setArtifactId( daoUtil.getString( nIndex++ ) );
 194  7
             site.setIdCluster( daoUtil.getInt( nIndex++ ) );
 195  7
             site.setCluster( daoUtil.getString( nIndex++ ) );
 196  7
             site.setScmUrl( daoUtil.getString( nIndex++ ) );
 197  7
             site.setJiraKey( daoUtil.getString( nIndex++ ) );
 198  7
             site.setTheme( daoUtil.getBoolean( nIndex++  ) );
 199  
 
 200  7
             siteList.add( site );
 201  7
         }
 202  
 
 203  1
         daoUtil.free( );
 204  1
         return siteList;
 205  
     }
 206  
 
 207  
     /**
 208  
      * {@inheritDoc }
 209  
      */
 210  
     @Override
 211  
     public List<Integer> selectIdSitesList( Plugin plugin )
 212  
     {
 213  0
         List<Integer> siteList = new ArrayList<Integer>( );
 214  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
 215  0
         daoUtil.executeQuery( );
 216  
 
 217  0
         while ( daoUtil.next( ) )
 218  
         {
 219  0
             siteList.add( daoUtil.getInt( 1 ) );
 220  
         }
 221  
 
 222  0
         daoUtil.free( );
 223  0
         return siteList;
 224  
     }
 225  
 
 226  
     /**
 227  
      * {@inheritDoc }
 228  
      */
 229  
     @Override
 230  
     public ReferenceList selectSitesReferenceList( Plugin plugin )
 231  
     {
 232  0
         ReferenceList siteList = new ReferenceList( );
 233  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
 234  0
         daoUtil.executeQuery( );
 235  
 
 236  0
         while ( daoUtil.next( ) )
 237  
         {
 238  0
             siteList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 5 ) );
 239  
         }
 240  
 
 241  0
         daoUtil.free( );
 242  0
         return siteList;
 243  
     }
 244  
 
 245  
     /**
 246  
      * {@inheritDoc }
 247  
      */
 248  
     @Override
 249  
     public List<Site> selectByCluster( int nClusterId, Plugin plugin )
 250  
     {
 251  5
         List<Site> siteList = new ArrayList<Site>( );
 252  5
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CLUSTER, plugin );
 253  5
         daoUtil.setInt( 1, nClusterId );
 254  5
         daoUtil.executeQuery( );
 255  
 
 256  11
         while ( daoUtil.next( ) )
 257  
         {
 258  6
             Site site = new Site( );
 259  6
             int nIndex = 1;
 260  
 
 261  6
             site.setId( daoUtil.getInt( nIndex++ ) );
 262  6
             site.setName( daoUtil.getString( nIndex++ ) );
 263  6
             site.setDescription( daoUtil.getString( nIndex++ ) );
 264  6
             site.setArtifactId( daoUtil.getString( nIndex++ ) );
 265  6
             site.setIdCluster( daoUtil.getInt( nIndex++ ) );
 266  6
             site.setCluster( daoUtil.getString( nIndex++ ) );
 267  6
             site.setScmUrl( daoUtil.getString( nIndex++ ) );
 268  6
             site.setJiraKey( daoUtil.getString( nIndex++ ) );
 269  6
             site.setTheme( daoUtil.getBoolean( nIndex++  ) );
 270  
 
 271  6
             siteList.add( site );
 272  6
         }
 273  
 
 274  5
         daoUtil.free( );
 275  5
         return siteList;
 276  
     }
 277  
 }