001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.impl.jcr.command;
023:
024: import org.jboss.portal.cms.impl.FileImpl;
025: import org.jboss.portal.cms.impl.FolderImpl;
026: import org.jboss.portal.cms.impl.jcr.JCRCommand;
027: import org.jboss.portal.cms.impl.jcr.JCRCommandContext;
028: import org.jboss.portal.cms.model.File;
029: import org.jboss.portal.cms.model.Folder;
030:
031: import javax.jcr.Node;
032: import javax.jcr.NodeIterator;
033: import javax.jcr.Session;
034: import javax.jcr.nodetype.NodeType;
035: import java.util.ArrayList;
036: import java.util.List;
037:
038: /**
039: * Retrieves the folder for the given path and sets the child folders and child files in the Folder obj.
040: *
041: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
042: * @author <a href="mailto:sohil.shah@jboss.com">Sohil Shah</a>
043: */
044: public class FolderGetListCommand extends JCRCommand {
045: /** The serialVersionUID */
046: private static final long serialVersionUID = -754630384075659266L;
047: String sFolderPath;
048:
049: public FolderGetListCommand(String sFolderPath) {
050: this .sFolderPath = sFolderPath;
051: }
052:
053: public Object execute() {
054: try {
055: Folder folder = new FolderImpl();
056:
057: Node folderNode = (Node) context.getSession().getItem(
058: sFolderPath);
059: folder.setName(folderNode.getName());
060: if (!"/".equals(sFolderPath)) {
061: folder.setDescription(folderNode.getProperty(
062: "portalcms:description").getString());
063: folder.setLastModified(folderNode.getProperty(
064: "portalcms:lastmodified").getDate().getTime());
065: }
066: folder.setBasePath(sFolderPath);
067:
068: // get child folders
069: List folders = new ArrayList();
070: List files = new ArrayList();
071: NodeIterator ni = folderNode.getNodes();
072: while (ni.hasNext()) {
073: try {
074: Node node = ni.nextNode();
075: NodeType nt = node.getPrimaryNodeType();
076: String ntName = nt.getName();
077: if (ntName.equals("portalcms:folder")) {
078: JCRCommand folderList = (JCRCommand) context
079: .getCommandFactory()
080: .createFolderGetCommand(node.getPath());
081: Folder childFolder = (Folder) context
082: .execute(folderList);
083: folders.add(childFolder);
084: } else if (ntName.equals("portalcms:file")) {
085: //JCRCommand fileList = (JCRCommand) context.getCommandFactory().createFileGetMetaDataCommand(node.getPath(), null);
086: //File childFile = (File) context.execute(fileList);
087: File childFile = this .getFileMetaData(node
088: .getPath(), context);
089: files.add(childFile);
090: }
091: } catch (Exception e) {
092: // if this occurs, typically it is due to accessDenied. don't
093: // worry about this resource
094: }
095: }
096: folder.setFolders(folders);
097: folder.setFiles(files);
098: return folder;
099:
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: return null;
104: }
105:
106: /**
107: * This method just gets the properties associated with the file and not its content. This enhances the performance.
108: *
109: * @param msPath
110: * @param JCRCommandContext
111: * @return
112: */
113: public File getFileMetaData(String msPath, JCRCommandContext context)
114: throws Exception {
115: File file = null;
116:
117: //build File
118: file = new FileImpl();
119: Session session = context.getSession();
120:
121: Node fileNode = (Node) session.getItem(msPath);
122: fileNode = (Node) session.getItem(msPath);
123: file.setCreationDate(fileNode.getProperty("jcr:created")
124: .getDate().getTime());
125: file.setLastModified(fileNode.getProperty("jcr:lastModified")
126: .getDate().getTime());
127: file.setBasePath(msPath);
128: file.setName(fileNode.getName());
129:
130: return file;
131: }
132: }
|