View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.document.business.rules;
35  
36  import fr.paris.lutece.portal.business.user.AdminUser;
37  
38  import java.util.HashMap;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  /**
45   * Default implementation of a rule
46   */
47  public abstract class AbstractRule implements Rule
48  {
49      private static final String PARAMETER_RULE_TYPE_ID = "id_rule_type";
50  
51      // Variables declarations
52      private int _nIdRule;
53      private String _strIdRuleType;
54      private Map<String, String> _mapAttributes = new HashMap<String, String>( );
55      private Locale _locale;
56      private AdminUser _user;
57  
58      /**
59       * Returns the IdRule
60       *
61       * @return The IdRule
62       */
63      public int getId( )
64      {
65          return _nIdRule;
66      }
67  
68      /**
69       * Sets the IdRule
70       *
71       * @param nIdRule
72       *            The IdRule
73       */
74      public void setId( int nIdRule )
75      {
76          _nIdRule = nIdRule;
77      }
78  
79      /**
80       * Returns the IdRuleType
81       *
82       * @return The IdRuleType
83       */
84      public String getRuleTypeId( )
85      {
86          return _strIdRuleType;
87      }
88  
89      /**
90       * Sets the IdRuleType
91       *
92       * @param strIdRuleType
93       *            The IdRuleType
94       */
95      public void setRuleTypeId( String strIdRuleType )
96      {
97          _strIdRuleType = strIdRuleType;
98      }
99  
100     /**
101      * Sets the locale
102      *
103      * @param locale
104      *            The locale
105      */
106     public void setLocale( Locale locale )
107     {
108         _locale = locale;
109     }
110 
111     /**
112      * Get the locale
113      * 
114      * @return The locale
115      */
116     public Locale getLocale( )
117     {
118         return _locale;
119     }
120 
121     /**
122      * Set an attribute
123      * 
124      * @param strAttributeName
125      *            the name of the attribute
126      * @param strAttributeValue
127      *            the value of the attribute
128      */
129     public void setAttribute( String strAttributeName, String strAttributeValue )
130     {
131         _mapAttributes.put( strAttributeName, strAttributeValue );
132     }
133 
134     /**
135      * Get the value of an attribute from its name
136      * 
137      * @param strAttributeNamethe
138      *            name of the attribute
139      * @return the value of the attribute
140      */
141     public String getAttribute( String strAttributeName )
142     {
143         return _mapAttributes.get( strAttributeName );
144     }
145 
146     /**
147      * Read attributes from a request
148      * 
149      * @param request
150      *            The request
151      */
152     public void readAttributes( HttpServletRequest request )
153     {
154         String strRuleTypeId = request.getParameter( PARAMETER_RULE_TYPE_ID );
155         setRuleTypeId( strRuleTypeId );
156 
157         String [ ] attributes = getAttributesList( );
158 
159         for ( int i = 0; i < attributes.length; i++ )
160         {
161             String strAttributeValue = request.getParameter( attributes [i] );
162 
163             if ( strAttributeValue != null )
164             {
165                 setAttribute( attributes [i], strAttributeValue );
166             }
167         }
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     @Override
174     public abstract boolean equals( Object obj );
175 
176     /**
177      * Gets the current user
178      * 
179      * @return The the current user
180      */
181     public AdminUser getUser( )
182     {
183         return _user;
184     }
185 
186     /**
187      * set the current user
188      * 
189      * @param user
190      *            the current user
191      *
192      */
193     public void setUser( AdminUser user )
194     {
195         _user = user;
196     }
197 }