Coverage Report - fr.paris.lutece.plugins.dila.business.fichelocale.dao.impl.LocalDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalDAO
0 %
0/141
0 %
0/18
1,9
 
 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.dila.business.fichelocale.dao.impl;
 35  
 
 36  
 import fr.paris.lutece.plugins.dila.business.fichelocale.dao.ILocalDAO;
 37  
 import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalDTO;
 38  
 import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalTypeDTO;
 39  
 import fr.paris.lutece.plugins.dila.service.DilaLocalTypeEnum;
 40  
 import fr.paris.lutece.plugins.dila.service.DilaPlugin;
 41  
 import fr.paris.lutece.plugins.dila.utils.DilaUtils;
 42  
 import fr.paris.lutece.portal.service.plugin.PluginService;
 43  
 import fr.paris.lutece.util.sql.DAOUtil;
 44  
 
 45  
 import java.io.Serializable;
 46  
 import java.util.ArrayList;
 47  
 import java.util.List;
 48  
 
 49  
 
 50  
 /**
 51  
  * Implementation of {@link ILocalDAO}
 52  
  */
 53  0
 public class LocalDAO implements ILocalDAO, Serializable
 54  
 {
 55  
     /** Serial ID */
 56  
     private static final long serialVersionUID = -6005693978102619298L;
 57  
     private static final String SQL_QUERY_NEW_PK = "SELECT max(id) FROM dila_local";
 58  
     private static final String SQL_QUERY_FIND_LOCAL_BY_ID_AND_AUDIENCE_AND_TYPE = "SELECT title , author, path, type_id FROM dila_local "
 59  
             + "WHERE id = ? AND audience_id = ? AND type_id = ?";
 60  
     private static final String SQL_QUERY_FIND_BY_ID_AND_TYPE_AND_AUDIENCE = "SELECT title FROM dila_local "
 61  
             + "WHERE id = ? AND type_id = ? AND audience_id = ?";
 62  
     private static final String SQL_QUERY_SELECT_ALL = " SELECT id, title, author, path, xml, audience_id, type_id, date_creation  "
 63  
             + "FROM  dila_local ORDER by id";
 64  
     private static final String SQL_QUERY_SELECT_ALL_BY_AUDIENCE_ID = " SELECT id, title, author, path, xml, audience_id, type_id, date_creation  "
 65  
             + "FROM  dila_local WHERE audience_id = ? ORDER by id";
 66  
     private static final String SQL_QUERY_INSERT = " INSERT INTO dila_local ( id , title , author, path, xml, audience_id, type_id ) "
 67  
             + " VALUES ( ?, ? ,?, ?, ?, ?, ? )";
 68  
     private static final String SQL_QUERY_DELETE = " DELETE FROM dila_local WHERE id = ?";
 69  
     private static final String SQL_QUERY_UPDATE = " UPDATE dila_local "
 70  
             + "SET title = ?, author = ?, path = ?, xml = ?, audience_id = ? WHERE id = ?";
 71  
     private static final String SQL_QUERY_FIND_XML_BY_ID = "SELECT xml FROM dila_local WHERE id = ?";
 72  
     private static final String SQL_QUERY_FIND_LAST_CARDS_BY_AUDIENCE = "SELECT id, title FROM dila_local "
 73  
             + "WHERE audience_id = ? AND type_id = '1' ORDER BY date_creation DESC";
 74  
 
 75  
     /**
 76  
      * Generates a new primary key
 77  
      * @return The new primary key
 78  
      */
 79  
     private Long newPrimaryKey( )
 80  
     {
 81  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 82  0
         daoUtil.executeQuery( );
 83  
 
 84  0
         Long nKey = 1L;
 85  
 
 86  0
         if ( daoUtil.next( ) )
 87  
         {
 88  0
             nKey = daoUtil.getLong( 1 ) + 1L;
 89  
         }
 90  
 
 91  0
         daoUtil.free( );
 92  
 
 93  0
         return nKey;
 94  
     }
 95  
 
 96  
     @Override
 97  
     public LocalDTO findLocalByIdAndTypeAndAudience( Long id, Long type, Long idAudience )
 98  
     {
 99  0
         StringBuilder sbQuery = new StringBuilder( SQL_QUERY_FIND_LOCAL_BY_ID_AND_AUDIENCE_AND_TYPE );
 100  
 
 101  0
         DAOUtil daoUtil = new DAOUtil( sbQuery.toString( ), PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 102  0
         daoUtil.setLong( 1, id );
 103  0
         daoUtil.setLong( 2, idAudience );
 104  0
         daoUtil.setLong( 3, type );
 105  
 
 106  0
         daoUtil.executeQuery( );
 107  
 
 108  0
         LocalDTO local = null;
 109  
 
 110  0
         if ( daoUtil.next( ) )
 111  
         {
 112  0
             local = new LocalDTO( );
 113  0
             local.setId( id );
 114  0
             local.setTitle( daoUtil.getString( 1 ) );
 115  0
             local.setAuthor( daoUtil.getString( 2 ) );
 116  0
             local.setBreadCrumb( daoUtil.getString( 3 ) );
 117  
 
 118  0
             DilaLocalTypeEnum typeEnum = DilaLocalTypeEnum.fromId( daoUtil.getLong( 4 ) );
 119  
 
 120  0
             LocalTypeDTO localType = new LocalTypeDTO( );
 121  0
             localType.setId( typeEnum.getId( ) );
 122  0
             localType.setLabel( typeEnum.getLabel( ) );
 123  0
             local.setType( localType );
 124  
         }
 125  
 
 126  0
         daoUtil.free( );
 127  
 
 128  0
         return local;
 129  
     }
 130  
 
 131  
     @Override
 132  
     public String findTitleByIdAndTypeAndAudience( Long id, Long type, Long idAudience )
 133  
     {
 134  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_ID_AND_TYPE_AND_AUDIENCE,
 135  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 136  0
         daoUtil.setLong( 1, id );
 137  0
         daoUtil.setLong( 2, type );
 138  0
         daoUtil.setLong( 3, idAudience );
 139  
 
 140  0
         daoUtil.executeQuery( );
 141  
 
 142  0
         String title = null;
 143  
 
 144  0
         if ( daoUtil.next( ) )
 145  
         {
 146  0
             title = daoUtil.getString( 1 );
 147  
         }
 148  
 
 149  0
         daoUtil.free( );
 150  
 
 151  0
         return title;
 152  
     }
 153  
 
 154  
     @Override
 155  
     public List<LocalDTO> findAll( )
 156  
     {
 157  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 158  0
         daoUtil.executeQuery( );
 159  
 
 160  0
         List<LocalDTO> results = new ArrayList<LocalDTO>( );
 161  
 
 162  0
         while ( daoUtil.next( ) )
 163  
         {
 164  0
             LocalDTO local = new LocalDTO( );
 165  
 
 166  0
             local.setId( daoUtil.getLong( 1 ) );
 167  0
             local.setTitle( daoUtil.getString( 2 ) );
 168  0
             local.setAuthor( daoUtil.getString( 3 ) );
 169  0
             local.setBreadCrumb( daoUtil.getString( 4 ) );
 170  0
             local.setXml( daoUtil.getString( 5 ) );
 171  0
             local.setIdAudience( daoUtil.getLong( 6 ) );
 172  0
             local.setDisplayPath( DilaUtils.convertBreadcrumbIntoDisplay( local.getBreadCrumb( ), local.getIdAudience( ) ) );
 173  
 
 174  0
             DilaLocalTypeEnum typeEnum = DilaLocalTypeEnum.fromId( daoUtil.getLong( 7 ) );
 175  
 
 176  0
             LocalTypeDTO type = new LocalTypeDTO( );
 177  0
             type.setId( typeEnum.getId( ) );
 178  0
             type.setLabel( typeEnum.getLabel( ) );
 179  0
             local.setType( type );
 180  
 
 181  0
             local.setCreationDate( daoUtil.getDate( 8 ) );
 182  
 
 183  0
             results.add( local );
 184  0
         }
 185  
 
 186  0
         daoUtil.free( );
 187  
 
 188  0
         return results;
 189  
     }
 190  
 
 191  
     @Override
 192  
     public Long insert( LocalDTO local, boolean addIdToBreadcrumb )
 193  
     {
 194  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 195  
 
 196  0
         local.setId( newPrimaryKey( ) );
 197  
 
 198  0
         daoUtil.setLong( 1, local.getId( ) );
 199  0
         daoUtil.setString( 2, local.getTitle( ) );
 200  0
         daoUtil.setString( 3, local.getAuthor( ) );
 201  
 
 202  0
         if ( addIdToBreadcrumb )
 203  
         {
 204  0
             daoUtil.setString( 4, local.getBreadCrumb( ) + ";" + local.getId( ) );
 205  
         }
 206  
         else
 207  
         {
 208  0
             daoUtil.setString( 4, local.getBreadCrumb( ) );
 209  
         }
 210  
 
 211  0
         daoUtil.setString(
 212  
                 5,
 213  0
                 local.getXml( ).replaceAll( "<%ID%>", "" + local.getId( ) )
 214  0
                         .replaceAll( "<%NIVEAU_ID%>", "" + local.getId( ) ) );
 215  0
         daoUtil.setLong( 6, local.getIdAudience( ) );
 216  0
         daoUtil.setLong( 7, local.getType( ).getId( ) );
 217  
 
 218  0
         daoUtil.executeUpdate( );
 219  0
         daoUtil.free( );
 220  
 
 221  0
         return local.getId( );
 222  
     }
 223  
 
 224  
     @Override
 225  
     public void delete( String idLocal )
 226  
     {
 227  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 228  
 
 229  0
         daoUtil.setString( 1, idLocal );
 230  
 
 231  0
         daoUtil.executeUpdate( );
 232  0
         daoUtil.free( );
 233  0
     }
 234  
 
 235  
     @Override
 236  
     public void store( LocalDTO localDTO, boolean addIdToBreadcrumb )
 237  
     {
 238  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 239  
 
 240  0
         daoUtil.setString( 1, localDTO.getTitle( ) );
 241  0
         daoUtil.setString( 2, localDTO.getAuthor( ) );
 242  
 
 243  0
         if ( addIdToBreadcrumb )
 244  
         {
 245  0
             daoUtil.setString( 3, localDTO.getBreadCrumb( ) + ";" + localDTO.getId( ) );
 246  
         }
 247  
         else
 248  
         {
 249  0
             daoUtil.setString( 3, localDTO.getBreadCrumb( ) );
 250  
         }
 251  
 
 252  0
         daoUtil.setString(
 253  
                 4,
 254  0
                 localDTO.getXml( ).replaceAll( "<%ID%>", "" + localDTO.getId( ) )
 255  0
                         .replaceAll( "<%NIVEAU_ID%>", "" + localDTO.getId( ) ) );
 256  0
         daoUtil.setLong( 5, localDTO.getIdAudience( ) );
 257  0
         daoUtil.setLong( 6, localDTO.getId( ) );
 258  
 
 259  0
         daoUtil.executeUpdate( );
 260  0
         daoUtil.free( );
 261  0
     }
 262  
 
 263  
     @Override
 264  
     public String findXmlById( Long id )
 265  
     {
 266  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_XML_BY_ID, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 267  0
         daoUtil.setLong( 1, id );
 268  
 
 269  0
         daoUtil.executeQuery( );
 270  
 
 271  0
         String xml = null;
 272  
 
 273  0
         if ( daoUtil.next( ) )
 274  
         {
 275  0
             xml = daoUtil.getString( 1 );
 276  
         }
 277  
 
 278  0
         daoUtil.free( );
 279  
 
 280  0
         return xml;
 281  
     }
 282  
 
 283  
     @Override
 284  
     public List<LocalDTO> findLastCardsByAudience( Long audienceId )
 285  
     {
 286  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_LAST_CARDS_BY_AUDIENCE,
 287  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 288  0
         daoUtil.setLong( 1, audienceId );
 289  
 
 290  0
         List<LocalDTO> result = new ArrayList<LocalDTO>( );
 291  
 
 292  0
         daoUtil.executeQuery( );
 293  
 
 294  0
         while ( daoUtil.next( ) )
 295  
         {
 296  0
             LocalDTO dto = new LocalDTO( );
 297  
 
 298  0
             dto.setId( daoUtil.getLong( 1 ) );
 299  0
             dto.setTitle( daoUtil.getString( 2 ) );
 300  
 
 301  0
             result.add( dto );
 302  0
         }
 303  
 
 304  0
         daoUtil.free( );
 305  
 
 306  0
         return result;
 307  
     }
 308  
 
 309  
     @Override
 310  
     public List<LocalDTO> findAllByAudienceId( Long audienceId )
 311  
     {
 312  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_AUDIENCE_ID,
 313  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 314  0
         daoUtil.setLong( 1, audienceId );
 315  0
         daoUtil.executeQuery( );
 316  
 
 317  0
         List<LocalDTO> results = new ArrayList<LocalDTO>( );
 318  
 
 319  0
         while ( daoUtil.next( ) )
 320  
         {
 321  0
             LocalDTO local = new LocalDTO( );
 322  
 
 323  0
             local.setId( daoUtil.getLong( 1 ) );
 324  0
             local.setTitle( daoUtil.getString( 2 ) );
 325  0
             local.setAuthor( daoUtil.getString( 3 ) );
 326  0
             local.setBreadCrumb( daoUtil.getString( 4 ) );
 327  0
             local.setXml( daoUtil.getString( 5 ) );
 328  0
             local.setIdAudience( daoUtil.getLong( 6 ) );
 329  0
             local.setDisplayPath( DilaUtils.convertBreadcrumbIntoDisplay( local.getBreadCrumb( ), local.getIdAudience( ) ) );
 330  
 
 331  0
             DilaLocalTypeEnum typeEnum = DilaLocalTypeEnum.fromId( daoUtil.getLong( 7 ) );
 332  
 
 333  0
             LocalTypeDTO type = new LocalTypeDTO( );
 334  0
             type.setId( typeEnum.getId( ) );
 335  0
             type.setLabel( typeEnum.getLabel( ) );
 336  0
             local.setType( type );
 337  
 
 338  0
             local.setCreationDate( daoUtil.getDate( 8 ) );
 339  
 
 340  0
             results.add( local );
 341  0
         }
 342  
 
 343  0
         daoUtil.free( );
 344  
 
 345  0
         return results;
 346  
     }
 347  
 }