001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.impl.localfs;
019
020import org.nuxeo.common.utils.Path;
021import org.nuxeo.ecm.core.api.NuxeoException;
022import org.nuxeo.ecm.platform.publisher.api.AbstractPublicationNode;
023import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
024import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
025
026import java.io.File;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.Collections;
030import java.util.List;
031
032public class FSPublicationNode extends AbstractPublicationNode implements PublicationNode {
033
034    private static final long serialVersionUID = 1L;
035
036    protected File folder;
037
038    protected String sid;
039
040    public FSPublicationNode(String path, String treeName, String sid) {
041        this(new File(path), treeName, sid);
042    }
043
044    public FSPublicationNode(File folder, String treeName, String sid) {
045        if (!folder.exists()) {
046            throw new IllegalArgumentException("Root publication folder does not exist");
047        }
048        this.folder = folder;
049        this.treeName = treeName;
050        this.sid = sid;
051    }
052
053    public List<PublishedDocument> getChildrenDocuments() {
054
055        List<PublishedDocument> childrenDocs = new ArrayList<PublishedDocument>();
056        List<File> children = Arrays.asList(folder.listFiles());
057        Collections.sort(children);
058        for (File child : children) {
059            if (!child.isDirectory()) {
060                try {
061                    childrenDocs.add(new FSPublishedDocument(child));
062                } catch (NotFSPublishedDocumentException e) {
063                    throw new NuxeoException("Error whild creating PublishedDocument from file", e);
064                }
065            }
066        }
067        return childrenDocs;
068    }
069
070    public List<PublicationNode> getChildrenNodes() {
071        List<PublicationNode> childrenNodes = new ArrayList<PublicationNode>();
072        List<File> children = Arrays.asList(folder.listFiles());
073        Collections.sort(children);
074        for (File child : children) {
075            if (child.isDirectory()) {
076                childrenNodes.add(new FSPublicationNode(child, getTreeConfigName(), sid));
077            }
078        }
079        return childrenNodes;
080    }
081
082    public String getName() {
083        return folder.getName();
084    }
085
086    public PublicationNode getParent() {
087        String parentPath = new Path(getPath()).removeLastSegments(1).toString();
088        File parentFolder = new File(parentPath);
089        return new FSPublicationNode(parentFolder, getTreeConfigName(), sid);
090    }
091
092    public String getPath() {
093        return folder.getAbsolutePath();
094    }
095
096    public String getTitle() {
097        return getName();
098    }
099
100    public String getSessionId() {
101        return sid;
102    }
103
104}