001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.ui.content.portlet;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.core.model.instance.Instance;
026: import org.jboss.portal.core.model.instance.InstanceContainer;
027: import org.jboss.portal.portlet.Portlet;
028: import org.jboss.portal.portlet.PortletInvokerException;
029: import org.jboss.portal.portlet.info.ModeInfo;
030: import org.jboss.portal.portlet.info.PortletInfo;
031:
032: import javax.portlet.ActionRequest;
033: import javax.portlet.ActionResponse;
034: import javax.portlet.GenericPortlet;
035: import javax.portlet.PortletException;
036: import javax.portlet.PortletMode;
037: import javax.portlet.PortletRequestDispatcher;
038: import javax.portlet.PortletSecurityException;
039: import javax.portlet.PortletURL;
040: import javax.portlet.RenderRequest;
041: import javax.portlet.RenderResponse;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.Comparator;
047: import java.util.Iterator;
048: import java.util.List;
049:
050: /**
051: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
052: * @version $Revision: 9221 $
053: */
054: public class PortletContentEditorPortlet extends GenericPortlet {
055:
056: public static final String DEFAULT_PORTLET_ICON = "/portal-core/images/portletIcon_Default1.gif";
057:
058: /** . */
059: private final PortletMode EDIT_CONTENT = new PortletMode(
060: "edit_content");
061:
062: /** . */
063: private final Mode INTERNAL_EDIT_CONTENT = Mode
064: .create("edit_content");
065:
066: /** . */
067: private InstanceContainer instanceContainer;
068:
069: public void init() throws PortletException {
070: instanceContainer = (InstanceContainer) getPortletContext()
071: .getAttribute("InstanceContainer");
072: }
073:
074: public void processAction(ActionRequest req, ActionResponse resp)
075: throws PortletException, PortletSecurityException,
076: IOException {
077: if ((req.getPortletMode().equals(EDIT_CONTENT))) {
078: String uri = req.getParameter("content.uri");
079: if (uri != null) {
080: resp.setRenderParameter("content.uri", uri);
081: }
082: }
083: }
084:
085: protected void doDispatch(RenderRequest req, RenderResponse resp)
086: throws PortletException, PortletSecurityException,
087: IOException {
088: if (EDIT_CONTENT.equals(req.getPortletMode())) {
089: doEditContent(req, resp);
090: } else {
091: super .doDispatch(req, resp);
092: }
093: }
094:
095: protected void doEditContent(RenderRequest req, RenderResponse resp)
096: throws PortletException, PortletSecurityException,
097: IOException {
098: getContent(req, resp, false);
099: }
100:
101: protected void getContent(RenderRequest req, RenderResponse resp,
102: boolean newContent) throws PortletException,
103: PortletSecurityException, IOException {
104: String selectedURI = req.getParameter("content.uri");
105:
106: //
107: PortletURL actionURL = resp.createActionURL();
108:
109: //
110: resp.setContentType("text/html");
111: PrintWriter writer = resp.getWriter();
112:
113: // load instances this user has access to.
114:
115: // Remove content editors
116: List available_instances = new ArrayList();
117: nextInstance: for (Iterator i = instanceContainer
118: .getDefinitions().iterator(); i.hasNext();) {
119: Instance instance = (Instance) i.next();
120:
121: //
122: try {
123: // Filter portlets that are editors
124: Portlet portlet = instance.getPortlet();
125: PortletInfo info = portlet.getInfo();
126: for (Iterator j = info.getCapabilities().getAllModes()
127: .iterator(); j.hasNext();) {
128: ModeInfo modeInfo = (ModeInfo) j.next();
129: if (modeInfo.getMode()
130: .equals(INTERNAL_EDIT_CONTENT)) {
131: continue nextInstance;
132: }
133: }
134:
135: //
136: available_instances.add(instance);
137: } catch (PortletInvokerException ignore) {
138: }
139: }
140:
141: // Sort alphabetically
142: Comparator simpleComparator = new Comparator() {
143: public int compare(Object o1, Object o2) {
144: Instance i1 = (Instance) o1;
145: Instance i2 = (Instance) o2;
146: return i1.getId().compareToIgnoreCase(i2.getId());
147: }
148: };
149: Collections.sort(available_instances, simpleComparator);
150:
151: //
152: Instance selectedInstance = null;
153: for (int i = 0; i < available_instances.size(); i++) {
154: Instance instance = (Instance) available_instances.get(i);
155: if (instance.getId().equals(selectedURI)) {
156: selectedInstance = instance;
157: }
158: }
159:
160: //
161: req.setAttribute("INSTANCES", available_instances);
162: req.setAttribute("SELECTED_INSTANCE", selectedInstance);
163: req.setAttribute("NEW_CONTENT", Boolean.valueOf(newContent));
164:
165: //
166: PortletRequestDispatcher dispatcher = getPortletContext()
167: .getRequestDispatcher(
168: "/WEB-INF/jsp/content/portlet_editor.jsp");
169: dispatcher.include(req, resp);
170:
171: //
172: writer.close();
173: }
174:
175: }
|