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