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.portlet.LiferayPortletConfig;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.servlet.SessionMessages;
022    import com.liferay.portal.kernel.util.ArrayUtil;
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.security.auth.PrincipalException;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.struts.PortletAction;
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(
073                                            (LiferayPortletConfig)portletConfig, actionRequest, false);
074                            }
075                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
076                                    deleteFolders(
077                                            (LiferayPortletConfig)portletConfig, actionRequest, true);
078                            }
079                            else if (cmd.equals(Constants.RESTORE)) {
080                                    restoreFolderFromTrash(actionRequest);
081                            }
082                            else if (cmd.equals(Constants.MOVE)) {
083                                    moveFolder(actionRequest);
084                            }
085    
086                            sendRedirect(actionRequest, actionResponse);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchFolderException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(actionRequest, e.getClass());
093    
094                                    setForward(actionRequest, "portlet.journal.error");
095                            }
096                            else if (e instanceof DuplicateFolderNameException ||
097                                             e instanceof FolderNameException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass());
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping actionMapping, ActionForm actionForm,
110                            PortletConfig portletConfig, RenderRequest renderRequest,
111                            RenderResponse renderResponse)
112                    throws Exception {
113    
114                    try {
115                            ActionUtil.getFolder(renderRequest);
116                    }
117                    catch (Exception e) {
118                            if (e instanceof NoSuchFolderException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(renderRequest, e.getClass());
122    
123                                    return actionMapping.findForward("portlet.journal.error");
124                            }
125                            else {
126                                    throw e;
127                            }
128                    }
129    
130                    return actionMapping.findForward(
131                            getForward(renderRequest, "portlet.journal.edit_folder"));
132            }
133    
134            protected void deleteFolders(
135                            LiferayPortletConfig liferayPortletConfig,
136                            ActionRequest actionRequest, boolean moveToTrash)
137                    throws Exception {
138    
139                    String deleteEntryTitle = null;
140    
141                    long[] deleteFolderIds = null;
142    
143                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
144    
145                    if (folderId > 0) {
146                            deleteFolderIds = new long[] {folderId};
147                    }
148                    else {
149                            deleteFolderIds = StringUtil.split(
150                                    ParamUtil.getString(actionRequest, "folderIds"), 0L);
151                    }
152    
153                    for (int i = 0; i < deleteFolderIds.length; i++) {
154                            long deleteFolderId = deleteFolderIds[i];
155    
156                            if (moveToTrash) {
157                                    JournalFolder folder =
158                                            JournalFolderServiceUtil.moveFolderToTrash(deleteFolderId);
159    
160                                    if (i == 0) {
161                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
162                                                    folder.getName());
163                                    }
164                            }
165                            else {
166                                    JournalFolderServiceUtil.deleteFolder(deleteFolderId);
167                            }
168    
169                            AssetPublisherUtil.removeRecentFolderId(
170                                    actionRequest, JournalArticle.class.getName(), deleteFolderId);
171                    }
172    
173                    if (moveToTrash && (deleteFolderIds.length > 0)) {
174                            Map<String, String[]> data = new HashMap<String, String[]>();
175    
176                            data.put(
177                                    "deleteEntryClassName",
178                                    new String[] {JournalFolder.class.getName()});
179    
180                            if (Validator.isNotNull(deleteEntryTitle)) {
181                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
182                            }
183    
184                            data.put(
185                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
186    
187                            SessionMessages.add(
188                                    actionRequest,
189                                    liferayPortletConfig.getPortletId() +
190                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
191    
192                            hideDefaultSuccessMessage(liferayPortletConfig, actionRequest);
193                    }
194            }
195    
196            protected void moveFolder(ActionRequest actionRequest) throws Exception {
197                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
198    
199                    long parentFolderId = ParamUtil.getLong(
200                            actionRequest, "parentFolderId");
201    
202                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
203                            JournalFolder.class.getName(), actionRequest);
204    
205                    JournalFolderServiceUtil.moveFolder(
206                            folderId, parentFolderId, serviceContext);
207            }
208    
209            protected void restoreFolderFromTrash(ActionRequest actionRequest)
210                    throws PortalException, SystemException {
211    
212                    long[] restoreEntryIds = StringUtil.split(
213                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
214    
215                    for (long restoreEntryId : restoreEntryIds) {
216                            JournalFolderServiceUtil.restoreFolderFromTrash(restoreEntryId);
217                    }
218            }
219    
220            protected void updateFolder(ActionRequest actionRequest) throws Exception {
221                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
222    
223                    long parentFolderId = ParamUtil.getLong(
224                            actionRequest, "parentFolderId");
225                    String name = ParamUtil.getString(actionRequest, "name");
226                    String description = ParamUtil.getString(actionRequest, "description");
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, false,
245                                    serviceContext);
246                    }
247            }
248    
249    }