View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.mydashboard.modules.grusupply.web;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.inject.Inject;
43  import javax.inject.Named;
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.collections.CollectionUtils;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import fr.paris.lutece.plugins.grubusiness.business.notification.EnumNotificationType;
50  import fr.paris.lutece.plugins.grubusiness.business.web.rs.DemandDisplay;
51  import fr.paris.lutece.plugins.grubusiness.business.web.rs.DemandResult;
52  import fr.paris.lutece.plugins.grubusiness.business.web.rs.NotificationResult;
53  import fr.paris.lutece.plugins.mydashboard.modules.grusupply.business.DemandDashboard;
54  import fr.paris.lutece.plugins.mydashboard.modules.grusupply.business.DemandDashboardHome;
55  import fr.paris.lutece.plugins.mydashboard.modules.grusupply.service.NotificationGruService;
56  import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent;
57  import fr.paris.lutece.portal.service.i18n.I18nService;
58  import fr.paris.lutece.portal.service.security.LuteceUser;
59  import fr.paris.lutece.portal.service.security.SecurityService;
60  import fr.paris.lutece.portal.service.template.AppTemplateService;
61  import fr.paris.lutece.portal.service.util.AppPropertiesService;
62  import fr.paris.lutece.util.html.HtmlTemplate;
63  
64  /**
65   * 
66   * MyDashboardComponentLastNotificationGRU
67   *
68   */
69  public class MyDashboardComponentLastNotificationGRU extends MyDashboardComponent
70  {
71  
72      /**
73       * 
74       */
75      private static final long      serialVersionUID                = 8297192924908575568L;
76  
77      private static final String    TEMPLATE_LAST_NOTIFICATION_LIST = "skin/plugins/mydashboard/modules/grusupply/dashboard_last_demand.html";
78      private static final String    DASHBOARD_COMPONENT_ID          = "mydashboard-grusupply.componentLastNotification";
79      private static final String    MESSAGE_COMPONENT_DESCRIPTION   = "module.mydashboard.grusupply.myDashboardComponentLastNotification.description";
80      private static final String    PROPERTY_LIMIT_RESULT           = AppPropertiesService.getProperty( "mydashboard-grusupply.limit.result.lastnotification", "5" );
81      // MARKS
82      private static final String    MARK_DEMAND_TYPE_LIST           = "demand_types_list";
83      private static final String    MARK_LIST_DEMAND                = "list_demands";
84  
85      @Inject
86      @Named( NotificationGruService.BEAN_NAME )
87      private NotificationGruService _notificationService;
88  
89      @Override
90      public String getDashboardData( HttpServletRequest request )
91      {
92          LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
93  
94          if ( user != null )
95          {
96              Map<String, Object> model = new HashMap<>( );
97              
98              DemandResult demandResult = _notificationService.getListDemand( user.getName( ), "1", PROPERTY_LIMIT_RESULT, EnumNotificationType.MYDASHBOARD.toString( ) );
99              List<DemandDashboard> listDemandDashboards = new ArrayList<>( );
100             
101             if ( demandResult != null && CollectionUtils.isNotEmpty( demandResult.getListDemandDisplay( ) ) )
102             {
103                 for( DemandDisplay demand : demandResult.getListDemandDisplay( ) )
104                 {
105                     NotificationResult notificationList = _notificationService.getListNotification( demand.getDemand( ).getId( ), demand.getDemand( ).getTypeId( ), user.getName( ) );
106                     
107                     DemandDashboardd/modules/grusupply/business/DemandDashboard.html#DemandDashboard">DemandDashboard demandDashboard = new DemandDashboard( demand.getDemand( ).getUID( ) , false );
108                     demandDashboard.setDemand( demand.getDemand( ) );
109                     demandDashboard.setStatus( demand.getStatus( ) );
110                     if ( notificationList != null && notificationList.getNotifications( ) != null )
111                     {
112                         demandDashboard.setListNotification( notificationList.getNotifications( ) );
113                     }
114                     listDemandDashboards.add( demandDashboard );
115                 }                
116                 listDemandDashboards = DemandDashboardHome.selectByDemandIds( listDemandDashboards );
117             }
118 
119             model.put( MARK_DEMAND_TYPE_LIST, _notificationService.getListDemandType( ) );
120             model.put( MARK_LIST_DEMAND, listDemandDashboards );
121 
122             HtmlTemplate htmTemplate = AppTemplateService.getTemplate( TEMPLATE_LAST_NOTIFICATION_LIST, request.getLocale( ), model );
123 
124             return htmTemplate.getHtml( );
125         }
126 
127         return StringUtils.EMPTY;
128     }
129 
130     @Override
131     public String getComponentId( )
132     {
133         return DASHBOARD_COMPONENT_ID;
134     }
135 
136     @Override
137     public String getComponentDescription( Locale locale )
138     {
139         return I18nService.getLocalizedString( MESSAGE_COMPONENT_DESCRIPTION, locale );
140     }
141 
142 }