View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.phraseanet.web;
35  
36  import fr.paris.lutece.plugins.phraseanet.business.account.Account;
37  import fr.paris.lutece.plugins.phraseanet.business.account.AccountHome;
38  import fr.paris.lutece.plugins.phraseanet.business.databox.Databox;
39  import fr.paris.lutece.plugins.phraseanet.business.media.MediaHandler;
40  import fr.paris.lutece.plugins.phraseanet.business.media.MediaHandlerHome;
41  import fr.paris.lutece.plugins.phraseanet.business.record.Metadata;
42  import fr.paris.lutece.plugins.phraseanet.business.template.Template;
43  import fr.paris.lutece.plugins.phraseanet.business.template.TemplateHome;
44  import fr.paris.lutece.plugins.phraseanet.dto.recordtype.RecordTypeDTO;
45  import fr.paris.lutece.plugins.phraseanet.service.Constants;
46  import fr.paris.lutece.plugins.phraseanet.service.PhraseanetService;
47  import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiAuthentication;
48  import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiCallException;
49  import fr.paris.lutece.portal.service.admin.AdminUserService;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  import fr.paris.lutece.portal.service.template.AppTemplateService;
53  import fr.paris.lutece.portal.service.util.AppLogService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
56  import fr.paris.lutece.portal.web.constants.Messages;
57  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
58  import fr.paris.lutece.util.html.HtmlTemplate;
59  import fr.paris.lutece.util.html.Paginator;
60  import fr.paris.lutece.util.url.UrlItem;
61  import java.util.ArrayList;
62  import java.util.Collection;
63  import java.util.HashMap;
64  import java.util.List;
65  import java.util.Locale;
66  import java.util.Map;
67  import javax.servlet.http.HttpServletRequest;
68  import org.apache.commons.lang.StringUtils;
69  import org.apache.log4j.Logger;
70  
71  
72  /**
73   * This class provides the user interface to manage  media handlers
74   * ( manage, create, modify, remove )
75   */
76  public class PhraseanetJspBean extends PluginAdminPageJspBean
77  {
78      // Right
79      public static final String RIGHT_MANAGE_PHRASEANET = "PHRASEANET_MANAGEMENT";
80  
81      // Parameters
82      private static final String PARAMETER_ID_MEDIA_HANDLER = "id_media_handler";
83      private static final String PARAMETER_NAME = "name";
84      private static final String PARAMETER_DESCRIPTION = "description";
85      private static final String PARAMETER_INSERT_TEMPLATE = "insert_template";
86      private static final String PARAMETER_MEDIA_TYPE = "media_type";
87      private static final String PARAMETER_PAGE_INDEX = "page_index";
88      
89      private static final String PARAMETER_ID_ACCOUNT = "id_account";
90      private static final String PARAMETER_ACCESS_URL = "accessUrl";
91      private static final String PARAMETER_CUSTOMER_ID = "customerId";
92      private static final String PARAMETER_CUSTOMER_SECRET = "customerSecret";
93      private static final String PARAMETER_AUTHORIZE_END_POINT = "authorizeEndPoint";
94      private static final String PARAMETER_ACCESS_END_POINT = "accessEndPoint";
95      private static final String PARAMETER_PHRASEANET_ID = "phraseanetId";
96      private static final String PARAMETER_PASSWORD = "password";
97      private static final String PARAMETER_TOKEN = "token";
98      
99      private static final String PARAMETER_ENREGISTRER = "save";
100     private static final String PARAMETER_GET_TOKEN = "get_token";
101     private static final String PARAMETER_CHECK_ACCOUNT = "check_account";
102     
103     private static final String PARAMETER_DEFAULT_TEMPLATE = "default_template";
104     
105     private static final String PARAMETER_ID_DATABOXE = "id_databoxe";
106 
107     // templates
108     private static final String TEMPLATE_MANAGE_MEDIA_HANDLERS = "/admin/plugins/phraseanet/manage_media_handlers.html";
109     private static final String TEMPLATE_CREATE_MEDIA_HANDLER_STEP1 = "/admin/plugins/phraseanet/create_media_handler_step1.html";
110     private static final String TEMPLATE_MODIFY_MEDIA_HANDLER_STEP1 = "/admin/plugins/phraseanet/modify_media_handler_step1.html";
111     private static final String TEMPLATE_CREATE_MEDIA_HANDLER_STEP2 = "/admin/plugins/phraseanet/create_media_handler_step2.html";
112     private static final String TEMPLATE_MODIFY_MEDIA_HANDLER_STEP2 = "/admin/plugins/phraseanet/modify_media_handler_step2.html";
113     
114     private static final String TEMPLATE_MANAGE_ACCOUNTS = "/admin/plugins/phraseanet/manage_accounts.html";
115     private static final String TEMPLATE_CREATE_ACCOUNT = "/admin/plugins/phraseanet/create_account.html";
116     private static final String TEMPLATE_MODIFY_ACCOUNT = "/admin/plugins/phraseanet/modify_account.html";
117     
118     private static final String TEMPLATE_MANAGE_TEMPLATES = "/admin/plugins/phraseanet/manage_templates.html";
119     private static final String TEMPLATE_MODIFY_TEMPLATE = "/admin/plugins/phraseanet/modify_template.html";
120     
121     private static final String TEMPLATE_DISPLAY_METADATAS = "/admin/plugins/phraseanet/display_metadatas.html";
122     private static final String TEMPLATE_DISPLAY_COLLECTIONS = "/admin/plugins/phraseanet/display_collections.html";
123 
124     // Properties
125     private static final String PROPERTY_PAGE_TITLE_MANAGE_MEDIA_HANDLERS = "phraseanet.manage_media_handlers.pageTitle";
126     private static final String PROPERTY_PAGE_TITLE_MODIFY_MEDIA_HANDLER_STEP1 = "phraseanet.modify_media_handler.pageTitle1";
127     private static final String PROPERTY_PAGE_TITLE_CREATE_MEDIA_HANDLER_STEP1 = "phraseanet.create_media_handler.pageTitle1";
128     private static final String PROPERTY_PAGE_TITLE_MODIFY_MEDIA_HANDLER_STEP2 = "phraseanet.modify_media_handler.pageTitle2";
129     private static final String PROPERTY_PAGE_TITLE_CREATE_MEDIA_HANDLER_STEP2 = "phraseanet.create_media_handler.pageTitle2";
130     private static final String PROPERTY_DEFAULT_LIST_MEDIA_HANDLER_PER_PAGE = "phraseanet.listMediaHandlers.itemsPerPage";
131     
132     private static final String PROPERTY_PAGE_TITLE_MANAGE_ACCOUNTS = "phraseanet.manage_accounts.pageTitle";
133     private static final String PROPERTY_PAGE_TITLE_MODIFY_ACCOUNT = "phraseanet.modify_account.pageTitle";
134     private static final String PROPERTY_PAGE_TITLE_CREATE_ACCOUNT = "phraseanet.create_account.pageTitle";
135     private static final String PROPERTY_DEFAULT_LIST_ACCOUNT_PER_PAGE = "phraseanet.listAccounts.itemsPerPage";    
136 
137     private static final String PROPERTY_PAGE_TITLE_MANAGE_TEMPLATES = "phraseanet.manage_templates.pageTitle";
138     private static final String PROPERTY_PAGE_TITLE_MODIFY_TEMLPATE = "phraseanet.modify_template.pageTitle";
139     
140     private static final String PROPERTY_URL_DOCUMENTATION = "phraseanet.url.documentation";
141     	
142     // Markers
143     private static final String MARK_MEDIA_HANDLER_LIST = "media_handler_list";
144     private static final String MARK_MEDIA_HANDLER = "media_handler";
145     private static final String MARK_ACCOUNT_LIST = "account_list";
146     private static final String MARK_ACCOUNT = "account";
147     private static final String MARK_PAGINATOR = "paginator";
148     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
149     private static final String MARK_RECORD_TYPE_LIST = "record_type_list";
150     private static final String MARK_MEDIA_HANDLER_NAME = "media_handler_name";
151     private static final String MARK_TEMPLATE = "template";
152     private static final String MARK_CHECK_ACCOUNT = "check_account";
153     private static final String MARK_DATABOXES_LIST = "databoxes_list";
154     private static final String MARK_METADATAS_LIST = "metadatas_list";
155     private static final String MARK_COLLECTIONS_LIST = "collections_list";
156     private static final String MARK_ID_MEDIA_TYPE = "media_type";
157     private static final String MARK_ID_MEDIA_DESCRIPTION = "description";  
158     private static final String MARK_ID_MEDIA_HANDLER = "id_media_handler";
159     private static final String MARK_URL_DOCUMENTATION = "url_documentation";
160 
161     // Jsp Definition
162     private static final String JSP_URL_DO_REMOVE_MEDIA_HANDLER = "jsp/admin/plugins/phraseanet/DoRemoveMediaHandler.jsp";
163     private static final String JSP_URL_MANAGE_MEDIA_HANDLERS = "jsp/admin/plugins/phraseanet/ManageMediaHandlers.jsp";
164     private static final String JSP_REDIRECT_TO_MANAGE_MEDIA_HANDLERS = "ManageMediaHandlers.jsp";
165     private static final String JSP_REDIRECT_TO_CREATE_MEDIA_HANDLER_STEP2 = "CreateMediaHandlerStep2.jsp";
166     private static final String JSP_REDIRECT_TO_MODIFY_MEDIA_HANDLER_STEP2 = "ModifyMediaHandlerStep2.jsp";
167     
168     private static final String JSP_URL_DO_REMOVE_ACCOUNT = "jsp/admin/plugins/phraseanet/DoRemoveAccount.jsp";
169     private static final String JSP_URL_MANAGE_ACCOUNTS = "jsp/admin/plugins/phraseanet/ManageAccounts.jsp";
170     private static final String JSP_URL_CREATE_ACCOUNT = "CreateAccount.jsp";
171     private static final String JSP_URL_MODIFY_ACCOUNT = "ModifyAccount.jsp";
172     private static final String JSP_REDIRECT_TO_MANAGE_ACCOUNTS = "ManageAccounts.jsp";
173     private static final String JSP_REDIRECT_TO_CALLBACK ="Callback.jsp";
174     
175     private static final String JSP_URL_MANAGE_TEMPLATES = "ManageTemplates.jsp";
176 
177     // Messages
178     private static final String MESSAGE_CONFIRM_REMOVE_MEDIA_HANDLER = "phraseanet.message.confirmRemoveMediaHandler";
179     private static final String MESSAGE_CONFIRM_REMOVE_ACCOUNT = "phraseanet.message.confirmRemoveAccount";
180     private static final String MESSAGE_ERROR = "phraseanet.message.error";
181     private static final String MESSAGE_ERROR_DELETE_ACCOUNT = "phraseanet.message.error.delete.account";
182     
183     // Constants
184     private static final String CONSTANT_MEDIA_TYPE_VIDEO = "video";
185     private static final String CONSTANT_MEDIA_TYPE_AUDIO = "audio";
186     private static final String CONSTANT_MEDIA_TYPE_IMAGE = "image";
187     private static final String CONSTANT_MEDIA_TYPE_FLASH = "flash";
188     private static final String CONSTANT_MEDIA_TYPE_DOCUMENT = "document";
189     private static final String CONSTANT_NAME_ICON_PICTURE = "_icon.png";
190     private static final String CONSTANT_PATH_ICONE_PICTURE = "images/admin/skin/plugins/phraseanet/";
191     private static final String CONSTANT_DEFAULT_URL_DOCUMENTATION = "http://dev.lutece.paris.fr/plugins/plugin-phraseanet/fr/";
192      private static Logger _logger = Logger.getLogger( Constants.LOGGER );
193     
194     // Variables
195     private int _nDefaultItemsPerPage;
196     private String _strCurrentPageIndex;
197     private int _nItemsPerPage;
198 
199     /**
200      * Returns the list of media_handler
201      *
202      * @param request The Http request
203      * @return the media_handlers list
204      */
205     public String getManageMediaHandlers( HttpServletRequest request )
206     {
207         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_MEDIA_HANDLERS );
208 
209         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
210         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_MEDIA_HANDLER_PER_PAGE, 50 );
211         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
212                 _nDefaultItemsPerPage );
213 
214         UrlItem url = new UrlItem( JSP_URL_MANAGE_MEDIA_HANDLERS );
215         String strUrl = url.getUrl(  );
216         Collection<MediaHandler> listMediaHandlers = MediaHandlerHome.findAll(  );
217 
218         LocalizedPaginator paginator = new LocalizedPaginator( (List<MediaHandler>) listMediaHandlers, _nItemsPerPage,
219                 strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale(  ) );
220 
221         Map<String, Object> model = new HashMap<String, Object>(  );
222 
223         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
224         model.put( MARK_PAGINATOR, paginator );
225         model.put( MARK_MEDIA_HANDLER_LIST, paginator.getPageItems(  ) );
226 
227         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_MEDIA_HANDLERS, getLocale(  ), model );
228 
229         return getAdminPage( templateList.getHtml(  ) );
230     }
231 
232     /**
233      * Returns the form step1 to create a media_handler
234      * @param request The Http request
235      * @return the html code of the media_handler form step 1
236      */
237     public String getCreateMediaHandlerStep1( HttpServletRequest request )
238     {
239         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_MEDIA_HANDLER_STEP1 );
240         Map<String, Object> model = new HashMap<String, Object>(  );        
241               	
242         model.put( MARK_RECORD_TYPE_LIST, getRecordTypeDTOList(  ) );
243         
244         model.put( MARK_ACCOUNT_LIST, AccountHome.findAll(  ) );
245 
246         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MEDIA_HANDLER_STEP1, getLocale(  ), model );
247 
248         return getAdminPage( template.getHtml(  ) );
249     }
250     
251     
252     /**
253      * Check step1 values
254      * @param request the request
255      * @return error message or step2 form
256      */
257     public String doCreateMediaHandlerStep2( HttpServletRequest request )
258     {
259     	String strUrl = StringUtils.EMPTY;
260     	
261     	String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
262     	String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
263         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );        
264         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
265         
266         if( StringUtils.isNotBlank( strMediaHandlerName )
267         		&& StringUtils.isNotBlank( strMediaHandlerDescription )
268         		&& StringUtils.isNotBlank( strIdAccount )
269         		&& StringUtils.isNotBlank( strMediaType ) )
270         {
271         	UrlItem url = new UrlItem( JSP_REDIRECT_TO_CREATE_MEDIA_HANDLER_STEP2 );
272         	url.addParameter( PARAMETER_NAME, strMediaHandlerName );
273         	url.addParameter( PARAMETER_DESCRIPTION, strMediaHandlerDescription );
274         	url.addParameter( PARAMETER_ID_ACCOUNT, strIdAccount );
275         	url.addParameter( PARAMETER_MEDIA_TYPE, strMediaType );
276         	strUrl = url.getUrl(  );
277         }
278         else
279         {
280         	strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
281         }
282         return strUrl;
283     }
284     
285     /**
286      * Returns the form step2 to create a media_handler
287      * @param request The Http request
288      * @return the html code of the media_handler form step 2
289      */
290     public String getCreateMediaHandlerStep2( HttpServletRequest request )
291     {
292         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_MEDIA_HANDLER_STEP2 );
293         Map<String, Object> model = new HashMap<String, Object>(  );
294         
295         String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
296         model.put( MARK_MEDIA_HANDLER_NAME, strMediaHandlerName );
297         
298         String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
299         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );        
300         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
301         
302         Account account = new Account(  );
303         if( StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) ) 
304         {
305         	int nIdAccount = Integer.parseInt( strIdAccount );
306         	account = AccountHome.findByPrimaryKey( nIdAccount );
307         } 
308         model.put( MARK_ACCOUNT , account );
309         model.put( MARK_ID_MEDIA_TYPE , strMediaType );
310         model.put( MARK_ID_MEDIA_DESCRIPTION , strMediaHandlerDescription ); 
311         model.put( MARK_URL_DOCUMENTATION, AppPropertiesService.getProperty( PROPERTY_URL_DOCUMENTATION , CONSTANT_DEFAULT_URL_DOCUMENTATION ) );
312         
313         if( StringUtils.isNotBlank( strMediaType ) ) 
314         {
315         	Template defaultTemplate = TemplateHome.findByPrimaryKey( strMediaType );
316         	model.put( PARAMETER_DEFAULT_TEMPLATE, defaultTemplate.getTemplate(  ) );
317         } 
318         
319         List<Databox> listDataboxes = new ArrayList<Databox> (  );
320         try 
321         {
322         	listDataboxes = PhraseanetService.getDataboxes( account );
323 		} 
324         catch( PhraseanetApiCallException e )
325 		{
326 			AppLogService.error( e );
327 		}
328         model.put( MARK_DATABOXES_LIST, listDataboxes );
329         
330         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_MEDIA_HANDLER_STEP2, getLocale(  ), model );
331 
332         return getAdminPage( template.getHtml(  ) );
333     }
334     
335     
336 
337     /**
338      * Process the data capture form of a new media_handler
339      *
340      * @param request The Http Request
341      * @return The Jsp URL of the process result
342      */
343     public String doCreateMediaHandler( HttpServletRequest request )
344     {
345         String strUrl = StringUtils.EMPTY;
346         String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
347         String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
348         String strInsertTemplate = request.getParameter( PARAMETER_INSERT_TEMPLATE );
349         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
350         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT ); 
351         
352         if ( StringUtils.isNotBlank( strMediaHandlerName ) && StringUtils.isNotBlank( strMediaHandlerDescription )
353                 && StringUtils.isNotBlank( strInsertTemplate ) && StringUtils.isNotBlank( strMediaType ) 
354                 && StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) )
355         {
356         	int nIdAccount = Integer.parseInt( strIdAccount );
357             MediaHandler mh = new MediaHandler(  );
358             mh.setName( strMediaHandlerName );
359             mh.setIdAccount( nIdAccount );
360             mh.setDescription( strMediaHandlerDescription );
361             mh.setIconUrl( CONSTANT_PATH_ICONE_PICTURE + strMediaType + CONSTANT_NAME_ICON_PICTURE );
362             mh.setInsertTemplate( strInsertTemplate );
363             mh.setMediaType( strMediaType );
364 
365             MediaHandlerHome.create( mh );
366 
367             strUrl = JSP_REDIRECT_TO_MANAGE_MEDIA_HANDLERS;
368         }
369         else
370         {
371             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
372         }
373 
374         return strUrl;
375     }
376 
377     /**
378      * Manages the removal form of a media_handler whose identifier is in the http request
379      *
380      * @param request The Http request
381      * @return the html code to confirm
382      */
383     public String getConfirmRemoveMediaHandler( HttpServletRequest request )
384     {
385         String strUrl = StringUtils.EMPTY;
386         String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
387 
388         if ( StringUtils.isNotBlank( strMediaHandlerId ) && StringUtils.isNumeric( strMediaHandlerId ) )
389         {
390             int nId = Integer.parseInt( strMediaHandlerId );
391             UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_MEDIA_HANDLER );
392             url.addParameter( PARAMETER_ID_MEDIA_HANDLER, nId );
393 
394             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_MEDIA_HANDLER, url.getUrl(  ),
395                     AdminMessage.TYPE_CONFIRMATION );
396         }
397         else
398         {
399             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
400         }
401         return strUrl;
402     }
403 
404     /**
405      * Handles the removal form of a media handler
406      *
407      * @param request The Http request
408      * @return the jsp URL to display the form to manage media handlers
409      */
410     public String doRemoveMediaHandler( HttpServletRequest request )
411     {
412         String strUrl = StringUtils.EMPTY;
413         String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
414 
415         if ( StringUtils.isNotBlank( strMediaHandlerId ) && StringUtils.isNumeric( strMediaHandlerId ) )
416         {
417             int nMediaHandlerId = Integer.parseInt( strMediaHandlerId );
418             MediaHandlerHome.remove( nMediaHandlerId );
419 
420             strUrl = JSP_REDIRECT_TO_MANAGE_MEDIA_HANDLERS;
421         }
422         else
423         {
424             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
425         }
426         return strUrl;
427     }
428 
429     /**
430      * Returns the step 1 form to update info about a media_handler
431      *
432      * @param request The Http request
433      * @return The HTML form to update info
434      */
435     public String getModifyMediaHandlerStep1( HttpServletRequest request )
436     {
437         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_MEDIA_HANDLER_STEP1 );
438 
439         String strUrl = StringUtils.EMPTY;
440         String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
441 
442         if ( StringUtils.isNotBlank( strMediaHandlerId ) && StringUtils.isNumeric( strMediaHandlerId ) )
443         {
444             int nMediaHandlerId = Integer.parseInt( strMediaHandlerId );
445             MediaHandler mh = MediaHandlerHome.findByPrimaryKey( nMediaHandlerId );
446 
447             if ( mh != null )
448             {
449                 Map<String, Object> model = new HashMap<String, Object>(  );
450                 model.put( MARK_MEDIA_HANDLER, mh );
451                 model.put( MARK_RECORD_TYPE_LIST, getRecordTypeDTOList(  ) );                
452                 model.put( MARK_ACCOUNT_LIST, AccountHome.findAll(  ) );
453                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MEDIA_HANDLER_STEP1, getLocale(  ),
454                         model );
455 
456                 strUrl = getAdminPage( template.getHtml(  ) );
457             }
458             else
459             {
460                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
461             }
462         }
463         else
464         {
465             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
466         }
467 
468         return strUrl;
469     }
470     
471     /**
472      * Check step1 values
473      * @param request the request
474      * @return error message or step2 form
475      */
476     public String doModifyMediaHandlerStep2( HttpServletRequest request )
477     {
478     	String strUrl = StringUtils.EMPTY;
479     	String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
480     	String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
481     	String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
482     	String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
483         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );
484         
485         if( StringUtils.isNotBlank( strMediaHandlerName ) && StringUtils.isNotBlank( strMediaHandlerId )
486         		&& StringUtils.isNotBlank( strMediaHandlerDescription )
487         		&& StringUtils.isNotBlank( strIdAccount )
488         		&& StringUtils.isNotBlank( strMediaType ) )
489         {
490         	UrlItem url = new UrlItem( JSP_REDIRECT_TO_MODIFY_MEDIA_HANDLER_STEP2 );
491         	url.addParameter( PARAMETER_ID_MEDIA_HANDLER, strMediaHandlerId );
492         	url.addParameter( PARAMETER_NAME, strMediaHandlerName );
493         	url.addParameter( PARAMETER_DESCRIPTION, strMediaHandlerDescription );
494         	url.addParameter( PARAMETER_ID_ACCOUNT, strIdAccount );
495         	url.addParameter( PARAMETER_MEDIA_TYPE, strMediaType );
496         	strUrl = url.getUrl(  ).replaceAll( " ", "%20" );
497         }
498         else
499         {
500         	strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
501         }
502         return strUrl;
503     }
504     
505     /**
506      * Returns the step 2 form to update info about a media_handler
507      *
508      * @param request The Http request
509      * @return The HTML form to update info
510      */
511     public String getModifyMediaHandlerStep2( HttpServletRequest request )
512     {    	
513     	setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_MEDIA_HANDLER_STEP2 );
514         Map<String, Object> model = new HashMap<String, Object>(  );
515         
516         String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
517         model.put( MARK_MEDIA_HANDLER_NAME, strMediaHandlerName );
518         
519         String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
520         String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
521         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );        
522         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
523         
524         Account account = new Account(  );
525         if( StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) ) 
526         {
527         	int nIdAccount = Integer.parseInt( strIdAccount );
528         	account = AccountHome.findByPrimaryKey( nIdAccount );
529         } 
530         model.put( MARK_ACCOUNT , account );
531         model.put( MARK_ID_MEDIA_TYPE , strMediaType );
532         model.put( MARK_ID_MEDIA_DESCRIPTION , strMediaHandlerDescription );  
533         model.put( MARK_ID_MEDIA_HANDLER , strMediaHandlerId );  
534         model.put( MARK_URL_DOCUMENTATION, AppPropertiesService.getProperty( PROPERTY_URL_DOCUMENTATION , CONSTANT_DEFAULT_URL_DOCUMENTATION ) );
535         
536         List<Databox> listDataboxes = new ArrayList<Databox> (  );
537         try 
538         {
539         	listDataboxes = PhraseanetService.getDataboxes( account );
540 		} 
541         catch( PhraseanetApiCallException e )
542 		{
543 			AppLogService.error( e );
544 		}
545         model.put( MARK_DATABOXES_LIST, listDataboxes );
546         
547         if ( StringUtils.isNotBlank( strMediaHandlerId ) && StringUtils.isNumeric( strMediaHandlerId ) )
548         {
549             int nMediaHandlerId = Integer.parseInt( strMediaHandlerId );
550             MediaHandler mh = MediaHandlerHome.findByPrimaryKey( nMediaHandlerId );
551             if( mh.getMediaType(  ).equals( strMediaType ) )
552             {
553             	model.put( PARAMETER_DEFAULT_TEMPLATE, mh.getInsertTemplate(  ) );
554             }
555             else
556             {
557             	Template defaultTemplate = TemplateHome.findByPrimaryKey( strMediaType );
558             	model.put( PARAMETER_DEFAULT_TEMPLATE, defaultTemplate.getTemplate(  ) );
559             }
560         }        
561         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_MEDIA_HANDLER_STEP2, getLocale(  ), model );
562         return getAdminPage( template.getHtml(  ) );
563     }
564 
565     /**
566      * Process the change form of a media_handler
567      *
568      * @param request The Http request
569      * @return The Jsp URL of the process result
570      */
571     public String doModifyMediaHandler( HttpServletRequest request )
572     {
573         String strUrl = StringUtils.EMPTY;
574         String strMediaHandlerId = request.getParameter( PARAMETER_ID_MEDIA_HANDLER );
575         String strMediaHandlerName = request.getParameter( PARAMETER_NAME );
576         String strMediaHandlerDescription = request.getParameter( PARAMETER_DESCRIPTION );
577         String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT ); 
578         String strInsertTemplate = request.getParameter( PARAMETER_INSERT_TEMPLATE );
579         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
580 
581         if ( StringUtils.isNotBlank( strMediaHandlerName ) && StringUtils.isNotBlank( strMediaHandlerDescription ) &&
582                 StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) 
583                 && StringUtils.isNotBlank( strInsertTemplate ) && StringUtils.isNotBlank( strMediaType ) )
584         {
585             int nMediaHandlerId = Integer.parseInt( strMediaHandlerId );
586             MediaHandler mh = MediaHandlerHome.findByPrimaryKey( nMediaHandlerId );
587             
588             int nIdAccount = Integer.parseInt( strIdAccount );
589             if ( mh != null )
590             {
591                 mh.setName( strMediaHandlerName );
592                 mh.setIdAccount( nIdAccount );
593                 mh.setDescription( strMediaHandlerDescription );
594                 mh.setIconUrl(  CONSTANT_PATH_ICONE_PICTURE + strMediaType + CONSTANT_NAME_ICON_PICTURE  );
595                 mh.setInsertTemplate( strInsertTemplate );
596                 mh.setMediaType( strMediaType );
597 
598                 MediaHandlerHome.update( mh );
599                 strUrl = JSP_REDIRECT_TO_MANAGE_MEDIA_HANDLERS;
600             }
601             else
602             {
603                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
604             }
605         }
606         else
607         {
608             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
609         }
610 
611         return strUrl;
612     }
613     
614     /**
615      * Method to display meta datas list by databoxe in popup
616      * @param request the request 
617      * @return simple template with meta datas list
618      */
619     public static String getListMetaDatas( HttpServletRequest request )
620     {
621     	Map<String, Object> model = new HashMap<String, Object>(  );
622     	
623     	String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );
624     	String strIdDataboxe = request.getParameter( PARAMETER_ID_DATABOXE );
625         
626         _logger.debug("Liste metadatas Databox Id : " + strIdDataboxe);
627     	
628     	if( StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) && StringUtils.isNotBlank( strIdDataboxe ) && StringUtils.isNumeric( strIdDataboxe ) )
629     	{
630     		int nIdAccount = Integer.parseInt( strIdAccount );
631     		int nIdDataboxe = Integer.parseInt( strIdDataboxe );
632     		
633     		Account account = AccountHome.findByPrimaryKey( nIdAccount );
634             _logger.debug("Account : " + account);
635                		
636     		List<Metadata> listMetadatas = new ArrayList<Metadata> (  );
637             
638             
639             try 
640             {
641             	listMetadatas = PhraseanetService.getDataboxeMetadatas( nIdDataboxe, account );
642                 _logger.debug("listMetadatas : " + listMetadatas);
643     		} 
644             catch( PhraseanetApiCallException e )
645     		{
646     			AppLogService.error( e );
647     		}
648             model.put( MARK_METADATAS_LIST, listMetadatas );
649     	}
650     	// Gets the locale of the user
651         Locale locale = AdminUserService.getLocale( request );
652     	HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_DISPLAY_METADATAS, locale, model );
653     	
654     	return templateList.getHtml(  );
655     }
656     
657     /**
658      * Method to display collection list by databoxe in popup
659      * @param request the request 
660      * @return simple template with collection list
661      */
662     public static String getListCollections( HttpServletRequest request )
663     {
664     	Map<String, Object> model = new HashMap<String, Object>(  );
665     	
666     	String strIdAccount = request.getParameter( PARAMETER_ID_ACCOUNT );
667     	String strIdDataboxe = request.getParameter( PARAMETER_ID_DATABOXE );
668     	
669     	if( StringUtils.isNotBlank( strIdAccount ) && StringUtils.isNumeric( strIdAccount ) && StringUtils.isNotBlank( strIdDataboxe ) && StringUtils.isNumeric( strIdDataboxe ) )
670     	{
671     		int nIdAccount = Integer.parseInt( strIdAccount );
672     		int nIdDataboxe = Integer.parseInt( strIdDataboxe );
673     		
674     		Account account = AccountHome.findByPrimaryKey( nIdAccount );
675     		
676     		List<fr.paris.lutece.plugins.phraseanet.business.databox.Collection> listCollections = new ArrayList<fr.paris.lutece.plugins.phraseanet.business.databox.Collection> (  );
677             try 
678             {
679             	listCollections = PhraseanetService.getColletions( nIdDataboxe, account );
680                 _logger.debug("listCollections : " + listCollections);
681     		} 
682             catch( PhraseanetApiCallException e )
683     		{
684             	AppLogService.error( e );
685     		}
686             model.put( MARK_COLLECTIONS_LIST, listCollections );
687     	}
688     	// Gets the locale of the user
689         Locale locale = AdminUserService.getLocale( request );
690     	HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_DISPLAY_COLLECTIONS, locale, model );
691     	
692     	return templateList.getHtml(  );
693     }
694     
695     /**
696      * Returns the list of accounts
697      *
698      * @param request The Http request
699      * @return the media_handlers list
700      */
701     public String getManageAccounts( HttpServletRequest request )
702     {
703         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_ACCOUNTS );
704 
705         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
706         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_LIST_ACCOUNT_PER_PAGE, 50 );
707         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
708                 _nDefaultItemsPerPage );
709 
710         UrlItem url = new UrlItem( JSP_URL_MANAGE_ACCOUNTS );
711         String strUrl = url.getUrl(  );
712         Collection<Account> listAccountlers = AccountHome.findAll(  );
713 
714         LocalizedPaginator paginator = new LocalizedPaginator( (List<Account>) listAccountlers, _nItemsPerPage,
715                 strUrl, PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale(  ) );
716 
717         Map<String, Object> model = new HashMap<String, Object>(  );
718 
719         model.put( MARK_NB_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
720         model.put( MARK_PAGINATOR, paginator );
721         model.put( MARK_ACCOUNT_LIST, paginator.getPageItems(  ) );
722 
723         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_ACCOUNTS, getLocale(  ), model );
724 
725         return getAdminPage( templateList.getHtml(  ) );
726     }
727     
728     /**
729      * Returns the form to create a account
730      *
731      * @param request The Http request
732      * @return the html code of the account form
733      */
734     public String getCreateAccount( HttpServletRequest request )
735     {
736         setPageTitleProperty( PROPERTY_PAGE_TITLE_CREATE_ACCOUNT );
737 
738         Map<String, Object> model = new HashMap<String, Object>(  );
739         
740         if( StringUtils.isNotBlank( request.getParameter( PARAMETER_CHECK_ACCOUNT ) ) )
741         {
742         	Account account = new Account(  );
743         	
744         	String strAccountName = request.getParameter( PARAMETER_NAME );
745             String strDescription = request.getParameter( PARAMETER_DESCRIPTION );        
746             String strAccessUrl = request.getParameter( PARAMETER_ACCESS_URL );
747             String strCustomerId = request.getParameter( PARAMETER_CUSTOMER_ID );
748             String strCustomerSecret = request.getParameter( PARAMETER_CUSTOMER_SECRET );
749             String strAuthorizeEndPoint = request.getParameter( PARAMETER_AUTHORIZE_END_POINT );
750             String strAccessEndPoint = request.getParameter( PARAMETER_ACCESS_END_POINT );
751             String strPhraseanetId = request.getParameter( PARAMETER_PHRASEANET_ID );
752             String strPassword = request.getParameter( PARAMETER_PASSWORD );
753             String strToken = request.getParameter( PARAMETER_TOKEN );
754             
755             account.setName( strAccountName );
756             account.setDescription( strDescription );
757             account.setAccessURL( strAccessUrl );
758             account.setCustomerId( strCustomerId );
759             account.setCustomerSecret( strCustomerSecret );
760             account.setAuthorizeEndPoint( strAuthorizeEndPoint );
761             account.setAccessEndPoint( strAccessEndPoint );
762             account.setPhraseanetId( strPhraseanetId );
763             account.setPassword( strPassword );
764             account.setToken( strToken );
765             model.put( MARK_ACCOUNT, account );
766             try 
767 			{					
768 				if( StringUtils.isNotBlank( PhraseanetApiAuthentication.getAccessToken( account ) ) )
769 				{
770 					model.put( MARK_CHECK_ACCOUNT, "true" );
771 				}
772 			} 
773 			catch( PhraseanetApiCallException e ) 
774 			{
775 				AppLogService.error( e );
776 				model.put( MARK_CHECK_ACCOUNT, "false" );
777 			}  
778         }
779 
780         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_ACCOUNT, getLocale(  ), model );
781         return getAdminPage( template.getHtml(  ) );
782     }
783 
784     /**
785      * Process the data capture form of a new account
786      *
787      * @param request The Http Request
788      * @return The Jsp URL of the process result
789      */
790     public String doCreateAccount( HttpServletRequest request )
791     {
792         String strUrl = StringUtils.EMPTY;
793         
794         String strAccountName = request.getParameter( PARAMETER_NAME );
795         String strDescription = request.getParameter( PARAMETER_DESCRIPTION );        
796         String strAccessUrl = request.getParameter( PARAMETER_ACCESS_URL );
797         String strCustomerId = request.getParameter( PARAMETER_CUSTOMER_ID );
798         String strCustomerSecret = request.getParameter( PARAMETER_CUSTOMER_SECRET );
799         String strAuthorizeEndPoint = request.getParameter( PARAMETER_AUTHORIZE_END_POINT );
800         String strAccessEndPoint = request.getParameter( PARAMETER_ACCESS_END_POINT );
801         String strPhraseanetId = request.getParameter( PARAMETER_PHRASEANET_ID );
802         String strPassword = request.getParameter( PARAMETER_PASSWORD );
803         String strToken = request.getParameter( PARAMETER_TOKEN );
804 
805         if ( StringUtils.isNotBlank( strAccountName ) && 
806                 StringUtils.isNotBlank( strDescription ) &&
807                 StringUtils.isNotBlank( strAccessUrl ) && 
808                 StringUtils.isNotBlank( strCustomerId ) &&
809                 //StringUtils.isNotBlank( strCustomerSecret ) && 
810                 StringUtils.isNotBlank( strAuthorizeEndPoint ) &&
811                 StringUtils.isNotBlank( strAccessEndPoint ) && 
812                 StringUtils.isNotBlank( strPhraseanetId ) &&
813                 StringUtils.isNotBlank( strPassword ) )
814         {
815         	
816             Account account = new Account(  );
817             account.setName( strAccountName );
818             account.setDescription( strDescription );
819             account.setAccessURL( strAccessUrl );
820             account.setCustomerId( strCustomerId );
821             account.setCustomerSecret( strCustomerSecret );
822             account.setAuthorizeEndPoint( strAuthorizeEndPoint );
823             account.setAccessEndPoint( strAccessEndPoint );
824             account.setPhraseanetId( strPhraseanetId );
825             account.setPassword( strPassword );
826             account.setToken( strToken );
827             
828             if( StringUtils.isNotBlank( request.getParameter( PARAMETER_ENREGISTRER ) ) )
829     		{
830             	AccountHome.create( account );
831                 strUrl = JSP_REDIRECT_TO_MANAGE_ACCOUNTS;
832     		}
833             else
834             {           
835                 strUrl = JSP_REDIRECT_TO_MANAGE_ACCOUNTS;              	
836             }
837         }
838         else
839         {
840             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
841         }
842 
843         return strUrl;
844     }    
845     
846     /**
847      * Manages the removal form of a account whose identifier is in the http request
848      *
849      * @param request The Http request
850      * @return the html code to confirm
851      */
852     public String getConfirmRemoveAccount( HttpServletRequest request )
853     {
854         String strUrl = StringUtils.EMPTY;
855         String strAccountId = request.getParameter( PARAMETER_ID_ACCOUNT );
856 
857         if ( StringUtils.isNotBlank( strAccountId ) && StringUtils.isNumeric( strAccountId ) )
858         {
859             int nId = Integer.parseInt( strAccountId );
860             UrlItem url = new UrlItem( JSP_URL_DO_REMOVE_ACCOUNT );
861             url.addParameter( PARAMETER_ID_ACCOUNT, nId );
862 
863             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ACCOUNT, url.getUrl(  ),
864                     AdminMessage.TYPE_CONFIRMATION );
865         }
866         else
867         {
868             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
869         }
870         return strUrl;
871     }
872     
873     /**
874      * Handles the removal form of a media handler
875      *
876      * @param request The Http request
877      * @return the jsp URL to display the form to manage accounts
878      */
879     public String doRemoveAccount( HttpServletRequest request )
880     {
881         String strUrl = StringUtils.EMPTY;
882         String strAccountId = request.getParameter( PARAMETER_ID_ACCOUNT );
883         if ( StringUtils.isNotBlank( strAccountId ) && StringUtils.isNumeric( strAccountId ) )
884         {        	
885         	int nAccountId = Integer.parseInt( strAccountId );
886         	if( MediaHandlerHome.checkMediaHandlerByAccount( nAccountId ) )
887         	{
888         		strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DELETE_ACCOUNT , AdminMessage.TYPE_STOP );
889         	}
890         	else
891         	{
892         		AccountHome.remove( nAccountId );
893                 strUrl = JSP_REDIRECT_TO_MANAGE_ACCOUNTS;
894         	}            
895         }
896         else
897         {
898             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
899         }
900         return strUrl;
901     }
902 
903     /**
904      * Returns the form to update info about a account
905      *
906      * @param request The Http request
907      * @return The HTML form to update info
908      */
909     public String getModifyAccount( HttpServletRequest request )
910     {
911         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_ACCOUNT  );
912         Map<String, Object> model = new HashMap<String, Object>(  );
913         
914         String strUrl = StringUtils.EMPTY;
915         String strAccountId = request.getParameter( PARAMETER_ID_ACCOUNT );
916 
917         if ( StringUtils.isNotBlank( strAccountId ) && StringUtils.isNumeric( strAccountId ) )
918         {
919             int nAccountId = Integer.parseInt( strAccountId );
920             Account account = new Account(  );
921             
922             if( StringUtils.isNotBlank( request.getParameter( PARAMETER_CHECK_ACCOUNT ) ) )
923             {
924             	String strAccountName = request.getParameter( PARAMETER_NAME );
925                 String strDescription = request.getParameter( PARAMETER_DESCRIPTION );        
926                 String strAccessUrl = request.getParameter( PARAMETER_ACCESS_URL );
927                 String strCustomerId = request.getParameter( PARAMETER_CUSTOMER_ID );
928                 String strCustomerSecret = request.getParameter( PARAMETER_CUSTOMER_SECRET );
929                 String strAuthorizeEndPoint = request.getParameter( PARAMETER_AUTHORIZE_END_POINT );
930                 String strAccessEndPoint = request.getParameter( PARAMETER_ACCESS_END_POINT );
931                 String strPhraseanetId = request.getParameter( PARAMETER_PHRASEANET_ID );
932                 String strPassword = request.getParameter( PARAMETER_PASSWORD );
933                 String strToken = request.getParameter( PARAMETER_TOKEN );
934                 
935                 account.setId( nAccountId );
936                 account.setName( strAccountName );
937                 account.setDescription( strDescription );
938                 account.setAccessURL( strAccessUrl );
939                 account.setCustomerId( strCustomerId );
940                 account.setCustomerSecret( strCustomerSecret );
941                 account.setAuthorizeEndPoint( strAuthorizeEndPoint );
942                 account.setAccessEndPoint( strAccessEndPoint );
943                 account.setPhraseanetId( strPhraseanetId );
944                 account.setPassword( strPassword );
945                 account.setToken( strToken );
946                 
947 				try 
948 				{					
949 					if( StringUtils.isNotBlank( PhraseanetApiAuthentication.getAccessToken( account ) ) )
950 					{
951 						model.put( MARK_CHECK_ACCOUNT, "true" );
952 					}
953 					else
954 					{
955 						model.put( MARK_CHECK_ACCOUNT, "false" );
956 					}
957 				} 
958 				catch( PhraseanetApiCallException e ) 
959 				{
960 					AppLogService.error( e );
961 					model.put( MARK_CHECK_ACCOUNT, "false" );
962 				}               
963             }
964             else
965             {
966             	account = AccountHome.findByPrimaryKey( nAccountId );
967             }
968 
969             if ( account != null )
970             {
971                 model.put( MARK_ACCOUNT, account );
972                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_ACCOUNT, getLocale(  ),
973                         model );
974 
975                 strUrl = getAdminPage( template.getHtml(  ) );
976             }
977             else
978             {
979                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
980             }
981         }
982         else
983         {
984             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
985         }
986         return strUrl;
987     }
988 
989     /**
990      * Process the change form of a account
991      *
992      * @param request The Http request
993      * @return The Jsp URL of the process result
994      */
995     public String doModifyAccount( HttpServletRequest request )
996     {
997         String strUrl = StringUtils.EMPTY;
998         
999         String strAccountId = request.getParameter( PARAMETER_ID_ACCOUNT );
1000         
1001         String strAccountName = request.getParameter( PARAMETER_NAME );
1002         String strDescription = request.getParameter( PARAMETER_DESCRIPTION );        
1003         String strAccessUrl = request.getParameter( PARAMETER_ACCESS_URL );
1004         String strCustomerId = request.getParameter( PARAMETER_CUSTOMER_ID );
1005         String strCustomerSecret = request.getParameter( PARAMETER_CUSTOMER_SECRET );
1006         String strAuthorizeEndPoint = request.getParameter( PARAMETER_AUTHORIZE_END_POINT );
1007         String strAccessEndPoint = request.getParameter( PARAMETER_ACCESS_END_POINT );
1008         String strPhraseanetId = request.getParameter( PARAMETER_PHRASEANET_ID );
1009         String strPassword = request.getParameter( PARAMETER_PASSWORD );
1010         String strToken = request.getParameter( PARAMETER_TOKEN );
1011 
1012         if ( StringUtils.isNotBlank( strAccountName ) && 
1013                 StringUtils.isNotBlank( strDescription ) &&
1014                 StringUtils.isNotBlank( strAccessUrl ) && 
1015                 StringUtils.isNotBlank( strCustomerId ) &&
1016                 //StringUtils.isNotBlank( strCustomerSecret ) && 
1017                 StringUtils.isNotBlank( strAuthorizeEndPoint ) &&
1018                 StringUtils.isNotBlank( strAccessEndPoint ) && 
1019                 StringUtils.isNotBlank( strPhraseanetId ) &&
1020                 StringUtils.isNotBlank( strPassword ) )
1021         {
1022             int nAccountId = Integer.parseInt( strAccountId );
1023             Account account = AccountHome.findByPrimaryKey( nAccountId );
1024 
1025             if ( account != null )
1026             {
1027                 account.setName( strAccountName );
1028                 account.setDescription( strDescription );
1029                 account.setAccessURL( strAccessUrl );
1030                 account.setCustomerId( strCustomerId );
1031                 account.setCustomerSecret( strCustomerSecret );
1032                 account.setAuthorizeEndPoint( strAuthorizeEndPoint );
1033                 account.setAccessEndPoint( strAccessEndPoint );
1034                 account.setPhraseanetId( strPhraseanetId );
1035                 account.setPassword( strPassword );
1036                 account.setToken( strToken );
1037                 
1038                 if( StringUtils.isNotBlank( request.getParameter( PARAMETER_ENREGISTRER ) ) )
1039         		{
1040                 	AccountHome.update( account );
1041                     UrlItem url = new UrlItem( JSP_URL_MODIFY_ACCOUNT );
1042                 	url.addParameter( PARAMETER_ID_ACCOUNT, strAccountId );
1043                     strUrl = url.getUrl(  );
1044                     
1045         		}
1046                 else if(StringUtils.isNotBlank( request.getParameter( PARAMETER_GET_TOKEN ) ))
1047                 {
1048                     AccountHome.update( account );
1049                     UrlItem url = new UrlItem(JSP_REDIRECT_TO_CALLBACK);
1050                 	url.addParameter( PARAMETER_ID_ACCOUNT, strAccountId );
1051                 	//url.addParameter( PARAMETER_NAME, strAccountName ); 
1052                 	//url.addParameter( PARAMETER_DESCRIPTION, strDescription );        
1053                 	//url.addParameter( PARAMETER_ACCESS_URL, strAccessUrl );
1054                 	url.addParameter( PARAMETER_CUSTOMER_ID, strCustomerId );
1055                 	//url.addParameter( PARAMETER_CUSTOMER_SECRET, strCustomerSecret );
1056                 	url.addParameter( PARAMETER_AUTHORIZE_END_POINT, strAuthorizeEndPoint );
1057                 	url.addParameter( PARAMETER_ACCESS_END_POINT, strAccessEndPoint );
1058                 	url.addParameter( PARAMETER_PHRASEANET_ID, strPhraseanetId );
1059                 	url.addParameter( PARAMETER_PASSWORD, strPassword );
1060                 	//url.addParameter( PARAMETER_CHECK_ACCOUNT, PARAMETER_CHECK_ACCOUNT );
1061                     strUrl = url.getUrl(  ).replaceAll( " ", "%20" );
1062                 }
1063                 else
1064                 {           
1065                     strUrl = JSP_REDIRECT_TO_MANAGE_ACCOUNTS;              	
1066                 }                
1067             }
1068             else
1069             {
1070                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
1071             }
1072         }
1073         else
1074         {
1075             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
1076         }
1077 
1078         return strUrl;
1079     }
1080     
1081     /**
1082      * Returns the list of default templates
1083      *
1084      * @param request The Http request
1085      * @return the templates list
1086      */
1087     public String getManageTemplates( HttpServletRequest request )
1088     {
1089         
1090         setPageTitleProperty( PROPERTY_PAGE_TITLE_MANAGE_TEMPLATES );
1091         Map<String, Object> model = new HashMap<String, Object>(  );
1092         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_TEMPLATES, getLocale(  ), model );
1093 
1094         return getAdminPage( templateList.getHtml(  ) );
1095     }
1096     
1097     /**
1098      * Returns the form to update info about a template
1099      *
1100      * @param request The Http request
1101      * @return The HTML form to update info
1102      */
1103     public String getModifyTemplate( HttpServletRequest request )
1104     {
1105         setPageTitleProperty( PROPERTY_PAGE_TITLE_MODIFY_TEMLPATE  );
1106         Map<String, Object> model = new HashMap<String, Object>(  );
1107         
1108         String strUrl = StringUtils.EMPTY;
1109         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
1110 
1111         if ( StringUtils.isNotBlank( strMediaType ) )
1112         {
1113             Template defaultTemplate = TemplateHome.findByPrimaryKey( strMediaType );
1114             
1115             if ( defaultTemplate != null )
1116             {
1117                 model.put( MARK_TEMPLATE, defaultTemplate );
1118 
1119                 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_TEMPLATE, getLocale(  ),
1120                         model );
1121 
1122                 strUrl = getAdminPage( template.getHtml(  ) );
1123             }
1124             else
1125             {
1126                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
1127             }
1128         }
1129         else
1130         {
1131             strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
1132         }
1133 
1134         return strUrl;
1135     }
1136 
1137     /**
1138      * Process the change form of a template
1139      *
1140      * @param request The Http request
1141      * @return The Jsp URL of the process result
1142      */
1143     public String doModifyTemplate( HttpServletRequest request )
1144     {
1145         String strUrl = StringUtils.EMPTY;
1146         
1147         String strDefaultTemplate = request.getParameter( PARAMETER_DEFAULT_TEMPLATE );
1148         String strMediaType = request.getParameter( PARAMETER_MEDIA_TYPE );
1149 
1150         if ( StringUtils.isNotBlank( strDefaultTemplate ) && StringUtils.isNotBlank( strMediaType ) )
1151         {
1152             Template defaultTemplate = TemplateHome.findByPrimaryKey( strMediaType );
1153 
1154             if ( defaultTemplate != null )
1155             {
1156             	defaultTemplate.setTemplate( strDefaultTemplate );
1157             	TemplateHome.update( defaultTemplate );
1158 	    		UrlItem url = new UrlItem( JSP_URL_MANAGE_TEMPLATES );
1159 	    		strUrl = url.getUrl(  );
1160             }
1161             else
1162             {
1163                 strUrl = AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, AdminMessage.TYPE_STOP );
1164             }
1165         }
1166         else
1167         {
1168             strUrl = AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
1169         }
1170 
1171         return strUrl;
1172     }
1173     
1174     /**
1175      * Method to get record type list 
1176      * @return type record type list 
1177      */
1178     private List<RecordTypeDTO> getRecordTypeDTOList(  )
1179     {
1180     	List<RecordTypeDTO> listRecordTypeDTO = new ArrayList<RecordTypeDTO>(  );
1181     	if( PhraseanetService.getMediaTypeValues(  ) != null )
1182         {  
1183 	    	for( String strRecordType : PhraseanetService.getMediaTypeValues(  ) )
1184 	    	{
1185 	    		RecordTypeDTO recordTypeDTO = new RecordTypeDTO(  );
1186 	    		recordTypeDTO.setName( strRecordType );
1187 	    		if( strRecordType.contains( CONSTANT_MEDIA_TYPE_VIDEO ) )
1188 	    		{
1189 	    			recordTypeDTO.setId( 1 );
1190 	    			recordTypeDTO.setIcon( CONSTANT_MEDIA_TYPE_VIDEO + CONSTANT_NAME_ICON_PICTURE );
1191 	    			
1192 	    		}
1193 	    		else if( strRecordType.contains( CONSTANT_MEDIA_TYPE_AUDIO ) ) 
1194 	    		{
1195 	    			recordTypeDTO.setId( 2 );
1196 	    			recordTypeDTO.setIcon( CONSTANT_MEDIA_TYPE_AUDIO + CONSTANT_NAME_ICON_PICTURE );
1197 	    		}
1198 	    		else if( strRecordType.contains( CONSTANT_MEDIA_TYPE_IMAGE ) ) 
1199 	    		{
1200 	    			recordTypeDTO.setId( 3 );
1201 	    			recordTypeDTO.setIcon( CONSTANT_MEDIA_TYPE_IMAGE + CONSTANT_NAME_ICON_PICTURE );
1202 	    		}
1203 	    		else if( strRecordType.contains( CONSTANT_MEDIA_TYPE_FLASH ) ) 
1204 	    		{
1205 	    			recordTypeDTO.setId( 4 );
1206 	    			recordTypeDTO.setIcon( CONSTANT_MEDIA_TYPE_FLASH + CONSTANT_NAME_ICON_PICTURE );
1207 	    		}
1208 	    		else
1209 	    		{
1210 	    			recordTypeDTO.setId( 5 );
1211 	    			recordTypeDTO.setIcon( CONSTANT_MEDIA_TYPE_DOCUMENT + CONSTANT_NAME_ICON_PICTURE );
1212 	    		}
1213 	    		listRecordTypeDTO.add( recordTypeDTO );
1214 	    	}
1215         }
1216     	return listRecordTypeDTO;
1217     }
1218 
1219 }
1220