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.document.modules.calendar.service;
35  
36  import fr.paris.lutece.plugins.calendar.business.CalendarHome;
37  import fr.paris.lutece.plugins.calendar.service.AgendaResource;
38  import fr.paris.lutece.plugins.calendar.service.CalendarPlugin;
39  import fr.paris.lutece.plugins.calendar.service.CalendarResourceIdService;
40  import fr.paris.lutece.plugins.calendar.web.CalendarJspBean;
41  import fr.paris.lutece.plugins.document.business.Document;
42  import fr.paris.lutece.plugins.document.business.workflow.DocumentAction;
43  import fr.paris.lutece.plugins.document.modules.calendar.business.MappingHome;
44  import fr.paris.lutece.plugins.document.modules.calendar.web.DocumentToCalendarJspBean;
45  import fr.paris.lutece.plugins.document.service.IDocumentActionsService;
46  import fr.paris.lutece.portal.business.rbac.RBAC;
47  import fr.paris.lutece.portal.business.user.AdminUser;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  import fr.paris.lutece.portal.service.rbac.RBACService;
51  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
52  
53  import java.util.ArrayList;
54  import java.util.List;
55  import java.util.Locale;
56  
57  
58  /**
59   * This Service manages mapping actions
60   */
61  public class DocumentActionsService extends PluginAdminPageJspBean implements IDocumentActionsService
62  {
63      private static final String PLUGIN_NAME = "documenttocalendar";
64      private static final String PARAMETER_BUTTON_NAME = "module.document.calendar.workflow.action.addEvent.name";
65      private static final String PARAMETER_BUTTON_DESCRIPTION = "module.document.calendar.workflow.action.addEvent.description";
66      private static final String PARAMETER_BUTTON_ACTION_URL = "jsp/admin/plugins/document/modules/calendar/SelectCalendar.jsp?";
67      /*
68       * private static final String PARAMETER_BUTTON_ICON_URL =
69       * "images/admin/skin/actions/calendar_add.png";
70       */
71      private static final String PARAMETER_BUTTON_ICON_URL = "icon-calendar";
72  
73      /**
74       * Return all actions to add to the document
75       * @param document The document
76       * @param locale The Locale
77       * @param user The current user
78       * @return document action list
79       */
80      public List<DocumentAction> getActions( Document document, Locale locale, AdminUser user )
81      {
82          List<DocumentAction> documentActionList = new ArrayList<DocumentAction>( );
83  
84          Plugin plugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
85  
86          //If the document has a mapping
87          if ( MappingHome.findByCodeDocumentType( document.getCodeDocumentType( ), plugin ) != null )
88          {
89              //If the user has permission
90              if ( RBACService.isAuthorized( CalendarResourceIdService.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
91                      CalendarResourceIdService.PERMISSION_MANAGE, user )
92                      && user.checkRight( DocumentToCalendarJspBean.RIGHT_MANAGE_DOCUMENTTOCALENDAR )
93                      && user.checkRight( CalendarJspBean.RIGHT_MANAGE_CALENDAR ) )
94              {
95                  Plugin calendarPlugin = PluginService.getPlugin( CalendarPlugin.PLUGIN_NAME );
96  
97                  List<AgendaResource> calendarList = CalendarHome.findAgendaResourcesList( calendarPlugin );
98  
99                  //If there is a calendar
100                 if ( calendarList.size( ) != 0 )
101                 {
102                     DocumentAction documentAction = new DocumentAction( );
103 
104                     documentAction.setNameKey( PARAMETER_BUTTON_NAME );
105                     documentAction.setDescriptionKey( PARAMETER_BUTTON_DESCRIPTION );
106                     documentAction.setUrl( PARAMETER_BUTTON_ACTION_URL );
107                     documentAction.setIconUrl( PARAMETER_BUTTON_ICON_URL );
108                     documentAction.setLocale( locale );
109 
110                     documentActionList.add( documentAction );
111                 }
112             }
113         }
114 
115         return documentActionList;
116     }
117 }