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.mylutece.modules.notification.service.folder;
35
36 import fr.paris.lutece.plugins.mylutece.modules.notification.business.folder.FolderHome;
37 import fr.paris.lutece.plugins.mylutece.modules.notification.business.folder.FolderType;
38 import fr.paris.lutece.plugins.mylutece.modules.notification.business.folder.IFolder;
39 import fr.paris.lutece.plugins.mylutece.modules.notification.service.NotificationPlugin;
40 import fr.paris.lutece.portal.service.spring.SpringContextService;
41
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Locale;
45
46
47 /**
48 *
49 * FolderService
50 *
51 */
52 public final class FolderService
53 {
54 private static final String BEAN_MYLUTECENOTIFICATION_FOLDERSERVICE = "mylutece-notification.folderService";
55 private List<FolderType> _listFolderTypes;
56
57 /**
58 * Private constructor
59 */
60 private FolderService( )
61 {
62 }
63
64 /**
65 * Get the instance of {@link FolderService}
66 * @return the instance of {@link FolderService}
67 */
68 public static FolderService getService( )
69 {
70 return (FolderService) SpringContextService.getPluginBean( NotificationPlugin.PLUGIN_NAME,
71 BEAN_MYLUTECENOTIFICATION_FOLDERSERVICE );
72 }
73
74 /**
75 * Find the notification by its primary key
76 * @param nIdFolder the id notification
77 * @param locale the {@link Locale}
78 * @return a {@link IFolder}
79 */
80 public IFolder findByPrimaryKey( int nIdFolder, Locale locale )
81 {
82 IFolder folder = FolderHome.findByPrimaryKey( nIdFolder );
83 folder.setFolderType( locale );
84
85 return folder;
86 }
87
88 /**
89 * Create a folder
90 * @param folder the folder
91 * @return the newly created notification id
92 */
93 public int create( IFolder folder )
94 {
95 int nNewPrimaryKey = -1;
96
97 if ( folder != null )
98 {
99 nNewPrimaryKey = FolderHome.create( folder );
100 }
101
102 return nNewPrimaryKey;
103 }
104
105 /**
106 * Update the folder
107 * @param folder the folder
108 */
109 public void update( IFolder folder )
110 {
111 if ( folder != null )
112 {
113 FolderHome.update( folder );
114 }
115 }
116
117 /**
118 * Remove the folder
119 * @param nIdFolder the id folder
120 */
121 public void remove( int nIdFolder )
122 {
123 FolderHome.remove( nIdFolder );
124 }
125
126 /**
127 * Find all folders
128 * @param locale the {@link Locale}
129 * @return a list of {@link IFolder}
130 */
131 public List<IFolder> findAll( Locale locale )
132 {
133 List<IFolder> listFolders = FolderHome.findAll( );
134
135 for ( IFolder folder : listFolders )
136 {
137 folder.setFolderType( locale );
138 }
139
140 return listFolders;
141 }
142
143 /**
144 * Find the folders associated to the user guid
145 * @param strUserGuid the user guid
146 * @param locale {@link Locale}
147 * @return a list of {@link IFolder}
148 */
149 public List<IFolder> findByUserGuid( String strUserGuid, Locale locale )
150 {
151 List<IFolder> listFolders = FolderHome.findByUserGuid( strUserGuid );
152
153 for ( IFolder folder : listFolders )
154 {
155 folder.setFolderType( locale );
156 }
157
158 return listFolders;
159 }
160
161 /**
162 * Get the list of folder types
163 * @param locale the {@link Locale}
164 * @return a list of {@link FolderType}
165 */
166 public List<FolderType> getFolderTypes( Locale locale )
167 {
168 if ( _listFolderTypes == null )
169 {
170 _listFolderTypes = new ArrayList<FolderType>( );
171
172 for ( IFolder folder : SpringContextService.getBeansOfType( IFolder.class ) )
173 {
174 folder.setFolderType( locale );
175 _listFolderTypes.add( folder.getFolderType( ) );
176 }
177 }
178
179 return _listFolderTypes;
180 }
181 }