001: package org.contineo.core.security;
002:
003: import java.security.AccessControlException;
004: import java.sql.SQLException;
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Iterator;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.contineo.core.FileBean;
012: import org.contineo.core.document.Document;
013: import org.contineo.core.document.dao.DocumentDAO;
014: import org.contineo.core.document.dao.TermDAO;
015: import org.contineo.core.doxter.Storer;
016: import org.contineo.core.searchengine.crawler.Indexer;
017: import org.contineo.core.searchengine.dao.SearchDocumentDAO;
018: import org.contineo.core.security.dao.MenuDAO;
019: import org.contineo.core.security.dao.UserDocDAO;
020: import org.contineo.util.config.SettingsConfig;
021:
022: /**
023: * Basic menu manager implementation
024: *
025: * @author Marco Meschieri
026: * @version $Id: MenuManagerImpl.java,v 1.1 2007/07/10 16:36:26 marco Exp $
027: * @since 3.0
028: */
029: public class MenuManagerImpl implements MenuManager {
030: protected static Log log = LogFactory.getLog(MenuManagerImpl.class);
031:
032: private MenuDAO menuDao;
033:
034: private SettingsConfig settings;
035:
036: private DocumentDAO documentDao;
037:
038: private TermDAO termDao;
039:
040: private Indexer indexer;
041:
042: private Storer storer;
043:
044: private SearchDocumentDAO searchDocDao;
045:
046: private UserDocDAO userDocDao;
047:
048: /**
049: * @see org.contineo.core.security.MenuManager#deleteMenu(org.contineo.core.security.Menu,
050: * java.lang.String)
051: */
052: public void deleteMenu(Menu menu, String userName) throws Exception {
053: log.debug("User " + userName
054: + " required the deletion of item " + menu.getMenuId());
055:
056: try {
057: boolean sqlop = true;
058: int id = menu.getMenuId();
059:
060: if (menuDao.isWriteEnable(id, userName)) {
061: int type = menu.getMenuType();
062:
063: if (type == Menu.MENUTYPE_FILE) {
064: deleteFile(menu, id, userName);
065: }
066:
067: // remove sub-elements
068: Collection children = menuDao.findByParentId(id);
069: Iterator childIter = children.iterator();
070: while (childIter.hasNext()) {
071: Menu m = (Menu) childIter.next();
072: deleteFile(m, m.getMenuId(), userName);
073: }
074:
075: boolean deleted = menuDao.delete(id);
076:
077: if (!deleted) {
078: sqlop = false;
079: }
080:
081: int parentId = menu.getMenuParent();
082:
083: Collection<ExtMenu> coll2 = new ArrayList<ExtMenu>();
084: Collection coll = menuDao.findByUserName(userName,
085: parentId);
086: Iterator iter = coll.iterator();
087: String docpath = settings.getValue("docdir");
088:
089: while (iter.hasNext()) {
090: Menu m = (Menu) iter.next();
091: ExtMenu xmenu = new ExtMenu(m);
092:
093: // calculate size of menu
094: int size = 0;
095:
096: if (m.getMenuType() == Menu.MENUTYPE_FILE) {
097: long sz = FileBean.getSize(docpath + "/"
098: + m.getMenuPath() + "/" + m.getMenuId()
099: + "/" + m.getMenuRef());
100: sz = sz / 1024;
101: size = (int) sz;
102: } else {
103: size = menuDao.findByUserName(userName,
104: m.getMenuId()).size();
105: }
106:
107: xmenu.setSize(size);
108:
109: // check if menu is writable
110: boolean writable = false;
111:
112: if (menuDao.isWriteEnable(m.getMenuId(), userName)) {
113: writable = true;
114: } else {
115: writable = false;
116: }
117:
118: xmenu.setWritable(writable);
119:
120: // only done on documents
121: // set the checkout/checkin status of the document
122: // set the checkout user, if the document is checked out
123: if (m.getMenuType() == Menu.MENUTYPE_FILE) {
124: Document doc = documentDao.findByMenuId(m
125: .getMenuId());
126:
127: if (doc != null) {
128: xmenu.setDocStatus(doc.getDocStatus());
129: xmenu
130: .setCheckoutUser(doc
131: .getCheckoutUser());
132: }
133: }
134:
135: coll2.add(xmenu);
136: }
137:
138: if (!sqlop) {
139: String message = "An error has occurred while deleting the item";
140: log.error(message);
141: throw new SQLException(message);
142: } else {
143: log.info("The item has been deleted");
144: }
145: } else {
146: String message = "User not allowed to delete item";
147: log.error(message);
148: throw new AccessControlException(message);
149: }
150: } catch (Exception e) {
151: log.error(e.getMessage(), e);
152: throw e;
153: }
154: }
155:
156: private boolean deleteFile(Menu menu, int id, String username) {
157: boolean sqlop = true;
158:
159: try {
160: searchDocDao.deleteByMenuId(id);
161:
162: userDocDao.delete(username, id);
163:
164: Document doc = documentDao.findByMenuId(id);
165:
166: if (doc != null) {
167: indexer.deleteFile(String.valueOf(id), doc
168: .getLanguage());
169: }
170:
171: boolean deleted2 = termDao.delete(id);
172:
173: boolean deleted1 = documentDao.deleteByMenuId(id);
174:
175: if (!deleted1 || !deleted2) {
176: sqlop = false;
177: }
178:
179: boolean deleted = menuDao.delete(id);
180:
181: if (!deleted) {
182: sqlop = false;
183: }
184:
185: // String path = conf.getValue("docdir");
186: String menupath = menu.getMenuPath() + "/"
187: + String.valueOf(id);
188:
189: // FileBean.deleteDir(path);
190: storer.delete(menupath);
191: } catch (Exception e) {
192: log.error(e.getMessage(), e);
193: sqlop = false;
194: }
195:
196: return sqlop;
197: }
198:
199: public void setDocumentDao(DocumentDAO documentDao) {
200: this .documentDao = documentDao;
201: }
202:
203: public void setIndexer(Indexer indexer) {
204: this .indexer = indexer;
205: }
206:
207: public void setMenuDao(MenuDAO menuDao) {
208: this .menuDao = menuDao;
209: }
210:
211: public void setSearchDocDao(SearchDocumentDAO searchDocDao) {
212: this .searchDocDao = searchDocDao;
213: }
214:
215: public void setSettings(SettingsConfig settings) {
216: this .settings = settings;
217: }
218:
219: public void setStorer(Storer storer) {
220: this .storer = storer;
221: }
222:
223: public void setTermDao(TermDAO termDao) {
224: this .termDao = termDao;
225: }
226:
227: public void setUserDocDao(UserDocDAO userDocDao) {
228: this.userDocDao = userDocDao;
229: }
230:
231: }
|