Coverage Report - fr.paris.lutece.plugins.dila.business.fichelocale.dao.impl.LocalFolderDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
LocalFolderDAO
0 %
0/83
0 %
0/14
2
 
 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.ILocalFolderDAO;
 37  
 import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalDTO;
 38  
 import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalFolderDTO;
 39  
 import fr.paris.lutece.plugins.dila.service.DilaPlugin;
 40  
 import fr.paris.lutece.portal.service.plugin.PluginService;
 41  
 import fr.paris.lutece.util.sql.DAOUtil;
 42  
 
 43  
 import java.io.Serializable;
 44  
 import java.util.ArrayList;
 45  
 import java.util.List;
 46  
 
 47  
 
 48  
 /**
 49  
  * Implementation of {@link ILocalFolderDAO}
 50  
  */
 51  0
 public class LocalFolderDAO implements ILocalFolderDAO, Serializable
 52  
 {
 53  
     /** Serial ID */
 54  
     private static final long serialVersionUID = -4915475113278829044L;
 55  
 
 56  
     // QUERIES
 57  
     private static final String SQL_QUERY_NEW_PK = "SELECT max(id) FROM dila_local_folder";
 58  
     private static final String SQL_QUERY_INSERT = " INSERT INTO dila_local_folder "
 59  
             + "( id, parent_theme_id, sibling_folder_id, position, presentation, local_id ) "
 60  
             + " VALUES ( ?, ? ,?, ?, ?, ? )";
 61  
     private static final String SQL_QUERY_SELECT_FOLDER_ID_BY_LOCAL_ID = "SELECT "
 62  
             + "id FROM dila_local_folder WHERE local_id = ?";
 63  
     private static final String SQL_QUERY_DELETE = "DELETE FROM dila_local_folder WHERE id = ?";
 64  
     private static final String SQL_QUERY_SELECT_FOLDER_BY_LOCAL_ID = "SELECT "
 65  
             + "dossier.id, dossier.parent_theme_id, dossier.sibling_folder_id, dossier.position, dossier.presentation, local.title, local.author, local.audience_id "
 66  
             + "FROM dila_local_folder dossier, dila_local local "
 67  
             + "WHERE dossier.local_id = ? AND dossier.local_id = local.id";
 68  
     private static final String SQL_QUERY_UPDATE = " UPDATE dila_local_folder"
 69  
             + " SET parent_theme_id = ?, sibling_folder_id = ?, position = ?, presentation = ?" + " WHERE id = ?";
 70  
     private static final String SQL_QUERY_FIND_FOLDERS_BY_PARENT_ID = "SELECT l.id, l.title, d.sibling_folder_id, d.position"
 71  
             + " FROM dila_local_folder d JOIN dila_local l ON d.local_id = l.id" + " WHERE d.parent_theme_id = ?";
 72  
 
 73  
     /**
 74  
      * Generates a new primary key
 75  
      * @return The new primary key
 76  
      */
 77  
     private Long newPrimaryKey( )
 78  
     {
 79  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 80  0
         daoUtil.executeQuery( );
 81  
 
 82  0
         Long nKey = 1L;
 83  
 
 84  0
         if ( daoUtil.next( ) )
 85  
         {
 86  
             // if the table is empty
 87  0
             nKey = daoUtil.getLong( 1 ) + 1L;
 88  
         }
 89  
 
 90  0
         daoUtil.free( );
 91  
 
 92  0
         return nKey;
 93  
     }
 94  
 
 95  
     @Override
 96  
     public Long create( LocalFolderDTO folder )
 97  
     {
 98  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 99  
 
 100  0
         folder.setId( newPrimaryKey( ) );
 101  
 
 102  0
         daoUtil.setLong( 1, folder.getId( ) );
 103  0
         daoUtil.setString( 2, folder.getParentThemeId( ) );
 104  0
         daoUtil.setString( 3, folder.getSiblingFolderId( ) );
 105  
 
 106  0
         if ( folder.getPosition( ) != null )
 107  
         {
 108  0
             daoUtil.setInt( 4, folder.getPosition( ) );
 109  
         }
 110  
         else
 111  
         {
 112  0
             daoUtil.setIntNull( 4 );
 113  
         }
 114  
 
 115  0
         daoUtil.setString( 5, folder.getPresentation( ) );
 116  0
         daoUtil.setLong( 6, folder.getLocalDTO( ).getId( ) );
 117  
 
 118  0
         daoUtil.executeUpdate( );
 119  0
         daoUtil.free( );
 120  
 
 121  0
         return folder.getId( );
 122  
     }
 123  
 
 124  
     @Override
 125  
     public Long findFolderIdByLocalId( String localId )
 126  
     {
 127  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_FOLDER_ID_BY_LOCAL_ID,
 128  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 129  0
         daoUtil.setString( 1, localId );
 130  0
         daoUtil.executeQuery( );
 131  
 
 132  0
         Long result = null;
 133  
 
 134  0
         if ( daoUtil.next( ) )
 135  
         {
 136  0
             result = daoUtil.getLong( 1 );
 137  
         }
 138  
 
 139  0
         daoUtil.free( );
 140  
 
 141  0
         return result;
 142  
     }
 143  
 
 144  
     @Override
 145  
     public void delete( Long dossierLocalId )
 146  
     {
 147  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 148  0
         daoUtil.setLong( 1, dossierLocalId );
 149  0
         daoUtil.executeUpdate( );
 150  
 
 151  0
         daoUtil.free( );
 152  0
     }
 153  
 
 154  
     @Override
 155  
     public LocalFolderDTO findFolderByLocalId( Long localId )
 156  
     {
 157  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_FOLDER_BY_LOCAL_ID,
 158  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 159  0
         daoUtil.setLong( 1, localId );
 160  0
         daoUtil.executeQuery( );
 161  
 
 162  0
         LocalFolderDTO folder = null;
 163  
 
 164  0
         if ( daoUtil.next( ) )
 165  
         {
 166  0
             folder = new LocalFolderDTO( );
 167  0
             folder.getLocalDTO( ).setId( localId );
 168  
 
 169  0
             folder.setId( daoUtil.getLong( 1 ) );
 170  0
             folder.setParentThemeId( daoUtil.getString( 2 ) );
 171  
 
 172  0
             if ( daoUtil.getString( 3 ) != null )
 173  
             {
 174  0
                 folder.setSiblingFolderId( daoUtil.getString( 3 ) );
 175  0
                 folder.setPosition( daoUtil.getInt( 4 ) );
 176  
             }
 177  
 
 178  0
             folder.setPresentation( daoUtil.getString( 5 ) );
 179  
 
 180  0
             folder.getLocalDTO( ).setTitle( daoUtil.getString( 6 ) );
 181  0
             folder.getLocalDTO( ).setAuthor( daoUtil.getString( 7 ) );
 182  0
             folder.getLocalDTO( ).setIdAudience( daoUtil.getLong( 8 ) );
 183  
         }
 184  
 
 185  0
         daoUtil.free( );
 186  
 
 187  0
         return folder;
 188  
     }
 189  
 
 190  
     @Override
 191  
     public void store( LocalFolderDTO folder )
 192  
     {
 193  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 194  
 
 195  0
         daoUtil.setString( 1, folder.getParentThemeId( ) );
 196  0
         daoUtil.setString( 2, folder.getSiblingFolderId( ) );
 197  
 
 198  0
         if ( folder.getPosition( ) != null )
 199  
         {
 200  0
             daoUtil.setInt( 3, folder.getPosition( ) );
 201  
         }
 202  
         else
 203  
         {
 204  0
             daoUtil.setIntNull( 3 );
 205  
         }
 206  
 
 207  0
         daoUtil.setString( 4, folder.getPresentation( ) );
 208  0
         daoUtil.setLong( 5, folder.getId( ) );
 209  
 
 210  0
         daoUtil.executeUpdate( );
 211  0
         daoUtil.free( );
 212  0
     }
 213  
 
 214  
     @Override
 215  
     public List<LocalFolderDTO> findLocalFoldersByParentTheme( String parentId )
 216  
     {
 217  0
         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_FOLDERS_BY_PARENT_ID,
 218  0
                 PluginService.getPlugin( DilaPlugin.PLUGIN_NAME ) );
 219  0
         daoUtil.setString( 1, parentId );
 220  0
         daoUtil.executeQuery( );
 221  
 
 222  0
         List<LocalFolderDTO> resultList = new ArrayList<LocalFolderDTO>( );
 223  
 
 224  0
         while ( daoUtil.next( ) )
 225  
         {
 226  0
             LocalFolderDTO localFolder = new LocalFolderDTO( );
 227  0
             LocalDTO local = new LocalDTO( );
 228  0
             local.setId( daoUtil.getLong( 1 ) );
 229  0
             local.setTitle( daoUtil.getString( 2 ) );
 230  0
             localFolder.setLocalDTO( local );
 231  0
             localFolder.setSiblingFolderId( daoUtil.getString( 3 ) );
 232  0
             localFolder.setPosition( daoUtil.getInt( 4 ) );
 233  0
             localFolder.setParentThemeId( parentId );
 234  0
             resultList.add( localFolder );
 235  0
         }
 236  
 
 237  0
         daoUtil.free( );
 238  
 
 239  0
         return resultList;
 240  
     }
 241  
 }