View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.directory.modules.multiview.web.record.panel.display.impl;
35  
36  import java.util.ArrayList;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  import org.apache.commons.lang3.math.NumberUtils;
45  
46  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.DirectoryRecordItem;
47  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.IRecordPanel;
48  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.configuration.RecordPanelConfiguration;
49  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.IRecordPanelDisplay;
50  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.util.IRecordListPosition;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  
53  /**
54   * Abstract class for RecordPanelDisplay class
55   */
56  public abstract class AbstractRecordPanelDisplay implements IRecordPanelDisplay, IRecordListPosition
57  {
58      // Template
59      private static final String TEMPLATE_RECORD_PANEL = "admin/plugins/directory/modules/multiview/panel/record_panel_template.html";
60  
61      // Marks
62      private static final String MARK_PANEL_ACTIVE = "panel_active";
63      private static final String MARK_PANEL_TECHNICAL_CODE = "panel_technical_code";
64      private static final String MARK_PANEL_TITLE = "panel_title";
65      private static final String MARK_PANEL_RECORD_NUMBER = "panel_recordNumber";
66  
67      // Constants
68      private static final int DEFAULT_RECORD_NUMBER = NumberUtils.INTEGER_ZERO;
69      private static final int DEFAULT_RECORD_PANEL_POSITION = NumberUtils.INTEGER_MINUS_ONE;
70  
71      // Variables
72      private boolean _bActive;
73      private String _strTemplate;
74      private IRecordPanel _recordPanel;
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public int getPosition( )
81      {
82          int nRecordPanelPosition = DEFAULT_RECORD_PANEL_POSITION;
83  
84          if ( _recordPanel != null )
85          {
86              RecordPanelConfiguration recordPanelConfiguration = _recordPanel.getRecordPanelConfiguration( );
87  
88              if ( recordPanelConfiguration != null )
89              {
90                  nRecordPanelPosition = recordPanelConfiguration.getPosition( );
91              }
92          }
93  
94          return nRecordPanelPosition;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public int getRecordNumber( )
102     {
103         int nRecordNumber = DEFAULT_RECORD_NUMBER;
104 
105         if ( _recordPanel != null )
106         {
107             List<DirectoryRecordItem> listDirectoryRecordItem = _recordPanel.getDirectoryRecordItemList( );
108 
109             if ( !CollectionUtils.isEmpty( listDirectoryRecordItem ) )
110             {
111                 nRecordNumber = listDirectoryRecordItem.size( );
112             }
113         }
114 
115         return nRecordNumber;
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public String getTemplate( )
123     {
124         return _strTemplate;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public boolean isActive( )
132     {
133         return _bActive;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public void setActive( boolean bActive )
141     {
142         _bActive = bActive;
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public String getTechnicalCode( )
150     {
151         String strTechnicalCode = StringUtils.EMPTY;
152 
153         IRecordPanel recordPanel = getRecordPanel( );
154         if ( recordPanel != null )
155         {
156             strTechnicalCode = recordPanel.getTechnicalCode( );
157         }
158 
159         return strTechnicalCode;
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public List<DirectoryRecordItem> getDirectoryRecordItemList( )
167     {
168         List<DirectoryRecordItem> listDirectoryRecordItemResult = new ArrayList<>( );
169 
170         if ( _recordPanel != null )
171         {
172             listDirectoryRecordItemResult = _recordPanel.getDirectoryRecordItemList( );
173         }
174 
175         return listDirectoryRecordItemResult;
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public IRecordPanel getRecordPanel( )
183     {
184         return _recordPanel;
185     }
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public void setRecordPanel( IRecordPanel recordPanel )
192     {
193         _recordPanel = recordPanel;
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public String buildTemplate( Locale locale )
201     {
202         String strTechnicalCode = StringUtils.EMPTY;
203         String strTitle = StringUtils.EMPTY;
204 
205         IRecordPanel recordPanel = getRecordPanel( );
206         if ( recordPanel != null )
207         {
208             strTechnicalCode = recordPanel.getTechnicalCode( );
209             strTitle = recordPanel.getTitle( );
210         }
211 
212         Map<String, Object> model = new LinkedHashMap<>( );
213         model.put( MARK_PANEL_ACTIVE, _bActive );
214         model.put( MARK_PANEL_TECHNICAL_CODE, strTechnicalCode );
215         model.put( MARK_PANEL_TITLE, strTitle );
216         model.put( MARK_PANEL_RECORD_NUMBER, getRecordNumber( ) );
217 
218         String strRecordPanelDisplayTemplate = AppTemplateService.getTemplate( TEMPLATE_RECORD_PANEL, locale, model ).getHtml( );
219         _strTemplate = strRecordPanelDisplayTemplate;
220 
221         return _strTemplate;
222     }
223 }