Source Code Cross Referenced for DirectoryTreeModel.java in  » Content-Management-System » contineo » org » contineo » web » document » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » contineo » org.contineo.web.document 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.contineo.web.document;
002:
003:        import com.icesoft.faces.component.tree.IceUserObject;
004:
005:        import org.apache.commons.logging.Log;
006:        import org.apache.commons.logging.LogFactory;
007:
008:        import org.contineo.core.FileBean;
009:        import org.contineo.core.document.Document;
010:        import org.contineo.core.document.dao.DocumentDAO;
011:        import org.contineo.core.document.dao.TermDAO;
012:        import org.contineo.core.doxter.Storer;
013:        import org.contineo.core.searchengine.crawler.Indexer;
014:        import org.contineo.core.searchengine.dao.SearchDocumentDAO;
015:        import org.contineo.core.security.ExtMenu;
016:        import org.contineo.core.security.Menu;
017:        import org.contineo.core.security.dao.MenuDAO;
018:        import org.contineo.core.security.dao.UserDocDAO;
019:
020:        import org.contineo.util.Context;
021:        import org.contineo.util.config.SettingsConfig;
022:
023:        import org.contineo.web.SessionManagement;
024:        import org.contineo.web.StyleBean;
025:        import org.contineo.web.i18n.Messages;
026:
027:        import java.util.ArrayList;
028:        import java.util.Collection;
029:        import java.util.Enumeration;
030:        import java.util.HashMap;
031:        import java.util.Iterator;
032:        import java.util.Map;
033:
034:        import javax.swing.tree.DefaultMutableTreeNode;
035:        import javax.swing.tree.DefaultTreeModel;
036:
037:        /**
038:         * A tree model specialized for Contineo's directories
039:         *
040:         * @author Marco Meschieri
041:         * @version $Id:$
042:         * @since ###release###
043:         */
044:        public class DirectoryTreeModel extends DefaultTreeModel {
045:            private static final long serialVersionUID = 1L;
046:            protected static Log log = LogFactory
047:                    .getLog(DirectoryTreeModel.class);
048:
049:            // Utility map of all directories (key is the menuId)
050:            private Map<Integer, Directory> directories = new HashMap<Integer, Directory>();
051:            private Directory selectedDir;
052:            private DefaultMutableTreeNode selectedNode;
053:
054:            public DirectoryTreeModel() {
055:                super (new DefaultMutableTreeNode());
056:                reload();
057:            }
058:
059:            public void reloadAll() {
060:                init();
061:                reload((DefaultMutableTreeNode) getRoot(), -1);
062:            }
063:
064:            public void reload(DefaultMutableTreeNode node, int depth) {
065:                Directory dir = ((Directory) node.getUserObject());
066:                String username = SessionManagement.getUsername();
067:                MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
068:                        MenuDAO.class);
069:
070:                try {
071:                    if (username != null) {
072:                        Collection<Menu> menus = menuDao.findByUserName(
073:                                username, dir.getMenuId());
074:
075:                        for (Menu menu : menus) {
076:                            if (menu.getMenuType() == Menu.MENUTYPE_DIRECTORY) {
077:                                addDir(username, node, menu, depth);
078:                                dir.setLeaf(false);
079:                            }
080:                        }
081:
082:                        dir.setCount(menus.size());
083:                    }
084:                } catch (Throwable t) {
085:                    log.error(t.getMessage(), t);
086:                }
087:            }
088:
089:            public void reload(DefaultMutableTreeNode node) {
090:                reload(node, 1);
091:            }
092:
093:            private void init() {
094:                directories.clear();
095:
096:                // build root node so that children can be attached
097:                MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
098:                        MenuDAO.class);
099:                Menu rootMenu = menuDao.findByPrimaryKey(Menu.MENUID_DOCUMENTS);
100:                Directory rootObject = new Directory(rootMenu);
101:                rootObject.setIcon(StyleBean.XP_BRANCH_CONTRACTED_ICON);
102:                rootObject.setDisplayText(null);
103:                rootObject.setContentTitle(null);
104:                rootObject.setPageContent(true);
105:
106:                String label = Messages.getMessage(rootMenu.getMenuText());
107:                rootObject.setDisplayText(label);
108:                rootObject.setContentTitle(label);
109:
110:                DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(
111:                        rootObject);
112:                rootObject.setWrapper(rootTreeNode);
113:                setRoot(rootTreeNode);
114:                rootTreeNode.setUserObject(rootObject);
115:                directories.put(rootMenu.getMenuId(), rootObject);
116:            }
117:
118:            public void reload() {
119:                init();
120:                reload((DefaultMutableTreeNode) getRoot());
121:            }
122:
123:            public Map<Integer, Directory> getDirectories() {
124:                return directories;
125:            }
126:
127:            public Directory getDirectory(int id) {
128:                return directories.get(id);
129:            }
130:
131:            /**
132:             * Adds a new directory in the specified place
133:             *
134:             * @param dir The new directory's menu
135:             * @param parent The parent directory
136:             */
137:            public void addNewDir(Menu dir, Directory parent) {
138:                Directory parentDir = parent;
139:
140:                if (parentDir == null) {
141:                    parentDir = selectedDir;
142:                }
143:
144:                parentDir.setCount(parentDir.getCount() + 1);
145:                parentDir.setLeaf(false);
146:
147:                DefaultMutableTreeNode parentNode = findDirectoryNode(parentDir
148:                        .getMenuId(), (DefaultMutableTreeNode) getRoot());
149:                addDir(SessionManagement.getUsername(), parentNode, dir);
150:            }
151:
152:            private void addDir(String username,
153:                    DefaultMutableTreeNode parentNode, Menu dir) {
154:                addDir(SessionManagement.getUsername(), parentNode, dir, -1);
155:            }
156:
157:            /**
158:             * Changes the currently selected directory and updates the documents list.
159:             *
160:             * @param directoryId
161:             */
162:            public void selectDirectory(Directory directory) {
163:                selectedDir = directory;
164:                selectedDir.setSelected(true);
165:                setTreeSelectedState((DefaultMutableTreeNode) getRoot());
166:                expandNodePath(selectedNode);
167:            }
168:
169:            /**
170:             * Finds the directory node with the specified identifier contained in a
171:             * sublevel of the the passed tree node
172:             *
173:             * @param direcoryId The directory identifier
174:             * @param parent The node in which the directory must be searched
175:             * @return The found tree node, null if not found
176:             */
177:            private DefaultMutableTreeNode findDirectoryNode(int direcoryId,
178:                    DefaultMutableTreeNode parent) {
179:                Directory dir = (Directory) parent.getUserObject();
180:
181:                if (dir.getMenu().getMenuId() == direcoryId) {
182:                    return parent;
183:                } else {
184:                    Enumeration<DefaultMutableTreeNode> enumer = (Enumeration<DefaultMutableTreeNode>) parent
185:                            .children();
186:
187:                    while (enumer.hasMoreElements()) {
188:                        DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) enumer
189:                                .nextElement();
190:                        DefaultMutableTreeNode node = findDirectoryNode(
191:                                direcoryId, childNode);
192:
193:                        if (node != null) {
194:                            return node;
195:                        }
196:                    }
197:                }
198:
199:                return null;
200:            }
201:
202:            /**
203:             * Adds a directory and all it's childs
204:             *
205:             * @param username
206:             * @param parent
207:             * @param dir
208:             * @param depth the maximum depth
209:             * @return
210:             */
211:            private DefaultMutableTreeNode addDir(String username,
212:                    DefaultMutableTreeNode parent, Menu dir, int depth) {
213:                MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
214:                        MenuDAO.class);
215:
216:                Directory cachedDir = directories.get(dir.getMenuId());
217:
218:                if ((cachedDir != null) && cachedDir.isLoaded()) {
219:                    DefaultMutableTreeNode node = findDirectoryNode(dir
220:                            .getMenuId(), parent);
221:
222:                    if (node != null) {
223:                        return node;
224:                    }
225:                }
226:
227:                // Component menu item
228:                Directory branchObject = new Directory(dir);
229:                branchObject.setDisplayText(dir.getMenuText());
230:                branchObject.setContentTitle(dir.getMenuText());
231:                branchObject.setIcon(StyleBean.XP_BRANCH_CONTRACTED_ICON);
232:
233:                DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode(
234:                        branchObject);
235:                branchObject.setWrapper(branchNode);
236:                branchObject.setPageContent(false);
237:                branchObject.setLeaf(true);
238:                branchObject.setExpanded(false);
239:                branchNode.setUserObject(branchObject);
240:
241:                // Iterate over subdirs
242:                Collection<Menu> children = menuDao.findByUserName(username,
243:                        dir.getMenuId());
244:
245:                for (Menu child : children) {
246:                    if (child.getMenuType() == Menu.MENUTYPE_DIRECTORY) {
247:                        branchObject.setLeaf(false);
248:
249:                        if (depth > 0) {
250:                            addDir(username, branchNode, child, depth - 1);
251:                        } else if (depth == -1) {
252:                            addDir(username, branchNode, child, depth);
253:                        }
254:                    }
255:                }
256:
257:                if ((depth > 0) || (depth == -1)) {
258:                    branchObject.setLoaded(true);
259:                    directories.put(dir.getMenuId(), branchObject);
260:                    parent.add(branchNode);
261:                    branchObject.setCount(children.size());
262:                    log.debug("added dir " + branchObject.getDisplayText());
263:                }
264:
265:                return branchNode;
266:            }
267:
268:            public void selectDirectory(int id) {
269:                selectedDir = directories.get(id);
270:                selectDirectory(selectedDir);
271:            }
272:
273:            /**
274:             * Set the selection state for all directories in the tree
275:             */
276:            protected void setTreeSelectedState(DefaultMutableTreeNode node) {
277:                if ((node.getUserObject() != null)
278:                        && node.getUserObject() instanceof  Directory) {
279:                    Directory dir = (Directory) node.getUserObject();
280:
281:                    if ((selectedDir != null) && (dir.getMenu() != null)
282:                            && dir.getMenu().equals(selectedDir.getMenu())) {
283:                        dir.setSelected(true);
284:                        dir.setIcon(dir.getBranchExpandedIcon());
285:                        selectedNode = node;
286:                    } else {
287:                        dir.setSelected(false);
288:                        dir.setIcon(dir.getBranchContractedIcon());
289:                    }
290:                }
291:
292:                Enumeration<DefaultMutableTreeNode> enumer = (Enumeration<DefaultMutableTreeNode>) node
293:                        .children();
294:
295:                while (enumer.hasMoreElements()) {
296:                    DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) enumer
297:                            .nextElement();
298:                    setTreeSelectedState(childNode);
299:                }
300:            }
301:
302:            public Directory getSelectedDir() {
303:                return selectedDir;
304:            }
305:
306:            public DefaultMutableTreeNode getSelectedNode() {
307:                return selectedNode;
308:            }
309:
310:            /**
311:             * Deletes a directory form the tree and from the database
312:             *
313:             * @param directory The element to be deleted
314:             */
315:            public void deleteDirectory(Directory directory) {
316:                DefaultMutableTreeNode dirNode = findDirectoryNode(directory
317:                        .getMenuId(), (DefaultMutableTreeNode) getRoot());
318:
319:                if (dirNode == null) {
320:                    return;
321:                }
322:
323:                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) dirNode
324:                        .getParent();
325:
326:                if (SessionManagement.isValid()) {
327:                    int id = directory.getMenuId();
328:                    MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
329:                            MenuDAO.class);
330:                    String userName = SessionManagement.getUsername();
331:                    int parentId = -1;
332:
333:                    try {
334:                        boolean sqlop = true;
335:
336:                        if (mdao.isWriteEnable(id, userName)) {
337:                            Menu menu = mdao.findByPrimaryKey(id);
338:                            int type = menu.getMenuType();
339:
340:                            if (type == Menu.MENUTYPE_FILE) {
341:                                deleteFile(menu, id, userName);
342:                            }
343:
344:                            // remove sub-elements
345:                            Collection children = mdao.findByParentId(id);
346:                            Iterator childIter = children.iterator();
347:
348:                            while (childIter.hasNext()) {
349:                                Menu m = (Menu) childIter.next();
350:                                deleteFile(m, m.getMenuId(), userName);
351:                            }
352:
353:                            boolean deleted = mdao.delete(id);
354:
355:                            if (!deleted) {
356:                                sqlop = false;
357:                            }
358:
359:                            parentId = menu.getMenuParent();
360:
361:                            Collection<ExtMenu> coll2 = new ArrayList<ExtMenu>();
362:                            Collection coll = mdao.findByUserName(userName,
363:                                    parentId);
364:                            Iterator iter = coll.iterator();
365:                            SettingsConfig settings = (SettingsConfig) Context
366:                                    .getInstance()
367:                                    .getBean(SettingsConfig.class);
368:                            String docpath = settings.getValue("docdir");
369:                            MenuDAO menuDao = (MenuDAO) Context.getInstance()
370:                                    .getBean(MenuDAO.class);
371:
372:                            while (iter.hasNext()) {
373:                                Menu m = (Menu) iter.next();
374:                                ExtMenu xmenu = new ExtMenu(m);
375:
376:                                // calculate size of menu
377:                                int size = 0;
378:
379:                                if (m.getMenuType() == Menu.MENUTYPE_FILE) {
380:                                    long sz = FileBean.getSize(docpath + "/"
381:                                            + m.getMenuPath() + "/"
382:                                            + m.getMenuId() + "/"
383:                                            + m.getMenuRef());
384:                                    sz = sz / 1024;
385:                                    size = (int) sz;
386:                                } else {
387:                                    size = menuDao.findByUserName(userName,
388:                                            m.getMenuId()).size();
389:                                }
390:
391:                                xmenu.setSize(size);
392:
393:                                // check if menu is writable
394:                                boolean writable = false;
395:
396:                                if (menuDao.isWriteEnable(m.getMenuId(),
397:                                        userName)) {
398:                                    writable = true;
399:                                } else {
400:                                    writable = false;
401:                                }
402:
403:                                xmenu.setWritable(writable);
404:
405:                                // only done on documents
406:                                // set the checkout/checkin status of the document
407:                                // set the checkout user, if the document is checked out
408:                                if (m.getMenuType() == Menu.MENUTYPE_FILE) {
409:                                    DocumentDAO ddao = (DocumentDAO) Context
410:                                            .getInstance().getBean(
411:                                                    DocumentDAO.class);
412:                                    Document doc = ddao.findByMenuId(m
413:                                            .getMenuId());
414:
415:                                    if (doc != null) {
416:                                        xmenu.setDocStatus(doc.getDocStatus());
417:                                        xmenu.setCheckoutUser(doc
418:                                                .getCheckoutUser());
419:                                    }
420:                                }
421:
422:                                coll2.add(xmenu);
423:                            }
424:
425:                            if (!sqlop) {
426:                                Messages
427:                                        .addLocalizedError("errors.action.deleteitem");
428:                            } else {
429:                                Messages
430:                                        .addLocalizedInfo("msg.action.deleteitem");
431:                            }
432:
433:                            Directory parentDirectory = (Directory) parentNode
434:                                    .getUserObject();
435:                            parentDirectory
436:                                    .setCount(parentDirectory.getCount() - 1);
437:                            parentNode.remove(dirNode);
438:                            selectDirectory(parentDirectory);
439:                        } else {
440:                            Messages
441:                                    .addLocalizedError("document.write.nopermission");
442:                        }
443:                    } catch (Exception e) {
444:                        log.error(e.getMessage(), e);
445:                        Messages.addError(e.getMessage());
446:                    }
447:                }
448:            }
449:
450:            private boolean deleteFile(Menu menu, int id, String username) {
451:                boolean sqlop = true;
452:
453:                try {
454:                    SearchDocumentDAO searchDocDao = (SearchDocumentDAO) Context
455:                            .getInstance().getBean(SearchDocumentDAO.class);
456:                    searchDocDao.deleteByMenuId(id);
457:
458:                    UserDocDAO uddao = (UserDocDAO) Context.getInstance()
459:                            .getBean(UserDocDAO.class);
460:                    uddao.delete(username, id);
461:
462:                    DocumentDAO ddao = (DocumentDAO) Context.getInstance()
463:                            .getBean(DocumentDAO.class);
464:                    Document doc = ddao.findByMenuId(id);
465:
466:                    if (doc != null) {
467:                        Indexer indexer = (Indexer) Context.getInstance()
468:                                .getBean(Indexer.class);
469:                        indexer.deleteFile(String.valueOf(id), doc
470:                                .getLanguage());
471:                    }
472:
473:                    TermDAO termDao = (TermDAO) Context.getInstance().getBean(
474:                            TermDAO.class);
475:                    boolean deleted2 = termDao.delete(id);
476:
477:                    boolean deleted1 = ddao.deleteByMenuId(id);
478:
479:                    if (!deleted1 || !deleted2) {
480:                        sqlop = false;
481:                    }
482:
483:                    MenuDAO mdao = (MenuDAO) Context.getInstance().getBean(
484:                            MenuDAO.class);
485:                    boolean deleted = mdao.delete(id);
486:
487:                    if (!deleted) {
488:                        sqlop = false;
489:                    }
490:
491:                    // String path = conf.getValue("docdir");
492:                    String menupath = menu.getMenuPath() + "/"
493:                            + String.valueOf(id);
494:
495:                    // FileBean.deleteDir(path);
496:                    Storer storer = (Storer) Context.getInstance().getBean(
497:                            Storer.class);
498:                    storer.delete(menupath);
499:                } catch (Exception e) {
500:                    log.error(e.getMessage(), e);
501:                    sqlop = false;
502:                }
503:
504:                return sqlop;
505:            }
506:
507:            /**
508:             * Expand all nodes from the passed one back to the root following the
509:             * parent/child relation.
510:             *
511:             * @param node the leaf node to expand
512:             */
513:            private void expandNodePath(DefaultMutableTreeNode node) {
514:                IceUserObject obj = (IceUserObject) node.getUserObject();
515:                obj.setExpanded(true);
516:
517:                if (!node.equals(getRoot())) {
518:                    expandNodePath((DefaultMutableTreeNode) node.getParent());
519:                }
520:            }
521:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.