2016-08-07 23:13:02 +02:00
|
|
|
/*
|
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2015 javafx.ninja <info@javafx.ninja>
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
*/
|
2016-08-07 16:57:55 +02:00
|
|
|
package ninja.javafx.smartcsv.validation;
|
|
|
|
|
|
2016-08-09 20:58:16 +02:00
|
|
|
import ninja.javafx.smartcsv.fx.table.model.ColumnValueProvider;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static java.util.Collections.sort;
|
|
|
|
|
import static java.util.stream.Collectors.joining;
|
2016-08-07 16:57:55 +02:00
|
|
|
|
|
|
|
|
/**
|
2016-08-07 23:13:02 +02:00
|
|
|
* Checks if the value is unique in the column
|
2016-08-07 16:57:55 +02:00
|
|
|
*/
|
2016-08-09 21:17:29 +02:00
|
|
|
public class UniqueValidation extends EmptyValueIsValid {
|
2016-08-07 16:57:55 +02:00
|
|
|
|
2016-08-09 20:58:16 +02:00
|
|
|
private ColumnValueProvider columnValueProvider;
|
|
|
|
|
private String column;
|
|
|
|
|
|
|
|
|
|
public UniqueValidation(ColumnValueProvider columnValueProvider, String column) {
|
|
|
|
|
this.columnValueProvider = columnValueProvider;
|
|
|
|
|
this.column = column;
|
|
|
|
|
}
|
2016-08-07 16:57:55 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void check(int row, String value, ValidationError error) {
|
2016-08-09 20:58:16 +02:00
|
|
|
|
|
|
|
|
List<Integer> lineNumbers = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
int numberOfRows = columnValueProvider.getNumberOfRows();
|
|
|
|
|
for (int currentRowOfIteration = 0; currentRowOfIteration < numberOfRows; currentRowOfIteration++) {
|
|
|
|
|
String storedValue = columnValueProvider.getValue(currentRowOfIteration, column);
|
|
|
|
|
|
2016-08-09 21:17:29 +02:00
|
|
|
if (value.equals(storedValue) && currentRowOfIteration != row) {
|
2016-08-09 20:58:16 +02:00
|
|
|
lineNumbers.add(currentRowOfIteration + 1); // show not 0 based line numbers to user
|
2016-08-07 16:57:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-09 20:58:16 +02:00
|
|
|
|
|
|
|
|
if (!lineNumbers.isEmpty()) {
|
|
|
|
|
if (lineNumbers.size() > 1) {
|
|
|
|
|
sort(lineNumbers);
|
|
|
|
|
error.add("validation.message.uniqueness.multiple", value, lineNumbers.stream().map(Object::toString).collect(joining(", ")));
|
|
|
|
|
} else {
|
|
|
|
|
error.add("validation.message.uniqueness.single", value, lineNumbers.get(0).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Type getType() {
|
|
|
|
|
return Type.UNIQUE;
|
|
|
|
|
}
|
|
|
|
|
}
|