View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.extend.modules.follow.web.component;
35  
36  import fr.paris.lutece.plugins.extend.business.extender.ResourceExtenderDTO;
37  import fr.paris.lutece.plugins.extend.business.extender.config.IExtenderConfig;
38  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistory;
39  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistoryFilter;
40  import fr.paris.lutece.plugins.extend.modules.follow.business.Follow;
41  import fr.paris.lutece.plugins.extend.modules.follow.business.FollowExtenderConfig;
42  import fr.paris.lutece.plugins.extend.modules.follow.service.IFollowService;
43  import fr.paris.lutece.plugins.extend.modules.follow.service.extender.FollowResourceExtender;
44  import fr.paris.lutece.plugins.extend.modules.follow.util.constants.FollowConstants;
45  import fr.paris.lutece.plugins.extend.service.extender.config.IResourceExtenderConfigService;
46  import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
47  import fr.paris.lutece.plugins.extend.util.ExtendErrorException;
48  import fr.paris.lutece.plugins.extend.util.JSONUtils;
49  import fr.paris.lutece.plugins.extend.web.component.AbstractResourceExtenderComponent;
50  import fr.paris.lutece.portal.service.security.LuteceUser;
51  import fr.paris.lutece.portal.service.security.SecurityService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.template.DatabaseTemplateService;
54  import fr.paris.lutece.portal.service.util.AppLogService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import com.fasterxml.jackson.databind.node.ObjectNode;
58  
59  import org.apache.commons.collections.CollectionUtils;
60  import org.apache.commons.lang3.StringUtils;
61  
62  import java.util.HashMap;
63  import java.util.List;
64  import java.util.Locale;
65  import java.util.Map;
66  
67  import javax.inject.Inject;
68  import javax.inject.Named;
69  import javax.servlet.http.HttpServletRequest;
70  
71  /**
72   *
73   * FollowResourceExtenderComponent
74   *
75   */
76  public class FollowResourceExtenderComponent extends AbstractResourceExtenderComponent
77  {
78      // TEMPLATES
79      private static final String TEMPLATE_FOLLOW = "skin/plugins/extend/modules/follow/follow.html";
80      private static final String TEMPLATE_FOLLOW_INFO = "admin/plugins/extend/modules/follow/follow_info.html";
81      private static final String TEMPLATE_FOLLOW_EXTENDER_CONFIG = "admin/plugins/extend/modules/follow/follow_extender_config.html";
82      
83      // MARKS
84      private static final String MARK_CONFIG = "config";
85      
86      @Inject
87      private IFollowService _followService;
88      @Inject
89      private IResourceExtenderHistoryService _resourceExtenderHistoryService;
90      @Inject
91      @Named( FollowConstants.BEAN_CONFIG_SERVICE )
92      private IResourceExtenderConfigService _followExtenderConfigService;
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void buildXmlAddOn( String strIdExtendableResource, String strExtendableResourceType, String strParameters, StringBuffer strXml )
99      {
100         // Nothing yet
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @SuppressWarnings( "deprecation" )
107     @Override
108     public String getPageAddOn( String strIdExtendableResource, String strExtendableResourceType, String strParameters, HttpServletRequest request )
109     {
110         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
111 
112         Follow follow = _followService.findByResource( strIdExtendableResource, strExtendableResourceType );
113         String strTemplateContent = DatabaseTemplateService.getTemplateFromKey( FollowConstants.MARK_EXTEND_FOLLOW );
114 
115         Map<String, Object> model = new HashMap<String, Object>( );
116         model.put( FollowConstants.MARK_FOLLOW, follow );
117         model.put( FollowConstants.MARK_ID_EXTENDABLE_RESOURCE, strIdExtendableResource );
118         model.put( FollowConstants.MARK_EXTENDABLE_RESOURCE_TYPE, strExtendableResourceType );
119         model.put( FollowConstants.MARK_SHOW, fetchShowParameter( strParameters ) );
120         model.put( FollowConstants.MARK_CAN_FOLLOW, true );
121         model.put( FollowConstants.MARK_CAN_DELETE_FOLLOW, isFollower( user, strIdExtendableResource, strExtendableResourceType ) );
122         model.put( FollowConstants.MARK_FOLLOW_CLOSED, !_followService.isAuthorized( request, 
123                 ( FollowExtenderConfig ) _followExtenderConfigService.find( FollowConstants.MARK_FOLLOW, strIdExtendableResource, strExtendableResourceType ) ) );
124         
125         model.put( FollowConstants.MARK_FOLLOW_HTML_CONTENT,
126                 AppTemplateService.getTemplateFromStringFtl( strTemplateContent, request.getLocale( ), model ).getHtml( ) );
127 
128         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FOLLOW, request.getLocale( ), model );
129 
130         return template.getHtml( );
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public String getInfoHtml( ResourceExtenderDTO resourceExtender, Locale locale, HttpServletRequest request )
138     {
139         if ( resourceExtender != null )
140         {
141             Map<String, Object> model = new HashMap<String, Object>( );
142             model.put( FollowConstants.MARK_FOLLOW,
143                     _followService.findByResource( resourceExtender.getIdExtendableResource( ), resourceExtender.getExtendableResourceType( ) ) );
144 
145             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FOLLOW_INFO, request.getLocale( ), model );
146 
147             return template.getHtml( );
148         }
149 
150         return StringUtils.EMPTY;
151     }
152 
153     /**
154      * Fetch show parameter.
155      *
156      * @param strParameters
157      *            the str parameters
158      * @return the string
159      */
160     private String fetchShowParameter( String strParameters )
161     {
162         String strShowParameter = StringUtils.EMPTY;
163         ObjectNode jsonParameters = JSONUtils.parseParameters( strParameters );
164 
165         if ( jsonParameters != null )
166         {
167             if ( jsonParameters.has( FollowConstants.JSON_KEY_SHOW ) )
168             {
169                 strShowParameter = jsonParameters.get( FollowConstants.JSON_KEY_SHOW ).asText( );
170             }
171             else
172             {
173                 AppLogService.debug( "No " + FollowConstants.JSON_KEY_SHOW + " found in " + jsonParameters );
174             }
175         }
176 
177         return strShowParameter;
178     }
179 
180     /**
181      *
182      * @param user
183      *            the User
184      * @param strIdExtendableResource
185      *            the IdExtendableResource
186      * @param strExtendableResourceType
187      *            the ExtendableResourceType
188      * @return if the user is a follower
189      */
190     private boolean isFollower( LuteceUser user, String strIdExtendableResource, String strExtendableResourceType )
191     {
192         boolean res = false;
193         
194         if( user != null )
195         {
196             ResourceExtenderHistoryFilter filter = new ResourceExtenderHistoryFilter( );
197     
198             filter.setExtenderType( FollowResourceExtender.RESOURCE_EXTENDER );
199             filter.setExtendableResourceType( strExtendableResourceType );
200             filter.setIdExtendableResource( strIdExtendableResource );
201             filter.setUserGuid( user.getName( ) );
202             filter.setAscSort( false );
203             filter.setSortedAttributeName( FollowConstants.ORDER_BY_DATE_CREATION );
204     
205             List<ResourceExtenderHistory> listHistories = _resourceExtenderHistoryService.findByFilter( filter );
206     
207             res = CollectionUtils.isNotEmpty( listHistories );
208         }
209         return res;
210     }
211 
212     @Override
213     public String getConfigHtml( ResourceExtenderDTO resourceExtender, Locale locale, HttpServletRequest request )
214     {
215         Map<String, Object> model = new HashMap< >( );        
216         model.put( MARK_CONFIG, _followExtenderConfigService.find( resourceExtender.getIdExtender( ) ) );
217         
218         HtmlTemplate html = AppTemplateService.getTemplate( TEMPLATE_FOLLOW_EXTENDER_CONFIG, locale, model );      
219         
220         return html.getHtml( );
221     }
222 
223     @Override
224     public IExtenderConfig getConfig( int nIdExtender )
225     {
226         return _followExtenderConfigService.find( nIdExtender );
227     }
228 
229     @Override
230     public void doSaveConfig( HttpServletRequest request, IExtenderConfig config ) throws ExtendErrorException
231     {
232         _followExtenderConfigService.update( config );
233     }
234 }