KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > PortletURLProviderImpl


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import javax.portlet.PortletMode;
26 import javax.portlet.WindowState;
27
28 import org.apache.avalon.framework.CascadingRuntimeException;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.cocoon.portal.LinkService;
32 import org.apache.cocoon.portal.PortalService;
33 import org.apache.cocoon.portal.coplet.CopletInstanceData;
34 import org.apache.cocoon.portal.event.CopletInstanceEvent;
35 import org.apache.cocoon.portal.event.Event;
36 import org.apache.cocoon.portal.event.ConvertableEvent;
37 import org.apache.cocoon.portal.event.impl.FullScreenCopletEvent;
38 import org.apache.cocoon.portal.layout.impl.CopletLayout;
39 import org.apache.cocoon.portal.pluto.om.PortletEntityImpl;
40 import org.apache.cocoon.portal.pluto.om.PortletWindowImpl;
41 import org.apache.pluto.om.window.PortletWindow;
42 import org.apache.pluto.services.information.PortletURLProvider;
43
44 /**
45  * Create the URL for a portlet.
46  *
47  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
48  *
49  * @version CVS $Id: PortletURLProviderImpl.java 290439 2005-09-20 12:51:04Z sylvain $
50  */

51 public class PortletURLProviderImpl
52        implements PortletURLProvider, CopletInstanceEvent, ConvertableEvent {
53     
54     /** The portlet window (target) */
55     protected final PortletWindow portletWindow;
56     
57     /** The new portlet mode */
58     protected PortletMode mode;
59     
60     /** The new window state */
61     protected WindowState state;
62     
63     /** Is this an action */
64     protected boolean action;
65     
66     /** Secure link? */
67     protected Boolean JavaDoc secure;
68     
69     /** Clear parameters */
70     protected boolean clearParameters;
71     
72     /** Parameters */
73     protected Map JavaDoc parameters;
74     
75     /** The generated url */
76     protected String JavaDoc generatedURL;
77     private final LinkService linkService;
78     private static final String JavaDoc DEFAULT_PORTLET_URL_REQUEST_PARAM = "url";
79
80     /**
81      * Constructor
82      */

83     public PortletURLProviderImpl(PortletWindow portletWindow,
84                                   ServiceManager manager) {
85         this.portletWindow = portletWindow;
86         PortalService service = null;
87         try {
88             service = (PortalService) manager.lookup(PortalService.ROLE);
89             this.linkService = service.getComponentManager().getLinkService();
90         } catch (ServiceException se) {
91             throw new CascadingRuntimeException("Unable to lookup portal service.", se);
92         } finally {
93             manager.release(service);
94         }
95     }
96
97     /**
98      * Constructor for factory
99      * @param service
100      * @param eventData
101      */

102     PortletURLProviderImpl(PortalService service, String JavaDoc eventData) {
103         this.linkService = service.getComponentManager().getLinkService();
104         PortletURLConverter urlConverter = new PortletURLConverter(eventData);
105         String JavaDoc copletId = urlConverter.getPortletId();
106         CopletInstanceData cid = service.getComponentManager().getProfileManager()
107             .getCopletInstanceData(copletId);
108         this.portletWindow = (PortletWindow)cid.getTemporaryAttribute("window");
109         this.mode = urlConverter.getMode();
110         this.state = urlConverter.getState();
111         this.action = urlConverter.isAction();
112         this.parameters = urlConverter.getParameters();
113         this.clearParameters = false;
114         this.secure = null;
115     }
116
117     /**
118      * Copy constructor
119      */

120     private PortletURLProviderImpl(PortletURLProviderImpl original) {
121         this.linkService = original.linkService;
122         this.portletWindow = original.portletWindow;
123         this.mode = original.mode;
124         this.state = original.state;
125         this.action = original.action;
126         this.secure = original.secure;
127         this.clearParameters = original.clearParameters;
128         this.generatedURL = original.generatedURL;
129         if (original.parameters != null) {
130             this.parameters = new HashMap JavaDoc(original.parameters.size());
131             this.parameters.putAll(original.parameters);
132         }
133     }
134
135     /**
136      * Return the window
137      */

138     public PortletWindow getPortletWindow() {
139         return this.portletWindow;
140     }
141     
142     /**
143      * @see org.apache.pluto.services.information.PortletURLProvider#setPortletMode(javax.portlet.PortletMode)
144      */

145     public void setPortletMode(PortletMode mode) {
146         this.mode = mode;
147     }
148
149     /**
150      * Return the portlet mode
151      */

152     public PortletMode getPortletMode() {
153         return this.mode;
154     }
155     
156     /**
157      * @see org.apache.pluto.services.information.PortletURLProvider#setWindowState(javax.portlet.WindowState)
158      */

159     public void setWindowState(WindowState state) {
160         this.state = state;
161     }
162
163     /**
164      * Return the portlet mode
165      */

166     public WindowState getWindowState() {
167         return this.state;
168     }
169
170     /**
171      * @see org.apache.pluto.services.information.PortletURLProvider#setAction()
172      */

173     public void setAction() {
174         this.action = true;
175     }
176
177     /**
178      * Is this an action?
179      */

180     public boolean isAction() {
181         return this.action;
182     }
183         
184     /**
185      * @see org.apache.pluto.services.information.PortletURLProvider#setSecure()
186      */

187     public void setSecure() {
188         this.secure = Boolean.TRUE;
189     }
190
191     /**
192      * @see org.apache.pluto.services.information.PortletURLProvider#clearParameters()
193      */

194     public void clearParameters() {
195         this.clearParameters = true;
196     }
197
198     /**
199      * @see org.apache.pluto.services.information.PortletURLProvider#setParameters(java.util.Map)
200      */

201     public void setParameters(Map JavaDoc parameters) {
202         this.parameters = parameters;
203     }
204     
205     /**
206      * Return the parameters
207      */

208     public Map JavaDoc getParameters() {
209         if ( this.parameters == null ) {
210             return Collections.EMPTY_MAP;
211         }
212         return this.parameters;
213     }
214
215     /**
216      * @see java.lang.Object#toString()
217      */

218     public String JavaDoc toString() {
219         return new PortletURLProviderImpl(this).getURL();
220     }
221
222     /**
223      * @see java.lang.Object#toString()
224      */

225     private String JavaDoc getURL() {
226         if ( this.generatedURL == null ) {
227             final PortletWindowImpl impl = (PortletWindowImpl)this.portletWindow;
228             final CopletLayout cl = impl.getLayout();
229             Event sizingEvent = null;
230             if ( cl != null ) {
231                 final CopletInstanceData cid = cl.getCopletInstanceData();
232                 WindowState oldState = (WindowState)cid.getTemporaryAttribute("window-state");
233                 if ( oldState == null ) {
234                     oldState = WindowState.NORMAL;
235                 }
236                 if ( this.state != null && !this.state.equals(oldState) ) {
237                     if ( oldState.equals(WindowState.MAXIMIZED) ) {
238                         sizingEvent = new FullScreenCopletEvent( cid, null );
239                     } else {
240                         if ( this.state.equals(WindowState.MAXIMIZED) ) {
241                             sizingEvent = new FullScreenCopletEvent( cid, cl );
242                         }
243                     }
244                 }
245             }
246
247             List JavaDoc l = new ArrayList JavaDoc();
248             if ( sizingEvent != null ) {
249                 l.add(sizingEvent);
250             }
251             l.add(this);
252             if (secure == null) {
253                 this.generatedURL = this.linkService.getLinkURI(l);
254             } else {
255                 this.generatedURL = this.linkService.getLinkURI(l, secure);
256             }
257         }
258         return linkService.encodeURL(this.generatedURL);
259     }
260
261     /**
262      * @see org.apache.cocoon.portal.event.ActionEvent#getTarget()
263      */

264     public Object JavaDoc getTarget() {
265         return ((PortletEntityImpl)this.portletWindow.getPortletEntity()).getCopletInstanceData();
266     }
267
268     /**
269      * Return the URL as a String
270      *
271      * @return The URL as a String
272      */

273     public String JavaDoc asString() {
274
275         final PortletWindowImpl impl = (PortletWindowImpl) this.portletWindow;
276         final CopletLayout cl = impl.getLayout();
277         if (cl == null) {
278             return "";
279         }
280         final CopletInstanceData cid = cl.getCopletInstanceData();
281         PortletURLConverter urlConverter = new PortletURLConverter(cid);
282
283         if (this.mode != null) {
284             urlConverter.setMode(this.mode);
285         }
286
287         if (this.state != null) {
288             urlConverter.setState(this.state);
289         }
290
291         if (this.action) {
292             urlConverter.setAction();
293         }
294
295         if (this.parameters != null) {
296             Iterator JavaDoc entries = this.parameters.entrySet().iterator();
297             while (entries.hasNext()) {
298                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
299                 String JavaDoc name = (String JavaDoc) entry.getKey();
300                 Object JavaDoc value = entry.getValue();
301                 String JavaDoc[] values = value instanceof String JavaDoc ?
302                     new String JavaDoc[]{(String JavaDoc) value} : (String JavaDoc[]) value;
303                 urlConverter.setParam(name, values);
304             }
305         }
306
307         return urlConverter.toString();
308     }
309
310     /**
311      * The request parameter to be used for this event (if events are not hidden)
312      *
313      * @return The request parameter name for this event.
314      */

315     public String JavaDoc getRequestParameterName() {
316         return DEFAULT_PORTLET_URL_REQUEST_PARAM;
317     }
318
319 }
320
Popular Tags