View Javadoc

1   /*
2    * Copyright (c) 2002-2014, 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.digglike.web.action;
35  
36  import fr.paris.lutece.plugins.digglike.business.Digg;
37  import fr.paris.lutece.plugins.digglike.business.DiggSubmit;
38  import fr.paris.lutece.plugins.digglike.service.DiggSubmitService;
39  import fr.paris.lutece.plugins.digglike.service.DigglikePlugin;
40  import fr.paris.lutece.plugins.digglike.service.DigglikeResourceIdService;
41  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
42  import fr.paris.lutece.plugins.digglike.web.DiggJspBean;
43  import fr.paris.lutece.portal.business.user.AdminUser;
44  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
45  import fr.paris.lutece.portal.service.message.AdminMessage;
46  import fr.paris.lutece.portal.service.message.AdminMessageService;
47  import fr.paris.lutece.portal.service.plugin.Plugin;
48  import fr.paris.lutece.portal.service.plugin.PluginService;
49  import fr.paris.lutece.portal.service.rbac.RBACService;
50  import fr.paris.lutece.portal.web.pluginaction.AbstractPluginAction;
51  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
52  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
53  
54  import org.apache.commons.lang.StringUtils;
55  
56  import java.util.Map;
57  
58  import javax.servlet.http.HttpServletRequest;
59  import javax.servlet.http.HttpServletResponse;
60  
61  
62  /**
63   *
64   * MassPinnedDiggSubmitAction
65   *
66   */
67  public class MassChangeVoteDiggSubmitAction extends AbstractPluginAction<DigglikeAdminSearchFields>
68      implements IDigglikeAction
69  {
70      private static final String ACTION_NAME = "Mass Change Vote DiggSubmit ";
71      private static final String MESSAGE_YOU_MUST_SELECT_DIGG_SUBMIT = "digglike.message.youMustSelectDiggSubmit";
72      private static final String PARAMETER_MASS_DISABLE_ACTION = "mass_disable_vote_action";
73      private static final String PARAMETER_MASS_ENABLE_ACTION = "mass_enable_vote_action";
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public void fillModel( HttpServletRequest request, AdminUser adminUser, Map<String, Object> model )
80      {
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public String getName(  )
88      {
89          return ACTION_NAME;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public String getButtonTemplate(  )
97      {
98          return null;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean isInvoked( HttpServletRequest request )
106     {
107         return ( ( request.getParameter( PARAMETER_MASS_DISABLE_ACTION ) != null ) ||
108         ( request.getParameter( PARAMETER_MASS_ENABLE_ACTION ) != null ) );
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public IPluginActionResult process( HttpServletRequest request, HttpServletResponse response, AdminUser adminUser,
116         DigglikeAdminSearchFields searchFields ) throws AccessDeniedException
117     {
118         IPluginActionResult result = new DefaultPluginActionResult(  );
119 
120         int nIdDiggSubmit;
121         String strRedirect = DiggJspBean.getJspManageDiggSubmit( request );
122 
123         if ( ( searchFields.getSelectedDiggSubmit(  ) != null ) && !searchFields.getSelectedDiggSubmit(  ).isEmpty(  ) )
124         {
125             //test All ressource selected before update
126             for ( String strIdDiggSubmit : searchFields.getSelectedDiggSubmit(  ) )
127             {
128                 if ( StringUtils.isNotBlank( strIdDiggSubmit ) && StringUtils.isNumeric( strIdDiggSubmit ) )
129                 {
130                     nIdDiggSubmit = DiggUtils.getIntegerParameter( strIdDiggSubmit );
131 
132                     DiggSubmit diggSubmit = DiggSubmitService.getService(  )
133                                                              .findByPrimaryKey( nIdDiggSubmit, false, getPlugin(  ) );
134 
135                     if ( ( diggSubmit == null ) ||
136                             !RBACService.isAuthorized( Digg.RESOURCE_TYPE,
137                                 DiggUtils.EMPTY_STRING + diggSubmit.getDigg(  ).getIdDigg(  ),
138                                 DigglikeResourceIdService.PERMISSION_MANAGE_DIGG_SUBMIT, adminUser ) )
139                     {
140                         throw new AccessDeniedException(  );
141                     }
142                 }
143             }
144 
145             boolean isDisabledVote = ( request.getParameter( PARAMETER_MASS_DISABLE_ACTION ) != null );
146 
147             //update all digg submit selected
148             for ( String strIdDiggSubmittoUpdate : searchFields.getSelectedDiggSubmit(  ) )
149             {
150                 if ( StringUtils.isNotBlank( strIdDiggSubmittoUpdate ) &&
151                         StringUtils.isNumeric( strIdDiggSubmittoUpdate ) )
152                 {
153                     nIdDiggSubmit = DiggUtils.getIntegerParameter( strIdDiggSubmittoUpdate );
154 
155                     DiggSubmit diggSubmit = DiggSubmitService.getService(  )
156                                                              .findByPrimaryKey( nIdDiggSubmit, false, getPlugin(  ) );
157                     diggSubmit.setDisableVote( isDisabledVote );
158                     DiggSubmitService.getService(  ).update( diggSubmit, getPlugin(  ) );
159                 }
160             }
161         }
162         else
163         {
164             strRedirect = AdminMessageService.getMessageUrl( request, MESSAGE_YOU_MUST_SELECT_DIGG_SUBMIT,
165                     AdminMessage.TYPE_INFO );
166         }
167 
168         result.setRedirect( strRedirect );
169 
170         return result;
171     }
172 
173     /**
174      * Gets the plugin
175      * @return the plugin
176      */
177     private Plugin getPlugin(  )
178     {
179         return PluginService.getPlugin( DigglikePlugin.PLUGIN_NAME );
180     }
181 }