View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.genericattributes.business.Entry;
44  import fr.paris.lutece.plugins.genericattributes.business.Field;
45  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
46  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
47  import fr.paris.lutece.plugins.genericattributes.business.Response;
48  import fr.paris.lutece.plugins.genericattributes.util.EntryTypeNumberingUtil;
49  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
50  import fr.paris.lutece.portal.service.i18n.I18nService;
51  import fr.paris.lutece.portal.service.message.AdminMessage;
52  import fr.paris.lutece.portal.service.message.AdminMessageService;
53  
54  /**
55   * Abstract entry type for incremental fields
56   */
57  public abstract class AbstractEntryTypeNumbering extends EntryTypeService
58  {
59      // PARAMETERS
60      private static final String PARAMETER_PREFIX = "prefix";
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
67      {
68          initCommonRequestData( entry, request );
69          String strTitle = request.getParameter( PARAMETER_TITLE );
70          String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
71          String strPrefix = request.getParameter( PARAMETER_PREFIX );
72          String strIndexed = request.getParameter( PARAMETER_INDEXED );
73          String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
74  
75          String strFieldError = StringUtils.EMPTY;
76  
77          if ( StringUtils.isBlank( strTitle ) )
78          {
79              strFieldError = ERROR_FIELD_TITLE;
80          }
81  
82          if ( StringUtils.isNotBlank( strFieldError ) )
83          {
84              Object [ ] tabRequiredFields = {
85                      I18nService.getLocalizedString( strFieldError, locale )
86              };
87  
88              return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
89          }
90  
91          entry.setCode( strCode );
92          entry.setTitle( strTitle );
93          entry.setCSSClass( strCSSClass );
94  
95          GenericAttributesUtils.createOrUpdateField( entry, FIELD_PREFIX, null, StringUtils.isNotEmpty( strPrefix ) ? strPrefix : StringUtils.EMPTY );
96          entry.setIndexed( strIndexed != null );
97          return null;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
105     {
106         int numbering = EntryTypeNumberingUtil.getInstance( ).getNextValue( entry.getIdEntry( ) );
107         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
108         response.setEntry( entry );
109         response.setResponseValue( String.valueOf( numbering ) );
110         response.setIterationNumber( getResponseIterationValue( request ) );
111         listResponse.add( response );
112 
113         return null;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String getResponseValueForExport( Entry entry, HttpServletRequest request, Response response, Locale locale )
121     {
122         return getResponseValue( entry, response );
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     @Override
129     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
130     {
131         return getResponseValue( entry, response );
132     }
133 
134     /**
135      * Get the response value
136      * 
137      * @param entry
138      *            The entry
139      * @return the response value of the response for this entry
140      * @param response
141      *            The response
142      */
143     private String getResponseValue( Entry entry, Response response )
144     {
145         if ( entry.getFields( ) == null )
146         {
147             entry.setFields( FieldHome.getFieldListByIdEntry( entry.getIdEntry( ) ) );
148         }
149         Field field = entry.getFieldByCode( FIELD_PREFIX );
150 
151         if ( field != null && StringUtils.isNotBlank( field.getValue( ) ) )
152         {
153             return field.getValue( ) + response.getResponseValue( );
154         }
155 
156         return response.getResponseValue( );
157     }
158 }