View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.rest.api.beans;
20  
21  import io.swagger.v3.oas.annotations.Parameter;
22  import io.swagger.v3.oas.annotations.media.Schema;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  import javax.validation.constraints.NotNull;
27  import javax.ws.rs.DefaultValue;
28  import javax.ws.rs.QueryParam;
29  import org.apache.syncope.common.lib.types.MatchingRule;
30  import org.apache.syncope.common.lib.types.UnmatchingRule;
31  import org.apache.syncope.common.rest.api.service.JAXRSService;
32  
33  public abstract class AbstractCSVSpec implements Serializable {
34  
35      private static final long serialVersionUID = 2253975790270165334L;
36  
37      private static final String PARAM_COLUMNSEPARATOR = "columnSeparator";
38  
39      private static final String PARAM_ARRAYELEMENTSEPARATOR = "arrayElementSeparator";
40  
41      private static final String PARAM_QUOTECHAR = "quoteChar";
42  
43      private static final String PARAM_ESCAPECHAR = "escapeChar";
44  
45      private static final String PARAM_LINESEPARATOR = "lineSeparator";
46  
47      private static final String PARAM_NULLVALUE = "nullValue";
48  
49      private static final String PARAM_ALLOWCOMMENTS = "allowComments";
50  
51      private static final String PARAM_MATCHING_RULE = "matchingRule";
52  
53      private static final String PARAM_UNMATCHING_RULE = "unmatchingRule";
54  
55      protected abstract static class Builder<T extends AbstractCSVSpec, B extends Builder<T, B>> {
56  
57          protected T instance;
58  
59          protected abstract T newInstance();
60  
61          protected T getInstance() {
62              if (instance == null) {
63                  instance = newInstance();
64              }
65              return instance;
66          }
67  
68          @SuppressWarnings("unchecked")
69          public B columnSeparator(final char columnSeparator) {
70              getInstance().setColumnSeparator(columnSeparator);
71              return (B) this;
72          }
73  
74          @SuppressWarnings("unchecked")
75          public B arrayElementSeparator(final String arrayElementSeparator) {
76              getInstance().setArrayElementSeparator(arrayElementSeparator);
77              return (B) this;
78          }
79  
80          @SuppressWarnings("unchecked")
81          public B quoteChar(final char quoteChar) {
82              getInstance().setQuoteChar(quoteChar);
83              return (B) this;
84          }
85  
86          @SuppressWarnings("unchecked")
87          public B escapeChar(final char escapeChar) {
88              getInstance().setEscapeChar(escapeChar);
89              return (B) this;
90          }
91  
92          @SuppressWarnings("unchecked")
93          public B lineSeparator(final String lineSeparatorChar) {
94              getInstance().setLineSeparator(lineSeparatorChar);
95              return (B) this;
96          }
97  
98          @SuppressWarnings("unchecked")
99          public B nullValue(final String nullValue) {
100             getInstance().setNullValue(nullValue);
101             return (B) this;
102         }
103 
104         @SuppressWarnings("unchecked")
105         public B allowComments(final boolean allowComments) {
106             getInstance().setAllowComments(allowComments);
107             return (B) this;
108         }
109 
110         @SuppressWarnings("unchecked")
111         public B unmatchingRule(final UnmatchingRule unmatchingRule) {
112             getInstance().setUnmatchingRule(unmatchingRule);
113             return (B) this;
114         }
115 
116         @SuppressWarnings("unchecked")
117         public B matchingRule(final MatchingRule matchingRule) {
118             getInstance().setMatchingRule(matchingRule);
119             return (B) this;
120         }
121 
122         @SuppressWarnings("unchecked")
123         public B provisioningAction(final String provisioningActions) {
124             getInstance().getProvisioningActions().add(provisioningActions);
125             return (B) this;
126         }
127 
128         public T build() {
129             return getInstance();
130         }
131     }
132 
133     protected String anyTypeKey;
134 
135     protected char columnSeparator = ',';
136 
137     protected String arrayElementSeparator = ";";
138 
139     protected char quoteChar = '"';
140 
141     protected Character escapeChar;
142 
143     protected String lineSeparator = "\n";
144 
145     protected String nullValue = "";
146 
147     protected Boolean allowComments;
148 
149     protected UnmatchingRule unmatchingRule = UnmatchingRule.PROVISION;
150 
151     protected MatchingRule matchingRule = MatchingRule.UPDATE;
152 
153     protected List<String> provisioningActions = new ArrayList<>();
154 
155     @Parameter(name = JAXRSService.PARAM_ANYTYPEKEY, description = "any object type", schema =
156             @Schema(implementation = String.class))
157     public String getAnyTypeKey() {
158         return anyTypeKey;
159     }
160 
161     @NotNull
162     @QueryParam(JAXRSService.PARAM_ANYTYPEKEY)
163     public void setAnyTypeKey(final String anyTypeKey) {
164         this.anyTypeKey = anyTypeKey;
165     }
166 
167     @Parameter(name = PARAM_COLUMNSEPARATOR, description = "separator for column values", schema =
168             @Schema(implementation = char.class, defaultValue = ","))
169     public char getColumnSeparator() {
170         return columnSeparator;
171     }
172 
173     @QueryParam(PARAM_COLUMNSEPARATOR)
174     public void setColumnSeparator(final char columnSeparator) {
175         this.columnSeparator = columnSeparator;
176     }
177 
178     @Parameter(name = PARAM_ARRAYELEMENTSEPARATOR, description = "separator for array elements within a "
179             + "column", schema =
180             @Schema(implementation = String.class, defaultValue = ";"))
181     public String getArrayElementSeparator() {
182         return arrayElementSeparator;
183     }
184 
185     @QueryParam(PARAM_ARRAYELEMENTSEPARATOR)
186     public void setArrayElementSeparator(final String arrayElementSeparator) {
187         this.arrayElementSeparator = arrayElementSeparator;
188     }
189 
190     @Parameter(name = PARAM_QUOTECHAR, description = "character used for quoting values "
191             + "that contain quote characters or linefeeds", schema =
192             @Schema(implementation = char.class, defaultValue = "\""))
193     public char getQuoteChar() {
194         return quoteChar;
195     }
196 
197     @QueryParam(PARAM_QUOTECHAR)
198     public void setQuoteChar(final char quoteChar) {
199         this.quoteChar = quoteChar;
200     }
201 
202     @Parameter(name = PARAM_ESCAPECHAR, description = "if any, used to escape values; "
203             + "most commonly defined as backslash", schema =
204             @Schema(implementation = Character.class))
205     public Character getEscapeChar() {
206         return escapeChar;
207     }
208 
209     @QueryParam(PARAM_ESCAPECHAR)
210     public void setEscapeChar(final Character escapeChar) {
211         this.escapeChar = escapeChar;
212     }
213 
214     @Parameter(name = PARAM_LINESEPARATOR, description = "character used to separate data rows", schema =
215             @Schema(implementation = String.class, defaultValue = "\\u000a"))
216     public String getLineSeparator() {
217         return lineSeparator;
218     }
219 
220     @QueryParam(PARAM_LINESEPARATOR)
221     public void setLineSeparator(final String lineSeparator) {
222         this.lineSeparator = lineSeparator;
223     }
224 
225     @Parameter(name = PARAM_NULLVALUE, description = "when asked to write null, this string value will be used "
226             + "instead", schema =
227             @Schema(implementation = String.class, defaultValue = ""))
228     public String getNullValue() {
229         return nullValue;
230     }
231 
232     @QueryParam(PARAM_NULLVALUE)
233     public void setNullValue(final String nullValue) {
234         this.nullValue = nullValue;
235     }
236 
237     @Parameter(name = PARAM_ALLOWCOMMENTS, description = "are hash comments, e.g. lines where the first non-whitespace "
238             + "character is '#' allowed? if so, they will be skipped without processing", schema =
239             @Schema(implementation = boolean.class, defaultValue = "false"))
240     public Boolean getAllowComments() {
241         return allowComments == null ? Boolean.FALSE : allowComments;
242     }
243 
244     @QueryParam(PARAM_ALLOWCOMMENTS)
245     @DefaultValue("false")
246     public void setAllowComments(final boolean allowComments) {
247         this.allowComments = allowComments;
248     }
249 
250     @Parameter(name = PARAM_UNMATCHING_RULE, required = true, schema =
251             @Schema(implementation = UnmatchingRule.class, defaultValue = "PROVISION"))
252     public UnmatchingRule getUnmatchingRule() {
253         return unmatchingRule;
254     }
255 
256     @QueryParam(PARAM_UNMATCHING_RULE)
257     public void setUnmatchingRule(final UnmatchingRule unmatchingRule) {
258         this.unmatchingRule = unmatchingRule;
259     }
260 
261     @Parameter(name = PARAM_MATCHING_RULE, required = true, schema =
262             @Schema(implementation = MatchingRule.class, defaultValue = "UPDATE"))
263     public MatchingRule getMatchingRule() {
264         return matchingRule;
265     }
266 
267     @QueryParam(PARAM_MATCHING_RULE)
268     public void setMatchingRule(final MatchingRule matchingRule) {
269         this.matchingRule = matchingRule;
270     }
271 
272     public List<String> getProvisioningActions() {
273         return provisioningActions;
274     }
275 
276     @QueryParam("provisioningActions")
277     public void setProvisioningActions(final List<String> provisioningActions) {
278         this.provisioningActions = provisioningActions;
279     }
280 }