View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.directories.web;
35  
36  import fr.paris.lutece.portal.web.xpages.XPage;
37  import fr.paris.lutece.util.html.HtmlTemplate;
38  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
39  import fr.paris.lutece.plugins.directories.business.DirectoryEntity;
40  import fr.paris.lutece.plugins.directories.business.DirectoryEntityHome;
41  import fr.paris.lutece.plugins.directories.business.DirectoryResponse;
42  import fr.paris.lutece.plugins.directories.business.DirectoryResponseHome;
43  import fr.paris.lutece.plugins.directories.service.EntryService;
44  import fr.paris.lutece.plugins.directories.util.DirectoriesConstants;
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.Response;
47  import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
48  import fr.paris.lutece.portal.business.file.File;
49  import fr.paris.lutece.portal.business.file.FileHome;
50  import fr.paris.lutece.portal.service.template.AppTemplateService;
51  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
52  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
53  import java.util.ArrayList;
54  import java.util.List;
55  import java.util.Map;
56  import java.util.stream.Collectors;
57  
58  import javax.servlet.http.HttpServletRequest;
59  
60  /**
61   * This class provides a simple implementation of an XPage
62   */
63  @Controller( xpageName = "directories", pageTitleI18nKey = "directories.xpage.directories.pageTitle", pagePathI18nKey = "directories.xpage.directories.pagePathLabel" )
64  public class DirectoriesApp extends MVCApplication
65  {
66      private static final long serialVersionUID = -7930244683357483911L;
67      private static final String TEMPLATE_ENTITY = "/skin/plugins/directories/directory_entity.html";
68      private static final String TEMPLATE_HTML_CODE_FORM = "skin/plugins/directories/html_code_form.html";
69      private static final String VIEW_DIRECTORY_ENTITY = "viewDirectoryEntity";
70      private static final String VIEW_MODIFY_DIRECTORY_ENTITY = "viewModifyDirectoryEntity";
71      private static final String PARAMETER_ID_ENTITY = "entity_id";
72      private static final String MARK_ENTITY = "entity";
73      private static final String MARK_LIST_RESPONSES = "list_responses";
74  
75      /**
76       * Returns Entity Data.
77       * 
78       * @param request
79       *            The HTTP request
80       * @return The view
81       */
82      @View( value = VIEW_DIRECTORY_ENTITY )
83      public XPage viewDirectoryEntity( HttpServletRequest request )
84      {
85          int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTITY ) );
86          Map<String, Object> model = getModel( );
87          List<Response> listResponse = new ArrayList<>( );
88          StringBuilder strBuilder = new StringBuilder( );
89          DirectoryEntity directoryEntity = DirectoryEntityHome.findByPrimaryKey( nId );
90          for ( DirectoryResponse directoryResponse : DirectoryResponseHome.getDirectoryResponsesListByIdEntity( nId ) )
91          {
92              Response response = ResponseHome.findByPrimaryKey( directoryResponse.getIdResponse( ) );
93              if ( ( response.getFile( ) != null ) && ( response.getFile( ).getIdFile( ) > 0 ) )
94              {
95                  File file = FileHome.findByPrimaryKey( response.getFile( ).getIdFile( ) );
96                  response.setFile( file );
97              }
98              listResponse.add( response );
99          }
100         directoryEntity.setResponses( listResponse );
101         model.put( MARK_ENTITY, directoryEntity );
102         List<Entry> listEntryFirstLevel = EntryService.getDirectoryEntryList( directoryEntity.getIdDirectory( ), true );
103 
104         for ( Entry entry : listEntryFirstLevel )
105         {
106             List<Response> listEntryResponse = listResponse.stream( ).filter( response -> response.getEntry( ).getIdEntry( ) == entry.getIdEntry( ) )
107                     .collect( Collectors.toCollection( ArrayList::new ) );
108             model.put( MARK_LIST_RESPONSES, listEntryResponse );
109             EntryService.getHtmlEntryReadOnly( model, entry.getIdEntry( ), strBuilder, getLocale( request ), true );
110         }
111         model.put( DirectoriesConstants.MARK_STR_ENTRY, strBuilder.toString( ) );
112         HtmlTemplate templateForm = AppTemplateService.getTemplate( TEMPLATE_HTML_CODE_FORM, getLocale( request ), model );
113         model.put( DirectoriesConstants.MARK_FORM_HTML, templateForm.getHtml( ) );
114 
115         return getXPage( TEMPLATE_ENTITY, request.getLocale( ), model );
116     }
117 
118     /**
119      * Returns Entity Data.
120      * 
121      * @param request
122      *            The HTTP request
123      * @return The view
124      */
125     @View( value = VIEW_MODIFY_DIRECTORY_ENTITY )
126     public XPage viewModifyDirectoryEntity( HttpServletRequest request )
127     {
128         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENTITY ) );
129         Map<String, Object> model = getModel( );
130         List<Response> listResponse = new ArrayList<>( );
131         StringBuilder strBuilder = new StringBuilder( );
132         DirectoryEntity directoryEntity = DirectoryEntityHome.findByPrimaryKey( nId );
133         for ( DirectoryResponse directoryResponse : DirectoryResponseHome.getDirectoryResponsesListByIdEntity( nId ) )
134         {
135             Response response = ResponseHome.findByPrimaryKey( directoryResponse.getIdResponse( ) );
136             if ( ( response.getFile( ) != null ) && ( response.getFile( ).getIdFile( ) > 0 ) )
137             {
138                 File file = FileHome.findByPrimaryKey( response.getFile( ).getIdFile( ) );
139                 response.setFile( file );
140             }
141             listResponse.add( response );
142         }
143         directoryEntity.setResponses( listResponse );
144         model.put( MARK_ENTITY, directoryEntity );
145         List<Entry> listEntryFirstLevel = EntryService.getDirectoryEntryList( directoryEntity.getIdDirectory( ), true );
146 
147         for ( Entry entry : listEntryFirstLevel )
148         {
149             List<Response> listEntryResponse = listResponse.stream( ).filter( response -> response.getEntry( ).getIdEntry( ) == entry.getIdEntry( ) )
150                     .collect( Collectors.toCollection( ArrayList::new ) );
151             model.put( MARK_LIST_RESPONSES, listEntryResponse );
152             EntryService.getHtmlEntry( model, entry.getIdEntry( ), strBuilder, getLocale( request ), true );
153         }
154         model.put( DirectoriesConstants.MARK_STR_ENTRY, strBuilder.toString( ) );
155         HtmlTemplate templateForm = AppTemplateService.getTemplate( TEMPLATE_HTML_CODE_FORM, getLocale( request ), model );
156         model.put( DirectoriesConstants.MARK_FORM_HTML, templateForm.getHtml( ) );
157 
158         return getXPage( TEMPLATE_ENTITY, request.getLocale( ), model );
159     }
160 
161 }