001: /*
002: * TemplateEditProvider.java
003: *
004: * Copyright 2002 Sun Microsystems, Inc. All
005: * rights reserved. Use of this product is subject
006: * to license terms. Federal Acquisitions:
007: * Commercial Software -- Government Users
008: * Subject to Standard License Terms and
009: * Conditions.
010: *
011: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
012: * are trademarks or registered trademarks of Sun Microsystems,
013: * Inc. in the United States and other countries.
014: */
015:
016: package com.sun.portal.providers.containers.template;
017:
018: import java.util.Hashtable;
019: import java.net.URL;
020: import java.net.URLEncoder;
021:
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.http.HttpServletResponse;
024:
025: import com.sun.portal.providers.Provider;
026: import com.sun.portal.providers.ProviderEditTypes;
027: import com.sun.portal.providers.ProviderException;
028: import com.sun.portal.providers.context.ProviderContext;
029: import com.sun.portal.providers.context.ContainerProviderContext;
030:
031: import com.sun.portal.desktop.context.ContextException;
032: import com.sun.portal.providers.InvalidEditFormDataException;
033:
034: public class TemplateEditProvider extends TemplateContainerProvider {
035:
036: public TemplateEditProvider() {
037: }
038:
039: public boolean isEditable() {
040: return true;
041: }
042:
043: public URL processEdit(HttpServletRequest req,
044: HttpServletResponse res) throws ProviderException {
045:
046: ContainerProviderContext cpc = getContainerProviderContext();
047: ProviderContext pc = getProviderContext();
048: Provider p = null;
049:
050: String targetprovider = req.getParameter("targetprovider");
051: String editArgs = req.getParameter("editArgs");
052:
053: if (targetprovider == null) {
054: throw new ProviderException(
055: "TemplateEditProvider.processEdit(): Unable to obtain the target provider");
056: }
057:
058: p = cpc.getProvider(req, getName(), targetprovider);
059:
060: if (p != null && !p.isEditable()) {
061: throw new ProviderException(
062: "TemplateEditProvider.processEdit():attempt to process non-editable channel="
063: + targetprovider);
064: }
065:
066: URL next = null;
067:
068: try {
069: next = p.processEdit(req, res);
070: } catch (InvalidEditFormDataException e) {
071: String error = pc.encodeURLParameter(e.getMessage());
072: StringBuffer args = new StringBuffer(64);
073: int errIndex = editArgs.indexOf("&error=");
074: if (errIndex > -1) {
075: args.append(pc.getDesktopURL(req)).append(
076: editArgs.substring(0, errIndex)).append(
077: "&error=").append(error);
078: } else {
079: args.append(pc.getDesktopURL(req)).append(editArgs)
080: .append("&error=").append(error);
081: }
082: try {
083: next = new URL(args.toString());
084: } catch (java.net.MalformedURLException m) {
085: throw new ProviderException(
086: "TemplateEditProvider.processEdit()", m);
087: }
088: }
089: return next;
090: }
091:
092: public StringBuffer getEdit(HttpServletRequest req,
093: HttpServletResponse res) throws ProviderException {
094:
095: ContainerProviderContext cpc = getContainerProviderContext();
096: ProviderContext pc = getProviderContext();
097: Provider cp = null;
098:
099: String provider = req.getParameter("provider");
100: String targetprovider = req.getParameter("targetprovider");
101: String containerName = req.getParameter("containerName");
102:
103: if (targetprovider == null) {
104: throw new ProviderException(
105: "TemplateEditProvider:processEdit(): Unable to obtain the target provider");
106: }
107:
108: cp = cpc.getProvider(req, getName(), targetprovider);
109:
110: if (cp != null && !cp.isEditable()) {
111: throw new ProviderException(
112: "TemplateEditProvider:processEdit(): attempt to process non-editable channel="
113: + targetprovider);
114: }
115:
116: StringBuffer editForm = null;
117:
118: editForm = cp.getEdit(req, res);
119:
120: if (editForm == null) {
121: throw new ProviderException(
122: "TemplateEditProvider:processEdit(): edit form was null for targetprovider="
123: + targetprovider);
124: }
125:
126: //
127: // double-check here. if the form type is complete, just return
128: // the content from the provider. this is to support old
129: // desktop action syntax such as:
130: // /dt?action=edit&provider=desktopEditContainer& \\
131: // targetprovider=SomeProvider
132: // where the edit provider is explcitly mentioned. with the new
133: // syntax and desktop code, the provider paramter is set to
134: // SomeProvider instead of edit provider, and targetprovider
135: // is omitted. the servlet decides whether to use the edit
136: // provider.
137: // /dt?action=edit&provider=SomeProvider
138: //
139: if (cp.getEditType() == ProviderEditTypes.EDIT_COMPLETE) {
140: return editForm;
141: }
142:
143: Hashtable tags = getStandardTags(req);
144:
145: //
146: // overwrite std help_link from desktop provider with link
147: // for provider (if it has one)
148: //
149: tags.put("help_link", getHelpLink("editPage", req));
150:
151: tags.put("title", cp.getTitle());
152:
153: StringBuffer args = new StringBuffer(64);
154: args.append("?").append(req.getQueryString());
155:
156: tags.put("provider", provider);
157: tags.put("targetprovider", targetprovider);
158: tags.put("editArgs", args.toString());
159: tags.put("contentOptions", editForm.toString());
160: if (containerName != null) {
161: tags.put(FRONT_CONTAINER_NAME, containerName);
162: }
163:
164: StringBuffer ob = getTemplate(EDIT_PROVIDER_TEMPLATE, tags);
165:
166: return ob;
167: }
168: }
|