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