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.crm.business.portlet;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import fr.paris.lutece.plugins.crm.business.demand.DemandType;
45  import fr.paris.lutece.plugins.crm.business.demand.category.Category;
46  import fr.paris.lutece.plugins.crm.service.category.CategoryService;
47  import fr.paris.lutece.plugins.crm.service.demand.DemandTypeService;
48  import fr.paris.lutece.plugins.crm.util.CrmUtils;
49  import fr.paris.lutece.plugins.crm.util.constants.CRMConstants;
50  import fr.paris.lutece.portal.business.portlet.Portlet;
51  import fr.paris.lutece.portal.service.i18n.I18nService;
52  import fr.paris.lutece.util.xml.XmlUtil;
53  
54  /**
55   * This class represents business objects DemandTypePortlet
56   */
57  public class DemandTypePortlet extends Portlet
58  {
59  
60      // Constants
61      private int _nIdCategory;
62  
63      /**
64       * Sets the identifier of the portlet type to the value specified in the ArticlesListPortletHome class
65       */
66      public DemandTypePortlet( )
67      {
68      }
69  
70      /**
71       * Returns the Xml code of the DemandeTypePortlet portlet without XML heading
72       *
73       * @param request
74       *            The HTTP Servlet request
75       * @return the Xml code of the form portlet content
76       */
77      public String getXml( HttpServletRequest request )
78      {
79  
80          Locale locale;
81  
82          if ( request != null )
83          {
84              locale = request.getLocale( );
85          }
86          else
87          {
88              locale = I18nService.getDefaultLocale( );
89          }
90  
91          StringBuffer strXml = new StringBuffer( );
92          XmlUtil.beginElement( strXml, CRMConstants.TAG_DEMANDE_TYPE_PORTLET );
93          XmlUtil.beginElement( strXml, CRMConstants.TAG_DEMANDE_TYPE_PORTLET_CONTENT );
94          XmlUtil.beginElement( strXml, CRMConstants.TAG_CATEGORY_LIST );
95          if ( _nIdCategory != CrmUtils.CONSTANT_ID_NULL )
96          {
97              Category category = null;
98              if ( _nIdCategory != CrmUtils.convertStringToInt( CRMConstants.NO_CATEGORY ) )
99  
100             {
101                 category = CategoryService.getService( ).findByPrimaryKey( _nIdCategory );
102             }
103             else
104             {
105                 category = new Category( );
106                 category.setIdCategory( _nIdCategory );
107                 category.setName( " " );
108             }
109 
110             List<DemandType> listDemandType = DemandTypeService.getService( ).findForLuteceUser( request, _nIdCategory );
111             if ( category != null )
112             {
113                 strXml.append( category.getXml( request, locale, listDemandType ) );
114             }
115         }
116         else
117         {
118             Collection<Category> listCategory = CategoryService.getService( ).getCategoriesList( );
119             Map<String, List<DemandType>> mapCategoryDemandType = DemandTypeService.getService( ).findForLuteceUser( request );
120             for ( Category category : listCategory )
121             {
122                 strXml.append( category.getXml( request, locale, mapCategoryDemandType.get( Integer.toString( category.getIdCategory( ) ) ) ) );
123             }
124 
125         }
126         XmlUtil.endElement( strXml, CRMConstants.TAG_CATEGORY_LIST );
127         XmlUtil.endElement( strXml, CRMConstants.TAG_DEMANDE_TYPE_PORTLET_CONTENT );
128         XmlUtil.endElement( strXml, CRMConstants.TAG_DEMANDE_TYPE_PORTLET );
129 
130         String str = addPortletTags( strXml );
131 
132         return str;
133     }
134 
135     /**
136      * the Category id associated to the portlet
137      * 
138      * @return the Category id associated to the portlet
139      */
140     public int getIdCategory( )
141     {
142         return _nIdCategory;
143     }
144 
145     /**
146      * set the Category id associated to the portlet
147      * 
148      * @param _nIdCategory
149      *            set the Category id associated to the portlet
150      */
151     public void setIdCategory( int _nIdCategory )
152     {
153         this._nIdCategory = _nIdCategory;
154     }
155 
156     /**
157      * Returns the Xml code of the form portlet with XML heading
158      *
159      * @param request
160      *            The HTTP Servlet Request
161      * @return the Xml code of the Articles List portlet
162      */
163     public String getXmlDocument( HttpServletRequest request )
164     {
165         return XmlUtil.getXmlHeader( ) + getXml( request );
166     }
167 
168     /**
169      * Updates the current instance of the form portlet object
170      */
171     public void update( )
172     {
173         DemandTypePortletHome.getInstance( ).update( this );
174     }
175 
176     /**
177      * Removes the current instance of the the form portlet object
178      */
179     public void remove( )
180     {
181         DemandTypePortletHome.getInstance( ).remove( this );
182     }
183 
184     @Override
185     public Map<String, String> getXslParams( )
186     {
187         Locale defaultLocale = I18nService.getDefaultLocale( );
188         HashMap<String, String> mapParams = new HashMap<String, String>( );
189         mapParams.put( CRMConstants.MARK_XSL_PARAM_I18N_LABEL_DEMAND_TYPES_LIST,
190                 I18nService.getLocalizedString( CRMConstants.PROPERTY_LABEL_DEMAND_TYPES_LIST, defaultLocale ) );
191         mapParams.put( CRMConstants.MARK_XSL_PARAM_I18N_LABEL_CRM_INFO, I18nService.getLocalizedString( CRMConstants.PROPERTY_LABEL_CRM_INFO, defaultLocale ) );
192         mapParams.put( CRMConstants.MARK_XSL_PARAM_I18N_LABEL_CRM_CONTACT,
193                 I18nService.getLocalizedString( CRMConstants.PROPERTY_LABEL_CRM_CONTACT, defaultLocale ) );
194         mapParams.put( CRMConstants.MARK_XSL_PARAM_I18N_LABEL_CRM_DATE_BEGIN,
195                 I18nService.getLocalizedString( CRMConstants.PROPERTY_LABEL_CRM_DATE_BEGIN, defaultLocale ) );
196         mapParams.put( CRMConstants.MARK_XSL_PARAM_I18N_LABEL_CRM_DATE_END,
197                 I18nService.getLocalizedString( CRMConstants.PROPERTY_LABEL_CRM_DATE_END, defaultLocale ) );
198         return mapParams;
199     }
200 
201 }