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 java.io.File;
023import java.io.IOException;
024import java.io.StringReader;
025
026import org.dom4j.Document;
027import org.dom4j.DocumentException;
028import org.dom4j.DocumentFactory;
029import org.dom4j.Element;
030import org.dom4j.Namespace;
031import org.dom4j.QName;
032import org.dom4j.io.SAXReader;
033import org.nuxeo.common.utils.FileUtils;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentRef;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
038import org.nuxeo.ecm.platform.publisher.helper.VersioningHelper;
039import org.nuxeo.ecm.platform.publisher.remoting.marshaling.CoreIODocumentModelMarshaler;
040
041public class FSPublishedDocument implements PublishedDocument {
042
043    private static final long serialVersionUID = 1L;
044
045    public static final Namespace nxfspub = new Namespace("nxfspub", "http://www.nuxeo.org/publisher/filesystem");
046
047    public static final QName pubInfoQN = DocumentFactory.getInstance().createQName("publicationInfo", nxfspub);
048
049    public static final QName sourceDocRefQN = DocumentFactory.getInstance().createQName("sourceDocumentRef", nxfspub);
050
051    public static final QName sourceRepositoryNameQN = DocumentFactory.getInstance().createQName(
052            "sourceRepositoryName", nxfspub);
053
054    public static final QName sourceServerQN = DocumentFactory.getInstance().createQName("sourceServer", nxfspub);
055
056    public static final QName sourceVersionQN = DocumentFactory.getInstance().createQName("sourceVersion", nxfspub);
057
058    public static final QName isPendingQN = DocumentFactory.getInstance().createQName("isPending", nxfspub);
059
060    protected DocumentRef sourceDocumentRef;
061
062    protected String sourceRepositoryName;
063
064    protected String sourceServer;
065
066    protected String sourceVersion;
067
068    protected String persistPath;
069
070    protected String parentPath;
071
072    protected boolean isPending;
073
074    protected String xmlRepresentation;
075
076    public FSPublishedDocument(File file) throws NotFSPublishedDocumentException {
077        parseXML(file);
078        persistPath = file.getAbsolutePath();
079        parentPath = file.getParent();
080    }
081
082    protected void parseXML(File file) throws NotFSPublishedDocumentException {
083        SAXReader xmlReader = new SAXReader();
084        try {
085            Document doc = xmlReader.read(file);
086            Element info = doc.getRootElement().element(pubInfoQN);
087            if (info == null) {
088                // valid xml file, but not a published document
089                throw new NotFSPublishedDocumentException();
090            }
091            sourceDocumentRef = new IdRef(info.element(sourceDocRefQN).getTextTrim());
092            sourceRepositoryName = info.element(sourceRepositoryNameQN).getTextTrim();
093            sourceServer = info.element(sourceServerQN).getTextTrim();
094            sourceVersion = info.element(sourceVersionQN).getTextTrim();
095            isPending = Boolean.parseBoolean(info.element(isPendingQN).getTextTrim());
096        } catch (DocumentException e) {
097            throw new NotFSPublishedDocumentException(e);
098        }
099    }
100
101    public void persist(String containerPath) throws IOException {
102        File output = new File(containerPath, sourceDocumentRef.toString());
103        FileUtils.writeFile(output, xmlRepresentation);
104        persistPath = output.getAbsolutePath();
105    }
106
107    public FSPublishedDocument(String server, DocumentModel doc) throws DocumentException {
108        this(server, doc, false);
109    }
110
111    public FSPublishedDocument(String server, DocumentModel doc, boolean isPending) throws DocumentException {
112
113        sourceRepositoryName = doc.getRepositoryName();
114        sourceDocumentRef = doc.getRef();
115        sourceVersion = VersioningHelper.getVersionLabelFor(doc);
116        sourceServer = server;
117        this.isPending = isPending;
118
119        CoreIODocumentModelMarshaler marshaler = new CoreIODocumentModelMarshaler();
120        String xmlDoc = marshaler.marshalDocument(doc);
121
122        SAXReader xmlReader = new SAXReader();
123        Document xml = xmlReader.read(new StringReader(xmlDoc));
124
125        xml.getRootElement().add(nxfspub);
126        Element info = xml.getRootElement().addElement(pubInfoQN);
127
128        info.addElement(sourceDocRefQN).setText(sourceDocumentRef.toString());
129        info.addElement(sourceRepositoryNameQN).setText(sourceRepositoryName);
130        if (sourceServer != null) {
131            info.addElement(sourceServerQN).setText(sourceServer);
132        } else {
133            info.addElement(sourceServerQN);
134        }
135        if (sourceVersion != null) {
136            info.addElement(sourceVersionQN).setText(sourceVersion);
137        } else {
138            info.addElement(sourceVersionQN);
139        }
140        info.addElement(isPendingQN).setText(String.valueOf(isPending));
141        xmlRepresentation = xml.asXML();
142    }
143
144    @Override
145    public DocumentRef getSourceDocumentRef() {
146        return sourceDocumentRef;
147    }
148
149    @Override
150    public String getSourceRepositoryName() {
151        return sourceRepositoryName;
152    }
153
154    @Override
155    public String getSourceServer() {
156        return sourceServer;
157    }
158
159    @Override
160    public String getSourceVersionLabel() {
161        return sourceVersion;
162    }
163
164    public String getPersistPath() {
165        return persistPath;
166    }
167
168    @Override
169    public String getPath() {
170        return getPersistPath();
171    }
172
173    @Override
174    public String getParentPath() {
175        return parentPath;
176    }
177
178    @Override
179    public boolean isPending() {
180        return isPending;
181    }
182
183    @Override
184    public Type getType() {
185        return Type.FILE_SYSTEM;
186    }
187
188}