001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
024    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
025    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    /**
032     * @author Jorge Ferrer
033     * @author Sergio González
034     */
035    public class ConfigurationActionImpl extends DefaultConfigurationAction {
036    
037            @Override
038            public void processAction(
039                            PortletConfig portletConfig, ActionRequest actionRequest,
040                            ActionResponse actionResponse)
041                    throws Exception {
042    
043                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
044    
045                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
046    
047                    if (Validator.isNotNull(cmd)) {
048                            if (tabs2.equals("display-settings")) {
049                                    validateRootFolder(actionRequest);
050                            }
051                            else if (tabs2.equals("document-added-email")) {
052                                    validateEmailFileEntryAdded(actionRequest);
053                            }
054                            else if (tabs2.equals("document-updated-email")) {
055                                    validateEmailFileEntryUpdated(actionRequest);
056                            }
057                            else if (tabs2.equals("email-from")) {
058                                    validateEmailFrom(actionRequest);
059                            }
060                    }
061    
062                    super.processAction(portletConfig, actionRequest, actionResponse);
063            }
064    
065            protected void validateEmailFileEntryAdded(ActionRequest actionRequest)
066                    throws Exception {
067    
068                    String emailFileEntryAddedSubject = getLocalizedParameter(
069                            actionRequest, "emailFileEntryAddedSubject");
070                    String emailFileEntryAddedBody = getLocalizedParameter(
071                            actionRequest, "emailFileEntryAddedBody");
072    
073                    if (Validator.isNull(emailFileEntryAddedSubject)) {
074                            SessionErrors.add(actionRequest, "emailFileEntryAddedSubject");
075                    }
076                    else if (Validator.isNull(emailFileEntryAddedBody)) {
077                            SessionErrors.add(actionRequest, "emailFileEntryAddedBody");
078                    }
079            }
080    
081            protected void validateEmailFileEntryUpdated(ActionRequest actionRequest)
082                    throws Exception {
083    
084                    String emailFileEntryUpdatedSubject = getLocalizedParameter(
085                            actionRequest, "emailFileEntryUpdatedSubject");
086                    String emailFileEntryUpdatedBody = getLocalizedParameter(
087                            actionRequest, "emailFileEntryUpdatedBody");
088    
089                    if (Validator.isNull(emailFileEntryUpdatedSubject)) {
090                            SessionErrors.add(actionRequest, "emailFileEntryUpdatedSubject");
091                    }
092                    else if (Validator.isNull(emailFileEntryUpdatedBody)) {
093                            SessionErrors.add(actionRequest, "emailFileEntryUpdatedBody");
094                    }
095            }
096    
097            protected void validateEmailFrom(ActionRequest actionRequest)
098                    throws Exception {
099    
100                    String emailFromName = getParameter(actionRequest, "emailFromName");
101                    String emailFromAddress = getParameter(
102                            actionRequest, "emailFromAddress");
103    
104                    if (Validator.isNull(emailFromName)) {
105                            SessionErrors.add(actionRequest, "emailFromName");
106                    }
107                    else if (!Validator.isEmailAddress(emailFromAddress) &&
108                                     !Validator.isVariableTerm(emailFromAddress)) {
109    
110                            SessionErrors.add(actionRequest, "emailFromAddress");
111                    }
112            }
113    
114            protected void validateRootFolder(ActionRequest actionRequest)
115                    throws Exception {
116    
117                    long rootFolderId = GetterUtil.getLong(
118                            getParameter(actionRequest, "rootFolderId"));
119    
120                    if (rootFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
121                            try {
122                                    DLAppLocalServiceUtil.getFolder(rootFolderId);
123                            }
124                            catch (NoSuchFolderException nsfe) {
125                                    SessionErrors.add(actionRequest, "rootFolderIdInvalid");
126                            }
127                    }
128            }
129    
130    }