001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.journal.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Layout;
028: import com.liferay.portal.security.auth.PrincipalException;
029: import com.liferay.portal.struts.PortletAction;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portal.util.WebKeys;
032: import com.liferay.portlet.journal.DuplicateTemplateIdException;
033: import com.liferay.portlet.journal.NoSuchTemplateException;
034: import com.liferay.portlet.journal.RequiredTemplateException;
035: import com.liferay.portlet.journal.TemplateDescriptionException;
036: import com.liferay.portlet.journal.TemplateIdException;
037: import com.liferay.portlet.journal.TemplateNameException;
038: import com.liferay.portlet.journal.TemplateSmallImageNameException;
039: import com.liferay.portlet.journal.TemplateSmallImageSizeException;
040: import com.liferay.portlet.journal.TemplateXslException;
041: import com.liferay.portlet.journal.model.JournalTemplate;
042: import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
043: import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
044: import com.liferay.portlet.journal.util.JournalUtil;
045: import com.liferay.util.JS;
046: import com.liferay.util.servlet.SessionErrors;
047: import com.liferay.util.servlet.UploadPortletRequest;
048:
049: import java.io.File;
050:
051: import javax.portlet.ActionRequest;
052: import javax.portlet.ActionResponse;
053: import javax.portlet.PortletConfig;
054: import javax.portlet.RenderRequest;
055: import javax.portlet.RenderResponse;
056:
057: import org.apache.struts.action.ActionForm;
058: import org.apache.struts.action.ActionForward;
059: import org.apache.struts.action.ActionMapping;
060:
061: /**
062: * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
063: *
064: * @author Brian Wing Shun Chan
065: *
066: */
067: public class EditTemplateAction extends PortletAction {
068:
069: public void processAction(ActionMapping mapping, ActionForm form,
070: PortletConfig config, ActionRequest req, ActionResponse res)
071: throws Exception {
072:
073: String cmd = ParamUtil.getString(req, Constants.CMD);
074:
075: try {
076: if (cmd.equals(Constants.ADD)
077: || cmd.equals(Constants.UPDATE)) {
078: updateTemplate(req);
079: } else if (cmd.equals(Constants.DELETE)) {
080: deleteTemplates(req);
081: }
082:
083: sendRedirect(req, res);
084: } catch (Exception e) {
085: if (e instanceof NoSuchTemplateException
086: || e instanceof PrincipalException) {
087:
088: SessionErrors.add(req, e.getClass().getName());
089:
090: setForward(req, "portlet.journal.error");
091: } else if (e instanceof DuplicateTemplateIdException
092: || e instanceof RequiredTemplateException
093: || e instanceof TemplateDescriptionException
094: || e instanceof TemplateIdException
095: || e instanceof TemplateNameException
096: || e instanceof TemplateSmallImageNameException
097: || e instanceof TemplateSmallImageSizeException
098: || e instanceof TemplateXslException) {
099:
100: SessionErrors.add(req, e.getClass().getName());
101:
102: if (e instanceof RequiredTemplateException) {
103: res.sendRedirect(ParamUtil.getString(req,
104: "redirect"));
105: }
106: } else {
107: throw e;
108: }
109: }
110: }
111:
112: public ActionForward render(ActionMapping mapping, ActionForm form,
113: PortletConfig config, RenderRequest req, RenderResponse res)
114: throws Exception {
115:
116: try {
117: String cmd = ParamUtil.getString(req, Constants.CMD);
118:
119: if (!cmd.equals(Constants.ADD)) {
120: ActionUtil.getTemplate(req);
121: }
122: } catch (NoSuchTemplateException nsse) {
123:
124: // Let this slide because the user can manually input a template id
125: // for a new template that does not yet exist.
126:
127: } catch (Exception e) {
128: if (//e instanceof NoSuchTemplateException ||
129: e instanceof PrincipalException) {
130:
131: SessionErrors.add(req, e.getClass().getName());
132:
133: return mapping.findForward("portlet.journal.error");
134: } else {
135: throw e;
136: }
137: }
138:
139: return mapping.findForward(getForward(req,
140: "portlet.journal.edit_template"));
141: }
142:
143: protected void deleteTemplates(ActionRequest req) throws Exception {
144: long groupId = ParamUtil.getLong(req, "groupId");
145:
146: String[] deleteTemplateIds = StringUtil.split(ParamUtil
147: .getString(req, "deleteTemplateIds"));
148:
149: for (int i = 0; i < deleteTemplateIds.length; i++) {
150: JournalTemplateServiceUtil.deleteTemplate(groupId,
151: deleteTemplateIds[i]);
152:
153: JournalUtil.removeRecentTemplate(req, deleteTemplateIds[i]);
154: }
155: }
156:
157: protected void updateTemplate(ActionRequest req) throws Exception {
158: UploadPortletRequest uploadReq = PortalUtil
159: .getUploadPortletRequest(req);
160:
161: String cmd = ParamUtil.getString(uploadReq, Constants.CMD);
162:
163: Layout layout = (Layout) uploadReq.getAttribute(WebKeys.LAYOUT);
164:
165: long groupId = ParamUtil.getLong(uploadReq, "groupId");
166:
167: String templateId = ParamUtil
168: .getString(uploadReq, "templateId");
169: boolean autoTemplateId = ParamUtil.getBoolean(uploadReq,
170: "autoTemplateId");
171:
172: String structureId = ParamUtil.getString(uploadReq,
173: "structureId");
174: String name = ParamUtil.getString(uploadReq, "name");
175: String description = ParamUtil.getString(uploadReq,
176: "description");
177:
178: String xsl = ParamUtil.getString(uploadReq, "xsl");
179: String xslContent = JS.decodeURIComponent(ParamUtil.getString(
180: uploadReq, "xslContent"));
181: boolean formatXsl = ParamUtil
182: .getBoolean(uploadReq, "formatXsl");
183:
184: if (Validator.isNull(xsl)) {
185: xsl = xslContent;
186: }
187:
188: String langType = ParamUtil.getString(uploadReq, "langType",
189: JournalTemplateImpl.LANG_TYPE_XSL);
190:
191: boolean cacheable = ParamUtil
192: .getBoolean(uploadReq, "cacheable");
193:
194: boolean smallImage = ParamUtil.getBoolean(uploadReq,
195: "smallImage");
196: String smallImageURL = ParamUtil.getString(uploadReq,
197: "smallImageURL");
198: File smallFile = uploadReq.getFile("smallFile");
199:
200: String[] communityPermissions = uploadReq
201: .getParameterValues("communityPermissions");
202: String[] guestPermissions = uploadReq
203: .getParameterValues("guestPermissions");
204:
205: JournalTemplate template = null;
206:
207: if (cmd.equals(Constants.ADD)) {
208:
209: // Add template
210:
211: template = JournalTemplateServiceUtil.addTemplate(
212: templateId, autoTemplateId, layout.getPlid(),
213: structureId, name, description, xsl, formatXsl,
214: langType, cacheable, smallImage, smallImageURL,
215: smallFile, communityPermissions, guestPermissions);
216: } else {
217:
218: // Update template
219:
220: template = JournalTemplateServiceUtil.updateTemplate(
221: groupId, templateId, structureId, name,
222: description, xsl, formatXsl, langType, cacheable,
223: smallImage, smallImageURL, smallFile);
224: }
225:
226: // Recent templates
227:
228: JournalUtil.addRecentTemplate(req, template);
229: }
230:
231: }
|