View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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 org.apache.pluto.portalImpl.om.window.impl;
35  
36  import org.apache.pluto.om.common.ObjectID;
37  import org.apache.pluto.om.entity.PortletEntity;
38  import org.apache.pluto.om.window.PortletWindow;
39  import org.apache.pluto.om.window.PortletWindowCtrl;
40  import org.apache.pluto.portalImpl.services.log.Log;
41  
42  import java.util.Collections;
43  import java.util.HashMap;
44  import java.util.Iterator;
45  import java.util.Map;
46  
47  import javax.portlet.PortletMode;
48  import javax.portlet.WindowState;
49  
50  
51  //import fr.paris.lutece.plugins.jsr168.pluto.LutecePlutoConstant;
52  public class PortletWindowImpl implements PortletWindow, PortletWindowCtrl
53  {
54      private ObjectID _objectId;
55      private String _id;
56      private PortletEntity _portletEntity;
57      final private Map _renderParameters;
58      private WindowState _windowState;
59      private PortletMode _portletMode;
60      final private Map _prevRenderParameters;
61      private WindowState _prevWindowState;
62      private PortletMode _prevPortletMode;
63  
64      public PortletWindowImpl( final String id )
65      {
66          _renderParameters = new HashMap(  );
67          _prevRenderParameters = new HashMap(  );
68          _id = id;
69          _portletMode = PortletMode.VIEW;
70          _prevPortletMode = PortletMode.VIEW;
71          _windowState = WindowState.NORMAL;
72          _prevWindowState = WindowState.NORMAL;
73      }
74  
75      public Map getRenderParameters(  )
76      {
77          return Collections.unmodifiableMap( _renderParameters );
78      }
79  
80      public String getParameter( final String paramName )
81      {
82          final String[] values = (String[]) _renderParameters.get( paramName );
83  
84          if ( values != null )
85          {
86              return values[0];
87          }
88  
89          return null;
90      }
91  
92      public String[] getParameterValues( final String paramName )
93      {
94          final String[] values = (String[]) _renderParameters.get( paramName );
95  
96          return values;
97      }
98  
99      synchronized public void setRenderParameter( final Map parameters )
100     {
101         if ( parameters == null )
102         {
103             throw new IllegalArgumentException( "Parameter 'parameters' can't be null." );
104         }
105 
106         Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
107             "portlet " + _id + " old render params " + _renderParameters.size(  ) + " / " + _renderParameters );
108         Log.debug(  /*LutecePlutoConstant.LOG_CATEGORY,*/
109             "portlet " + _id + " new render params " + parameters.size(  ) + " / " + parameters );
110 
111         _renderParameters.clear(  );
112 
113         final Iterator itEntries = parameters.entrySet(  ).iterator(  );
114 
115         while ( itEntries.hasNext(  ) )
116         {
117             final Map.Entry entry = (Map.Entry) itEntries.next(  );
118             final Object name = entry.getKey(  );
119             final Object value = entry.getValue(  );
120 
121             if ( !( name instanceof String ) )
122             {
123                 throw new IllegalArgumentException( "Key name must be a String (key '" + name + "' (" +
124                     name.getClass(  ).getName(  ) + "))." );
125             }
126 
127             if ( value instanceof String )
128             {
129                 _renderParameters.put( name, new String[] { (String) value } );
130             }
131             else if ( value instanceof String[] )
132             {
133                 _renderParameters.put( name, value );
134             }
135             else
136             {
137                 throw new IllegalArgumentException( 
138                     "Values must be a String or String[] type String ou String[] (key '" + name + "' value is " +
139                     value.getClass(  ).getName(  ) + ")." );
140             }
141         }
142     }
143 
144     public void setPortletMode( final PortletMode portletMode )
145     {
146         if ( _portletMode != null )
147         {
148             _portletMode = portletMode;
149         }
150         else
151         {
152             _portletMode = PortletMode.VIEW;
153         }
154     }
155 
156     public PortletMode getPortletMode(  )
157     {
158         return _portletMode;
159     }
160 
161     public void setWindowState( final WindowState windowState )
162     {
163         if ( windowState != null )
164         {
165             _windowState = windowState;
166         }
167         else
168         {
169             _windowState = WindowState.NORMAL;
170         }
171     }
172 
173     public WindowState getWindowState(  )
174     {
175         return _windowState;
176     }
177 
178     // PortletWindow implementation.
179 
180     /**
181     * Returns the identifier of this portlet instance window as object id
182     *
183     * @return the object identifier
184     **/
185     public ObjectID getId(  )
186     {
187         if ( _objectId == null )
188         {
189             _objectId = org.apache.pluto.portalImpl.util.ObjectID.createFromString( _id );
190         }
191 
192         return _objectId;
193     }
194 
195     /**
196      * Returns the portlet entity
197      *
198      * @return the portlet entity
199      **/
200     public PortletEntity getPortletEntity(  )
201     {
202         return _portletEntity;
203     }
204 
205     // PortletWindowCtrl implementation.
206     /**
207      * binds an identifier to this portlet window
208      *
209      * @param id the new identifier
210      */
211     public void setId( String id )
212     {
213         _id = id;
214         _objectId = null;
215     }
216 
217     /**
218      * binds a portlet instance to this portlet window
219      *
220      * @param portletEntity a portlet entity object
221      **/
222     public void setPortletEntity( PortletEntity portletEntity )
223     {
224         _portletEntity = portletEntity;
225     }
226 
227     public void saveValues(  )
228     {
229         _prevPortletMode = _portletMode;
230         _prevWindowState = _windowState;
231         _prevRenderParameters.clear(  );
232         _prevRenderParameters.putAll( _renderParameters );
233     }
234 
235     public void restoreValues(  )
236     {
237         _portletMode = _prevPortletMode;
238         _windowState = _prevWindowState;
239         _renderParameters.clear(  );
240         _renderParameters.putAll( _prevRenderParameters );
241     }
242 
243     /**
244      * Return the previous portlet mode
245      *
246      * @return the previous portlet mode
247      */
248     public PortletMode getPrevPortletMode(  )
249     {
250         return _prevPortletMode;
251     }
252 
253     /**
254      * Return the previous window state
255      *
256      * @return the previous window state
257      */
258     public WindowState getPrevWindowState(  )
259     {
260         return _prevWindowState;
261     }
262 }