01: package org.contineo.core.document;
02:
03: import java.util.Collection;
04: import java.util.Iterator;
05:
06: import org.apache.commons.logging.Log;
07: import org.apache.commons.logging.LogFactory;
08: import org.contineo.core.document.dao.DocumentDAO;
09: import org.contineo.core.document.dao.TermDAO;
10: import org.contineo.core.doxter.Storer;
11: import org.contineo.core.searchengine.crawler.Indexer;
12: import org.contineo.core.searchengine.dao.SearchDocumentDAO;
13: import org.contineo.core.security.Menu;
14: import org.contineo.core.security.dao.MenuDAO;
15: import org.contineo.core.security.dao.UserDocDAO;
16: import org.contineo.util.Context;
17:
18: /**
19: * utility class for deleting menus
20: *
21: * @author Sebastian Stein
22: */
23: public class DeleteItemUtil {
24: protected static Log log = LogFactory.getLog(DeleteItemUtil.class);
25:
26: /**
27: * deletes the given menu and all sub-menus; does not perform an access
28: * check
29: *
30: * @param menuId menu to be deleted; can be a folder, menu or document
31: * @param userName the user requesting the delete action
32: */
33: public static boolean deleteMenu(int menuId, String userName) {
34: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
35: MenuDAO.class);
36: Menu menu = mdao.findByPrimaryKey(menuId);
37: if (menu == null)
38: return true;
39: int type = menu.getMenuType();
40: try {
41: if (type == Menu.MENUTYPE_FILE) {
42: deleteFile(menu, menuId, userName);
43: } else if (type == Menu.MENUTYPE_DIRECTORY) {
44: Collection children = mdao.findByParentId(menuId);
45:
46: Iterator childIter = children.iterator();
47: while (childIter.hasNext()) {
48: Menu m = (Menu) childIter.next();
49: deleteFile(m, m.getMenuId(), userName);
50: }
51: }
52: return mdao.delete(menuId);
53: } catch (Exception ex) {
54: ex.printStackTrace();
55: log.error(ex.getMessage(), ex);
56: return false;
57: }
58: }
59:
60: /**
61: * deletes the given menu; does not perform an access check; only used
62: * internally
63: */
64: private static void deleteFile(Menu menu, int id, String username)
65: throws Exception {
66: SearchDocumentDAO searchDocDao = (SearchDocumentDAO) Context
67: .getInstance().getBean(SearchDocumentDAO.class);
68: searchDocDao.deleteByMenuId(id);
69: UserDocDAO uddao = (UserDocDAO) Context.getInstance().getBean(
70: UserDocDAO.class);
71: uddao.delete(username, id);
72: DocumentDAO ddao = (DocumentDAO) Context.getInstance().getBean(
73: DocumentDAO.class);
74: Document doc = ddao.findByMenuId(id);
75: if (doc != null) {
76: Indexer indexer = (Indexer) Context.getInstance().getBean(
77: Indexer.class);
78: indexer.deleteFile(String.valueOf(id), doc.getLanguage());
79: }
80: // int docId = doc.getDocId();
81: boolean deleted1 = ddao.deleteByMenuId(id);
82: TermDAO termDao = (TermDAO) Context.getInstance().getBean(
83: TermDAO.class);
84: boolean deleted2 = termDao.delete(id);
85: MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
86: MenuDAO.class);
87: boolean deleted = mdao.delete(id);
88: // String path = conf.getValue("docdir");
89: String menupath = menu.getMenuPath() + "/" + String.valueOf(id);
90: // FileBean.deleteDir(path);
91: Storer storer = (Storer) Context.getInstance().getBean(
92: Storer.class);
93: if (!storer.delete(menupath) || !deleted || !deleted1
94: || !deleted2)
95: throw new Exception();
96: }
97: }
|