001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * Created on Oct 21, 2003
020: */
021: package org.apache.roller.ui.authoring.struts.actions;
022:
023: import java.text.MessageFormat;
024: import java.util.Comparator;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.LinkedList;
028: import java.util.Set;
029: import java.util.TreeSet;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.struts.action.ActionError;
037: import org.apache.struts.action.ActionErrors;
038: import org.apache.struts.action.ActionForm;
039: import org.apache.struts.action.ActionForward;
040: import org.apache.struts.action.ActionMapping;
041: import org.apache.struts.action.ActionMessage;
042: import org.apache.struts.action.ActionMessages;
043: import org.apache.struts.actions.DispatchAction;
044: import org.apache.roller.RollerException;
045: import org.apache.roller.business.BookmarkManager;
046: import org.apache.roller.business.Roller;
047: import org.apache.roller.business.RollerFactory;
048: import org.apache.roller.pojos.BookmarkData;
049: import org.apache.roller.pojos.FolderData;
050: import org.apache.roller.pojos.WebsiteData;
051: import org.apache.roller.ui.core.BasePageModel;
052: import org.apache.roller.ui.core.RollerRequest;
053: import org.apache.roller.ui.core.RollerSession;
054: import org.apache.roller.ui.authoring.struts.formbeans.BookmarksForm;
055: import org.apache.roller.ui.core.RequestConstants;
056: import org.apache.roller.util.cache.CacheManager;
057:
058: /**
059: * Actions that are initiated from the BookmarksForm.
060: *
061: * @struts.action name="bookmarksForm" path="/roller-ui/authoring/bookmarks" parameter="method"
062: * @struts.action-forward name="BookmarksForm" path=".BookmarksForm"
063: *
064: * @author Dave Johnson
065: */
066: public class BookmarksAction extends DispatchAction {
067: private static Log mLogger = LogFactory.getFactory().getInstance(
068: BookmarksAction.class);
069:
070: /**
071: * Present the BookmarksForm loaded with folder specified by request.
072: * @param mapping Action mapping.
073: * @param actionForm Form bean.
074: * @param request Request.
075: * @param response Response.
076: * @return Forward to BookmarksForm or access-denied.
077: * @throws RollerException
078: */
079: public ActionForward selectFolder(ActionMapping mapping,
080: ActionForm actionForm, HttpServletRequest request,
081: HttpServletResponse response) throws RollerException {
082: BookmarksPageModel pageModel = new BookmarksPageModel(request,
083: response, mapping, (BookmarksForm) actionForm);
084: if (RollerSession.getRollerSession(request)
085: .isUserAuthorizedToAuthor(
086: pageModel.getFolder().getWebsite())) {
087: request.setAttribute("model", pageModel);
088: return mapping.findForward("BookmarksForm");
089: } else {
090: return mapping.findForward("access-denied");
091: }
092: }
093:
094: /**
095: * Delete folders and bookmarks indicated by BookmarksForm bean.
096: * @param mapping Action mapping.
097: * @param actionForm Form bean.
098: * @param request Request.
099: * @param response Response.
100: * @return Forward to BookmarksForm or access-denied.
101: * @throws RollerException
102: */
103: public ActionForward deleteSelected(ActionMapping mapping,
104: ActionForm actionForm, HttpServletRequest request,
105: HttpServletResponse response) throws RollerException {
106: Roller roller = RollerFactory.getRoller();
107: BookmarksPageModel pageModel = new BookmarksPageModel(request,
108: response, mapping, (BookmarksForm) actionForm);
109: WebsiteData website = pageModel.getFolder().getWebsite();
110: if (RollerSession.getRollerSession(request)
111: .isUserAuthorizedToAuthor(website)) {
112: BookmarkManager bmgr = roller.getBookmarkManager();
113: BookmarksForm form = (BookmarksForm) actionForm;
114:
115: mLogger.debug("Deleting folders and bookmarks.");
116:
117: String folders[] = form.getSelectedFolders();
118: if (null != folders) {
119: for (int i = 0; i < folders.length; i++) {
120: FolderData fd = bmgr.getFolder(folders[i]);
121: bmgr.removeFolder(fd); // removes child folders and bookmarks too
122: }
123: }
124:
125: BookmarkData bookmark = null;
126: String bookmarks[] = form.getSelectedBookmarks();
127: if (null != bookmarks) {
128: for (int j = 0; j < bookmarks.length; j++) {
129: bookmark = bmgr.getBookmark(bookmarks[j]);
130: bmgr.removeBookmark(bookmark);
131: }
132: }
133: RollerFactory.getRoller().flush();
134:
135: CacheManager.invalidate(website);
136:
137: // recreate model now that folder is deleted
138: pageModel = new BookmarksPageModel(request, response,
139: mapping, (BookmarksForm) actionForm);
140: request.setAttribute("model", pageModel);
141: return mapping.findForward("BookmarksForm");
142: } else {
143: return mapping.findForward("access-denied");
144: }
145: }
146:
147: /**
148: * Move folders and bookmarks indicated by BookmarksForm bean.
149: * @param mapping Action mapping.
150: * @param actionForm Form bean.
151: * @param request Request.
152: * @param response Response.
153: * @return Forward to BookmarksForm or access-denied.
154: * @throws RollerException
155: */
156: public ActionForward moveSelected(ActionMapping mapping,
157: ActionForm actionForm, HttpServletRequest request,
158: HttpServletResponse response) throws RollerException {
159: ActionMessages messages = new ActionMessages();
160: ActionForward forward = mapping.findForward("BookmarksForm");
161: Roller roller = RollerFactory.getRoller();
162: BookmarksPageModel pageModel = new BookmarksPageModel(request,
163: response, mapping, (BookmarksForm) actionForm);
164: request.setAttribute("model", pageModel);
165: WebsiteData website = pageModel.getFolder().getWebsite();
166:
167: if (RollerSession.getRollerSession(request)
168: .isUserAuthorizedToAuthor(website)) {
169: try {
170: BookmarkManager bmgr = roller.getBookmarkManager();
171: BookmarksForm form = (BookmarksForm) actionForm;
172:
173: mLogger
174: .debug("Moving folders and bookmarks to folder, id="
175: + form.getMoveToFolderId());
176:
177: // Move subfolders to new folder.
178: String folders[] = form.getSelectedFolders();
179: FolderData parent = bmgr.getFolder(form
180: .getMoveToFolderId());
181: if (null != folders) {
182: for (int i = 0; i < folders.length; i++) {
183: FolderData fd = bmgr.getFolder(folders[i]);
184:
185: // Don't move folder into itself.
186: if (!fd.getId().equals(parent.getId())
187: && !parent.descendentOf(fd)) {
188: fd.setParent(parent);
189: bmgr.saveFolder(fd);
190: } else {
191: messages.add(null, new ActionMessage(
192: "bookmarksForm.warn.notMoving", fd
193: .getName()));
194: }
195: }
196: }
197:
198: // Move bookmarks.
199: String bookmarks[] = form.getSelectedBookmarks();
200: if (null != bookmarks) {
201: for (int j = 0; j < bookmarks.length; j++) {
202: // maybe we should be using folder.addBookmark()?
203: BookmarkData bd = bmgr
204: .getBookmark(bookmarks[j]);
205: bd.setFolder(parent);
206: bmgr.saveBookmark(bd);
207: }
208: }
209: RollerFactory.getRoller().flush();
210:
211: CacheManager.invalidate(website);
212:
213: saveMessages(request, messages);
214:
215: // recreate model now that folder is altered
216: pageModel = new BookmarksPageModel(request, response,
217: mapping, (BookmarksForm) actionForm);
218: request.setAttribute("model", pageModel);
219: } catch (RollerException e) {
220: ActionErrors errors = new ActionErrors();
221: errors.add(null, new ActionError(
222: "bookmarksForm.error.move"));
223: saveErrors(request, errors);
224: }
225: } else {
226: forward = mapping.findForward("access-denied");
227: }
228: return forward;
229: }
230:
231: private static final class FolderPathComparator implements
232: Comparator {
233: public int compare(Object o1, Object o2) {
234: FolderData f1 = (FolderData) o1;
235: FolderData f2 = (FolderData) o2;
236: int res = 0;
237: try {
238: res = f1.getPath().compareTo(f2.getPath());
239: } catch (RollerException e) {
240: mLogger.error("ERROR: sorting folders");
241: }
242: return res;
243: }
244: }
245:
246: public class BookmarksPageModel extends BasePageModel {
247: private List folderPath = null;
248: private TreeSet allFolders = null;
249: private FolderData folder = null;
250:
251: public BookmarksPageModel(HttpServletRequest request,
252: HttpServletResponse response, ActionMapping mapping,
253: BookmarksForm form) throws RollerException {
254: super ("", request, response, mapping);
255:
256: RollerRequest rreq = RollerRequest
257: .getRollerRequest(request);
258: RollerSession rollerSession = RollerSession
259: .getRollerSession(request);
260: BookmarkManager bmgr = RollerFactory.getRoller()
261: .getBookmarkManager();
262:
263: allFolders = new TreeSet(new FolderPathComparator());
264:
265: // Find folderid wherever it may be
266: String folderId = (String) request
267: .getAttribute(RequestConstants.FOLDER_ID);
268: if (null == folderId) {
269: folderId = request
270: .getParameter(RequestConstants.FOLDER_ID);
271: }
272: if (null == folderId) {
273: folderId = form.getFolderId();
274: }
275:
276: if (null == folderId || folderId.equals("null")) {
277: website = rreq.getWebsite();
278: folder = bmgr.getRootFolder(website);
279: folderId = folder.getId();
280: } else {
281: folder = bmgr.getFolder(folderId);
282: website = folder.getWebsite();
283: }
284: form.setFolderId(folder.getId());
285:
286: if (null != folder.getParent()) {
287: folderPath = new LinkedList();
288: folderPath.add(0, folder);
289: FolderData parent = folder.getParent();
290: while (parent != null) {
291: folderPath.add(0, parent);
292: parent = parent.getParent();
293: }
294: request.setAttribute(RequestConstants.PARENT_ID, folder
295: .getParent().getId());
296: }
297:
298: // Build list of all folders, except for current one, sorted by path.
299: Iterator iter = bmgr.getAllFolders(website).iterator();
300: while (iter.hasNext()) {
301: FolderData fd = (FolderData) iter.next();
302: if (!fd.getId().equals(folderId)) {
303: allFolders.add(fd);
304: }
305: }
306: if (allFolders.size() > 0) {
307: request.setAttribute("allFolders", allFolders); // for Struts tags
308: } else {
309: allFolders = null;
310: }
311: request.setAttribute("folder", folder); // for Struts tags
312: }
313:
314: public String getTitle() {
315: if (folderPath == null || folderPath.isEmpty()) {
316: return bundle.getString("bookmarksForm.rootTitle");
317: } else {
318: return MessageFormat.format(bundle
319: .getString("bookmarksForm.folderTitle"),
320: new Object[] { folder.getName() });
321: }
322: }
323:
324: public List getFolderPath() {
325: return folderPath;
326: }
327:
328: public Set getAllFolders() {
329: return allFolders;
330: }
331:
332: public FolderData getFolder() {
333: return folder;
334: }
335:
336: }
337: }
|