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.portlets.entityeditor;
18  
19  import java.io.IOException;
20  import java.util.Collection;
21  
22  import javax.portlet.ActionRequest;
23  import javax.portlet.ActionResponse;
24  import javax.portlet.PortletConfig;
25  import javax.portlet.PortletContext;
26  import javax.portlet.PortletException;
27  import javax.portlet.RenderRequest;
28  import javax.portlet.RenderResponse;
29  
30  import org.apache.jetspeed.CommonPortletServices;
31  import org.apache.jetspeed.PortalReservedParameters;
32  import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
33  import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
34  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
35  import org.apache.jetspeed.request.RequestContext;
36  import org.apache.pluto.om.entity.PortletEntity;
37  import org.apache.pluto.om.portlet.PortletDefinition;
38  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
39  import org.apache.velocity.context.Context;
40  
41  public class PortletEntityBrowserPortlet extends GenericVelocityPortlet
42  {
43  
44      private PortletEntityAccessComponent entityAccess;
45      private PortletRegistry registry;
46      
47  
48       /* (non-Javadoc)
49       * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#init(javax.portlet.PortletConfig)
50       */
51      public void init(PortletConfig config) throws PortletException
52      {
53          super.init(config);
54          PortletContext context = getPortletContext();
55          entityAccess = (PortletEntityAccessComponent)context.getAttribute(CommonPortletServices.CPS_ENTITY_ACCESS_COMPONENT);
56          registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
57      }
58  
59      public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
60      {
61          Collection portletApps = registry.getPortletApplications();
62          Context context = getContext(request);
63          context.put("portletApps", portletApps);
64          context.put("entityAccess", entityAccess);
65          context.put("portletContext", getPortletContext());
66          RequestContext requestContext = (RequestContext) request.getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
67          context.put("jetspeedContextPath", requestContext.getRequest().getContextPath());
68          super.doView(request, response);
69      }
70      
71      protected final void doCreate(ActionRequest request, ActionResponse response) throws PortletException
72      {
73          try
74          {
75              PortletDefinition pd = getPortletDefintion(request);
76              String newId = request.getParameter("newEntityId");
77              PortletEntity entity = null;
78              
79              if(newId != null)
80              {
81                  entity = entityAccess.newPortletEntityInstance(pd, newId);
82              }
83              else
84              {
85                  entity = entityAccess.newPortletEntityInstance(pd);
86              }
87              
88              entityAccess.storePortletEntity(entity);
89          }
90          catch (PortletEntityNotStoredException e)
91          {
92              throw new PortletException(e.getMessage(), e);
93          }
94          catch (PortletException e)
95          {
96              throw new PortletException(e.getMessage(), e);
97          }
98      }
99      
100     protected final PortletDefinition getPortletDefintion(ActionRequest request) throws PortletException
101     {
102         String portletUniqueName = request.getParameter("selectedPortlet");
103         if(portletUniqueName == null)
104         {
105             throw new PortletException("There was no 'portletUniqueName' parameter specified in the request.");
106         }
107         else
108         {
109            return registry.getPortletDefinitionByUniqueName(portletUniqueName);            
110         }
111     }
112     
113     public void processAction(ActionRequest request, ActionResponse actionResponse) throws PortletException, IOException
114     {
115         String action = request.getParameter("action");
116         
117         if(action == null)
118         {
119             throw new PortletException("Requires that action either be 'edit' or 'create'");
120         }
121         else if(action.equals("create"))
122         {
123             doCreate(request, actionResponse);
124         }
125         else
126         {
127             throw new PortletException("Requires that action to be 'create'");
128         }
129     }
130     
131     
132 
133 }