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;
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        public Builder documentModelFactory(CSVImporterDocumentFactory factory) {
055            this.CSVImporterDocumentFactory = factory;
056            return this;
057        }
058
059        public Builder dateFormat(String dateFormat) {
060            this.dateFormat = dateFormat;
061            return this;
062        }
063
064        public Builder listSeparatorRegex(String listSeparatorRegex) {
065            this.listSeparatorRegex = listSeparatorRegex;
066            return this;
067        }
068
069        public Builder commentMarker(Character commentMarker) {
070            this.commentMarker = commentMarker;
071            return this;
072        }
073
074        public Builder escapeCharacter(Character escapeCharacter) {
075            this.escapeCharacter = escapeCharacter;
076            return this;
077        }
078
079        public Builder updateExisting(boolean updateExisting) {
080            this.updateExisting = updateExisting;
081            return this;
082        }
083
084        public Builder checkAllowedSubTypes(boolean checkAllowedSubTypes) {
085            this.checkAllowedSubTypes = checkAllowedSubTypes;
086            return this;
087        }
088
089        public Builder sendEmail(boolean sendEmail) {
090            this.sendEmail = sendEmail;
091            return this;
092        }
093
094        public Builder batchSize(int batchSize) {
095            this.batchSize = batchSize;
096            return this;
097        }
098
099        public CSVImporterOptions build() {
100            return new CSVImporterOptions(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, commentMarker,
101                    escapeCharacter, updateExisting, checkAllowedSubTypes, sendEmail, batchSize);
102        }
103    }
104
105    protected final CSVImporterDocumentFactory CSVImporterDocumentFactory;
106
107    protected final String dateFormat;
108
109    protected final String listSeparatorRegex;
110
111    protected final Character commentMarker;
112
113    protected final Character escapeCharacter;
114
115    protected final boolean updateExisting;
116
117    protected final boolean checkAllowedSubTypes;
118
119    protected final boolean sendEmail;
120
121    protected final int batchSize;
122
123    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
124            String listSeparatorRegex, boolean updateExisting, boolean checkAllowedSubTypes, boolean sendEmail,
125            int batchSize) {
126        this(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, '\\', updateExisting, checkAllowedSubTypes,
127                sendEmail, batchSize);
128    }
129
130    /**
131     * @since 7.2
132     */
133    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
134            String listSeparatorRegex, Character escapeCharacter, boolean updateExisting, boolean checkAllowedSubTypes,
135            boolean sendEmail, int batchSize) {
136        this(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, null, escapeCharacter, updateExisting,
137                checkAllowedSubTypes, sendEmail, batchSize);
138    }
139
140    /**
141     * @since 8.3
142     */
143    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
144            String listSeparatorRegex, Character commentMarker, Character escapeCharacter, boolean updateExisting,
145            boolean checkAllowedSubTypes, boolean sendEmail, int batchSize) {
146        this.CSVImporterDocumentFactory = CSVImporterDocumentFactory;
147        this.dateFormat = dateFormat;
148        this.listSeparatorRegex = listSeparatorRegex;
149        this.commentMarker = commentMarker;
150        this.escapeCharacter = escapeCharacter;
151        this.updateExisting = updateExisting;
152        this.checkAllowedSubTypes = checkAllowedSubTypes;
153        this.sendEmail = sendEmail;
154        this.batchSize = batchSize;
155    }
156
157    public CSVImporterDocumentFactory getCSVImporterDocumentFactory() {
158        return CSVImporterDocumentFactory;
159    }
160
161    public String getDateFormat() {
162        return dateFormat;
163    }
164
165    public String getListSeparatorRegex() {
166        return listSeparatorRegex;
167    }
168
169    public Character getCommentMarker() {
170        return commentMarker;
171    }
172
173    public Character getEscapeCharacter() {
174        return escapeCharacter;
175    }
176
177    public boolean updateExisting() {
178        return updateExisting;
179    }
180
181    public boolean checkAllowedSubTypes() {
182        return checkAllowedSubTypes;
183    }
184
185    public boolean sendEmail() {
186        return sendEmail;
187    }
188
189    public int getBatchSize() {
190        return batchSize;
191    }
192}