View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.demo.preferences;
18  
19  import java.io.IOException;
20  import java.text.MessageFormat;
21  import java.util.ResourceBundle;
22  
23  import javax.portlet.ActionRequest;
24  import javax.portlet.ActionResponse;
25  import javax.portlet.GenericPortlet;
26  import javax.portlet.PortletConfig;
27  import javax.portlet.PortletContext;
28  import javax.portlet.PortletException;
29  import javax.portlet.PortletRequestDispatcher;
30  import javax.portlet.PortletSession;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  /***
35   * <p>
36   * PreferencePortlet
37   * </p>
38   * 
39   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
40   * @version $Id: PreferencePortlet.java 516448 2007-03-09 16:25:47Z ate $
41   *
42   */
43  public class PreferencePortlet extends GenericPortlet
44  {
45  
46      /***
47       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
48       */
49      protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
50      {
51          PortletContext context = getPortletContext();
52          ResourceBundle resource = getPortletConfig().getResourceBundle(request.getLocale());
53  
54          request.setAttribute("viewMessage", resource.getString("preference.label.MyModeIsView"));
55  
56          PortletRequestDispatcher rd = context.getRequestDispatcher("/WEB-INF/demo/preference/pref-view.jsp");
57          rd.include(request, response);
58      }
59  
60      /***
61       * @see javax.portlet.GenericPortlet#init()
62       */
63      public void init(PortletConfig config) throws PortletException
64      {
65  //        System.out.println("Initializing Preference portlet example. ");
66          super.init(config);
67      }
68  
69      /***
70       * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
71       */
72      public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
73      {
74          ResourceBundle resource = getPortletConfig().getResourceBundle(request.getLocale());
75          // Integer iCount = (Integer) request.getAttribute("org.apache.jetspeed.invocationCount");
76          Integer iCount = (Integer) request.getPortletSession().getAttribute("org.apache.jetspeed.invocationCount");
77          if (iCount == null)
78          {
79              iCount = new Integer(0);
80          }
81  
82          int count = iCount.intValue();
83          count++;
84  
85          response.setRenderParameter("invocationCount", String.valueOf(count));
86  
87          MessageFormat format = new MessageFormat(resource.getString("preference.label.processActionIWasInvoked"));
88          String[] patterns = { Integer.toString(count)};
89          response.setRenderParameter("invokeMessage", format.format(patterns));
90          request.getPortletSession().setAttribute("org.apache.jetspeed.invocationCount", new Integer(count), PortletSession.PORTLET_SCOPE);
91  //        System.out.println(resource.getString("preference.label.IWasInvoked"));
92      }
93  
94  }