1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
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 }