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.directory.business;
35
36 import fr.paris.lutece.plugins.directory.business.attribute.DirectoryAttributeHome;
37 import fr.paris.lutece.plugins.directory.service.record.IRecordService;
38 import fr.paris.lutece.plugins.directory.service.record.RecordService;
39 import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
40 import fr.paris.lutece.portal.service.plugin.Plugin;
41 import fr.paris.lutece.portal.service.spring.SpringContextService;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.util.ReferenceList;
44
45 import org.apache.commons.beanutils.BeanUtils;
46
47 import java.lang.reflect.InvocationTargetException;
48
49 import java.util.List;
50 import java.util.Map;
51
52 /**
53 * This class provides instances management methods (create, find, ...) for Directory objects
54 */
55 public final class DirectoryHome
56 {
57 // Static variable pointed at the DAO instance
58 private static IDirectoryDAO _dao = SpringContextService.getBean( "directoryDAO" );
59
60 /**
61 * Private constructor - this class need not be instantiated
62 */
63 private DirectoryHome( )
64 {
65 }
66
67 /**
68 * Creation of an instance of directory
69 *
70 * @param directory
71 * The instance of the Directory which contains the informations to store
72 * @param plugin
73 * the Plugin
74 * @return The primary key of the new directory.
75 */
76 public static int create( Directory directory, Plugin plugin )
77 {
78 int nIdDirectory = _dao.insert( directory, plugin );
79
80 // Create directory attributes associated to the directory
81 Map<String, Object> mapAttributes = DirectoryUtils.depopulate( directory );
82 DirectoryAttributeHome.create( nIdDirectory, mapAttributes );
83
84 return nIdDirectory;
85 }
86
87 /**
88 * Copy of an instance of directory
89 *
90 * @param directory
91 * The instance of the directory who must copy
92 * @param plugin
93 * the Plugin
94 *
95 */
96 public static void copy( Directory directory, Plugin plugin )
97 {
98 List<IEntry> listEntry;
99 EntryFilter filter = new EntryFilter( );
100 filter.setIdDirectory( directory.getIdDirectory( ) );
101 filter.setIsEntryParentNull( EntryFilter.FILTER_TRUE );
102 listEntry = EntryHome.getEntryList( filter, plugin );
103
104 directory.setDateCreation( DirectoryUtils.getCurrentTimestamp( ) );
105 directory.setIdDirectory( create( directory, plugin ) );
106
107 for ( IEntry entry : listEntry )
108 {
109 entry = EntryHome.findByPrimaryKey( entry.getIdEntry( ), plugin );
110 entry.setDirectory( directory );
111 EntryHome.copy( entry, plugin );
112 }
113 }
114
115 /**
116 * Update of the directory which is specified in parameter
117 *
118 * @param directory
119 * The instance of the directory which contains the informations to update
120 * @param plugin
121 * the Plugin
122 *
123 */
124 public static void update( Directory directory, Plugin plugin )
125 {
126 // Remove directory attributes associated to the directory
127 DirectoryAttributeHome.remove( directory.getIdDirectory( ) );
128
129 // Add directory Attribute
130 Map<String, Object> mapAttributes = DirectoryUtils.depopulate( directory );
131 DirectoryAttributeHome.create( directory.getIdDirectory( ), mapAttributes );
132
133 _dao.store( directory, plugin );
134 }
135
136 /**
137 * Remove the directory whose identifier is specified in parameter
138 *
139 * @param nIdDirectory
140 * The directory Id
141 * @param plugin
142 * the Plugin
143 */
144 public static void remove( int nIdDirectory, Plugin plugin )
145 {
146 // Remove records associated to the directory
147 RecordFieldFilter recordFilter = new RecordFieldFilter( );
148 recordFilter.setIdDirectory( nIdDirectory );
149
150 IRecordService recordService = SpringContextService.getBean( RecordService.BEAN_SERVICE );
151
152 List<Record> listRecord = recordService.getListRecord( recordFilter, plugin );
153
154 for ( Record record : listRecord )
155 {
156 recordService.remove( record.getIdRecord( ), plugin );
157 }
158
159 // Remove directory attributes associated to the directory
160 DirectoryAttributeHome.remove( nIdDirectory );
161
162 // Remove entries associated to the directory
163 EntryFilter entryFilter = new EntryFilter( );
164 entryFilter.setIdDirectory( nIdDirectory );
165
166 List<IEntry> listEntry = EntryHome.getEntryList( entryFilter, plugin );
167
168 for ( IEntry entry : listEntry )
169 {
170 EntryHome.remove( entry.getIdEntry( ), plugin );
171 }
172
173 // Remove the directory
174 _dao.delete( nIdDirectory, plugin );
175 }
176
177 // /////////////////////////////////////////////////////////////////////////
178 // Finders
179 /**
180 * Returns an instance of a directory whose identifier is specified in parameter
181 *
182 * @param nKey
183 * The entry primary key
184 * @param plugin
185 * the Plugin
186 * @return an instance of directory
187 */
188 public static Directory findByPrimaryKey( int nKey, Plugin plugin )
189 {
190 Directory directory = _dao.load( nKey, plugin );
191 Map<String, Object> mapAttributes = DirectoryAttributeHome.findByPrimaryKey( nKey );
192
193 try
194 {
195 BeanUtils.populate( directory, mapAttributes );
196 }
197 catch( IllegalAccessException e )
198 {
199 AppLogService.error( e );
200 }
201 catch( InvocationTargetException e )
202 {
203 AppLogService.error( e );
204 }
205
206 return directory;
207 }
208
209 /**
210 * Load the data of all the directory who verify the filter and returns them in a list
211 *
212 * @param filter
213 * the filter
214 * @param plugin
215 * the plugin
216 * @return the list of form
217 */
218 public static List<Directory> getDirectoryList( DirectoryFilter filter, Plugin plugin )
219 {
220 List<Directory> listDirectories = _dao.selectDirectoryList( filter, plugin );
221
222 for ( Directory directory : listDirectories )
223 {
224 Map<String, Object> mapAttributes = DirectoryAttributeHome.findByPrimaryKey( directory.getIdDirectory( ) );
225
226 try
227 {
228 BeanUtils.populate( directory, mapAttributes );
229 }
230 catch( IllegalAccessException e )
231 {
232 AppLogService.error( e );
233 }
234 catch( InvocationTargetException e )
235 {
236 AppLogService.error( e );
237 }
238 }
239
240 return listDirectories;
241 }
242
243 /**
244 * Load the data of all enable directory returns them in a reference list
245 *
246 * @param plugin
247 * the plugin
248 * @return a reference list of enable directory
249 */
250 public static ReferenceList getDirectoryList( Plugin plugin )
251 {
252 return _dao.getEnableDirectoryList( plugin );
253 }
254 }