001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.platform.publisher.impl.localfs;
021
022import org.nuxeo.common.utils.Path;
023import org.nuxeo.ecm.core.api.NuxeoException;
024import org.nuxeo.ecm.platform.publisher.api.AbstractPublicationNode;
025import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
026import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
027
028import java.io.File;
029import java.util.ArrayList;
030import java.util.Arrays;
031import java.util.Collections;
032import java.util.List;
033
034public class FSPublicationNode extends AbstractPublicationNode implements PublicationNode {
035
036    private static final long serialVersionUID = 1L;
037
038    protected File folder;
039
040    protected String sid;
041
042    public FSPublicationNode(String path, String treeName, String sid) {
043        this(new File(path), treeName, sid);
044    }
045
046    public FSPublicationNode(File folder, String treeName, String sid) {
047        if (!folder.exists()) {
048            throw new IllegalArgumentException("Root publication folder does not exist");
049        }
050        this.folder = folder;
051        this.treeName = treeName;
052        this.sid = sid;
053    }
054
055    public List<PublishedDocument> getChildrenDocuments() {
056
057        List<PublishedDocument> childrenDocs = new ArrayList<PublishedDocument>();
058        List<File> children = Arrays.asList(folder.listFiles());
059        Collections.sort(children);
060        for (File child : children) {
061            if (!child.isDirectory()) {
062                try {
063                    childrenDocs.add(new FSPublishedDocument(child));
064                } catch (NotFSPublishedDocumentException e) {
065                    throw new NuxeoException("Error whild creating PublishedDocument from file", e);
066                }
067            }
068        }
069        return childrenDocs;
070    }
071
072    public List<PublicationNode> getChildrenNodes() {
073        List<PublicationNode> childrenNodes = new ArrayList<PublicationNode>();
074        List<File> children = Arrays.asList(folder.listFiles());
075        Collections.sort(children);
076        for (File child : children) {
077            if (child.isDirectory()) {
078                childrenNodes.add(new FSPublicationNode(child, getTreeConfigName(), sid));
079            }
080        }
081        return childrenNodes;
082    }
083
084    public String getName() {
085        return folder.getName();
086    }
087
088    public PublicationNode getParent() {
089        String parentPath = new Path(getPath()).removeLastSegments(1).toString();
090        File parentFolder = new File(parentPath);
091        return new FSPublicationNode(parentFolder, getTreeConfigName(), sid);
092    }
093
094    public String getPath() {
095        return folder.getAbsolutePath();
096    }
097
098    public String getTitle() {
099        return getName();
100    }
101
102    public String getSessionId() {
103        return sid;
104    }
105
106}