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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.csv.core;
021
022import java.io.Serializable;
023import java.text.DateFormat;
024import java.text.SimpleDateFormat;
025
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.runtime.services.config.ConfigurationService;
028
029/**
030 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
031 * @since 5.7
032 */
033public class CSVImporterOptions implements Serializable {
034
035    private static final long serialVersionUID = 1L;
036
037    public static final String LEGACY_DATE_FORMAT_PROP = "nuxeo.csv.import.legacyDateFormat";
038
039    public static final String LEGACY_DATE_FORMAT = "MM/dd/yyyy";
040
041    public static final CSVImporterOptions DEFAULT_OPTIONS = new Builder().build();
042
043    public static class Builder {
044
045        private CSVImporterDocumentFactory CSVImporterDocumentFactory = new DefaultCSVImporterDocumentFactory();
046
047        private String dateFormat;
048
049        private String listSeparatorRegex = "\\|";
050
051        private Character commentMarker;
052
053        private Character escapeCharacter = '\\';
054
055        private boolean updateExisting = true;
056
057        private boolean checkAllowedSubTypes = true;
058
059        private boolean sendEmail;
060
061        private int batchSize = 50;
062
063        private ImportMode importMode = ImportMode.CREATE;
064
065        public Builder documentModelFactory(CSVImporterDocumentFactory factory) {
066            CSVImporterDocumentFactory = factory;
067            return this;
068        }
069
070        public Builder dateFormat(String dateFormat) {
071            this.dateFormat = dateFormat;
072            return this;
073        }
074
075        public Builder listSeparatorRegex(String listSeparatorRegex) {
076            this.listSeparatorRegex = listSeparatorRegex;
077            return this;
078        }
079
080        public Builder commentMarker(Character commentMarker) {
081            this.commentMarker = commentMarker;
082            return this;
083        }
084
085        public Builder escapeCharacter(Character escapeCharacter) {
086            this.escapeCharacter = escapeCharacter;
087            return this;
088        }
089
090        public Builder updateExisting(boolean updateExisting) {
091            this.updateExisting = updateExisting;
092            return this;
093        }
094
095        public Builder checkAllowedSubTypes(boolean checkAllowedSubTypes) {
096            this.checkAllowedSubTypes = checkAllowedSubTypes;
097            return this;
098        }
099
100        public Builder sendEmail(boolean sendEmail) {
101            this.sendEmail = sendEmail;
102            return this;
103        }
104
105        public Builder batchSize(int batchSize) {
106            this.batchSize = batchSize;
107            return this;
108        }
109
110        public Builder importMode(ImportMode importMode) {
111            this.importMode = importMode;
112            return this;
113        }
114
115        public CSVImporterOptions build() {
116            return new CSVImporterOptions(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, commentMarker,
117                    escapeCharacter, updateExisting, checkAllowedSubTypes, sendEmail, batchSize, importMode);
118        }
119    }
120
121    public enum ImportMode {
122        CREATE, IMPORT
123    }
124
125    protected ImportMode importMode;
126
127    protected final CSVImporterDocumentFactory CSVImporterDocumentFactory;
128
129    protected final DateFormat dateFormat;
130
131    protected final String listSeparatorRegex;
132
133    protected final Character commentMarker;
134
135    protected final Character escapeCharacter;
136
137    protected final boolean updateExisting;
138
139    protected final boolean checkAllowedSubTypes;
140
141    protected final boolean sendEmail;
142
143    protected final int batchSize;
144
145    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
146            String listSeparatorRegex, boolean updateExisting, boolean checkAllowedSubTypes, boolean sendEmail,
147            int batchSize, ImportMode importMode) {
148        this(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, '\\', updateExisting, checkAllowedSubTypes,
149                sendEmail, batchSize, importMode);
150    }
151
152    /**
153     * @since 7.2
154     */
155    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
156            String listSeparatorRegex, Character escapeCharacter, boolean updateExisting, boolean checkAllowedSubTypes,
157            boolean sendEmail, int batchSize, ImportMode importMode) {
158        this(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, null, escapeCharacter, updateExisting,
159                checkAllowedSubTypes, sendEmail, batchSize, importMode);
160    }
161
162    /**
163     * @since 8.3
164     */
165    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
166            String listSeparatorRegex, Character commentMarker, Character escapeCharacter, boolean updateExisting,
167            boolean checkAllowedSubTypes, boolean sendEmail, int batchSize, ImportMode importMode) {
168        this.CSVImporterDocumentFactory = CSVImporterDocumentFactory;
169        CSVImporterDocumentFactory.setImporterOptions(this);
170        this.dateFormat = computeDateFormat(dateFormat);
171        this.listSeparatorRegex = listSeparatorRegex;
172        this.commentMarker = commentMarker;
173        this.escapeCharacter = escapeCharacter;
174        this.updateExisting = updateExisting;
175        this.checkAllowedSubTypes = checkAllowedSubTypes;
176        this.sendEmail = sendEmail;
177        this.batchSize = batchSize;
178        this.importMode = importMode;
179    }
180
181    protected DateFormat computeDateFormat(String dateFormat) {
182        if (dateFormat != null) {
183            return new SimpleDateFormat(dateFormat);
184        }
185
186        return Framework.getService(ConfigurationService.class).isBooleanTrue(LEGACY_DATE_FORMAT_PROP)
187                ? new SimpleDateFormat(LEGACY_DATE_FORMAT)
188                : null;
189    }
190
191    public CSVImporterDocumentFactory getCSVImporterDocumentFactory() {
192        return CSVImporterDocumentFactory;
193    }
194
195    public DateFormat getDateFormat() {
196        return dateFormat;
197    }
198
199    public String getListSeparatorRegex() {
200        return listSeparatorRegex;
201    }
202
203    public Character getCommentMarker() {
204        return commentMarker;
205    }
206
207    public Character getEscapeCharacter() {
208        return escapeCharacter;
209    }
210
211    public boolean updateExisting() {
212        return updateExisting;
213    }
214
215    public boolean checkAllowedSubTypes() {
216        return checkAllowedSubTypes;
217    }
218
219    public boolean sendEmail() {
220        return sendEmail;
221    }
222
223    public int getBatchSize() {
224        return batchSize;
225    }
226
227    public ImportMode getImportMode() {
228        return importMode;
229    }
230}