View Javadoc
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  package fr.paris.lutece.plugins.deployment.business;
35  
36  import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
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  public class ApplicationDAO implements IApllicationDAO
44  {
45      private static final String SQL_FILTER_ID_APPLICATION = " id_application = ? ";
46      private static final String SQL_FILTER_WORKGROUP = " workgroup = ? ";
47      private static final String SQL_ORDER_CODE_ASC = " ORDER BY code ASC ";
48      private static final String SQL_QUERY_NEW_PK = " SELECT max(id_application) FROM deployment_application ";
49      private static final String SQL_QUERY_SELECT_APPLICATION = "SELECT id_application,code,name,url_site,webapp_name,workgroup,repo_type, artifact_id FROM deployment_application ";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO deployment_application (id_application,code,name,url_site,webapp_name,workgroup,repo_type, artifact_id )VALUES(?,?,?,?,?,?,?,?)";
51      private static final String SQL_QUERY_UPDATE = "UPDATE deployment_application SET id_application=?,code=?,name=?,url_site=?,webapp_name=?,workgroup=?,repo_type=?, artifact_id = ?  WHERE"
52              + SQL_FILTER_ID_APPLICATION;
53      private static final String SQL_QUERY_DELETE = "DELETE FROM deployment_application WHERE" + SQL_FILTER_ID_APPLICATION;
54  
55      /**
56       * Generates a new primary key
57       *
58       * @param plugin
59       *            the plugin
60       * @return The new primary key
61       */
62      public int newPrimaryKey( Plugin plugin )
63      {
64          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
65          daoUtil.executeQuery( );
66  
67          int nKey;
68  
69          if ( !daoUtil.next( ) )
70          {
71              // if the table is empty
72              nKey = 1;
73          }
74  
75          nKey = daoUtil.getInt( 1 ) + 1;
76          daoUtil.free( );
77  
78          return nKey;
79      }
80  
81      public void delete( int nIdApplication, Plugin plugin )
82      {
83          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
84          daoUtil.setInt( 1, nIdApplication );
85          daoUtil.executeUpdate( );
86          daoUtil.free( );
87      }
88  
89      public List<Application> findByFilter( FilterDeployment filter, Plugin plugin )
90      {
91          List<Application> listApllication = new ArrayList<Application>( );
92          List<String> listStrFilter = new ArrayList<String>( );
93          Application application;
94  
95          if ( filter.containsIdApplicationFilter( ) )
96          {
97              listStrFilter.add( SQL_FILTER_ID_APPLICATION );
98          }
99  
100         if ( filter.containsWorkgroupFilter( ) )
101         {
102             listStrFilter.add( SQL_FILTER_WORKGROUP );
103         }
104 
105         String strSQL = DeploymentUtils.buildRequetteWithFilter( SQL_QUERY_SELECT_APPLICATION, listStrFilter, SQL_ORDER_CODE_ASC );
106         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
107         int nIndex = 1;
108 
109         if ( filter.containsIdApplicationFilter( ) )
110         {
111             daoUtil.setInt( nIndex, filter.getIdApplication( ) );
112             nIndex++;
113         }
114 
115         if ( filter.containsWorkgroupFilter( ) )
116         {
117             daoUtil.setString( nIndex, filter.getWorkgroup( ) );
118             nIndex++;
119         }
120 
121         daoUtil.executeQuery( );
122 
123         while ( daoUtil.next( ) )
124         {
125             application = new Application( );
126             application.setIdApplication( daoUtil.getInt( 1 ) );
127             application.setCode( daoUtil.getString( 2 ) );
128             application.setName( daoUtil.getString( 3 ) );
129             application.setUrlRepo( daoUtil.getString( 4 ) );
130             application.setWebAppName( daoUtil.getString( 5 ) );
131             application.setWorkgroup( daoUtil.getString( 6 ) );
132             application.setRepoType( daoUtil.getString( 7 ) );
133             application.setArtifactId( daoUtil.getString( 8 ) );
134             listApllication.add( application );
135         }
136 
137         daoUtil.free( );
138 
139         return listApllication;
140     }
141 
142     public Application findByPrimaryKey( int nIdApplication, Plugin plugin )
143     {
144         FilterDeployment filter = new FilterDeployment( );
145         filter.setIdApplication( nIdApplication );
146 
147         List<Application> listApplications = findByFilter( filter, plugin );
148 
149         return ( listApplications.size( ) > 0 ) ? listApplications.get( 0 ) : null;
150     }
151 
152     public int insert( Application application, Plugin plugin )
153     {
154         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
155         daoUtil.setString( 2, application.getCode( ) );
156         daoUtil.setString( 3, application.getName( ) );
157         daoUtil.setString( 4, application.getUrlRepo( ) );
158         daoUtil.setString( 5, application.getWebAppName( ) );
159         daoUtil.setString( 6, application.getWorkgroup( ) );
160         daoUtil.setString( 7, application.getRepoType( ) );
161         daoUtil.setString( 8, application.getArtifactId( ) );
162         application.setIdApplication( newPrimaryKey( plugin ) );
163         daoUtil.setInt( 1, application.getIdApplication( ) );
164 
165         daoUtil.executeUpdate( );
166         daoUtil.free( );
167 
168         return application.getIdApplication( );
169     }
170 
171     public void update( Application application, Plugin plugin )
172     {
173         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
174         daoUtil.setInt( 1, application.getIdApplication( ) );
175         daoUtil.setString( 2, application.getCode( ) );
176         daoUtil.setString( 3, application.getName( ) );
177         daoUtil.setString( 4, application.getUrlRepo( ) );
178         daoUtil.setString( 5, application.getWebAppName( ) );
179         daoUtil.setString( 6, application.getWorkgroup( ) );
180         daoUtil.setString( 7, application.getRepoType( ) );
181         daoUtil.setString( 8, application.getArtifactId( ) );
182 
183         daoUtil.setInt( 9, application.getIdApplication( ) );
184 
185         daoUtil.executeUpdate( );
186         daoUtil.free( );
187     }
188 }