AbstractRecordPanelDisplay.java

/*
 * Copyright (c) 2002-2018, Mairie de Paris
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice
 *     and the following disclaimer.
 *
 *  2. Redistributions in binary form must reproduce the above copyright notice
 *     and the following disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *
 *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * License 1.0
 */
package fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import fr.paris.lutece.plugins.directory.modules.multiview.business.record.DirectoryRecordItem;
import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.IRecordPanel;
import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.configuration.RecordPanelConfiguration;
import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.IRecordPanelDisplay;
import fr.paris.lutece.plugins.directory.modules.multiview.web.record.util.IRecordListPosition;
import fr.paris.lutece.portal.service.template.AppTemplateService;

/**
 * Abstract class for RecordPanelDisplay class
 */
public abstract class AbstractRecordPanelDisplay implements IRecordPanelDisplay, IRecordListPosition
{
    // Template
    private static final String TEMPLATE_RECORD_PANEL = "admin/plugins/directory/modules/multiview/panel/record_panel_template.html";

    // Marks
    private static final String MARK_PANEL_ACTIVE = "panel_active";
    private static final String MARK_PANEL_TECHNICAL_CODE = "panel_technical_code";
    private static final String MARK_PANEL_TITLE = "panel_title";
    private static final String MARK_PANEL_RECORD_NUMBER = "panel_recordNumber";

    // Constants
    private static final int DEFAULT_RECORD_NUMBER = NumberUtils.INTEGER_ZERO;
    private static final int DEFAULT_RECORD_PANEL_POSITION = NumberUtils.INTEGER_MINUS_ONE;

    // Variables
    private boolean _bActive;
    private String _strTemplate;
    private IRecordPanel _recordPanel;

    /**
     * {@inheritDoc}
     */
    @Override
    public int getPosition( )
    {
        int nRecordPanelPosition = DEFAULT_RECORD_PANEL_POSITION;

        if ( _recordPanel != null )
        {
            RecordPanelConfiguration recordPanelConfiguration = _recordPanel.getRecordPanelConfiguration( );

            if ( recordPanelConfiguration != null )
            {
                nRecordPanelPosition = recordPanelConfiguration.getPosition( );
            }
        }

        return nRecordPanelPosition;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRecordNumber( )
    {
        int nRecordNumber = DEFAULT_RECORD_NUMBER;

        if ( _recordPanel != null )
        {
            List<DirectoryRecordItem> listDirectoryRecordItem = _recordPanel.getDirectoryRecordItemList( );

            if ( !CollectionUtils.isEmpty( listDirectoryRecordItem ) )
            {
                nRecordNumber = listDirectoryRecordItem.size( );
            }
        }

        return nRecordNumber;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getTemplate( )
    {
        return _strTemplate;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isActive( )
    {
        return _bActive;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setActive( boolean bActive )
    {
        _bActive = bActive;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getTechnicalCode( )
    {
        String strTechnicalCode = StringUtils.EMPTY;

        IRecordPanel recordPanel = getRecordPanel( );
        if ( recordPanel != null )
        {
            strTechnicalCode = recordPanel.getTechnicalCode( );
        }

        return strTechnicalCode;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<DirectoryRecordItem> getDirectoryRecordItemList( )
    {
        List<DirectoryRecordItem> listDirectoryRecordItemResult = new ArrayList<>( );

        if ( _recordPanel != null )
        {
            listDirectoryRecordItemResult = _recordPanel.getDirectoryRecordItemList( );
        }

        return listDirectoryRecordItemResult;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IRecordPanel getRecordPanel( )
    {
        return _recordPanel;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setRecordPanel( IRecordPanel recordPanel )
    {
        _recordPanel = recordPanel;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String buildTemplate( Locale locale )
    {
        String strTechnicalCode = StringUtils.EMPTY;
        String strTitle = StringUtils.EMPTY;

        IRecordPanel recordPanel = getRecordPanel( );
        if ( recordPanel != null )
        {
            strTechnicalCode = recordPanel.getTechnicalCode( );
            strTitle = recordPanel.getTitle( );
        }

        Map<String, Object> model = new LinkedHashMap<>( );
        model.put( MARK_PANEL_ACTIVE, _bActive );
        model.put( MARK_PANEL_TECHNICAL_CODE, strTechnicalCode );
        model.put( MARK_PANEL_TITLE, strTitle );
        model.put( MARK_PANEL_RECORD_NUMBER, getRecordNumber( ) );

        String strRecordPanelDisplayTemplate = AppTemplateService.getTemplate( TEMPLATE_RECORD_PANEL, locale, model ).getHtml( );
        _strTemplate = strRecordPanelDisplayTemplate;

        return _strTemplate;
    }
}