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