001
014
015 package com.liferay.portlet.bookmarks.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.LiferayWindowState;
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.HttpUtil;
025 import com.liferay.portal.kernel.util.ParamUtil;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.security.auth.PrincipalException;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.service.ServiceContextFactory;
031 import com.liferay.portal.struts.PortletAction;
032 import com.liferay.portal.theme.ThemeDisplay;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portal.util.WebKeys;
035 import com.liferay.portlet.asset.AssetCategoryException;
036 import com.liferay.portlet.asset.AssetTagException;
037 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
038 import com.liferay.portlet.bookmarks.EntryURLException;
039 import com.liferay.portlet.bookmarks.NoSuchEntryException;
040 import com.liferay.portlet.bookmarks.NoSuchFolderException;
041 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
042 import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
043 import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;
044
045 import java.util.HashMap;
046 import java.util.Map;
047
048 import javax.portlet.ActionRequest;
049 import javax.portlet.ActionResponse;
050 import javax.portlet.PortletConfig;
051 import javax.portlet.RenderRequest;
052 import javax.portlet.RenderResponse;
053 import javax.portlet.WindowState;
054
055 import org.apache.struts.action.ActionForm;
056 import org.apache.struts.action.ActionForward;
057 import org.apache.struts.action.ActionMapping;
058
059
063 public class EditEntryAction extends PortletAction {
064
065 @Override
066 public void processAction(
067 ActionMapping actionMapping, ActionForm actionForm,
068 PortletConfig portletConfig, ActionRequest actionRequest,
069 ActionResponse actionResponse)
070 throws Exception {
071
072 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073
074 try {
075 BookmarksEntry entry = null;
076
077 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
078 entry = updateEntry(actionRequest);
079 }
080 else if (cmd.equals(Constants.DELETE)) {
081 deleteEntry(actionRequest, false);
082 }
083 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
084 deleteEntry(actionRequest, true);
085 }
086 else if (cmd.equals(Constants.RESTORE)) {
087 restoreEntryFromTrash(actionRequest);
088 }
089 else if (cmd.equals(Constants.SUBSCRIBE)) {
090 subscribeEntry(actionRequest);
091 }
092 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
093 unsubscribeEntry(actionRequest);
094 }
095
096 WindowState windowState = actionRequest.getWindowState();
097
098 if (!windowState.equals(LiferayWindowState.POP_UP)) {
099 sendRedirect(actionRequest, actionResponse);
100 }
101 else {
102 String redirect = PortalUtil.escapeRedirect(
103 ParamUtil.getString(actionRequest, "redirect"));
104
105 if (Validator.isNotNull(redirect)) {
106 if (cmd.equals(Constants.ADD) && (entry != null)) {
107 String portletId = HttpUtil.getParameter(
108 redirect, "p_p_id", false);
109
110 String namespace = PortalUtil.getPortletNamespace(
111 portletId);
112
113 redirect = HttpUtil.addParameter(
114 redirect, namespace + "className",
115 BookmarksEntry.class.getName());
116 redirect = HttpUtil.addParameter(
117 redirect, namespace + "classPK",
118 entry.getEntryId());
119 }
120
121 actionResponse.sendRedirect(redirect);
122 }
123 }
124 }
125 catch (Exception e) {
126 if (e instanceof NoSuchEntryException ||
127 e instanceof PrincipalException) {
128
129 SessionErrors.add(actionRequest, e.getClass());
130
131 setForward(actionRequest, "portlet.bookmarks.error");
132 }
133 else if (e instanceof EntryURLException ||
134 e instanceof NoSuchFolderException) {
135
136 SessionErrors.add(actionRequest, e.getClass());
137 }
138 else if (e instanceof AssetCategoryException ||
139 e instanceof AssetTagException) {
140
141 SessionErrors.add(actionRequest, e.getClass(), e);
142 }
143 else {
144 throw e;
145 }
146 }
147 }
148
149 @Override
150 public ActionForward render(
151 ActionMapping actionMapping, ActionForm actionForm,
152 PortletConfig portletConfig, RenderRequest renderRequest,
153 RenderResponse renderResponse)
154 throws Exception {
155
156 try {
157 ActionUtil.getEntry(renderRequest);
158 }
159 catch (Exception e) {
160 if (e instanceof NoSuchEntryException ||
161 e instanceof PrincipalException) {
162
163 SessionErrors.add(renderRequest, e.getClass());
164
165 return actionMapping.findForward("portlet.bookmarks.error");
166 }
167 else {
168 throw e;
169 }
170 }
171
172 return actionMapping.findForward(
173 getForward(renderRequest, "portlet.bookmarks.edit_entry"));
174 }
175
176 protected void deleteEntry(ActionRequest actionRequest, boolean moveToTrash)
177 throws Exception {
178
179 String deleteEntryTitle = null;
180
181 long[] deleteEntryIds = null;
182
183 long entryId = ParamUtil.getLong(actionRequest, "entryId");
184
185 if (entryId > 0) {
186 deleteEntryIds = new long[] {entryId};
187 }
188 else {
189 deleteEntryIds = StringUtil.split(
190 ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
191 }
192
193 for (int i = 0; i < deleteEntryIds.length; i++) {
194 long deleteEntryId = deleteEntryIds[i];
195
196 if (moveToTrash) {
197 BookmarksEntry entry =
198 BookmarksEntryServiceUtil.moveEntryToTrash(deleteEntryId);
199
200 if (i == 0) {
201 deleteEntryTitle = entry.getName();
202 }
203 }
204 else {
205 BookmarksEntryServiceUtil.deleteEntry(deleteEntryId);
206 }
207 }
208
209 if (moveToTrash && (deleteEntryIds.length > 0)) {
210 Map<String, String[]> data = new HashMap<String, String[]>();
211
212 data.put(
213 "deleteEntryClassName",
214 new String[] {BookmarksEntry.class.getName()});
215
216 if (Validator.isNotNull(deleteEntryTitle)) {
217 data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
218 }
219
220 data.put(
221 "restoreEntryIds", ArrayUtil.toStringArray(deleteEntryIds));
222
223 SessionMessages.add(
224 actionRequest,
225 PortalUtil.getPortletId(actionRequest) +
226 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
227
228 hideDefaultSuccessMessage(actionRequest);
229 }
230 }
231
232 protected void restoreEntryFromTrash(ActionRequest actionRequest)
233 throws PortalException, SystemException {
234
235 long[] restoreFolderIds = StringUtil.split(
236 ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
237
238 for (long restoreFolderId : restoreFolderIds) {
239 BookmarksFolderServiceUtil.restoreFolderFromTrash(restoreFolderId);
240 }
241
242 long[] restoreEntryIds = StringUtil.split(
243 ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
244
245 for (long restoreEntryId : restoreEntryIds) {
246 BookmarksEntryServiceUtil.restoreEntryFromTrash(restoreEntryId);
247 }
248 }
249
250 protected void subscribeEntry(ActionRequest actionRequest)
251 throws Exception {
252
253 long entryId = ParamUtil.getLong(actionRequest, "entryId");
254
255 BookmarksEntryServiceUtil.subscribeEntry(entryId);
256 }
257
258 protected void unsubscribeEntry(ActionRequest actionRequest)
259 throws Exception {
260
261 long entryId = ParamUtil.getLong(actionRequest, "entryId");
262
263 BookmarksEntryServiceUtil.unsubscribeEntry(entryId);
264 }
265
266 protected BookmarksEntry updateEntry(ActionRequest actionRequest)
267 throws Exception {
268
269 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
270 WebKeys.THEME_DISPLAY);
271
272 long entryId = ParamUtil.getLong(actionRequest, "entryId");
273
274 long groupId = themeDisplay.getScopeGroupId();
275 long folderId = ParamUtil.getLong(actionRequest, "folderId");
276 String name = ParamUtil.getString(actionRequest, "name");
277 String url = ParamUtil.getString(actionRequest, "url");
278 String description = ParamUtil.getString(actionRequest, "description");
279
280 ServiceContext serviceContext = ServiceContextFactory.getInstance(
281 BookmarksEntry.class.getName(), actionRequest);
282
283 BookmarksEntry entry = null;
284
285 if (entryId <= 0) {
286
287
288
289 entry = BookmarksEntryServiceUtil.addEntry(
290 groupId, folderId, name, url, description, serviceContext);
291
292 AssetPublisherUtil.addAndStoreSelection(
293 actionRequest, BookmarksEntry.class.getName(),
294 entry.getEntryId(), -1);
295 }
296 else {
297
298
299
300 entry = BookmarksEntryServiceUtil.updateEntry(
301 entryId, groupId, folderId, name, url, description,
302 serviceContext);
303 }
304
305 AssetPublisherUtil.addRecentFolderId(
306 actionRequest, BookmarksEntry.class.getName(), folderId);
307
308 return entry;
309 }
310
311 }