View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.parisvideo.web;
35  
36  import java.util.Arrays;
37  import java.util.Calendar;
38  import java.util.Collection;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.ArrayList;
42  import java.util.Locale;
43  import java.util.Map;
44  
45  import javax.servlet.http.HttpServletRequest;
46  
47  import com.tvnavig.server.searchengine.service.common.AssetOGC;
48  import com.tvnavig.server.searchengine.service.common.AssetPlayer;
49  import com.tvnavig.server.user.service.common.UserDomainResult;
50  
51  import fr.paris.lutece.plugins.parisvideo.business.ParisVideoAccount;
52  import fr.paris.lutece.plugins.parisvideo.business.ParisVideoHome;
53  import fr.paris.lutece.plugins.parisvideo.service.ParisvideoService;
54  import fr.paris.lutece.portal.service.admin.AdminUserService;
55  import fr.paris.lutece.portal.service.plugin.Plugin;
56  import fr.paris.lutece.portal.service.plugin.PluginService;
57  import fr.paris.lutece.portal.service.template.AppTemplateService;
58  import fr.paris.lutece.portal.service.util.AppPathService;
59  import fr.paris.lutece.portal.web.insert.InsertServiceJspBean;
60  import fr.paris.lutece.portal.web.insert.InsertServiceSelectionBean;
61  import fr.paris.lutece.util.html.HtmlTemplate;
62  import fr.paris.lutece.util.html.Paginator;
63  
64  /**
65   * This class provides the user interface to manage features ( manage, create,
66   * modify, remove )
67   */
68  public class ParisvideoJspBean extends InsertServiceJspBean implements InsertServiceSelectionBean
69  {
70  	private static final long serialVersionUID = 1L;
71  
72  	// Right
73  	public static final String RIGHT_MANAGE_PARISVIDEO = "PARISVIDEO_MANAGEMENT";
74  
75  	// templates
76  	private static final String TEMPLATE_VIDEO_CHOOSE = "/admin/plugins/parisvideo/list_videos.html";
77  	private static final String TEMPLATE_VIDEO = "/admin/plugins/parisvideo/voir_video.html";
78  
79  	// Markers
80  	private static final String MARK_PAGINATOR = "paginator";
81  	private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
82      private static final String PARAMETER_INPUT = "input";
83      private static final String BASE_URL = "baseUrl";
84      private static final String MARK_LIST_LIST = "videos";
85  
86  	private static final String TEMPLATE_AUT_1 = "/admin/plugins/parisvideo/auth_1.html";
87  	private static final String TEMPLATE_AUT_2 = "/admin/plugins/parisvideo/auth_2.html";
88  
89  	private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
90  
91      private String _input;
92  
93      //Variables
94      private int _nDefaultItemsPerPage = 6;
95      private String _strCurrentPageIndex;
96      private int _nItemsPerPage;
97  
98  	private List<AssetOGC>  _videoList;
99  
100 	private static Plugin _plugin = null;
101 
102     private void init( HttpServletRequest request )
103     {
104         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
105         //_user = AdminUserService.getAdminUser( request );
106         _plugin  = PluginService.getPlugin( strPluginName );
107         _input = request.getParameter( PARAMETER_INPUT );
108     }
109 
110     /**
111      * donne accès au form de saisie du login et mot de passe
112      */
113 	public String getInsertServiceSelectorUI(HttpServletRequest request) 
114 	{
115         init( request );
116 
117         Map<String, Object> model = new HashMap<String, Object>(  );
118         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
119         model.put( PARAMETER_INPUT, _input );
120         
121 		ParisVideoAccount parisVideoAccount = null;
122         Collection<ParisVideoAccount> _collec = ParisVideoHome.findAll( _plugin );
123         
124         if( _collec == null || _collec.size() == 0 )
125         {
126         	parisVideoAccount = new ParisVideoAccount();
127         } 
128         else 
129         {
130         	//we take the first one in database
131         	parisVideoAccount = (ParisVideoAccount) _collec.toArray()[0];
132         }
133         
134         ParisvideoService parisService = new ParisvideoService();
135         
136         List<UserDomainResult> domainList = new ArrayList<UserDomainResult>();
137 		try 
138 		{
139 	        UserDomainResult[] domainArray = null;
140 			domainArray = parisService.getDomain(parisVideoAccount.getLogin(), parisVideoAccount.getPassword());
141 			domainList = Arrays.asList( domainArray );
142 		} 
143 		catch (Exception e) 
144 		{
145 		}
146         
147         model.put( "domains", domainList );
148 
149         Locale locale = AdminUserService.getLocale( request );
150         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_AUT_2, locale, model );
151 
152         return template.getHtml(  );
153 	}
154 	
155 	/**
156 	 * video list for a domain
157 	 */
158 	public String doAuth2(HttpServletRequest request) throws Exception
159 	{
160         Map<String, Object> model = new HashMap<String, Object>(  );
161         model.put( PARAMETER_INPUT, request.getParameter( PARAMETER_INPUT ) );
162         
163         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
164         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
165 
166         String strDomain = request.getParameter("domain");
167         if( strDomain != null && strDomain.length()>0 )
168         {//if we come from the search form (and not from the pagination)
169 
170         	Integer domainId = Integer.decode( strDomain );
171 
172             String strBeginDate = request.getParameter("beginDate");
173             Calendar beginDate = getCalendarFromString(strBeginDate);
174             
175             String strEndDate = request.getParameter("endDate");
176             Calendar endDate = getCalendarFromString(strEndDate);
177 
178             String searchSubject = request.getParameter("searchSubject");
179             if(searchSubject!=null && searchSubject.length()==0)
180             	searchSubject = null;
181             
182 	        ParisvideoService parisService = new ParisvideoService();
183 	        _videoList = parisService.getVideos(domainId, searchSubject, null, beginDate, endDate, null);
184         }
185         
186         String strBaseUrl = AppPathService.getBaseUrl( request );
187         String strUrl = strBaseUrl + "jsp/admin/plugins/parisvideo/DoAuth2.jsp";
188 
189         Paginator paginator = new Paginator( _videoList, _nItemsPerPage, strUrl, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
190 
191         model.put( MARK_NB_ITEMS_PER_PAGE, "" + _nItemsPerPage );
192         model.put( MARK_PAGINATOR, paginator );
193         model.put( MARK_LIST_LIST, paginator.getPageItems(  ) );
194         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
195 
196         Locale locale = AdminUserService.getLocale( request );
197         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIDEO_CHOOSE, locale, model );
198 
199         return template.getHtml(  );
200 	}
201 
202 	/**
203 	 * 
204 	 * @param strBeginDate 20090523
205 	 * @return 
206 	 */
207 	private Calendar getCalendarFromString(String strDate) 
208 	{
209 		Calendar beginDate = null;
210 		if(strDate != null && strDate.length()>0)
211 		{
212 			beginDate = Calendar.getInstance();
213 			String strYear = strDate.substring(0, 4);
214 			int year = Integer.parseInt(strYear);
215 			String strMonth = strDate.substring(5, 7);
216 			int month = Integer.parseInt(strMonth);
217 			String strDay = strDate.substring(8, 10);
218 			int date = Integer.parseInt(strDay);
219 			beginDate.set(year, month, date);
220 		}
221 		return beginDate;
222 	}
223 	
224 	/**
225 	 * 
226 	 */
227 	public String doVideoChoose(HttpServletRequest request) throws Exception
228 	{
229         Map<String, Object> model = new HashMap<String, Object>(  );
230         
231         if(_input == null)
232         	_input = request.getParameter( PARAMETER_INPUT );
233         
234         model.put( PARAMETER_INPUT, _input );
235         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
236 
237         String strVideoId = request.getParameter("videoId");
238         int videoId = Integer.parseInt(strVideoId);
239 
240         ParisvideoService parisService = new ParisvideoService();
241         AssetPlayer ap = parisService.getVideo(videoId);
242 
243         String strHtmlCode = ap.getHtmlEmbed();
244         
245         return insertUrlWithoutEscape( request, _input, strHtmlCode );
246 	}
247 
248 	/**
249 	 * 
250 	 */
251 	public String doVoir(HttpServletRequest request) throws Exception
252 	{
253         String strVideoId = request.getParameter("id");
254         int videoId = Integer.parseInt(strVideoId);
255 
256         ParisvideoService parisService = new ParisvideoService();
257         AssetPlayer ap = parisService.getVideo(videoId);
258 
259         Map<String, String> model = new HashMap<String, String>(  );
260         model.put( BASE_URL, AppPathService.getBaseUrl( request ) );
261         model.put( "code", ap.getHtmlEmbed() );
262 
263         Locale locale = AdminUserService.getLocale( request );
264         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIDEO, locale, model );
265 
266         return template.getHtml();
267 	}
268 }