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.plugins.participatoryideation.web.include;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.apache.commons.lang.StringUtils;
46
47 import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistory;
48 import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistoryFilter;
49 import fr.paris.lutece.plugins.extend.modules.comment.business.Comment;
50 import fr.paris.lutece.plugins.extend.modules.comment.business.CommentFilter;
51 import fr.paris.lutece.plugins.extend.modules.comment.service.CommentService;
52 import fr.paris.lutece.plugins.extend.modules.comment.service.ICommentService;
53 import fr.paris.lutece.plugins.extend.modules.follow.service.extender.FollowResourceExtender;
54 import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
55 import fr.paris.lutece.plugins.extend.service.extender.history.ResourceExtenderHistoryService;
56 import fr.paris.lutece.plugins.participatoryideation.business.proposal.Proposal;
57 import fr.paris.lutece.plugins.participatoryideation.business.proposal.ProposalHome;
58 import fr.paris.lutece.plugins.participatoryideation.business.proposal.ProposalSearcher;
59 import fr.paris.lutece.plugins.participatoryideation.service.ProposalService;
60 import fr.paris.lutece.portal.service.content.PageData;
61 import fr.paris.lutece.portal.service.includes.PageInclude;
62 import fr.paris.lutece.portal.service.security.LuteceUser;
63 import fr.paris.lutece.portal.service.security.SecurityService;
64 import fr.paris.lutece.portal.service.spring.SpringContextService;
65 import fr.paris.lutece.portal.service.template.AppTemplateService;
66 import fr.paris.lutece.util.html.HtmlTemplate;
67
68
69
70
71 public class HeaderInclude implements PageInclude
72 {
73
74 public static final String MARK_HEADER_INCLUDE = "header_ideation_include";
75
76
77 private static ICommentService _commentService;
78
79
80 private static final String TEMPLATE_HEADER_INCLUDE = "/skin/plugins/participatoryideation/header_include.html";
81
82
83
84
85
86 private static final String MARK_HEADER_PROJECTS_SUBMITTED = "projectsHeaderSubmitted";
87 private static final String MARK_HEADER_PROJECTS_PARTICIPATE = "projectsHeaderParticipate";
88 private static final String MARK_HEADER_PROJECTS_COMMENTED = "projectsHeaderCommented";
89
90 private static final String MARK_HEADER_COLOR = "header_color";
91
92 private static final String CLASS_CSS_OUT = "logged-out";
93 private static final String CLASS_CSS_IN = "logged-in";
94
95 private static IResourceExtenderHistoryService _resourceExtenderHistoryService = SpringContextService
96 .getBean( ResourceExtenderHistoryService.BEAN_SERVICE );
97
98
99
100
101 @Override
102 public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
103 {
104 rootModel.put( MARK_HEADER_INCLUDE, getHeaderTemplate( request ) );
105 }
106
107
108
109
110
111
112 private static String getHeaderTemplate( HttpServletRequest request )
113 {
114 Map<String, Object> model = new HashMap<String, Object>( );
115 Locale locale = ( request == null ) ? null : request.getLocale( );
116
117 LuteceUser _user = SecurityService.getInstance( ).getRegisteredUser( request );
118
119 model.put( MARK_HEADER_COLOR, CLASS_CSS_OUT );
120 model.put( MARK_HEADER_PROJECTS_SUBMITTED, "" );
121 model.put( MARK_HEADER_PROJECTS_PARTICIPATE, "" );
122 model.put( MARK_HEADER_PROJECTS_COMMENTED, "" );
123
124 if ( _user != null )
125 {
126 String strLuteceUserName = _user.getName( );
127 ProposalSearcheration/business/proposal/ProposalSearcher.html#ProposalSearcher">ProposalSearcher _proposalSearcher = new ProposalSearcher( );
128 _proposalSearcher.setLuteceUserName( strLuteceUserName );
129 _proposalSearcher.setIsPublished( true );
130
131 Collection<Proposal> proposalsSubmitted = ProposalHome.getProposalsListSearch( _proposalSearcher );
132
133 CommentFilter _commentFilter = new CommentFilter( );
134 _commentFilter.setLuteceUserName( strLuteceUserName );
135
136 Collection<Proposal> proposalsCommented = getProposalsCommentedByUser(
137 getCommentService( ).findByResource( "*", Proposal.PROPERTY_RESOURCE_TYPE, _commentFilter, 0, 10000, false ) );
138
139 ResourceExtenderHistoryFilter filter = new ResourceExtenderHistoryFilter( );
140
141 filter.setExtenderType( FollowResourceExtender.RESOURCE_EXTENDER );
142 filter.setUserGuid( strLuteceUserName );
143
144
145 List<ResourceExtenderHistory> listHistories = _resourceExtenderHistoryService.findByFilter( filter );
146
147 Collection<Proposal> proposalsParticipate = getProposalsParticipatedByUser( listHistories );
148
149 model.put( MARK_HEADER_COLOR, CLASS_CSS_IN );
150 model.put( MARK_HEADER_PROJECTS_SUBMITTED, proposalsSubmitted.size( ) );
151 model.put( MARK_HEADER_PROJECTS_PARTICIPATE, proposalsParticipate.size( ) );
152 model.put( MARK_HEADER_PROJECTS_COMMENTED, proposalsCommented.size( ) );
153 }
154
155 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_HEADER_INCLUDE, locale, model );
156
157 return template.getHtml( );
158 }
159
160
161
162
163
164
165 private static ICommentService getCommentService( )
166 {
167 if ( _commentService == null )
168 {
169 _commentService = SpringContextService.getBean( CommentService.BEAN_SERVICE );
170 }
171 return _commentService;
172 }
173
174
175
176
177
178
179
180 private static Collection<Proposal> getProposalsCommentedByUser( Collection<Comment> listComments )
181 {
182
183 Collection<Proposal> proposalsList = new ArrayList<Proposal>( );
184 List<Integer> IdExtendableResourceList = new ArrayList<Integer>( );
185 for ( Comment comment : listComments )
186 {
187
188 String idRessource = comment.getIdExtendableResource( );
189
190 if ( idRessource != null && StringUtils.isNotEmpty( idRessource ) && StringUtils.isNumeric( idRessource ) )
191 {
192 int nIdProposal = Integer.parseInt( idRessource );
193 Proposal proposal = ProposalHome.findByPrimaryKey( nIdProposal );
194 if ( proposal != null && ProposalService.getInstance( ).isPublished( proposal ) && !IdExtendableResourceList.contains( nIdProposal ) )
195 {
196 proposalsList.add( proposal );
197 IdExtendableResourceList.add( nIdProposal );
198 }
199 }
200 }
201
202 return proposalsList;
203 }
204
205
206
207
208
209
210
211 private static Collection<Proposal> getProposalsParticipatedByUser( Collection<ResourceExtenderHistory> listFollow )
212 {
213
214 Collection<Proposal> proposalsList = new ArrayList<Proposal>( );
215 for ( ResourceExtenderHistory follow : listFollow )
216 {
217
218 String idRessource = follow.getIdExtendableResource( );
219
220 if ( idRessource != null && StringUtils.isNotEmpty( idRessource ) && StringUtils.isNumeric( idRessource ) )
221 {
222 int nIdProposal = Integer.parseInt( idRessource );
223 Proposal proposal = ProposalHome.findByPrimaryKey( nIdProposal );
224 if ( proposal != null && ProposalService.getInstance( ).isPublished( proposal ) )
225 {
226 proposalsList.add( proposal );
227 }
228 }
229 }
230
231 return proposalsList;
232 }
233
234 }