1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.service.mailinglist;
35
36 import fr.paris.lutece.portal.business.mailinglist.MailingList;
37 import fr.paris.lutece.portal.business.mailinglist.MailingListFilter;
38 import fr.paris.lutece.portal.business.mailinglist.MailingListHome;
39 import fr.paris.lutece.portal.business.mailinglist.MailingListUsersFilter;
40 import fr.paris.lutece.portal.business.mailinglist.Recipient;
41 import fr.paris.lutece.portal.business.user.AdminUser;
42 import fr.paris.lutece.portal.business.user.AdminUserHome;
43 import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
44 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
45 import fr.paris.lutece.util.ReferenceItem;
46 import fr.paris.lutece.util.ReferenceList;
47
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.List;
51
52
53
54
55
56 public final class AdminMailingListService
57 {
58 public static final String ALL_ROLES = "*";
59
60
61
62
63 private AdminMailingListService( )
64 {
65 }
66
67
68
69
70 public static void init( )
71 {
72
73 MailingList.init( );
74 }
75
76
77
78
79
80
81 public static ReferenceList getMailingLists( AdminUser user )
82 {
83 ReferenceList list = new ReferenceList( );
84
85 for ( MailingList mailinglist : getUserMailingLists( user ) )
86 {
87 list.addItem( mailinglist.getId( ), mailinglist.getName( ) );
88 }
89
90 return list;
91 }
92
93
94
95
96
97
98 public static Collection<MailingList> getUserMailingLists( AdminUser user )
99 {
100 Collection<MailingList> listMailinglists = new ArrayList<MailingList>( );
101
102
103 listMailinglists.addAll( MailingListHome.findByWorkgroup( AdminWorkgroupService.ALL_GROUPS ) );
104
105
106 ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );
107
108 for ( ReferenceItem workgroup : listWorkgroups )
109 {
110 listMailinglists.addAll( MailingListHome.findByWorkgroup( workgroup.getCode( ) ) );
111 }
112
113 return listMailinglists;
114 }
115
116
117
118
119
120
121
122
123 public static List<MailingList> getUserMailingListsByFilter( AdminUser user, MailingListFilter filter )
124 {
125 MailingListFilter mailingListFilter = new MailingListFilter( filter );
126
127 mailingListFilter.setWorkgroup( AdminWorkgroupService.ALL_GROUPS );
128
129 List<MailingList> listMailingLists = MailingListHome.findsByFilter( mailingListFilter );
130
131
132 ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );
133
134 for ( ReferenceItem workgroup : listWorkgroups )
135 {
136 mailingListFilter = new MailingListFilter( filter );
137 mailingListFilter.setWorkgroup( workgroup.getCode( ) );
138 listMailingLists.addAll( MailingListHome.findsByFilter( mailingListFilter ) );
139 }
140
141 return listMailingLists;
142 }
143
144
145
146
147
148
149 public static Collection<Recipient> getRecipients( int nIdMailingList )
150 {
151 Collection<Recipient> listRecipients = new ArrayList<Recipient>( );
152 MailingList mailinglist = MailingListHome.findByPrimaryKey( nIdMailingList );
153
154 if ( mailinglist != null )
155 {
156 for ( MailingListUsersFilter filter : mailinglist.getFilters( ) )
157 {
158 listRecipients.addAll( getRecipients( filter.getWorkgroup( ), filter.getRole( ) ) );
159 }
160 }
161
162 return listRecipients;
163 }
164
165
166
167
168
169
170
171 public static Collection<Recipient> getRecipients( String strWorkgroup, String strRole )
172 {
173 Collection<Recipient> listRecipients = new ArrayList<Recipient>( );
174 Collection<AdminUser> listUsers;
175
176 if ( ( strWorkgroup != null ) && ( !strWorkgroup.equals( AdminWorkgroupService.ALL_GROUPS ) ) )
177 {
178 listUsers = AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroup );
179 }
180 else
181 {
182 listUsers = AdminUserHome.findUserList( );
183 }
184
185 for ( AdminUser user : listUsers )
186 {
187 if ( ( strRole != null ) && ( !strRole.equals( ALL_ROLES ) ) )
188 {
189 if ( !user.isInRole( strRole ) )
190 {
191
192 continue;
193 }
194 }
195
196 Recipient recipient = new Recipient( );
197 recipient.setName( user.getFirstName( ) + " " + user.getLastName( ) );
198 recipient.setEmail( user.getEmail( ) );
199 listRecipients.add( recipient );
200 }
201
202 return listRecipients;
203 }
204
205
206
207
208
209
210
211 public static boolean checkFilter( MailingListUsersFilter filter, int nIdMailingList )
212 {
213 return MailingListHome.checkFilter( filter, nIdMailingList );
214 }
215 }