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.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  import javax.servlet.http.HttpServletRequest;
39  import org.apache.commons.lang.StringUtils;
40  import fr.paris.lutece.plugins.directories.service.DirectoriesPlugin;
41  import fr.paris.lutece.plugins.directories.service.EntryService;
42  import fr.paris.lutece.plugins.genericattributes.business.Entry;
43  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
44  import fr.paris.lutece.plugins.genericattributes.business.Response;
45  import fr.paris.lutece.plugins.genericattributes.service.entrytype.AbstractEntryTypeMyLuteceUser;
46  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
47  import fr.paris.lutece.plugins.mylutece.business.attribute.AttributeHome;
48  import fr.paris.lutece.plugins.mylutece.business.attribute.IAttribute;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.util.ReferenceItem;
51  import fr.paris.lutece.util.ReferenceList;
52  
53  /**
54   * class EntryTypeText
55   */
56  public class EntryTypeMyLuteceAttribute extends AbstractEntryTypeMyLuteceUser
57  {
58      /**
59       * Name of the bean of this service
60       */
61      public static final String BEAN_NAME = "directories.entryTypeMyLuteceAttribute";
62      public static final String CONSTANT_COMMA = ",";
63      private static final String TEMPLATE_CREATE = "admin/plugins/directories/entries/create_entry_type_mylutece_user_attribute.html";
64      private static final String TEMPLATE_MODIFY = "admin/plugins/directories/entries/modify_entry_type_mylutece_user_attribute.html";
65      private static final String TEMPLATE_READONLY_BACKOFFICE = "admin/plugins/directories/entries/readonly_entry_type_mylutece_user_attribute.html";
66      private static final String TEMPLATE_EDITION_BACKOFFICE = "admin/plugins/directories/entries/fill_entry_type_mylutece_user_attribute.html";
67      private static final String TEMPLATE_EDITION_FRONTOFFICE = "skin/plugins/directories/entries/fill_entry_type_mylutece_user_attribute.html";
68      private static final String TEMPLATE_READONLY_FRONTOFFICE = "skin/plugins/directories/entries/readonly_entry_type_mylutece_user_attribute.html";
69      private static final String PROPERTY_ENTRY_TITLE = "directories.entryTypeMyLuteceUserAttribute.title";
70      private static final String PARAMETER_ONLY_DISPLAY_IN_BACK = "only_display_in_back";
71      private static final String PARAMETER_MYLUTECE_ATTRIBUTE_NAME = "mylutece_attribute_name";
72  
73      /**
74       * {@inheritDoc}
75       */
76      @Override
77      public String getTemplateHtmlForm( Entry entry, boolean bDisplayFront )
78      {
79          if ( bDisplayFront )
80          {
81              return TEMPLATE_EDITION_FRONTOFFICE;
82          }
83          return TEMPLATE_EDITION_BACKOFFICE;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public String getTemplateCreate( Entry entry, boolean bDisplayFront )
91      {
92          return TEMPLATE_CREATE;
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public String getTemplateModify( Entry entry, boolean bDisplayFront )
100     {
101         return TEMPLATE_MODIFY;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
109     {
110         initCommonRequestData( entry, request );
111         String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
112         entry.setTitle( I18nService.getLocalizedString( PROPERTY_ENTRY_TITLE, locale ) );
113         entry.setComment( StringUtils.EMPTY );
114         entry.setMandatory( Boolean.parseBoolean( request.getParameter( PARAMETER_MANDATORY ) ) );
115         entry.setCSSClass( request.getParameter( PARAMETER_CSS_CLASS ) );
116         entry.setTitle( request.getParameter( PARAMETER_TITLE ) );
117         entry.setHelpMessage( request.getParameter( PARAMETER_HELP_MESSAGE ) );
118         entry.setIndexed( request.getParameter( PARAMETER_INDEXED ) != null );
119         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
120         entry.setCode( request.getParameter( PARAMETER_ENTRY_CODE ) );
121         GenericAttributesUtils.createOrUpdateField( entry, FIELD_MYLUTECE_ATTRIBUTE_NAME_CODE, null,
122                 request.getParameter( PARAMETER_MYLUTECE_ATTRIBUTE_NAME ) );
123         return null;
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
131     {
132         String strValueEntry = request.getParameter( PREFIX_ATTRIBUTE + entry.getIdEntry( ) ).trim( );
133         if ( strValueEntry == null )
134         {
135             strValueEntry = "";
136         }
137         Response response = new Response( );
138         response.setEntry( entry );
139         response.setResponseValue( strValueEntry );
140         if ( StringUtils.isNotBlank( response.getResponseValue( ) ) )
141         {
142             response.setToStringValueResponse( getResponseValueForRecap( entry, request, response, locale ) );
143         }
144         else
145         {
146             response.setToStringValueResponse( StringUtils.EMPTY );
147         }
148 
149         response.setIterationNumber( getResponseIterationValue( request ) );
150         listResponse.add( response );
151         return null;
152     }
153 
154     /**
155      * Get a reference list with every lutece user attributes
156      * 
157      * @param strLangage
158      *            the langage of admin user
159      * @return The reference list with every user attributes
160      */
161     public ReferenceList getLuteceUserAttributesRefList( String strLangage )
162     {
163         ReferenceList referenceList = new ReferenceList( );
164         List<IAttribute> listMyLuteceAttribute = AttributeHome.findAll( Locale.forLanguageTag( strLangage ), DirectoriesPlugin.getPlugin( ) );
165         for ( IAttribute attribute : listMyLuteceAttribute )
166         {
167             ReferenceItem item = new ReferenceItem( );
168             item.setName( attribute.getTitle( ) );
169             item.setCode( String.valueOf( attribute.getIdAttribute( ) ) );
170             referenceList.add( item );
171         }
172         return referenceList;
173     }
174 
175     /**
176      * Get a reference list with every lutece user attributes
177      * 
178      * @param strLangage
179      *            the langage of admin user
180      * @return The reference list with every user attributes
181      */
182     public ReferenceList getMyLuteceUserPickerList( String strIdEntryType, String strIdDirectory )
183     {
184         List<Entry> listEntry = EntryService.getEntryListFromType( Integer.valueOf( strIdEntryType ), Integer.valueOf( strIdDirectory ) );
185         ReferenceList listEntryOpt = new ReferenceList( );
186         for ( Entry entry : listEntry )
187         {
188             ReferenceItem entryOpt = new ReferenceItem( );
189             entryOpt.setName( entry.getTitle( ) );
190             entryOpt.setCode( String.valueOf( entry.getIdEntry( ) ) );
191             listEntryOpt.add( entryOpt );
192         }
193         return listEntryOpt;
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
201     {
202         return response.getResponseValue( );
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
210     {
211         return response.getResponseValue( );
212     }
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public String getTemplateEntryReadOnly( boolean bDisplayFront )
219     {
220         if ( bDisplayFront )
221         {
222             return TEMPLATE_READONLY_FRONTOFFICE;
223         }
224         return TEMPLATE_READONLY_BACKOFFICE;
225     }
226 }