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