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.journal.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
032    import com.liferay.portlet.journal.DuplicateFolderNameException;
033    import com.liferay.portlet.journal.FolderNameException;
034    import com.liferay.portlet.journal.NoSuchFolderException;
035    import com.liferay.portlet.journal.model.JournalArticle;
036    import com.liferay.portlet.journal.model.JournalFolder;
037    import com.liferay.portlet.journal.service.JournalFolderServiceUtil;
038    import com.liferay.portlet.trash.util.TrashUtil;
039    
040    import java.util.HashMap;
041    import java.util.Map;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.ActionResponse;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Sergio Gonz??lez
055     */
056    public class EditFolderAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping actionMapping, ActionForm actionForm,
061                            PortletConfig portletConfig, ActionRequest actionRequest,
062                            ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069                                    updateFolder(actionRequest);
070                            }
071                            else if (cmd.equals(Constants.DELETE)) {
072                                    deleteFolders(actionRequest, false);
073                            }
074                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
075                                    deleteFolders(actionRequest, true);
076                            }
077                            else if (cmd.equals(Constants.RESTORE)) {
078                                    restoreFolderFromTrash(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.MOVE)) {
081                                    moveFolder(actionRequest);
082                            }
083    
084                            sendRedirect(actionRequest, actionResponse);
085                    }
086                    catch (Exception e) {
087                            if (e instanceof NoSuchFolderException ||
088                                    e instanceof PrincipalException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass());
091    
092                                    setForward(actionRequest, "portlet.journal.error");
093                            }
094                            else if (e instanceof DuplicateFolderNameException ||
095                                             e instanceof FolderNameException) {
096    
097                                    SessionErrors.add(actionRequest, e.getClass());
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103            }
104    
105            @Override
106            public ActionForward render(
107                            ActionMapping actionMapping, ActionForm actionForm,
108                            PortletConfig portletConfig, RenderRequest renderRequest,
109                            RenderResponse renderResponse)
110                    throws Exception {
111    
112                    try {
113                            ActionUtil.getFolder(renderRequest);
114                    }
115                    catch (Exception e) {
116                            if (e instanceof NoSuchFolderException ||
117                                    e instanceof PrincipalException) {
118    
119                                    SessionErrors.add(renderRequest, e.getClass());
120    
121                                    return actionMapping.findForward("portlet.journal.error");
122                            }
123                            else {
124                                    throw e;
125                            }
126                    }
127    
128                    return actionMapping.findForward(
129                            getForward(renderRequest, "portlet.journal.edit_folder"));
130            }
131    
132            protected void deleteFolders(
133                            ActionRequest actionRequest, boolean moveToTrash)
134                    throws Exception {
135    
136                    String deleteEntryTitle = null;
137    
138                    long[] deleteFolderIds = null;
139    
140                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
141    
142                    if (folderId > 0) {
143                            deleteFolderIds = new long[] {folderId};
144                    }
145                    else {
146                            deleteFolderIds = StringUtil.split(
147                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
148                    }
149    
150                    for (int i = 0; i < deleteFolderIds.length; i++) {
151                            long deleteFolderId = deleteFolderIds[i];
152    
153                            if (moveToTrash) {
154                                    JournalFolder folder =
155                                            JournalFolderServiceUtil.moveFolderToTrash(deleteFolderId);
156    
157                                    if (i == 0) {
158                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
159                                                    folder.getName());
160                                    }
161                            }
162                            else {
163                                    JournalFolderServiceUtil.deleteFolder(deleteFolderId);
164                            }
165    
166                            AssetPublisherUtil.removeRecentFolderId(
167                                    actionRequest, JournalArticle.class.getName(), deleteFolderId);
168                    }
169    
170                    if (moveToTrash && (deleteFolderIds.length > 0)) {
171                            Map<String, String[]> data = new HashMap<String, String[]>();
172    
173                            data.put(
174                                    "deleteEntryClassName",
175                                    new String[] {JournalFolder.class.getName()});
176    
177                            if (Validator.isNotNull(deleteEntryTitle)) {
178                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
179                            }
180    
181                            data.put(
182                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
183    
184                            SessionMessages.add(
185                                    actionRequest,
186                                    PortalUtil.getPortletId(actionRequest) +
187                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
188    
189                            hideDefaultSuccessMessage(actionRequest);
190                    }
191            }
192    
193            protected void moveFolder(ActionRequest actionRequest) throws Exception {
194                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
195    
196                    long parentFolderId = ParamUtil.getLong(
197                            actionRequest, "parentFolderId");
198    
199                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
200                            JournalFolder.class.getName(), actionRequest);
201    
202                    JournalFolderServiceUtil.moveFolder(
203                            folderId, parentFolderId, serviceContext);
204            }
205    
206            protected void restoreFolderFromTrash(ActionRequest actionRequest)
207                    throws PortalException, SystemException {
208    
209                    long[] restoreEntryIds = StringUtil.split(
210                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
211    
212                    for (long restoreEntryId : restoreEntryIds) {
213                            JournalFolderServiceUtil.restoreFolderFromTrash(restoreEntryId);
214                    }
215            }
216    
217            protected void updateFolder(ActionRequest actionRequest) throws Exception {
218                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
219    
220                    long parentFolderId = ParamUtil.getLong(
221                            actionRequest, "parentFolderId");
222                    String name = ParamUtil.getString(actionRequest, "name");
223                    String description = ParamUtil.getString(actionRequest, "description");
224    
225                    boolean mergeWithParentFolder = ParamUtil.getBoolean(
226                            actionRequest, "mergeWithParentFolder");
227    
228                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
229                            JournalFolder.class.getName(), actionRequest);
230    
231                    if (folderId <= 0) {
232    
233                            // Add folder
234    
235                            JournalFolderServiceUtil.addFolder(
236                                    serviceContext.getScopeGroupId(), parentFolderId, name,
237                                    description, serviceContext);
238                    }
239                    else {
240    
241                            // Update folder
242    
243                            JournalFolderServiceUtil.updateFolder(
244                                    folderId, parentFolderId, name, description,
245                                    mergeWithParentFolder, serviceContext);
246                    }
247            }
248    
249    }