1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.crm.modules.mydashboard.web;
35
36 import java.text.SimpleDateFormat;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Map;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.apache.commons.lang.StringUtils;
47
48 import fr.paris.lutece.plugins.crm.business.demand.Demand;
49 import fr.paris.lutece.plugins.crm.business.demand.DemandFilter;
50 import fr.paris.lutece.plugins.crm.business.demand.DemandStatusCRM;
51 import fr.paris.lutece.plugins.crm.business.demand.DemandType;
52 import fr.paris.lutece.plugins.crm.business.demand.PaginationFilterSortManager;
53 import fr.paris.lutece.plugins.crm.business.user.CRMUser;
54 import fr.paris.lutece.plugins.crm.service.category.CategoryService;
55 import fr.paris.lutece.plugins.crm.service.demand.DemandService;
56 import fr.paris.lutece.plugins.crm.service.demand.DemandStatusCRMService;
57 import fr.paris.lutece.plugins.crm.service.demand.DemandTypeService;
58 import fr.paris.lutece.plugins.crm.service.parameters.AdvancedParametersService;
59 import fr.paris.lutece.plugins.crm.service.user.CRMUserAttributesService;
60 import fr.paris.lutece.plugins.crm.service.user.CRMUserService;
61 import fr.paris.lutece.plugins.crm.util.ListUtils;
62 import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
63 import fr.paris.lutece.plugins.mydashboard.service.MyDashboardComponent;
64 import fr.paris.lutece.portal.service.i18n.I18nService;
65 import fr.paris.lutece.portal.service.security.LuteceUser;
66 import fr.paris.lutece.portal.service.security.SecurityService;
67 import fr.paris.lutece.portal.service.template.AppTemplateService;
68 import fr.paris.lutece.portal.service.util.AppLogService;
69 import fr.paris.lutece.portal.service.util.AppPathService;
70 import fr.paris.lutece.portal.web.l10n.LocaleService;
71 import fr.paris.lutece.util.html.HtmlTemplate;
72 import fr.paris.lutece.util.html.IPaginator;
73
74
75
76
77 public class MyDashboardCRMComponent extends MyDashboardComponent
78 {
79 private static final String DASHBOARD_COMPONENT_ID = "crm-mydashboard.demandsComponent";
80 private static final String MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION = "module.crm.mydashboard.component.demands.description";
81 private static final String TEMPLATE_DASHBOARD_COMPONENT = "skin/plugins/crm/modules/mydashboard/demands_component.html";
82 private static final String MARK_XPAGE_MYDASHBOARD = "mydashboard";
83
84
85
86 @Override
87 public String getDashboardData( HttpServletRequest request )
88 {
89 LuteceUser user = SecurityService.getInstance().getRegisteredUser(request);
90
91 createOrUpdateCRMAccount( user );
92
93 CRMUserService crmUserService = CRMUserService.getService( );
94
95 CRMUser crmUser = crmUserService.findByUserGuid( user.getName() );
96
97 if( crmUser != null )
98 {
99 Map<String, Object> model = new HashMap<String, Object>( );
100 if ( crmUser != null )
101 {
102
103 DemandFilter dFilter = new DemandFilter( );
104 dFilter.setIdCRMUser( crmUser.getIdCRMUser( ) );
105
106 PaginationFilterSortManager paginationFilterSortManager = new PaginationFilterSortManager( request,MARK_XPAGE_MYDASHBOARD);
107
108 String strSession = (String) ( request.getParameter( CRMConstants.PARAMETER_SESSION ) );
109
110 if ( StringUtils.isBlank( strSession ) )
111 {
112 paginationFilterSortManager.cleanSession( );
113 }
114
115 String strIdStatusToSort = (String) ( request.getParameter( CRMConstants.PARAMETER_ID_STATUS ) );
116 String strSortField = (String) ( request.getParameter( CRMConstants.PARAMETER_SORT_ATTRIBUTE ) );
117 String strSortOrder = (String) ( request.getParameter( CRMConstants.PARAMETER_SORT_ORDER ) );
118
119 int nIdStatusToSort = -1;
120
121 if ( StringUtils.isNotEmpty( strIdStatusToSort ) )
122 {
123 nIdStatusToSort = Integer.parseInt( strIdStatusToSort );
124 }
125
126 if ( StringUtils.isNotEmpty( strSortField ) && StringUtils.isNotEmpty( strSortOrder ) )
127 {
128 paginationFilterSortManager.storeSort( nIdStatusToSort, strSortField,
129 Boolean.parseBoolean( strSortOrder ) );
130 }
131
132 String strModificationDate = request.getParameter( CRMConstants.PARAMETER_MODIFICATIONDATE );
133 String strDemandType = request.getParameter( CRMConstants.PARAMETER_DEMANDTYPE );
134 String strNotification = request.getParameter( CRMConstants.PARAMETER_NOTIFICATION );
135
136 if ( StringUtils.isNotBlank( strModificationDate ) || StringUtils.isNotBlank( strDemandType ) ||
137 StringUtils.isNotBlank( strNotification ) )
138 {
139 paginationFilterSortManager.cleanSessionFilter( );
140 }
141
142 if ( StringUtils.isNotBlank( strModificationDate ) )
143 {
144 Date modificationDate = checkFormatModificationDateFilter( strModificationDate, request );
145 paginationFilterSortManager.storeFilterModificationDate( modificationDate );
146 paginationFilterSortManager.storeFilterStringModificationDate( strModificationDate );
147 }
148
149 if ( StringUtils.isNotBlank( strDemandType ) )
150 {
151 int nIdDemandType = Integer.parseInt( strDemandType );
152 paginationFilterSortManager.storeFilterDemandType( nIdDemandType );
153 }
154
155 if ( StringUtils.isNotBlank( strNotification ) )
156 {
157 paginationFilterSortManager.storeFilterNotification( strNotification );
158 }
159
160 Date dateModificationSession = paginationFilterSortManager.retrieveFilterModificationDate( );
161
162 if ( dateModificationSession != null )
163 {
164 dFilter.setDateModification( dateModificationSession );
165 model.put( CRMConstants.MARK_MODIFICATIONDATE,
166 paginationFilterSortManager.retrieveFilterStringModificationDate( ) );
167 }
168
169 Integer nIdDemandTypeSession = paginationFilterSortManager.retrieveFilterDemandType( );
170
171 if ( ( nIdDemandTypeSession != null ) && ( nIdDemandTypeSession >= 0 ) )
172 {
173 dFilter.setIdDemandType( nIdDemandTypeSession );
174 }
175
176 String strNotificationSession = paginationFilterSortManager.retrieveFilterNotification( );
177
178 if ( StringUtils.isNotBlank( strNotificationSession ) )
179 {
180 dFilter.setNotification( strNotificationSession );
181 }
182
183 model.put( CRMConstants.MARK_MAP_DEMANDS_LIST,
184 DemandService.getService().findByFilterMap( dFilter, request.getLocale( ), paginationFilterSortManager ) );
185 model.put( CRMConstants.MARK_FILTER, dFilter );
186
187 Map<String, IPaginator<Demand>> mapPaginator = new HashMap<String, IPaginator<Demand>>( );
188 Map<String, String> mapNbItemsPerPage = new HashMap<String, String>( );
189 int nIdStatus;
190
191 for ( DemandStatusCRM statusCRM : DemandStatusCRMService.getService( )
192 .getAllStatusCRM( request.getLocale( ) ) )
193 {
194 nIdStatus = statusCRM.getIdStatusCRM( );
195
196 IPaginator<Demand> paginator = paginationFilterSortManager.retrievePaginator( nIdStatus );
197 int nItemsPerPage = paginationFilterSortManager.retrieveItemsPerPage( nIdStatus );
198
199 mapNbItemsPerPage.put( Integer.toString( nIdStatus ), Integer.toString( nItemsPerPage ) );
200 mapPaginator.put( Integer.toString( nIdStatus ), paginator );
201 }
202 model.put( CRMConstants.MARK_STATUS_CRM_LIST, DemandStatusCRMService.getService( ).getAllStatusCRM( request.getLocale( ) ) );
203
204 model.put( CRMConstants.MARK_MAP_PAGINATOR, mapPaginator );
205 model.put( CRMConstants.MARK_MAP_NB_ITEMS_PER_PAGE, mapNbItemsPerPage );
206 model.put( CRMConstants.MARK_DISPLAYDRAFT,
207 AdvancedParametersService.getService( ).isParameterValueByKey( CRMConstants.CONSTANT_DISPLAYDRAFT ) );
208
209 }
210 model.put( CRMConstants.MARK_LOCALE, request.getLocale( ) );
211 model.put( CRMConstants.MARK_MAP_DEMAND_TYPES_LIST, DemandTypeService.getService().findForLuteceUser( request ) );
212 model.put( CRMConstants.MARK_CATEGORIES_LIST,
213 CategoryService.getService().getCategories( request.getLocale( ), false, true ) );
214 model.put( CRMConstants.MARK_DEMAND_TYPES_LIST, DemandTypeService.getService().findAll( ) );
215
216 model.put( CRMConstants.MARK_CRM_USER, crmUser );
217 List<DemandType> listAllOpenedDemandType = initListAllOpenedDemandType( );
218
219 model.put( CRMConstants.MARK_DEMAND_TYPES_REFLIST,
220 ListUtils.toReferenceList( listAllOpenedDemandType, "idDemandType", "label", "" ) );
221
222 model.put(CRMConstants.MARK_MAP_DO_LOGIN,SecurityService.getInstance( ).getLoginPageUrl() );
223 model.put(CRMConstants.MARK_BASE_URL, AppPathService.getBaseUrl(request));
224 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DASHBOARD_COMPONENT,
225 LocaleService.getDefault( ), model );
226
227 return template.getHtml( );
228 }
229 return "";
230 }
231
232
233
234
235
236 private List<DemandType> initListAllOpenedDemandType( )
237 {
238 List<DemandType> listAllDemandType = DemandTypeService.getService( ).findAll( );
239 List<DemandType> listAllOpenedDemandType = new ArrayList<DemandType>( );
240
241 for ( DemandType demandType : listAllDemandType )
242 {
243 if ( demandType.isOpen( ) )
244 {
245 listAllOpenedDemandType.add( demandType );
246 }
247 }
248
249 return listAllOpenedDemandType;
250 }
251
252
253
254
255 private Date checkFormatModificationDateFilter( String strModificationDate, HttpServletRequest request )
256
257 {
258 SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" );
259 sdf.setLenient( true );
260
261 Date d = new Date( );
262
263 try
264 {
265 d = sdf.parse( strModificationDate );
266 }
267 catch ( Exception e )
268 {
269 AppLogService.error(e);
270 }
271
272
273 return d;
274 }
275
276 @Override
277 public String getComponentId( )
278 {
279 return DASHBOARD_COMPONENT_ID;
280 }
281
282 @Override
283 public String getComponentDescription( Locale locale )
284 {
285 return I18nService.getLocalizedString( MESSAGE_DASHBOARD_COMPONENT_DESCRIPTION, locale );
286 }
287
288
289
290
291
292
293 private void createOrUpdateCRMAccount( LuteceUser user )
294 {
295 if( user!=null )
296 {
297
298 CRMUser crmUser = CRMUserService.getService().findByUserGuid( user.getName( ) );
299
300 if ( crmUser == null )
301 {
302 crmUser = new CRMUser( );
303 crmUser.setUserGuid( user.getName( ) );
304 crmUser.setStatus( CRMUser.STATUS_ACTIVATED );
305
306 Map<String, String> userAttributes = new HashMap<String, String>( );
307
308 for ( String strUserAttributeKey : CRMUserAttributesService.getService().getUserAttributeKeys( ) )
309 {
310 userAttributes.put( strUserAttributeKey, user.getUserInfo( strUserAttributeKey ) );
311 }
312
313 crmUser.setUserAttributes( userAttributes );
314 CRMUserService.getService().create( crmUser );
315 }
316 else if ( crmUser.isMustBeUpdated())
317 {
318 crmUser.setMustBeUpdated(false);
319 crmUser.setStatus( CRMUser.STATUS_ACTIVATED );
320 Map<String, String> userAttributes = new HashMap<String, String>( );
321 for ( String strUserAttributeKey : CRMUserAttributesService.getService().getUserAttributeKeys( ) )
322 {
323 userAttributes.put( strUserAttributeKey, user.getUserInfo( strUserAttributeKey ) );
324 }
325
326 crmUser.setUserAttributes( userAttributes );
327 CRMUserService.getService().update( crmUser );
328
329 }
330 }
331 }
332
333
334
335 }