001: package org.sakaiproject.portal.portlet;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Properties;
010:
011: import javax.portlet.ActionRequest;
012: import javax.portlet.ActionResponse;
013: import javax.portlet.GenericPortlet;
014: import javax.portlet.PortletConfig;
015: import javax.portlet.PortletException;
016: import javax.portlet.PortletMode;
017: import javax.portlet.PortletModeException;
018: import javax.portlet.PortletSecurityException;
019: import javax.portlet.PortletURL;
020: import javax.portlet.RenderRequest;
021: import javax.portlet.RenderResponse;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.sakaiproject.exception.IdUnusedException;
026: import org.sakaiproject.exception.PermissionException;
027: import org.sakaiproject.portal.api.PortalService;
028: import org.sakaiproject.portal.api.PortletApplicationDescriptor;
029: import org.sakaiproject.portal.api.PortletDescriptor;
030: import org.sakaiproject.portal.render.cover.ToolRenderService;
031: import org.sakaiproject.site.api.Site;
032: import org.sakaiproject.site.api.ToolConfiguration;
033: import org.sakaiproject.site.cover.SiteService;
034:
035: public class PlacementPortlet extends GenericPortlet {
036:
037: private static final Log log = LogFactory
038: .getLog(PlacementPortlet.class);
039:
040: private PortletRenderEngine rengine;
041:
042: private PortalService portalService;
043:
044: @Override
045: public void processAction(ActionRequest request,
046: ActionResponse response) throws PortletException,
047: IOException {
048: // find the tool configuration and save the settings
049: String portletContext = request.getParameter("context");
050: String portletName = request.getParameter("portlet");
051: String placementId = (String) request
052: .getAttribute(PortalService.PLACEMENT_ATTRIBUTE);
053: String setportlet = request.getParameter("setportlet");
054: log.debug("Got, PortletContext:" + portletContext
055: + "; PortletName:" + portletName + " Placement Id "
056: + placementId);
057: if ("true".equals(setportlet)) {
058: // locate the placement, then the site in which the site is
059: try {
060: ToolConfiguration config = SiteService
061: .findTool(placementId);
062: Site siteEdit = SiteService.getSite(config.getSiteId());
063: // bind the placement to the site
064: config = siteEdit.getTool(placementId);
065: Properties p = config.getPlacementConfig();
066: log
067: .debug("Changing "
068: + PortalService.TOOL_PORTLET_CONTEXT_PATH
069: + " from "
070: + p
071: .getProperty(PortalService.TOOL_PORTLET_CONTEXT_PATH)
072: + " to " + portletContext);
073: log
074: .debug("Changing "
075: + PortalService.TOOL_PORTLET_NAME
076: + " from "
077: + p
078: .getProperty(PortalService.TOOL_PORTLET_NAME)
079: + " to " + portletName);
080: p.setProperty(PortalService.TOOL_PORTLET_CONTEXT_PATH,
081: portletContext);
082: p.setProperty(PortalService.TOOL_PORTLET_NAME,
083: portletName);
084: config.save();
085: SiteService.save(siteEdit);
086: ToolRenderService.getInstance().reset(config);
087: log
088: .debug("Save Complete ================================");
089: } catch (IdUnusedException e) {
090: throw new PortletException(
091: "Tool has no site to save configuration with ",
092: e);
093: } catch (PermissionException e) {
094: throw new PortletSecurityException(
095: "Permission denied ", e);
096: }
097: }
098:
099: }
100:
101: @Override
102: public void init(PortletConfig config) throws PortletException {
103:
104: super .init(config);
105: // this should be a spring bean, but for the moment I dont want to bind
106: // to spring.
107: String renderEngineClass = config
108: .getInitParameter("renderEngineImpl");
109: if (renderEngineClass == null
110: || renderEngineClass.trim().length() == 0) {
111: renderEngineClass = PortletRenderEngine.DEFAULT_RENDER_ENGINE;
112: }
113:
114: try {
115: Class c = Class.forName(renderEngineClass);
116: rengine = (PortletRenderEngine) c.newInstance();
117: rengine.init();
118: } catch (Exception e) {
119: throw new PortletException("Failed to start velocity ", e);
120: }
121: portalService = org.sakaiproject.portal.api.cover.PortalService
122: .getInstance();
123: }
124:
125: public void doView(RenderRequest request, RenderResponse response)
126: throws PortletException, IOException {
127:
128: PortletRenderContext rcontext = rengine
129: .newRenderContext(request);
130: rcontext.put("test", "testing");
131: rcontext.put("portletapplications",
132: getPortletApplications(response));
133: try {
134: response.setContentType("text/html");
135: rengine.render("view", rcontext, response.getWriter());
136: } catch (Exception e) {
137: throw new PortletException(
138: "Failed to render view template ", e);
139: }
140: }
141:
142: protected void doEdit(RenderRequest request, RenderResponse response)
143: throws PortletException, IOException {
144: PortletRenderContext rcontext = rengine
145: .newRenderContext(request);
146: rcontext.put("test", "testing");
147: rcontext.put("portletapplications",
148: getPortletApplications(response));
149:
150: try {
151: response.setContentType("text/html");
152: rengine.render("edit", rcontext, response.getWriter());
153: } catch (Exception e) {
154: throw new PortletException(
155: "Failed to render edit template ", e);
156: }
157: }
158:
159: private List<Map> getPortletApplications(RenderResponse response)
160: throws PortletModeException {
161: List<Map> portletApps = new ArrayList<Map>();
162: for (Iterator<PortletApplicationDescriptor> i = portalService
163: .getRegisteredApplications(); i.hasNext();) {
164: PortletApplicationDescriptor portletApplication = i.next();
165: Map<String, Object> portletApp = new HashMap<String, Object>();
166: portletApp.put("name", portletApplication
167: .getApplicationContext());
168: List<Map> portlets = new ArrayList<Map>();
169: for (Iterator<PortletDescriptor> pai = portletApplication
170: .getPortlets(); pai.hasNext();) {
171: PortletDescriptor pd = pai.next();
172: Map<String, String> portletDesc = new HashMap<String, String>();
173: portletDesc.put("name", pd.getPortletName());
174: PortletURL purl = response.createActionURL();
175: purl.setParameter("context", portletApplication
176: .getApplicationId());
177: purl.setParameter("portlet", pd.getPortletId());
178: purl.setParameter("setportlet", "true");
179: purl.setPortletMode(PortletMode.VIEW);
180: portletDesc.put("url", purl.toString());
181: portlets.add(portletDesc);
182: }
183: portletApp.put("portlets", portlets);
184: portletApps.add(portletApp);
185: }
186: return portletApps;
187:
188: }
189:
190: protected void doHelp(RenderRequest request, RenderResponse response)
191: throws PortletException, IOException {
192: PortletRenderContext rcontext = rengine
193: .newRenderContext(request);
194: rcontext.put("test", "testing");
195: try {
196: response.setContentType("text/html");
197: rengine.render("help", rcontext, response.getWriter());
198: } catch (Exception e) {
199: throw new PortletException(
200: "Failed to render help template ", e);
201: }
202: }
203: }
|