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