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 escapeCharacter = '\\';
043
044        private boolean updateExisting = true;
045
046        private boolean checkAllowedSubTypes = true;
047
048        private boolean sendEmail = false;
049
050        private int batchSize = 50;
051
052        public Builder documentModelFactory(CSVImporterDocumentFactory factory) {
053            this.CSVImporterDocumentFactory = factory;
054            return this;
055        }
056
057        public Builder dateFormat(String dateFormat) {
058            this.dateFormat = dateFormat;
059            return this;
060        }
061
062        public Builder listSeparatorRegex(String listSeparatorRegex) {
063            this.listSeparatorRegex = listSeparatorRegex;
064            return this;
065        }
066
067        public Builder escapeCharacter(Character escapeCharacter) {
068            this.escapeCharacter = escapeCharacter;
069            return this;
070        }
071
072        public Builder updateExisting(boolean updateExisting) {
073            this.updateExisting = updateExisting;
074            return this;
075        }
076
077        public Builder checkAllowedSubTypes(boolean checkAllowedSubTypes) {
078            this.checkAllowedSubTypes = checkAllowedSubTypes;
079            return this;
080        }
081
082        public Builder sendEmail(boolean sendEmail) {
083            this.sendEmail = sendEmail;
084            return this;
085        }
086
087        public Builder batchSize(int batchSize) {
088            this.batchSize = batchSize;
089            return this;
090        }
091
092        public CSVImporterOptions build() {
093            return new CSVImporterOptions(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, escapeCharacter,
094                    updateExisting, checkAllowedSubTypes, sendEmail, batchSize);
095        }
096    }
097
098    protected final CSVImporterDocumentFactory CSVImporterDocumentFactory;
099
100    protected final String dateFormat;
101
102    protected final String listSeparatorRegex;
103
104    protected final Character escapeCharacter;
105
106    protected final boolean updateExisting;
107
108    protected final boolean checkAllowedSubTypes;
109
110    protected final boolean sendEmail;
111
112    protected final int batchSize;
113
114    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
115            String listSeparatorRegex, boolean updateExisting, boolean checkAllowedSubTypes, boolean sendEmail,
116            int batchSize) {
117        this(CSVImporterDocumentFactory, dateFormat, listSeparatorRegex, '\\', updateExisting, checkAllowedSubTypes,
118                sendEmail, batchSize);
119    }
120
121    /**
122     * @since 7.2
123     */
124    protected CSVImporterOptions(CSVImporterDocumentFactory CSVImporterDocumentFactory, String dateFormat,
125            String listSeparatorRegex, Character escapeCharacter, boolean updateExisting, boolean checkAllowedSubTypes,
126            boolean sendEmail, int batchSize) {
127        this.CSVImporterDocumentFactory = CSVImporterDocumentFactory;
128        this.dateFormat = dateFormat;
129        this.listSeparatorRegex = listSeparatorRegex;
130        this.escapeCharacter = escapeCharacter;
131        this.updateExisting = updateExisting;
132        this.checkAllowedSubTypes = checkAllowedSubTypes;
133        this.sendEmail = sendEmail;
134        this.batchSize = batchSize;
135    }
136
137    public CSVImporterDocumentFactory getCSVImporterDocumentFactory() {
138        return CSVImporterDocumentFactory;
139    }
140
141    public String getDateFormat() {
142        return dateFormat;
143    }
144
145    public String getListSeparatorRegex() {
146        return listSeparatorRegex;
147    }
148
149    public Character getEscapeCharacter() {
150        return escapeCharacter;
151    }
152
153    public boolean updateExisting() {
154        return updateExisting;
155    }
156
157    public boolean checkAllowedSubTypes() {
158        return checkAllowedSubTypes;
159    }
160
161    public boolean sendEmail() {
162        return sendEmail;
163    }
164
165    public int getBatchSize() {
166        return batchSize;
167    }
168}