0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.jetspeed.portlets.pam;
0018:
0019: import java.io.IOException;
0020: import java.util.ArrayList;
0021: import java.util.Iterator;
0022: import java.util.LinkedHashMap;
0023: import java.util.List;
0024: import java.util.Locale;
0025: import java.util.StringTokenizer;
0026:
0027: import javax.portlet.ActionRequest;
0028: import javax.portlet.ActionResponse;
0029: import javax.portlet.PortletConfig;
0030: import javax.portlet.PortletContext;
0031: import javax.portlet.PortletException;
0032: import javax.portlet.PortletMode;
0033: import javax.portlet.PortletSession;
0034: import javax.portlet.RenderRequest;
0035: import javax.portlet.RenderResponse;
0036:
0037: import org.apache.jetspeed.CommonPortletServices;
0038: import org.apache.jetspeed.components.portletregistry.FailedToStorePortletDefinitionException;
0039: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
0040: import org.apache.jetspeed.components.portletregistry.RegistryException;
0041: import org.apache.jetspeed.om.common.GenericMetadata;
0042: import org.apache.jetspeed.om.common.LocalizedField;
0043: import org.apache.jetspeed.om.common.MutableDescription;
0044: import org.apache.jetspeed.om.common.MutableDisplayName;
0045: import org.apache.jetspeed.om.common.MutableLanguage;
0046: import org.apache.jetspeed.om.common.ParameterComposite;
0047: import org.apache.jetspeed.om.common.SecurityRoleRefComposite;
0048: import org.apache.jetspeed.om.common.UserAttribute;
0049: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
0050: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
0051: import org.apache.jetspeed.om.common.preference.PreferenceComposite;
0052: import org.apache.jetspeed.page.PageManager;
0053: import org.apache.jetspeed.portlets.pam.beans.PortletApplicationBean;
0054: import org.apache.jetspeed.search.SearchEngine;
0055: import org.apache.pluto.om.common.SecurityRoleRef;
0056: import org.apache.pluto.om.portlet.ContentType;
0057: import org.apache.portals.bridges.beans.TabBean;
0058: import org.apache.portals.bridges.common.GenericServletPortlet;
0059:
0060: /**
0061: * This portlet is a tabbed editor user interface for editing both portlet defintions
0062: * and portlet applications.
0063: *
0064: * @author <a href="mailto:jford@apache.com">Jeremy Ford</a>
0065: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
0066: * @version $Id: PortletApplicationDetail.java 348264 2005-11-22 22:06:45Z taylor $
0067: */
0068: public class PortletApplicationDetail extends GenericServletPortlet {
0069: private static final String PORTLET_ACTION = "portlet_action";
0070: private final String VIEW_PA = "portletApplication";
0071: private final String VIEW_PD = "portletDefinition";
0072:
0073: private static final String PORTLET_APP_ACTION_PREFIX = "portlet_app.";
0074: private static final String PORTLET_ACTION_PREFIX = "portlet.";
0075:
0076: private PortletContext context;
0077: private PortletRegistry registry;
0078: private PageManager pageManager;
0079: private SearchEngine searchEngine;
0080: private LinkedHashMap paTabMap = new LinkedHashMap();
0081: private LinkedHashMap pdTabMap = new LinkedHashMap();
0082:
0083: public void init(PortletConfig config) throws PortletException {
0084: super .init(config);
0085: context = getPortletContext();
0086: registry = (PortletRegistry) context
0087: .getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
0088: searchEngine = (SearchEngine) context
0089: .getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
0090: pageManager = (PageManager) context
0091: .getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
0092: if (null == registry) {
0093: throw new PortletException(
0094: "Failed to find the Portlet Registry on portlet initialization");
0095: }
0096:
0097: TabBean tb1 = new TabBean("pa_details");
0098: TabBean tb2 = new TabBean("pa_metadata");
0099: TabBean tb3 = new TabBean("pa_portlets");
0100: TabBean tb4 = new TabBean("pa_user_attribtues");
0101:
0102: paTabMap.put(tb1.getId(), tb1);
0103: paTabMap.put(tb2.getId(), tb2);
0104: paTabMap.put(tb3.getId(), tb3);
0105: paTabMap.put(tb4.getId(), tb4);
0106:
0107: TabBean tb_1 = new TabBean("pd_details");
0108: TabBean tb_2 = new TabBean("pd_metadata");
0109: TabBean tb_3 = new TabBean("pd_preferences");
0110: TabBean tb_4 = new TabBean("pd_languages");
0111: TabBean tb_5 = new TabBean("pd_parameters");
0112: TabBean tb_6 = new TabBean("pd_security");
0113: TabBean tb_7 = new TabBean("pd_content_type");
0114:
0115: pdTabMap.put(tb_1.getId(), tb_1);
0116: pdTabMap.put(tb_2.getId(), tb_2);
0117: pdTabMap.put(tb_3.getId(), tb_3);
0118: pdTabMap.put(tb_4.getId(), tb_4);
0119: pdTabMap.put(tb_5.getId(), tb_5);
0120: pdTabMap.put(tb_6.getId(), tb_6);
0121: pdTabMap.put(tb_7.getId(), tb_7);
0122: }
0123:
0124: public void doView(RenderRequest request, RenderResponse response)
0125: throws PortletException, IOException
0126:
0127: {
0128: response.setContentType("text/html");
0129:
0130: String paName = (String) request.getPortletSession()
0131: .getAttribute(
0132: PortletApplicationResources.PAM_CURRENT_PA,
0133: PortletSession.APPLICATION_SCOPE);
0134:
0135: MutablePortletApplication pa = registry
0136: .getPortletApplication(paName);
0137:
0138: if (null != pa) {
0139: request.setAttribute(VIEW_PA,
0140: new PortletApplicationBean(pa));
0141:
0142: String pdefName = (String) request
0143: .getPortletSession()
0144: .getAttribute(
0145: PortletApplicationResources.REQUEST_SELECT_PORTLET,
0146: PortletSession.APPLICATION_SCOPE);
0147: PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa
0148: .getPortletDefinitionByName(pdefName);
0149:
0150: request.setAttribute(VIEW_PD, pdef);
0151:
0152: request.setAttribute("tabs", paTabMap.values());
0153: request.setAttribute("portlet_tabs", pdTabMap.values());
0154:
0155: TabBean selectedTab = (TabBean) request
0156: .getPortletSession()
0157: .getAttribute(
0158: PortletApplicationResources.REQUEST_SELECT_TAB,
0159: PortletSession.APPLICATION_SCOPE);
0160: if (selectedTab == null) {
0161: selectedTab = (TabBean) paTabMap.values().iterator()
0162: .next();
0163: }
0164:
0165: //this supports tabs for portlets
0166: if (selectedTab.getId().equals("pa_portlets")) {
0167: TabBean selectedPortletTab = (TabBean) request
0168: .getPortletSession().getAttribute(
0169: "selected_portlet_tab");
0170: if (selectedPortletTab == null) {
0171: selectedPortletTab = (TabBean) pdTabMap.values()
0172: .iterator().next();
0173: }
0174: if (selectedPortletTab.getId().equals("pd_security")) {
0175: setupSecurityContraintContext(request, null, pdef);
0176: }
0177: request.setAttribute("selected_portlet_tab",
0178: selectedPortletTab);
0179: } else if (selectedTab.getId().equals("pa_details")) {
0180: setupSecurityContraintContext(request, pa, null);
0181: }
0182:
0183: request.setAttribute(
0184: PortletApplicationResources.REQUEST_SELECT_TAB,
0185: selectedTab);
0186: }
0187:
0188: super .doView(request, response);
0189: }
0190:
0191: private void setupSecurityContraintContext(RenderRequest request,
0192: MutablePortletApplication pa,
0193: PortletDefinitionComposite pdef) throws PortletException {
0194: try {
0195: List securityContraintRefList = pageManager
0196: .getPageSecurity().getSecurityConstraintsDefs();
0197: request.setAttribute("securityContraintRefList",
0198: securityContraintRefList);
0199: if (pdef == null) {
0200: request.setAttribute("currentSecurityConstraintRef", pa
0201: .getJetspeedSecurityConstraint());
0202: } else {
0203: request.setAttribute("currentSecurityConstraintRef",
0204: pdef.getJetspeedSecurityConstraint());
0205: }
0206: } catch (Exception e) {
0207: throw new PortletException(
0208: "Failed to retrieve security constraint references from "
0209: + (pdef == null ? "portlet application "
0210: + pa.getName()
0211: : "portlet definition "
0212: + pdef.getName()));
0213: }
0214: }
0215:
0216: public void processAction(ActionRequest actionRequest,
0217: ActionResponse actionResponse) throws PortletException,
0218: IOException {
0219: String paName = (String) actionRequest.getPortletSession()
0220: .getAttribute(
0221: PortletApplicationResources.PAM_CURRENT_PA,
0222: PortletSession.APPLICATION_SCOPE);
0223:
0224: String selectedPortlet = actionRequest
0225: .getParameter(PortletApplicationResources.REQUEST_SELECT_PORTLET);
0226: if (selectedPortlet != null) {
0227: actionRequest.getPortletSession().setAttribute(
0228: PortletApplicationResources.REQUEST_SELECT_PORTLET,
0229: selectedPortlet, PortletSession.APPLICATION_SCOPE);
0230: }
0231:
0232: String selectedTab = actionRequest
0233: .getParameter(PortletApplicationResources.REQUEST_SELECT_TAB);
0234: if (selectedTab != null) {
0235: TabBean tab = (TabBean) paTabMap.get(selectedTab);
0236: actionRequest.getPortletSession().setAttribute(
0237: PortletApplicationResources.REQUEST_SELECT_TAB,
0238: tab, PortletSession.APPLICATION_SCOPE);
0239: }
0240:
0241: String selectedPortletTab = actionRequest
0242: .getParameter("selected_portlet_tab");
0243: if (selectedPortletTab != null) {
0244: TabBean tab = (TabBean) pdTabMap.get(selectedPortletTab);
0245: actionRequest.getPortletSession().setAttribute(
0246: "selected_portlet_tab", tab);
0247: }
0248:
0249: String action = actionRequest.getParameter(PORTLET_ACTION);
0250: if (action != null) {
0251: MutablePortletApplication pa = registry
0252: .getPortletApplication(paName);
0253: if (isAppAction(action)) {
0254: action = getAction(PORTLET_APP_ACTION_PREFIX, action);
0255:
0256: if (action.endsWith("metadata")) {
0257: processMetadataAction(actionRequest,
0258: actionResponse, pa, null, action);
0259: } else if (action.endsWith("user_attribute")) {
0260: processUserAttributeAction(actionRequest,
0261: actionResponse, pa, action);
0262: } else if (action.endsWith("edit_security_constraint")) {
0263: processSecurityRef(actionRequest, actionResponse,
0264: pa, null, action);
0265: }
0266: searchEngine.update(pa);
0267: } else if (isPortletAction(action)) {
0268: action = getAction(PORTLET_ACTION_PREFIX, action);
0269: String pdefName = (String) actionRequest
0270: .getPortletSession()
0271: .getAttribute(
0272: PortletApplicationResources.REQUEST_SELECT_PORTLET,
0273: PortletSession.APPLICATION_SCOPE);
0274:
0275: try {
0276: PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa
0277: .getPortletDefinitionByName(pdefName);
0278: if (action.endsWith("metadata")) {
0279: processMetadataAction(actionRequest,
0280: actionResponse, null, pdef, action);
0281: } else if (action.endsWith("portlet")) {
0282: processPortletAction(actionRequest,
0283: actionResponse, pa, pdef, action);
0284: } else if (action.endsWith("preference")) {
0285: processPreferenceAction(actionRequest,
0286: actionResponse, pa, pdef, action);
0287: } else if (action.endsWith("language")) {
0288: processLanguage(actionRequest, actionResponse,
0289: pa, pdef, action);
0290: } else if (action.endsWith("parameter")) {
0291: processParameter(actionRequest, actionResponse,
0292: pa, pdef, action);
0293: } else if (action.endsWith("security")) {
0294: processSecurity(actionRequest, actionResponse,
0295: pa, pdef, action);
0296: } else if (action.endsWith("content_type")) {
0297: processContentType(actionRequest,
0298: actionResponse, pa, pdef, action);
0299: } else if (action
0300: .endsWith("edit_security_constraint")) {
0301: processSecurityRef(actionRequest,
0302: actionResponse, null, pdef, action);
0303: }
0304: searchEngine.update(pdef);
0305: } catch (RegistryException e) {
0306: throw new PortletException(
0307: "A Registry action has failed. "
0308: + e.getMessage());
0309: }
0310: }
0311: }
0312: }
0313:
0314: private boolean isAppAction(String action) {
0315: return action.startsWith(PORTLET_APP_ACTION_PREFIX);
0316: }
0317:
0318: private boolean isPortletAction(String action) {
0319: return action.startsWith(PORTLET_ACTION_PREFIX);
0320: }
0321:
0322: private String getAction(String prefix, String action) {
0323: return action.substring(prefix.length());
0324: }
0325:
0326: /**
0327: * @param actionRequest
0328: * @param actionResponse
0329: * @param pa
0330: * @param action
0331: */
0332: private void processUserAttributeAction(
0333: ActionRequest actionRequest, ActionResponse actionResponse,
0334: MutablePortletApplication mpa, String action)
0335: throws PortletException, IOException {
0336: boolean modified = false;
0337: if (action.equals("edit_user_attribute")) {
0338: String userAttrName = "";
0339:
0340: Iterator userAttrIter = mpa.getUserAttributes().iterator();
0341: while (userAttrIter.hasNext()) {
0342: UserAttribute userAttr = (UserAttribute) userAttrIter
0343: .next();
0344:
0345: userAttrName = userAttr.getName();
0346: String description = actionRequest
0347: .getParameter(userAttrName + ":description");
0348: if (!userAttr.getDescription().equals(description)) {
0349: userAttr.setDescription(description);
0350: modified = true;
0351: }
0352: }
0353: } else if (action.equals("add_user_attribute")) {
0354: String userAttrName = actionRequest
0355: .getParameter("user_attr_name");
0356: String userAttrDesc = actionRequest
0357: .getParameter("user_attr_desc");
0358: if (userAttrName != null
0359: && userAttrName.trim().length() > 0) {
0360: mpa.addUserAttribute(userAttrName.trim(), userAttrDesc);
0361: modified = true;
0362: }
0363: } else if (action.equals("remove_user_attribute")) {
0364: String[] userAttrNames = actionRequest
0365: .getParameterValues("user_attr_id");
0366: if (userAttrNames != null) {
0367: String userAttrName = "";
0368: int count = 0;
0369: Iterator userAttrIter = mpa.getUserAttributes()
0370: .iterator();
0371: while (userAttrIter.hasNext()) {
0372: UserAttribute userAttr = (UserAttribute) userAttrIter
0373: .next();
0374: for (int ix = 0; ix < userAttrNames.length; ix++) {
0375: userAttrName = userAttrNames[ix];
0376: if (userAttr.getName().equals(userAttrName)) {
0377: userAttrIter.remove();
0378: count++;
0379: break;
0380: }
0381: }
0382: }
0383: modified = count > 0;
0384: }
0385: }
0386:
0387: if (modified) {
0388: try {
0389: registry.updatePortletApplication(mpa);
0390: } catch (RegistryException e) {
0391: throw new PortletException(
0392: "Failed to update portlet application while performing action "
0393: + action, e);
0394: }
0395: }
0396: }
0397:
0398: /**
0399: * @param actionRequest
0400: * @param actionResponse
0401: * @param pa
0402: * @param action
0403: * @throws PortletException
0404: * @throws IOException
0405: */
0406: private void processMetadataAction(ActionRequest actionRequest,
0407: ActionResponse actionResponse,
0408: MutablePortletApplication pa,
0409: PortletDefinitionComposite pd, String action)
0410: throws PortletException, IOException {
0411: GenericMetadata meta = null;
0412:
0413: if (pd != null) {
0414: meta = pd.getMetadata();
0415: } else if (pa != null) {
0416: meta = pa.getMetadata();
0417: }
0418:
0419: if (meta == null) {
0420: return;
0421: }
0422:
0423: boolean modified = false;
0424: if (action.equals("edit_metadata")) {
0425: Iterator fieldsIter = meta.getFields().iterator();
0426: while (fieldsIter.hasNext()) {
0427: LocalizedField field = (LocalizedField) fieldsIter
0428: .next();
0429: String id = field.getId().toString();
0430: String value = actionRequest
0431: .getParameter(id + ":value");
0432: if (value != null) {
0433: if (!value.equals(field.getValue())) {
0434: field.setValue(value);
0435: modified = true;
0436: }
0437: }
0438: }
0439: } else if (action.equals("remove_metadata")) {
0440: String[] ids = actionRequest
0441: .getParameterValues("metadata_id");
0442: if (ids != null) {
0443: Iterator fieldsIter = meta.getFields().iterator();
0444: int count = 0;
0445: while (fieldsIter.hasNext()) {
0446: LocalizedField field = (LocalizedField) fieldsIter
0447: .next();
0448: String id = field.getId().toString();
0449:
0450: for (int i = 0; i < ids.length; i++) {
0451: String mid = ids[i];
0452: if (mid.equals(id)) {
0453: fieldsIter.remove();
0454: count++;
0455: break;
0456: }
0457: }
0458: }
0459: modified = count > 0;
0460: }
0461: } else if (action.equals("add_metadata")) {
0462: String name = actionRequest.getParameter("name");
0463: String value = actionRequest.getParameter("value");
0464: String localeParam = actionRequest.getParameter("locale");
0465:
0466: if (localeParam == null || name.trim().length() == 0) {
0467: localeParam = "en"; //need to default better
0468: }
0469: Locale locale = new Locale(localeParam);
0470:
0471: if (name != null && name.trim().length() > 0) {
0472: meta.addField(locale, name, value);
0473: modified = true;
0474: }
0475: }
0476:
0477: if (modified) {
0478: try {
0479: if (pd == null) {
0480: registry.updatePortletApplication(pa);
0481: } else {
0482: registry.savePortletDefinition(pd);
0483: }
0484: } catch (RegistryException e) {
0485: throw new PortletException("Failed to perform action "
0486: + action
0487: + " on "
0488: + (pd == null ? "portlet application "
0489: + pa.getName() : "portlet definition "
0490: + pd.getName()));
0491: }
0492: }
0493: }
0494:
0495: private void processPortletAction(ActionRequest actionRequest,
0496: ActionResponse actionResponse,
0497: MutablePortletApplication pa,
0498: PortletDefinitionComposite portlet, String action)
0499: throws RegistryException {
0500: if (action.equals("edit_portlet")) {
0501: String displayNameParam = actionRequest
0502: .getParameter("display_name");
0503: if (displayNameParam == null) {
0504: int index = 0;
0505: Iterator displayNameIter = portlet.getDisplayNameSet()
0506: .iterator();
0507: while (displayNameIter.hasNext()) {
0508: MutableDisplayName displayName = (MutableDisplayName) displayNameIter
0509: .next();
0510: displayNameParam = actionRequest
0511: .getParameter("display_name:" + index);
0512:
0513: //this should never happen
0514: if (displayNameParam != null) {
0515: if (displayNameParam.length() == 0) {
0516: displayNameIter.remove();
0517: } else if (!displayNameParam.equals(displayName
0518: .getDisplayName())) {
0519: displayName
0520: .setDisplayName(displayNameParam);
0521: }
0522: }
0523: index++;
0524: }
0525: String expirationCache = actionRequest
0526: .getParameter("expirationCache");
0527: if (expirationCache != null) {
0528: try {
0529: Integer.parseInt(expirationCache);
0530: portlet.setExpirationCache(expirationCache);
0531: } catch (NumberFormatException e) {
0532: }
0533: }
0534: } else {
0535: String locale = actionRequest.getParameter("locale");
0536: portlet.addDisplayName(new Locale(locale),
0537: displayNameParam);
0538: }
0539: } else if (action.equals("remove_portlet")) {
0540: //TODO should this be allowed??
0541: } else if (action.equals("add_portlet")) {
0542:
0543: }
0544: registry.savePortletDefinition(portlet);
0545: }
0546:
0547: /**
0548: * @param actionRequest
0549: * @param actionResponse
0550: * @param pa
0551: * @param pdef
0552: * @param action
0553: * @throws RegistryException
0554: */
0555: private void processPreferenceAction(ActionRequest actionRequest,
0556: ActionResponse actionResponse,
0557: MutablePortletApplication pa,
0558: PortletDefinitionComposite portlet, String action)
0559: throws RegistryException {
0560: if (action.equals("add_preference")) {
0561: String name = actionRequest.getParameter("name");
0562: String value = actionRequest.getParameter("value");
0563:
0564: PreferenceComposite pref = (PreferenceComposite) portlet
0565: .getPreferenceSet().get(name);
0566: if (pref == null) {
0567: portlet.addPreference(name, new String[] { value });
0568: } else {
0569: pref.addValue(value);
0570: }
0571: } else if (action.equals("edit_preference")) {
0572: String[] prefNames = actionRequest
0573: .getParameterValues("pref_edit_id");
0574: for (int i = 0; i < prefNames.length; i++) {
0575: String prefName = prefNames[i];
0576: PreferenceComposite prefComp = (PreferenceComposite) portlet
0577: .getPreferenceSet().get(prefName);
0578: String[] values = prefComp.getValueArray();
0579: for (int j = 0; j < values.length; j++) {
0580: String value = values[j];
0581: String newValue = actionRequest
0582: .getParameter(prefName + ":" + j);
0583: if (!value.equals(newValue)) {
0584: prefComp.setValueAt(j, newValue);
0585: }
0586: }
0587: }
0588: } else if (action.equals("remove_preference")) {
0589: String[] prefNames = actionRequest
0590: .getParameterValues("pref_remove_id");
0591:
0592: Iterator prefIter = portlet.getPreferenceSet().iterator();
0593: while (prefIter.hasNext()) {
0594: PreferenceComposite pref = (PreferenceComposite) prefIter
0595: .next();
0596: String name = pref.getName();
0597:
0598: for (int i = 0; i < prefNames.length; i++) {
0599: String prefName = prefNames[i];
0600: if (name.equals(prefName)) {
0601: prefIter.remove();
0602: break;
0603: }
0604: }
0605: }
0606: // registry.getPersistenceStore().getTransaction().commit();
0607: }
0608: registry.savePortletDefinition(portlet);
0609: }
0610:
0611: /**
0612: * @param actionRequest
0613: * @param actionResponse
0614: * @param pa
0615: * @param pdef
0616: * @param action
0617: * @throws RegistryException
0618: */
0619: private void processLanguage(ActionRequest actionRequest,
0620: ActionResponse actionResponse,
0621: MutablePortletApplication pa,
0622: PortletDefinitionComposite portlet, String action)
0623: throws RegistryException {
0624: if (action.equals("add_language")) {
0625: String title = actionRequest.getParameter("title");
0626: String shortTitle = actionRequest
0627: .getParameter("short_title");
0628: String keywords = actionRequest.getParameter("keyword");
0629: String locale = actionRequest.getParameter("locale");
0630:
0631: portlet.addLanguage(title, shortTitle, keywords,
0632: new Locale(locale));
0633: } else if (action.equals("remove_language")) {
0634: String[] removeIds = actionRequest
0635: .getParameterValues("language_remove_id");
0636: if (removeIds != null) {
0637: int id = 0;
0638: Iterator langIter = portlet.getLanguageSet().iterator();
0639: while (langIter.hasNext()) {
0640: langIter.next();
0641:
0642: int currentId = id++;
0643: for (int i = 0; i < removeIds.length; i++) {
0644: String removeId = removeIds[i];
0645: String tempId = "" + currentId;
0646: if (removeId.equals(tempId)) {
0647: langIter.remove();
0648: break;
0649: }
0650: }
0651: }
0652: }
0653: } else if (action.equals("edit_language")) {
0654: String[] editIds = actionRequest
0655: .getParameterValues("language_edit_id");
0656: if (editIds != null) {
0657: //technically, the size and set of edit ids should be
0658: //equal to the size and set of the language set
0659:
0660: String id;
0661: int index = 0;
0662: Iterator langIter = portlet.getLanguageSet().iterator();
0663: while (langIter.hasNext()) {
0664: id = editIds[index];
0665:
0666: String title = actionRequest.getParameter("title:"
0667: + id);
0668: String shortTitle = actionRequest
0669: .getParameter("short_title:" + id);
0670:
0671: //must cast to interface to avoid class loader issues
0672: MutableLanguage lang = (MutableLanguage) langIter
0673: .next();
0674:
0675: if (!lang.getTitle().equals(title)) {
0676: lang.setTitle(title);
0677: }
0678:
0679: Iterator keywordIter = lang.getKeywords();
0680: int keywordIndex = 0;
0681: ArrayList keywordList = new ArrayList();
0682:
0683: while (keywordIter.hasNext()) {
0684: keywordIter.next(); //retrieve the next keyword
0685: String keywordParam = actionRequest
0686: .getParameter("keyword:" + id + ":"
0687: + keywordIndex);
0688:
0689: if (keywordParam != null
0690: && keywordParam.length() > 0) {
0691: keywordList.add(keywordParam);
0692: }
0693:
0694: keywordIndex++;
0695: }
0696:
0697: lang.setKeywords(keywordList);
0698: if (lang.getShortTitle() == null
0699: || !lang.getShortTitle().equals(shortTitle)) {
0700: lang.setShortTitle(shortTitle);
0701: }
0702: index++;
0703: }
0704: }
0705: }
0706:
0707: registry.savePortletDefinition(portlet);
0708: }
0709:
0710: /**
0711: * @param actionRequest
0712: * @param actionResponse
0713: * @param pa
0714: * @param pdef
0715: * @param action
0716: * @throws FailedToStorePortletDefinitionException
0717: */
0718: private void processParameter(ActionRequest actionRequest,
0719: ActionResponse actionResponse,
0720: MutablePortletApplication pa,
0721: PortletDefinitionComposite portlet, String action)
0722: throws FailedToStorePortletDefinitionException {
0723: if (action.equals("add_parameter")) {
0724: String name = actionRequest.getParameter("name");
0725: if (name != null) {
0726: String description = actionRequest
0727: .getParameter("description");
0728: String locale = actionRequest.getParameter("locale");
0729:
0730: ParameterComposite parameter = (ParameterComposite) portlet
0731: .getInitParameterSet().get(name);
0732: if (parameter == null) {
0733: String value = actionRequest.getParameter("value");
0734: parameter = portlet.addInitParameter(name, value,
0735: description, new Locale(locale));
0736: } else {
0737: parameter.addDescription(new Locale(locale),
0738: description);
0739: }
0740: }
0741: } else if (action.equals("edit_parameter")) {
0742: String[] paramIds = actionRequest
0743: .getParameterValues("parameter_edit_id");
0744:
0745: if (paramIds != null) {
0746: for (int i = 0; i < paramIds.length; i++) {
0747: String paramId = paramIds[i];
0748: ParameterComposite param = (ParameterComposite) portlet
0749: .getInitParameterSet().get(paramId);
0750:
0751: String value = actionRequest.getParameter(paramId
0752: + ":value");
0753: param.setValue(value);
0754:
0755: int index = 0;
0756: Iterator descIter = param.getDescriptionSet()
0757: .iterator();
0758: while (descIter.hasNext()) {
0759: MutableDescription description = (MutableDescription) descIter
0760: .next();
0761: String descParam = actionRequest
0762: .getParameter(paramId + ":description:"
0763: + index);
0764: //changing locale not allowed.
0765:
0766: if (descParam != null) {
0767: if (descParam.length() == 0) {
0768: descIter.remove();
0769: } else if (!descParam.equals(description
0770: .getDescription())) {
0771: description.setDescription(descParam);
0772: }
0773: }
0774: index++;
0775: }
0776: }
0777: }
0778: } else if (action.equals("remove_parameter")) {
0779: String[] paramIds = actionRequest
0780: .getParameterValues("parameter_remove_id");
0781:
0782: if (paramIds != null) {
0783: Iterator paramIter = portlet.getInitParameterSet()
0784: .iterator();
0785: while (paramIter.hasNext()) {
0786: ParameterComposite param = (ParameterComposite) paramIter
0787: .next();
0788:
0789: for (int i = 0; i < paramIds.length; i++) {
0790: String paramId = paramIds[i];
0791: if (param.getName().equals(paramId)) {
0792: paramIter.remove();
0793: break;
0794: }
0795: }
0796: }
0797: }
0798: }
0799: registry.savePortletDefinition(portlet);
0800: }
0801:
0802: /**
0803: * @param actionRequest
0804: * @param actionResponse
0805: * @param pa
0806: * @param pdef
0807: * @param action
0808: * @throws FailedToStorePortletDefinitionException
0809: */
0810: private void processSecurity(ActionRequest actionRequest,
0811: ActionResponse actionResponse,
0812: MutablePortletApplication pa,
0813: PortletDefinitionComposite portlet, String action)
0814: throws FailedToStorePortletDefinitionException {
0815: if (action.equals("add_security")) {
0816: String name = actionRequest.getParameter("name");
0817:
0818: if (name != null) {
0819: String link = actionRequest.getParameter("link");
0820:
0821: SecurityRoleRefComposite securityRoleRef = (SecurityRoleRefComposite) portlet
0822: .getInitSecurityRoleRefSet().get(name);
0823: if (securityRoleRef == null && link != null) {
0824: securityRoleRef = (SecurityRoleRefComposite) portlet
0825: .addSecurityRoleRef(name, link);
0826: }
0827:
0828: if (securityRoleRef != null) {
0829: String description = actionRequest
0830: .getParameter("description");
0831: if (description != null && description.length() > 0) {
0832: String locale = actionRequest
0833: .getParameter("locale");
0834: if (locale == null) {
0835: locale = "en";
0836: }
0837: securityRoleRef.addDescription(new Locale(
0838: locale), description);
0839: }
0840: }
0841: }
0842: } else if (action.equals("edit_security")) {
0843: Iterator securityIter = portlet.getInitSecurityRoleRefSet()
0844: .iterator();
0845: while (securityIter.hasNext()) {
0846: SecurityRoleRefComposite secRef = (SecurityRoleRefComposite) securityIter
0847: .next();
0848: String name = secRef.getRoleName();
0849:
0850: //TODO: should this be editable
0851: // String newName = actionRequest.getParameter(name + ":name");
0852: String link = actionRequest
0853: .getParameter(name + ":link");
0854:
0855: if (!secRef.getRoleLink().equals(link)) {
0856: secRef.setRoleLink(link);
0857: }
0858:
0859: int index = 0;
0860: Iterator descIter = secRef.getDescriptionSet()
0861: .iterator();
0862: while (descIter.hasNext()) {
0863: MutableDescription description = (MutableDescription) descIter
0864: .next();
0865: String descParam = actionRequest.getParameter(name
0866: + ":description:" + index);
0867: //changing locale not allowed.
0868:
0869: if (descParam != null) {
0870: if (descParam.length() == 0) {
0871: descIter.remove();
0872: } else if (!descParam.equals(description
0873: .getDescription())) {
0874: description.setDescription(descParam);
0875: }
0876: }
0877:
0878: index++;
0879: }
0880: }
0881: } else if (action.equals("remove_security")) {
0882: String[] securityIds = actionRequest
0883: .getParameterValues("security_remove_id");
0884: if (securityIds != null) {
0885: for (int i = 0; i < securityIds.length; i++) {
0886: String id = securityIds[i];
0887: SecurityRoleRef secRef = portlet
0888: .getInitSecurityRoleRefSet().get(id);
0889: portlet.getInitSecurityRoleRefSet().remove(secRef);
0890: }
0891: /*
0892: Iterator securityIter = portlet.getInitSecurityRoleRefSet()..iterator();
0893: while (securityIter.hasNext())
0894: {
0895: SecurityRoleRefComposite secRef = (SecurityRoleRefComposite) securityIter.next();
0896: for(int i=0; i<securityIds.length; i++)
0897: {
0898: String id = securityIds[i];
0899: if(secRef.getRoleName().equals(id))
0900: {
0901: securityIter.remove();
0902: break;
0903: }
0904: }
0905: }
0906: */
0907: }
0908: }
0909: registry.savePortletDefinition(portlet);
0910: }
0911:
0912: private void processSecurityRef(ActionRequest actionRequest,
0913: ActionResponse actionResponse,
0914: MutablePortletApplication pa,
0915: PortletDefinitionComposite pdef, String action)
0916: throws PortletException {
0917: String ref = actionRequest
0918: .getParameter("security-constraint-ref");
0919: String currentRef = "";
0920: if (pa != null) {
0921: currentRef = pa.getJetspeedSecurityConstraint();
0922: } else {
0923: currentRef = pdef.getJetspeedSecurityConstraint();
0924: }
0925: if (currentRef == null) {
0926: currentRef = "";
0927: }
0928: if (!currentRef.equals(ref)) {
0929: if (ref.length() == 0) {
0930: ref = null;
0931: }
0932:
0933: try {
0934: if (pa != null) {
0935: pa.setJetspeedSecurityConstraint(ref);
0936: registry.updatePortletApplication(pa);
0937: } else {
0938: pdef.setJetspeedSecurityConstraint(ref);
0939: registry.savePortletDefinition(pdef);
0940: }
0941: } catch (RegistryException e) {
0942: throw new PortletException("Failed to perform action "
0943: + action
0944: + " on "
0945: + (pdef == null ? "portlet application "
0946: + pa.getName() : "portlet definition "
0947: + pdef.getName()));
0948: }
0949: }
0950: }
0951:
0952: /**
0953: * @param actionRequest
0954: * @param actionResponse
0955: * @param pa
0956: * @param pdef
0957: * @param action
0958: * @throws FailedToStorePortletDefinitionException
0959: */
0960: private void processContentType(ActionRequest actionRequest,
0961: ActionResponse actionResponse,
0962: MutablePortletApplication pa,
0963: PortletDefinitionComposite portlet, String action)
0964: throws FailedToStorePortletDefinitionException {
0965: if (action.equals("add_content_type")) {
0966: String contentType = actionRequest
0967: .getParameter("content_type");
0968: if (contentType != null) {
0969: ArrayList allModes = new ArrayList();
0970: String[] modes = actionRequest
0971: .getParameterValues("mode");
0972: if (modes != null) {
0973: for (int i = 0; i < modes.length; i++) {
0974: String mode = modes[i];
0975: //contentTypeImpl.addPortletMode(mode);
0976: allModes.add(new PortletMode(mode));
0977: }
0978: }
0979:
0980: String customModes = actionRequest
0981: .getParameter("custom_modes");
0982: StringTokenizer tok = new StringTokenizer(customModes,
0983: ",");
0984: while (tok.hasMoreTokens()) {
0985: //contentTypeImpl.addPortletMode(tok.nextToken());
0986: allModes.add(tok.nextToken());
0987: }
0988:
0989: portlet.addContentType(contentType, allModes);
0990: }
0991: } else if (action.equals("remove_content_type")) {
0992: String[] contentIds = actionRequest
0993: .getParameterValues("content_type_remove_id");
0994: if (contentIds != null) {
0995: Iterator contentIter = portlet.getContentTypeSet()
0996: .iterator();
0997: while (contentIter.hasNext()) {
0998: ContentType contentType = (ContentType) contentIter
0999: .next();
1000: for (int i = 0; i < contentIds.length; i++) {
1001: String id = contentIds[i];
1002: if (contentType.getContentType().equals(id)) {
1003: contentIter.remove();
1004: break;
1005: }
1006: }
1007: }
1008: }
1009: }
1010: registry.savePortletDefinition(portlet);
1011: // registry.getPersistenceStore().getTransaction().commit();
1012: }
1013:
1014: /*
1015: private String createXml(MutablePortletApplication pa)
1016: {
1017: StringBuffer buffer = new StringBuffer();
1018:
1019: buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1020:
1021: //TODO: add namespace
1022: buffer.append("<portlet-app id=\"");
1023: buffer.append(pa.getApplicationIdentifier());
1024: buffer.append("\" version=\"");
1025: buffer.append(pa.getVersion());
1026: buffer.append("\">\n");
1027:
1028: Iterator portletDefsIter = pa.getPortletDefinitions().iterator();
1029: while (portletDefsIter.hasNext())
1030: {
1031: PortletDefinitionComposite pDef = (PortletDefinitionComposite) portletDefsIter.next();
1032: buffer.append(createPortletDefinitionXml(pDef));
1033: }
1034:
1035: buffer.append("</portlet-app>\n");
1036:
1037: return buffer.toString();
1038: }
1039: */
1040:
1041: /*
1042: private String createPortletDefinitionXml(PortletDefinitionComposite pDef)
1043: {
1044: StringBuffer buffer = new StringBuffer();
1045:
1046: buffer.append("<portlet id=\"");
1047: buffer.append(pDef.getPortletIdentifier());
1048: buffer.append("\">\n");
1049:
1050: Iterator paramIter = pDef.getInitParameterSet().iterator();
1051: while (paramIter.hasNext())
1052: {
1053: ParameterComposite param = (ParameterComposite) paramIter.next();
1054: buffer.append("<init-param>\n");
1055:
1056: addDescriptions(buffer, param.getDescriptionSet());
1057:
1058: buffer.append("\t<name>");
1059: buffer.append(param.getName());
1060: buffer.append("</name>\n");
1061: buffer.append("\t<value>");
1062: buffer.append(param.getValue());
1063: buffer.append("</value>\n");
1064: buffer.append("</init-param>\n");
1065: }
1066:
1067: buffer.append("\t<portlet-name>");
1068: buffer.append(pDef.getName());
1069: buffer.append("</portlet-name>\n");
1070:
1071: Iterator displayNameIter = pDef.getDisplayNameSet().iterator();
1072: while (displayNameIter.hasNext())
1073: {
1074: MutableDisplayName displayName = (MutableDisplayName) displayNameIter.next();
1075: buffer.append("\t<display-name");
1076: if(displayName.getLocale() != null)
1077: {
1078: buffer.append(" xml:lang=\"");
1079: buffer.append(displayName.getLocale().getCountry());
1080: buffer.append("\"");
1081: }
1082: buffer.append(">");
1083: buffer.append(displayName.getDisplayName());
1084: buffer.append("</display-name>\n");
1085: }
1086:
1087: addDescriptions(buffer, pDef.getDescriptionSet());
1088:
1089: buffer.append("\t<portlet-class>");
1090: buffer.append(pDef.getClassName());
1091: buffer.append("</portlet-class>\n");
1092:
1093: buffer.append("\t<expiration-cache>");
1094: buffer.append(pDef.getExpirationCache());
1095: buffer.append("</expiration-cache>\n");
1096:
1097:
1098: Iterator contentTypeIter = pDef.getContentTypeSet().iterator();
1099: while (contentTypeIter.hasNext())
1100: {
1101: buffer.append("\t<supports>\n");
1102: ContentType contentType = (ContentType) contentTypeIter.next();
1103: buffer.append("\t\t<mime-type>\n");
1104: buffer.append(contentType.getContentType());
1105: buffer.append("</mime-type>\n");
1106:
1107: Iterator modeIter = contentType.getPortletModes();
1108: while (modeIter.hasNext())
1109: {
1110: PortletMode mode = (PortletMode) modeIter.next();
1111: buffer.append("\t\t<portlet-mode>");
1112: buffer.append(mode.toString());
1113: buffer.append("</portlet-mode>\n");
1114: }
1115:
1116: buffer.append("</supports>");
1117: }
1118:
1119:
1120:
1121: String resourceBundle = pDef.getResourceBundle();
1122: if(resourceBundle == null)
1123: {
1124: //<portlet-info>
1125: //StringBuffer supportedLocaleBuffer = new StringBuffer();
1126: StringBuffer portletInfoBuffer = new StringBuffer();
1127:
1128: Iterator langIter = pDef.getLanguageSet().iterator();
1129: while (langIter.hasNext())
1130: {
1131: MutableLanguage lang = (MutableLanguage) langIter.next();
1132:
1133: //supportedLocaleBuffer.append("\t<supported-locale>");
1134: //supportedLocaleBuffer.append(lang.getLocale().getCountry());
1135: //supportedLocaleBuffer.append("</supported-locale>\n");
1136:
1137:
1138: //lang.
1139: portletInfoBuffer.append("\t<portlet-info>\n");
1140: portletInfoBuffer.append("\t\t<title>");
1141: portletInfoBuffer.append(lang.getTitle());
1142: portletInfoBuffer.append("</title>\n");
1143: if(lang.getShortTitle() != null)
1144: {
1145: portletInfoBuffer.append("\t\t<short-title>");
1146: portletInfoBuffer.append(lang.getShortTitle());
1147: portletInfoBuffer.append("</short-title>\n");
1148: }
1149: Iterator keywordIter = lang.getKeywords();
1150: if(keywordIter.hasNext())
1151: {
1152: portletInfoBuffer.append("\t\t<keywords>");
1153: while (keywordIter.hasNext())
1154: {
1155: String keyword = (String) keywordIter.next();
1156: portletInfoBuffer.append(keyword);
1157: if(keywordIter.hasNext())
1158: {
1159: portletInfoBuffer.append(",");
1160: }
1161: }
1162: portletInfoBuffer.append("</keywords>\n");
1163: }
1164: portletInfoBuffer.append("\t</portlet-info>\n");
1165: }
1166:
1167: // buffer.append(supportedLocaleBuffer);
1168: buffer.append(portletInfoBuffer);
1169: }
1170: else
1171: {
1172: Iterator supportIter = pDef.getSupportedLocales().iterator();
1173: while (supportIter.hasNext())
1174: {
1175: Locale locale = (Locale) supportIter.next();
1176: buffer.append("\t<supported-locale>");
1177: buffer.append(locale.getCountry());
1178: buffer.append("<supported-locale>\n");
1179: }
1180: }
1181:
1182: buffer.append("\t<portlet-preferences>\n");
1183: Iterator prefIter = pDef.getPreferenceSet().iterator();
1184: while (prefIter.hasNext())
1185: {
1186: PreferenceComposite pref = (PreferenceComposite) prefIter.next();
1187: buffer.append("\t\t<preference>\n");
1188: buffer.append("\t\t\t<name>);");
1189: buffer.append(pref.getName());
1190: buffer.append("</name>\n");
1191: String[] values = pref.getValueArray();
1192: for (int i = 0; i < values.length; i++)
1193: {
1194: String value = values[i];
1195: buffer.append("\t\t\t<value>");
1196: buffer.append(value);
1197: buffer.append("</value>\n");
1198: }
1199:
1200: buffer.append("\t\t</preference>\n");
1201: }
1202: buffer.append("</portlet-preferences>");
1203:
1204: buffer.append("</portlet>\n");
1205:
1206: return buffer.toString();
1207: }
1208: */
1209:
1210: /*
1211: private void addDescriptions(StringBuffer buffer, DescriptionSet descriptions)
1212: {
1213: Iterator descIter = descriptions.iterator();
1214: MutableDescription desc = (MutableDescription) descIter.next();
1215: buffer.append("\t<description");
1216: if(desc.getLocale() != null)
1217: {
1218: buffer.append(" xml:lang=\"");
1219: buffer.append(desc.getLocale().getCountry());
1220: buffer.append("\"");
1221: }
1222: buffer.append(">");
1223: buffer.append(desc.getDescription());
1224: buffer.append("</description>\n");
1225: }
1226: */
1227: }
|