View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.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   * Page include to add the
70   */
71  public class HeaderInclude implements PageInclude
72  {
73  
74      public static final String MARK_HEADER_INCLUDE = "header_ideation_include";
75  
76      // session variable
77      private static ICommentService _commentService;
78  
79      // Template
80      private static final String TEMPLATE_HEADER_INCLUDE = "/skin/plugins/participatoryideation/header_include.html";
81  
82      // Properties
83  
84      // Mark
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       * {@inheritDoc}
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      * @param request
110      * @return
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             // filter.setIdExtendableResource("*");
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      * Get the comment service
162      * 
163      * @return the comment service
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      * get the proposals List
176      * 
177      * @param listComments
178      * @return
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      * get the proposals List
207      * 
208      * @param listFollow
209      * @return
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 }