001/*
002 * (C) Copyright 2012 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 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.routing.core.persistence;
020
021import java.io.IOException;
022import java.util.zip.ZipFile;
023
024import org.nuxeo.common.utils.Path;
025import org.nuxeo.ecm.core.api.CloseableFile;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.PathRef;
030import org.nuxeo.ecm.core.api.security.ACP;
031import org.nuxeo.ecm.core.io.DocumentPipe;
032import org.nuxeo.ecm.core.io.DocumentReader;
033import org.nuxeo.ecm.core.io.DocumentWriter;
034import org.nuxeo.ecm.core.io.ExportedDocument;
035import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
036import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
037import org.nuxeo.ecm.core.io.impl.plugins.NuxeoArchiveReader;
038import org.nuxeo.ecm.platform.filemanager.api.FileImporterContext;
039import org.nuxeo.ecm.platform.filemanager.service.extension.ExportedZipImporter;
040
041/**
042 * Imports a route document from a zip archive using the IO core service . Existing route model with the same path as
043 * the are one to be imported is deleted before import.
044 *
045 * @since 5.6
046 */
047public class RouteModelsZipImporter extends ExportedZipImporter {
048
049    private static final long serialVersionUID = 1L;
050
051    @Override
052    public DocumentModel createOrUpdate(FileImporterContext context) throws IOException {
053        try (CloseableFile source = context.getBlob().getCloseableFile()) {
054            ZipFile zip = getArchiveFileIfValid(source.getFile());
055            if (zip == null) {
056                return null;
057            }
058            zip.close();
059
060            boolean overWrite = false;
061            DocumentReader reader = new NuxeoArchiveReader(source.getFile());
062            ExportedDocument root = reader.read();
063            String parentPath = context.getParentPath();
064            PathRef rootRef = new PathRef(parentPath, root.getPath().toString());
065            ACP currentRouteModelACP = null;
066            CoreSession session = context.getSession();
067            if (session.exists(rootRef)) {
068                DocumentModel target = session.getDocument(rootRef);
069                if (target.getPath().removeLastSegments(1).equals(new Path(parentPath))) {
070                    overWrite = true;
071                    // clean up existing route before import
072                    DocumentModel routeModel = session.getDocument(rootRef);
073                    currentRouteModelACP = routeModel.getACP();
074                    session.removeDocument(rootRef);
075                }
076            }
077
078            DocumentWriter writer = new DocumentModelWriter(session, parentPath, 10);
079            reader.close();
080            reader = new NuxeoArchiveReader(source.getFile());
081
082            DocumentRef resultingRef;
083            if (context.isOverwrite() && overWrite) {
084                resultingRef = rootRef;
085            } else {
086                String rootName = root.getPath().lastSegment();
087                resultingRef = new PathRef(parentPath, rootName);
088            }
089
090            try {
091                DocumentPipe pipe = new DocumentPipeImpl(10);
092                pipe.setReader(reader);
093                pipe.setWriter(writer);
094                pipe.run();
095            } finally {
096                reader.close();
097                writer.close();
098            }
099
100            DocumentModel newRouteModel = session.getDocument(resultingRef);
101            if (currentRouteModelACP != null && context.isOverwrite() && overWrite) {
102                newRouteModel.setACP(currentRouteModelACP, true);
103            }
104            return session.saveDocument(newRouteModel);
105        }
106    }
107
108}