View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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.parisconnect.web;
35   
36  import fr.paris.lutece.plugins.parisconnect.business.Alert;
37  import fr.paris.lutece.plugins.parisconnect.business.AlertHome;
38  import fr.paris.lutece.plugins.parisconnect.business.Category;
39  import fr.paris.lutece.plugins.parisconnect.business.CategoryHome;
40  import fr.paris.lutece.plugins.parisconnect.business.Newsletter;
41  import fr.paris.lutece.plugins.parisconnect.service.ParisConnectService;
42  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43  import fr.paris.lutece.portal.web.xpages.XPage;
44  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
47  import fr.paris.lutece.util.ReferenceList;
48  import fr.paris.lutece.util.url.UrlItem;
49  import fr.paris.lutece.portal.service.message.SiteMessageService;
50  import fr.paris.lutece.portal.service.message.SiteMessage;
51  import fr.paris.lutece.portal.service.message.SiteMessageException;
52  import fr.paris.lutece.portal.service.security.LuteceUser;
53  import fr.paris.lutece.portal.service.security.SecurityService;
54  import fr.paris.lutece.portal.service.security.UserNotSignedException;
55  
56  import java.util.ArrayList;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Map;
60  import javax.servlet.http.HttpServletRequest; 
61  
62  import org.apache.commons.lang.StringUtils;
63  
64  /**
65   * This class provides the user interface to manage Alert xpages ( manage, create, modify, remove )
66   */
67  @Controller( xpageName = "alert" , pageTitleI18nKey = "parisconnect.xpage.alert.pageTitle" , pagePathI18nKey = "parisconnect.xpage.alert.pagePathLabel" )
68  public class AlertXPage extends MVCApplication
69  {
70      // Templates
71      private static final String TEMPLATE_MANAGE_ALERTS="/skin/plugins/parisconnect/manage_alerts.html";
72       
73      
74      // Parameters
75      private static final String PARAMETER_ID_ALERT="id";
76      private static final String KEY_NO_CATEGORY = "no_category";
77      
78      
79      // Markers
80      private static final String MARK_ALERT_LIST = "alert_list";
81      private static final String MARK_USER_ALERT_HASH = "user_alert_hash";
82      private static final String MARK_CATEGORY_LIST = "category_list";
83      private static final String MARK_CATEGORY_MAP = "category_map";
84      private static final String MARK_CATEGORY_LIST_ALERT_HASH = "category_list_alert_hash";
85      
86      
87      // Message
88      
89      // Views
90      private static final String VIEW_MANAGE_ALERTS = "manageAlerts";
91  
92      // Actions
93      
94      private static final String ACTION_SUBSCRIBE_ALERT = "subscribeAlert";
95      private static final String ACTION_UNSUBSCRIBE_ALERT = "unSubscribeAlert";
96      
97  
98  
99     
100     @View( value = VIEW_MANAGE_ALERTS, defaultView = true )
101     public XPage getManageAlerts( HttpServletRequest request )
102     {
103     	
104         Map<String, Object> model = getModel(  );
105         
106         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser(request);
107    	 
108    	 
109  	   
110     	HashMap<String,Alert> userAlertMap=new HashMap<>();
111     	
112     	if(user!=null)
113    	 	{
114     		List<Alert> listUserAlert=ParisConnectService.getInstance().getUserAlerts(user.getName());
115         	
116         	
117 	    	if(listUserAlert != null)
118 	    	{
119 	    		for(Alert alertUser:listUserAlert)
120 		    	{
121 	    			userAlertMap.put(alertUser.getIdPc(),alertUser);
122 		    	}
123 	    	}
124    	 	}
125     	
126     	List<Alert> listAlert=ParisConnectService.getInstance().getAlerts();
127     	HashMap<String ,List<Alert>> mapCategoryListAlert=new HashMap<>();
128     	mapCategoryListAlert.put(KEY_NO_CATEGORY, new ArrayList<Alert>());
129     
130     	List<Category> listCategory=CategoryHome.getCategorysList();
131     	
132      
133 	 	if(listCategory!=null && listCategory.size() > 0)
134 		{
135 	 		HashMap<String, Category> mapCategory=new HashMap<>();
136 	 		for(Category category:listCategory)
137 	 		{
138 	 			mapCategory.put(category.getCode(), category);
139 	 		}
140 	 		model.put( MARK_CATEGORY_MAP,mapCategory);
141 		}
142 	 	
143 	 	for(Alert alert:listAlert)
144 	 	{
145 	 		
146 	 		if(alert.getCategory()!=null)
147 	 		{
148 	 			if(!mapCategoryListAlert.containsKey(alert.getCategory()))
149 	 			{
150 	 				mapCategoryListAlert.put(alert.getCategory(), new ArrayList<Alert>());
151 	 			}
152 	 			mapCategoryListAlert.get(alert.getCategory()).add(alert);
153 	 		}
154 	 		else
155 	 		{
156 	 			mapCategoryListAlert.get(KEY_NO_CATEGORY).add(alert);
157 	 		}
158 	 		
159 	 	}
160 
161      
162      
163 
164        model.put( MARK_USER_ALERT_HASH, userAlertMap);
165        model.put( MARK_ALERT_LIST, listAlert);
166        model.put( MARK_CATEGORY_LIST,listCategory );
167        model.put(MARK_CATEGORY_LIST_ALERT_HASH,mapCategoryListAlert);
168        
169 
170         return getXPage( TEMPLATE_MANAGE_ALERTS, request.getLocale(  ), model );
171     }
172     
173     
174 
175     /**
176      * Handles the removal form of a newsletter
177      *
178      * @param request The Http request
179      * @return the jsp URL to display the form to manage newsletters
180      */
181     @Action( ACTION_SUBSCRIBE_ALERT )
182     public XPage doSubscribe( HttpServletRequest request ) throws UserNotSignedException
183     {
184     	LuteceUser user = SecurityService.getInstance( ).getRemoteUser(request);
185     	 
186     	 
187     	 if(user==null)
188     	 {
189     	      throw new UserNotSignedException(  );
190     	 }
191         String strIdAlert= request.getParameter( PARAMETER_ID_ALERT) ;
192 	    if(!StringUtils.isEmpty(strIdAlert))
193 	    {
194 	    	ParisConnectService.getInstance().subscribeAlert(user.getName(), strIdAlert);
195 	    	
196 	    }
197         
198       return redirectView( request, VIEW_MANAGE_ALERTS );
199     }
200     
201     /**
202      * Handles the removal form of a newsletter
203      *
204      * @param request The Http request
205      * @return the jsp URL to display the form to manage newsletters
206      */
207     @Action( ACTION_UNSUBSCRIBE_ALERT )
208     public XPage doUnSubscribe( HttpServletRequest request ) throws UserNotSignedException
209     {
210     	LuteceUser user = SecurityService.getInstance( ).getRemoteUser(request);
211    	 
212    	 
213    	 if(user==null)
214    	 {
215    	      throw new UserNotSignedException(  );
216    	 }
217        String strIdAlert= request.getParameter( PARAMETER_ID_ALERT) ;
218 	    if(!StringUtils.isEmpty(strIdAlert))
219 	    {
220 	    	ParisConnectService.getInstance().unSubscribeAlert(user.getName(), strIdAlert);
221 	    	
222 	    }
223        
224      return redirectView( request, VIEW_MANAGE_ALERTS );
225     }
226 
227    
228 }