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.NoSuchRepositoryEntryException;
018    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
027    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
028    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    
034    /**
035     * @author Jorge Ferrer
036     * @author Sergio Gonz??lez
037     */
038    public class ConfigurationActionImpl extends DefaultConfigurationAction {
039    
040            @Override
041            public void processAction(
042                            PortletConfig portletConfig, ActionRequest actionRequest,
043                            ActionResponse actionResponse)
044                    throws Exception {
045    
046                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
047    
048                    String tabs2 = ParamUtil.getString(actionRequest, "tabs2");
049    
050                    if (Validator.isNotNull(cmd)) {
051                            if (tabs2.equals("display-settings")) {
052                                    validateDisplayStyleViews(actionRequest);
053                            }
054                            else if (tabs2.equals("document-added-email")) {
055                                    validateEmailFileEntryAdded(actionRequest);
056                            }
057                            else if (tabs2.equals("document-updated-email")) {
058                                    validateEmailFileEntryUpdated(actionRequest);
059                            }
060                            else if (tabs2.equals("email-from")) {
061                                    validateEmailFrom(actionRequest);
062                            }
063    
064                            String portletResource = ParamUtil.getString(
065                                    actionRequest, "portletResource");
066    
067                            String rootPortletId = PortletConstants.getRootPortletId(
068                                    portletResource);
069    
070                            if (tabs2.equals("display-settings") ||
071                                    rootPortletId.equals(PortletKeys.DOCUMENT_LIBRARY_DISPLAY) ||
072                                    rootPortletId.equals(PortletKeys.MEDIA_GALLERY_DISPLAY)) {
073    
074                                    validateRootFolder( actionRequest);
075                            }
076                    }
077    
078                    super.processAction(portletConfig, actionRequest, actionResponse);
079            }
080    
081            protected void validateDisplayStyleViews(ActionRequest actionRequest)
082                    throws Exception {
083    
084                    String displayViews = GetterUtil.getString(
085                            getParameter(actionRequest, "displayViews"));
086    
087                    if (Validator.isNull(displayViews)) {
088                            SessionErrors.add(actionRequest, "displayViewsInvalid");
089                    }
090            }
091    
092            protected void validateEmailFileEntryAdded(ActionRequest actionRequest)
093                    throws Exception {
094    
095                    String emailFileEntryAddedSubject = getLocalizedParameter(
096                            actionRequest, "emailFileEntryAddedSubject");
097                    String emailFileEntryAddedBody = getLocalizedParameter(
098                            actionRequest, "emailFileEntryAddedBody");
099    
100                    if (Validator.isNull(emailFileEntryAddedSubject)) {
101                            SessionErrors.add(actionRequest, "emailFileEntryAddedSubject");
102                    }
103                    else if (Validator.isNull(emailFileEntryAddedBody)) {
104                            SessionErrors.add(actionRequest, "emailFileEntryAddedBody");
105                    }
106            }
107    
108            protected void validateEmailFileEntryUpdated(ActionRequest actionRequest)
109                    throws Exception {
110    
111                    String emailFileEntryUpdatedSubject = getLocalizedParameter(
112                            actionRequest, "emailFileEntryUpdatedSubject");
113                    String emailFileEntryUpdatedBody = getLocalizedParameter(
114                            actionRequest, "emailFileEntryUpdatedBody");
115    
116                    if (Validator.isNull(emailFileEntryUpdatedSubject)) {
117                            SessionErrors.add(actionRequest, "emailFileEntryUpdatedSubject");
118                    }
119                    else if (Validator.isNull(emailFileEntryUpdatedBody)) {
120                            SessionErrors.add(actionRequest, "emailFileEntryUpdatedBody");
121                    }
122            }
123    
124            protected void validateEmailFrom(ActionRequest actionRequest)
125                    throws Exception {
126    
127                    String emailFromName = getParameter(actionRequest, "emailFromName");
128                    String emailFromAddress = getParameter(
129                            actionRequest, "emailFromAddress");
130    
131                    if (Validator.isNull(emailFromName)) {
132                            SessionErrors.add(actionRequest, "emailFromName");
133                    }
134                    else if (!Validator.isEmailAddress(emailFromAddress) &&
135                                     !Validator.isVariableTerm(emailFromAddress)) {
136    
137                            SessionErrors.add(actionRequest, "emailFromAddress");
138                    }
139            }
140    
141            protected void validateRootFolder(ActionRequest actionRequest)
142                    throws Exception {
143    
144                    long rootFolderId = GetterUtil.getLong(
145                            getParameter(actionRequest, "rootFolderId"));
146    
147                    if (rootFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
148                            try {
149                                    DLAppLocalServiceUtil.getFolder(rootFolderId);
150                            }
151                            catch (Exception e) {
152                                    if (e instanceof NoSuchFolderException ||
153                                            e instanceof NoSuchRepositoryEntryException) {
154    
155                                            SessionErrors.add(actionRequest, "rootFolderIdInvalid");
156                                    }
157                                    else {
158                                            throw e;
159                                    }
160                            }
161                    }
162            }
163    
164    }