001/*
002 * (C) Copyright 2019 Nuxeo (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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.filemanager.api;
021
022import org.nuxeo.ecm.core.api.Blob;
023import org.nuxeo.ecm.core.api.CoreSession;
024
025/**
026 * Class containing everything needed to create or update a document from a {@code Blob} with the {@link FileManager}.
027 *
028 * @since 10.10
029 */
030public class FileImporterContext {
031
032    protected final CoreSession session;
033
034    protected final Blob blob;
035
036    protected final String parentPath;
037
038    protected String fileName;
039
040    protected boolean overwrite;
041
042    protected boolean mimeTypeCheck;
043
044    protected boolean excludeOneToMany;
045
046    protected boolean persistDocument;
047
048    public static Builder builder(CoreSession session, Blob blob, String parentPath) {
049        return new Builder(session, blob, parentPath);
050    }
051
052    protected FileImporterContext(Builder builder) {
053        session = builder.session;
054        blob = builder.blob;
055        parentPath = builder.parentPath;
056        overwrite = builder.overwrite;
057        mimeTypeCheck = builder.mimeTypeCheck;
058        excludeOneToMany = builder.excludeOneToMany;
059        fileName = builder.fileName;
060        persistDocument = builder.persistDocument;
061    }
062
063    public CoreSession getSession() {
064        return session;
065    }
066
067    public Blob getBlob() {
068        return blob;
069    }
070
071    public String getParentPath() {
072        return parentPath;
073    }
074
075    public String getFileName() {
076        return fileName;
077    }
078
079    public boolean isOverwrite() {
080        return overwrite;
081    }
082
083    public boolean isMimeTypeCheck() {
084        return mimeTypeCheck;
085    }
086
087    public boolean isExcludeOneToMany() {
088        return excludeOneToMany;
089    }
090
091    public boolean isPersistDocument() {
092        return persistDocument;
093    }
094
095    public static class Builder {
096
097        protected final CoreSession session;
098
099        protected final Blob blob;
100
101        protected final String parentPath;
102
103        protected String fileName;
104
105        protected boolean overwrite;
106
107        protected boolean mimeTypeCheck = true;
108
109        protected boolean excludeOneToMany;
110
111        protected boolean persistDocument = true;
112
113        public Builder(CoreSession session, Blob blob, String parentPath) {
114            this.session = session;
115            this.blob = blob;
116            this.parentPath = parentPath;
117        }
118
119        /**
120         * Overrides the file name from the given {@code blob}.
121         */
122        public Builder fileName(String fileName) {
123            this.fileName = fileName;
124            return this;
125        }
126
127        /**
128         * Whether to overwrite an existing file with the same title.
129         * <p>
130         * Defaults to {@code false}.
131         */
132        public Builder overwrite(boolean overwrite) {
133            this.overwrite = overwrite;
134            return this;
135        }
136
137        /**
138         * Whether to check the blob's mime-type against the file name.
139         * <p>
140         * Defaults to {@code true}.
141         */
142        public Builder mimeTypeCheck(boolean mimeTypeCheck) {
143            this.mimeTypeCheck = mimeTypeCheck;
144            return this;
145        }
146
147        /**
148         * Whether to exclude the importers creating more than one document for the given blob when selecting the
149         * importer.
150         * <p>
151         * Defaults to {@code false}.
152         */
153        public Builder excludeOneToMany(boolean excludeOneToMany) {
154            this.excludeOneToMany = excludeOneToMany;
155            return this;
156        }
157
158        /**
159         * Whether to persist the created or updated document.
160         * <p>
161         * If the document is not persisted, it's the caller's responsibility to persist it.
162         * <p>
163         * Defaults to {@code true}.
164         */
165        public Builder persistDocument(boolean persistDocument) {
166            this.persistDocument = persistDocument;
167            return this;
168        }
169
170        public FileImporterContext build() {
171            return new FileImporterContext(this);
172        }
173    }
174
175}