1 /*
2 * Copyright (c) 2002-2022, City of 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.genericattributes.business;
35
36 import fr.paris.lutece.plugins.genericattributes.service.file.GenericAttributeFileService;
37 import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
38 import fr.paris.lutece.portal.business.file.FileHome;
39 import fr.paris.lutece.portal.service.plugin.Plugin;
40 import fr.paris.lutece.portal.service.spring.SpringContextService;
41 import fr.paris.lutece.portal.service.util.AppException;
42 import fr.paris.lutece.util.sql.TransactionManager;
43
44 import java.util.List;
45
46 /**
47 * This class provides instances management methods (create, find, ...) for Response objects
48 */
49 public final class ResponseHome
50 {
51 // Static variable pointed at the DAO instance
52 private static IResponseDAO _dao = SpringContextService.getBean( "genericattributes.responseDAO" );
53 private static Plugin _plugin;
54
55 /**
56 * Private constructor - this class need not be instantiated
57 */
58 private ResponseHome( )
59 {
60 }
61
62 /**
63 * Creation of an instance of response
64 *
65 * @param response
66 * The instance of the response which contains the informations to store
67 *
68 */
69 public static void create( Response response )
70 {
71 TransactionManager.beginTransaction( getPlugin( ) );
72
73 try
74 {
75 if ( response.getFile( ) != null )
76 {
77 GenericAttributeFileService.getInstance().save( response.getFile( ) );
78 }
79
80 _dao.insert( response, getPlugin( ) );
81 TransactionManager.commitTransaction( getPlugin( ) );
82 }
83 catch( Exception e )
84 {
85 TransactionManager.rollBack( getPlugin( ) );
86 throw new AppException( e.getMessage( ), e );
87 }
88 }
89
90 /**
91 * Update of the response which is specified in parameter
92 *
93 * @param response
94 * The instance of the Response which contains the informations to update
95 *
96 */
97 public static void update( Response response )
98 {
99 TransactionManager.beginTransaction( getPlugin( ) );
100
101 try
102 {
103 if ( response.getFile( ) != null )
104 {
105 FileHome.update( response.getFile( ) );
106 }
107
108 _dao.store( response, getPlugin( ) );
109 TransactionManager.commitTransaction( getPlugin( ) );
110 }
111 catch( Exception e )
112 {
113 TransactionManager.rollBack( getPlugin( ) );
114 throw new AppException( e.getMessage( ), e );
115 }
116 }
117
118 /**
119 * Remove a response from its id
120 *
121 * @param nIdResponse
122 * The id of the response
123 */
124 public static void remove( int nIdResponse )
125 {
126 Response response = findByPrimaryKey( nIdResponse );
127
128 TransactionManager.beginTransaction( getPlugin( ) );
129
130 try
131 {
132 if ( response != null )
133 {
134 if ( response.getFile( ) != null )
135 {
136 GenericAttributeFileService.getInstance().delete( response.getFile( ).getFileKey( ) );
137 }
138
139 _dao.delete( nIdResponse, getPlugin( ) );
140 }
141
142 TransactionManager.commitTransaction( getPlugin( ) );
143 }
144 catch( Exception e )
145 {
146 TransactionManager.rollBack( getPlugin( ) );
147 throw new AppException( e.getMessage( ), e );
148 }
149 }
150
151 // /////////////////////////////////////////////////////////////////////////
152 // Finders
153
154 /**
155 * Returns an instance of a Response whose identifier is specified in parameter
156 *
157 * @param nKey
158 * The entry primary key
159 * @return an instance of Response
160 */
161 public static Response findByPrimaryKey( int nKey )
162 {
163 return _dao.load( nKey, getPlugin( ) );
164 }
165
166 /**
167 * Load the data of all the response who verify the filter and returns them in a list
168 *
169 * @param filter
170 * the filter
171 * @return the list of response
172 */
173 public static List<Response> getResponseList( ResponseFilter filter )
174 {
175 return _dao.selectListByFilter( filter, getPlugin( ) );
176 }
177
178 /**
179 * return a list of statistic on the entry
180 *
181 * @param nIdEntry
182 * the id of the entry
183 * @return return a list of statistic on the entry
184 */
185 public static List<StatisticEntrySubmit> getStatisticByIdEntry( int nIdEntry )
186 {
187 return _dao.getStatisticByIdEntry( nIdEntry, getPlugin( ) );
188 }
189
190 /**
191 * Get the max number from a given id resource
192 *
193 * @param nIdEntry
194 * the id of the entry
195 * @return the max number
196 */
197 public static int findMaxNumber( int nIdEntry )
198 {
199 return _dao.getMaxNumber( nIdEntry, getPlugin( ) );
200 }
201
202 /**
203 * Get the generic attributes plugin
204 *
205 * @return The generic attributes plugin
206 */
207 private static Plugin getPlugin( )
208 {
209 if ( _plugin == null )
210 {
211 _plugin = GenericAttributesUtils.getPlugin( );
212 }
213
214 return _plugin;
215 }
216 }