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.webappcontainer.business;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  /**
43   * This class provides Data Access methods for Sites objects
44   */
45  public final class SiteDAO implements ISiteDAO
46  {
47  	// Constants
48  	private static final String SQL_QUERY_SELECT = "SELECT site_url, site_description, site_encoding, site_hat, site_workgroup_key, site_rebuild_html_page, site_redirect_non_html_content, site_use_proxy FROM webappcontainer_site WHERE site_code = ? ";
49  
50  	private static final String SQL_QUERY_SELECTALL = "SELECT site_code, site_url, site_description, site_encoding, site_hat, site_workgroup_key, site_rebuild_html_page, site_redirect_non_html_content, site_use_proxy FROM webappcontainer_site ORDER BY site_code ";
51  
52  	private static final String SQL_QUERY_INSERT = "INSERT INTO webappcontainer_site ( site_code, site_url, site_description, site_encoding, site_hat, site_workgroup_key, site_rebuild_html_page, site_redirect_non_html_content, site_use_proxy )  VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? ) ";
53  
54  	private static final String SQL_QUERY_DELETE = "DELETE FROM webappcontainer_site WHERE site_code = ? ";
55  
56  	private static final String SQL_QUERY_UPDATE = "UPDATE webappcontainer_site SET site_url = ?, site_description = ?, site_encoding = ?, site_hat = ?, site_workgroup_key = ?, site_rebuild_html_page = ?, site_redirect_non_html_content = ?, site_use_proxy = ? WHERE site_code = ?  ";
57  
58  	// //////////////////////////////////////////////////////////////////////
59  	// Methods using a dynamic pool
60  
61  	/**
62  	 * Insert a new record in the table.
63  	 * 
64  	 * @param site The site object
65  	 * @param plugin The {@link Plugin}
66  	 */
67  	public void insert( Site site, Plugin plugin )
68  	{
69  		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
70  		daoUtil.setString( 1, site.getCode() );
71  		daoUtil.setString( 2, site.getUrl() );
72  		daoUtil.setString( 3, site.getDescription() );
73  		daoUtil.setString( 4, site.getEncoding() );
74  		daoUtil.setString( 5, site.getHat() );
75  		daoUtil.setString( 6, site.getWorkgroup() );
76  		daoUtil.setBoolean( 7, site.isRebuildHtmlPage() );
77  		daoUtil.setBoolean( 8, site.isRedirectNonHtmlContent() );
78  		daoUtil.setBoolean( 9, site.isUseProxy() );
79  
80  		daoUtil.executeUpdate();
81  		daoUtil.free();
82  	}
83  
84  	/**
85  	 * Load the data of Site from the table
86  	 * 
87  	 * @param strSiteCode The code of {@link Site}
88  	 * @param plugin The {@link Plugin}
89  	 * @return the instance of the {@link Site}
90  	 */
91  	public Site load( String strSiteCode, Plugin plugin )
92  	{
93  		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
94  		daoUtil.setString( 1, strSiteCode );
95  		daoUtil.executeQuery();
96  
97  		Site site = null;
98  
99  		if ( daoUtil.next() )
100 		{
101 			site = new Site();
102 			site.setCode( strSiteCode );
103 			site.setUrl( daoUtil.getString( 1 ) );
104 			site.setDescription( daoUtil.getString( 2 ) );
105 			site.setEncoding( daoUtil.getString( 3 ) );
106 			site.setHat( daoUtil.getString( 4 ) );
107 			site.setWorkgroup( daoUtil.getString( 5 ) );
108 			site.setRebuildHtmlPage( daoUtil.getBoolean( 6 ) );
109 			site.setRedirectNonHtmlContent( daoUtil.getBoolean( 7 ) );
110 			site.setUseProxy( daoUtil.getBoolean( 8 ) );
111 		}
112 
113 		daoUtil.free();
114 
115 		return site;
116 	}
117 
118 	/**
119 	 * Delete a record from the table
120 	 * 
121 	 * @param strSiteCode The {@link Site} code
122 	 * @param plugin The {@link Plugin}
123 	 */
124 	public void delete( String strSiteCode, Plugin plugin )
125 	{
126 		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
127 		daoUtil.setString( 1, strSiteCode );
128 		daoUtil.executeUpdate();
129 		daoUtil.free();
130 	}
131 
132 	/**
133 	 * Update the record in the table
134 	 * 
135 	 * @param site The reference of {@link Site}
136 	 * @param plugin The {@link Plugin}
137 	 */
138 	public void store( Site site, Plugin plugin )
139 	{
140 		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
141 
142 		daoUtil.setString( 1, site.getUrl() );
143 		daoUtil.setString( 2, site.getDescription() );
144 		daoUtil.setString( 3, site.getEncoding() );
145 		daoUtil.setString( 4, site.getHat() );
146 		daoUtil.setString( 5, site.getWorkgroup() );
147 		daoUtil.setBoolean( 6, site.isRebuildHtmlPage() );
148 		daoUtil.setBoolean( 7, site.isRedirectNonHtmlContent() );
149 		daoUtil.setBoolean( 8, site.isUseProxy() );
150 
151 		daoUtil.setString( 9, site.getCode() );
152 
153 		daoUtil.executeUpdate();
154 		daoUtil.free();
155 	}
156 
157 	/**
158 	 * Load the list of sites
159 	 * 
160 	 * @param plugin The {@link Plugin}
161 	 * @return The Collection of the Sites
162 	 */
163 	public Collection<Site> selectAll( Plugin plugin )
164 	{
165 		Collection<Site> sitesList = new ArrayList<Site>();
166 		DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
167 		daoUtil.executeQuery();
168 
169 		while (daoUtil.next())
170 		{
171 			Site site = new Site();
172 			site.setCode( daoUtil.getString( 1 ) );
173 			site.setUrl( daoUtil.getString( 2 ) );
174 			site.setDescription( daoUtil.getString( 3 ) );
175 			site.setEncoding( daoUtil.getString( 4 ) );
176 			site.setHat( daoUtil.getString( 5 ) );
177 			site.setWorkgroup( daoUtil.getString( 6 ) );
178 			site.setRebuildHtmlPage( daoUtil.getBoolean( 7 ) );
179 			site.setRedirectNonHtmlContent( daoUtil.getBoolean( 8 ) );
180 			site.setUseProxy( daoUtil.getBoolean( 9 ) );
181 			sitesList.add( site );
182 		}
183 
184 		daoUtil.free();
185 
186 		return sitesList;
187 	}
188 }