001
014
015 package com.liferay.portlet.trash.action;
016
017 import com.liferay.portal.TrashPermissionException;
018 import com.liferay.portal.kernel.json.JSONObject;
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.trash.TrashHandler;
023 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
024 import com.liferay.portal.kernel.trash.TrashRenderer;
025 import com.liferay.portal.kernel.util.Constants;
026 import com.liferay.portal.kernel.util.ObjectValuePair;
027 import com.liferay.portal.kernel.util.ParamUtil;
028 import com.liferay.portal.kernel.util.StringUtil;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.service.ServiceContextFactory;
032 import com.liferay.portal.struts.PortletAction;
033 import com.liferay.portal.theme.ThemeDisplay;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portal.util.WebKeys;
036 import com.liferay.portlet.trash.model.TrashEntry;
037 import com.liferay.portlet.trash.service.TrashEntryServiceUtil;
038 import com.liferay.portlet.trash.util.TrashUtil;
039
040 import java.util.ArrayList;
041 import java.util.HashMap;
042 import java.util.List;
043 import java.util.Map;
044
045 import javax.portlet.ActionRequest;
046 import javax.portlet.ActionResponse;
047 import javax.portlet.PortletConfig;
048 import javax.portlet.RenderRequest;
049 import javax.portlet.RenderResponse;
050 import javax.portlet.WindowState;
051
052 import org.apache.struts.action.ActionForm;
053 import org.apache.struts.action.ActionForward;
054 import org.apache.struts.action.ActionMapping;
055
056
060 public class EditEntryAction extends PortletAction {
061
062 @Override
063 public void processAction(
064 ActionMapping actionMapping, ActionForm actionForm,
065 PortletConfig portletConfig, ActionRequest actionRequest,
066 ActionResponse actionResponse)
067 throws Exception {
068
069 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070
071 try {
072 List<ObjectValuePair<String, Long>> entryOVPs = null;
073
074 if (cmd.equals(Constants.CHECK)) {
075 JSONObject jsonObject = ActionUtil.checkEntry(actionRequest);
076
077 writeJSON(actionRequest, actionResponse, jsonObject);
078
079 return;
080 }
081 else if (cmd.equals(Constants.DELETE)) {
082 deleteEntries(actionRequest);
083 }
084 else if (cmd.equals(Constants.EMPTY_TRASH)) {
085 emptyTrash(actionRequest);
086 }
087 else if (cmd.equals(Constants.MOVE)) {
088 entryOVPs = moveEntry(actionRequest);
089 }
090 else if (cmd.equals(Constants.RENAME)) {
091 entryOVPs = restoreRename(actionRequest);
092 }
093 else if (cmd.equals(Constants.RESTORE)) {
094 entryOVPs = restoreEntries(actionRequest);
095 }
096 else if (cmd.equals(Constants.OVERRIDE)) {
097 entryOVPs = restoreOverride(actionRequest);
098 }
099
100 if (cmd.equals(Constants.RENAME) || cmd.equals(Constants.RESTORE) ||
101 cmd.equals(Constants.OVERRIDE) || cmd.equals(Constants.MOVE)) {
102
103 addRestoreData(actionRequest, entryOVPs);
104 }
105
106 sendRedirect(actionRequest, actionResponse);
107 }
108 catch (Exception e) {
109 if (e instanceof TrashPermissionException) {
110 TrashPermissionException tpe = (TrashPermissionException)e;
111
112 SessionErrors.add(actionRequest, tpe.getClass(), tpe);
113 }
114 else {
115 SessionErrors.add(actionRequest, e.getClass());
116 }
117
118 WindowState windowState = actionRequest.getWindowState();
119
120 if (windowState.equals(LiferayWindowState.EXCLUSIVE) ||
121 windowState.equals(LiferayWindowState.POP_UP)) {
122
123 sendRedirect(actionRequest, actionResponse);
124 }
125 }
126 }
127
128 @Override
129 public ActionForward render(
130 ActionMapping actionMapping, ActionForm actionForm,
131 PortletConfig portletConfig, RenderRequest renderRequest,
132 RenderResponse renderResponse)
133 throws Exception {
134
135 return actionMapping.findForward(
136 getForward(renderRequest, "portlet.trash.view"));
137 }
138
139 protected void addRestoreData(
140 ActionRequest actionRequest,
141 List<ObjectValuePair<String, Long>> entryOVPs)
142 throws Exception {
143
144 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
145 WebKeys.THEME_DISPLAY);
146
147 if ((entryOVPs == null) || (entryOVPs.size() <= 0)) {
148 return;
149 }
150
151 List<String> restoreClassNames = new ArrayList<String>();
152 List<String> restoreEntryLinks = new ArrayList<String>();
153 List<String> restoreEntryMessages = new ArrayList<String>();
154 List<String> restoreLinks = new ArrayList<String>();
155 List<String> restoreMessages = new ArrayList<String>();
156
157 for (int i = 0; i < entryOVPs.size(); i++) {
158 ObjectValuePair<String, Long> entryOVP = entryOVPs.get(i);
159
160 TrashHandler trashHandler =
161 TrashHandlerRegistryUtil.getTrashHandler(entryOVP.getKey());
162
163 String restoreEntryLink = trashHandler.getRestoreContainedModelLink(
164 actionRequest, entryOVP.getValue());
165 String restoreLink = trashHandler.getRestoreContainerModelLink(
166 actionRequest, entryOVP.getValue());
167 String restoreMessage = trashHandler.getRestoreMessage(
168 actionRequest, entryOVP.getValue());
169
170 if (Validator.isNull(restoreLink) ||
171 Validator.isNull(restoreMessage)) {
172
173 continue;
174 }
175
176 restoreClassNames.add(trashHandler.getClassName());
177 restoreEntryLinks.add(restoreEntryLink);
178
179 TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
180 entryOVP.getValue());
181
182 String restoreEntryTitle = trashRenderer.getTitle(
183 themeDisplay.getLocale());
184
185 restoreEntryMessages.add(restoreEntryTitle);
186
187 restoreLinks.add(restoreLink);
188 restoreMessages.add(restoreMessage);
189 }
190
191 Map<String, List<String>> data = new HashMap<String, List<String>>();
192
193 data.put("restoreClassNames", restoreClassNames);
194 data.put("restoreEntryLinks", restoreEntryLinks);
195 data.put("restoreEntryMessages", restoreEntryMessages);
196 data.put("restoreLinks", restoreLinks);
197 data.put("restoreMessages", restoreMessages);
198
199 SessionMessages.add(
200 actionRequest,
201 PortalUtil.getPortletId(actionRequest) +
202 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
203
204 hideDefaultSuccessMessage(actionRequest);
205 }
206
207 protected void deleteEntries(ActionRequest actionRequest) throws Exception {
208 long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
209
210 if (trashEntryId > 0) {
211 TrashEntryServiceUtil.deleteEntry(trashEntryId);
212
213 return;
214 }
215
216 long[] deleteEntryIds = StringUtil.split(
217 ParamUtil.getString(actionRequest, "deleteThrashEntryIds"), 0L);
218
219 if (deleteEntryIds.length > 0) {
220 for (int i = 0; i < deleteEntryIds.length; i++) {
221 TrashEntryServiceUtil.deleteEntry(deleteEntryIds[i]);
222 }
223
224 return;
225 }
226
227 String className = ParamUtil.getString(actionRequest, "className");
228 long classPK = ParamUtil.getLong(actionRequest, "classPK");
229
230 if (Validator.isNotNull(className) && (classPK > 0)) {
231 TrashEntryServiceUtil.deleteEntry(className, classPK);
232 }
233 }
234
235 protected void emptyTrash(ActionRequest actionRequest) throws Exception {
236 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
237 WebKeys.THEME_DISPLAY);
238
239 long groupId = ParamUtil.getLong(
240 actionRequest, "groupId", themeDisplay.getScopeGroupId());
241
242 TrashEntryServiceUtil.deleteEntries(groupId);
243 }
244
245 protected List<ObjectValuePair<String, Long>> getEntryOVPs(
246 String className, long classPK) {
247
248 List<ObjectValuePair<String, Long>> entryOVPs =
249 new ArrayList<ObjectValuePair<String, Long>>();
250
251 ObjectValuePair<String, Long> entryOVP =
252 new ObjectValuePair<String, Long>(className, classPK);
253
254 entryOVPs.add(entryOVP);
255
256 return entryOVPs;
257 }
258
259 protected List<ObjectValuePair<String, Long>> moveEntry(
260 ActionRequest actionRequest)
261 throws Exception {
262
263 long containerModelId = ParamUtil.getLong(
264 actionRequest, "containerModelId");
265 String className = ParamUtil.getString(actionRequest, "className");
266 long classPK = ParamUtil.getLong(actionRequest, "classPK");
267
268 ServiceContext serviceContext = ServiceContextFactory.getInstance(
269 className, actionRequest);
270
271 TrashEntryServiceUtil.moveEntry(
272 className, classPK, containerModelId, serviceContext);
273
274 return getEntryOVPs(className, classPK);
275 }
276
277 protected List<ObjectValuePair<String, Long>> restoreEntries(
278 ActionRequest actionRequest)
279 throws Exception {
280
281 long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
282
283 if (trashEntryId > 0) {
284 TrashEntry entry = TrashEntryServiceUtil.restoreEntry(trashEntryId);
285
286 return getEntryOVPs(entry.getClassName(), entry.getClassPK());
287 }
288
289 long[] restoreEntryIds = StringUtil.split(
290 ParamUtil.getString(actionRequest, "restoreTrashEntryIds"), 0L);
291
292 List<ObjectValuePair<String, Long>> entryOVPs =
293 new ArrayList<ObjectValuePair<String, Long>>();
294
295 for (int i = 0; i < restoreEntryIds.length; i++) {
296 TrashEntry entry = TrashEntryServiceUtil.restoreEntry(trashEntryId);
297
298 entryOVPs.addAll(
299 getEntryOVPs(entry.getClassName(), entry.getClassPK()));
300 }
301
302 return entryOVPs;
303 }
304
305 protected List<ObjectValuePair<String, Long>> restoreOverride(
306 ActionRequest actionRequest)
307 throws Exception {
308
309 long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
310
311 long duplicateEntryId = ParamUtil.getLong(
312 actionRequest, "duplicateEntryId");
313
314 TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
315 trashEntryId, duplicateEntryId, null);
316
317 return getEntryOVPs(entry.getClassName(), entry.getClassPK());
318 }
319
320 protected List<ObjectValuePair<String, Long>> restoreRename(
321 ActionRequest actionRequest)
322 throws Exception {
323
324 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
325 WebKeys.THEME_DISPLAY);
326
327 long trashEntryId = ParamUtil.getLong(actionRequest, "trashEntryId");
328
329 String newName = ParamUtil.getString(actionRequest, "newName");
330
331 if (Validator.isNull(newName)) {
332 String oldName = ParamUtil.getString(actionRequest, "oldName");
333
334 newName = TrashUtil.getNewName(themeDisplay, null, 0, oldName);
335 }
336
337 TrashEntry entry = TrashEntryServiceUtil.restoreEntry(
338 trashEntryId, 0, newName);
339
340 return getEntryOVPs(entry.getClassName(), entry.getClassPK());
341 }
342
343 }