1
14
15 package com.liferay.portlet.bookmarks.action;
16
17 import com.liferay.portal.kernel.servlet.SessionErrors;
18 import com.liferay.portal.kernel.util.Constants;
19 import com.liferay.portal.kernel.util.ParamUtil;
20 import com.liferay.portal.model.LayoutTypePortlet;
21 import com.liferay.portal.security.auth.PrincipalException;
22 import com.liferay.portal.service.ServiceContext;
23 import com.liferay.portal.service.ServiceContextFactory;
24 import com.liferay.portal.struts.PortletAction;
25 import com.liferay.portal.theme.ThemeDisplay;
26 import com.liferay.portal.util.WebKeys;
27 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
28 import com.liferay.portlet.bookmarks.EntryURLException;
29 import com.liferay.portlet.bookmarks.NoSuchEntryException;
30 import com.liferay.portlet.bookmarks.NoSuchFolderException;
31 import com.liferay.portlet.bookmarks.model.BookmarksEntry;
32 import com.liferay.portlet.bookmarks.service.BookmarksEntryServiceUtil;
33 import com.liferay.portlet.tags.TagsEntryException;
34
35 import javax.portlet.ActionRequest;
36 import javax.portlet.ActionResponse;
37 import javax.portlet.PortletConfig;
38 import javax.portlet.RenderRequest;
39 import javax.portlet.RenderResponse;
40
41 import org.apache.struts.action.ActionForm;
42 import org.apache.struts.action.ActionForward;
43 import org.apache.struts.action.ActionMapping;
44
45
50 public class EditEntryAction extends PortletAction {
51
52 public void processAction(
53 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54 ActionRequest actionRequest, ActionResponse actionResponse)
55 throws Exception {
56
57 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58
59 try {
60 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61 updateEntry(actionRequest);
62 }
63 else if (cmd.equals(Constants.DELETE)) {
64 deleteEntry(actionRequest);
65 }
66
67 ThemeDisplay themeDisplay =
68 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
69
70 LayoutTypePortlet layoutTypePortlet =
71 themeDisplay.getLayoutTypePortlet();
72
73 if (layoutTypePortlet.hasPortletId(
74 portletConfig.getPortletName())) {
75
76 sendRedirect(actionRequest, actionResponse);
77 }
78 else {
79 String redirect = ParamUtil.getString(
80 actionRequest, "redirect");
81
82 actionResponse.sendRedirect(redirect);
83 }
84 }
85 catch (Exception e) {
86 if (e instanceof NoSuchEntryException ||
87 e instanceof PrincipalException) {
88
89 SessionErrors.add(actionRequest, e.getClass().getName());
90
91 setForward(actionRequest, "portlet.bookmarks.error");
92 }
93 else if (e instanceof EntryURLException ||
94 e instanceof NoSuchFolderException) {
95
96 SessionErrors.add(actionRequest, e.getClass().getName());
97 }
98 else if (e instanceof TagsEntryException) {
99 SessionErrors.add(actionRequest, e.getClass().getName(), e);
100 }
101 else {
102 throw e;
103 }
104 }
105 }
106
107 public ActionForward render(
108 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109 RenderRequest renderRequest, RenderResponse renderResponse)
110 throws Exception {
111
112 try {
113 ActionUtil.getEntry(renderRequest);
114 }
115 catch (Exception e) {
116 if (e instanceof NoSuchEntryException ||
117 e instanceof PrincipalException) {
118
119 SessionErrors.add(renderRequest, e.getClass().getName());
120
121 return mapping.findForward("portlet.bookmarks.error");
122 }
123 else {
124 throw e;
125 }
126 }
127
128 return mapping.findForward(
129 getForward(renderRequest, "portlet.bookmarks.edit_entry"));
130 }
131
132 protected void deleteEntry(ActionRequest actionRequest) throws Exception {
133 long entryId = ParamUtil.getLong(actionRequest, "entryId");
134
135 BookmarksEntryServiceUtil.deleteEntry(entryId);
136 }
137
138 protected void updateEntry(ActionRequest actionRequest) throws Exception {
139 long entryId = ParamUtil.getLong(actionRequest, "entryId");
140
141 long folderId = ParamUtil.getLong(actionRequest, "folderId");
142 String name = ParamUtil.getString(actionRequest, "name");
143 String url = ParamUtil.getString(actionRequest, "url");
144 String comments = ParamUtil.getString(actionRequest, "comments");
145
146 ServiceContext serviceContext = ServiceContextFactory.getInstance(
147 BookmarksEntry.class.getName(), actionRequest);
148
149 if (entryId <= 0) {
150
151
153 BookmarksEntry entry = BookmarksEntryServiceUtil.addEntry(
154 folderId, name, url, comments, serviceContext);
155
156 AssetPublisherUtil.addAndStoreSelection(
157 actionRequest, BookmarksEntry.class.getName(),
158 entry.getEntryId(), -1);
159 }
160 else {
161
162
164 BookmarksEntryServiceUtil.updateEntry(
165 entryId, folderId, name, url, comments, serviceContext);
166 }
167
168 AssetPublisherUtil.addRecentFolderId(
169 actionRequest, BookmarksEntry.class.getName(), folderId);
170 }
171
172 }