Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Header row configuration option #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ public boolean isHeaderExists() {
return config.isHeaderExists();
}

@ConfigurationProperty(
displayMessageKey = "UI_CSV_HEADER_ROW",
helpMessageKey = "UI_CSV_HEADER_ROW_HELP")
public int getHeaderRow() {
return config.getHeaderRow();
}

@ConfigurationProperty(
displayMessageKey = "UI_CSV_READ_ONLY",
helpMessageKey = "UI_CSV_READ_ONLY_HELP")
Expand Down Expand Up @@ -188,6 +195,10 @@ public void setHeaderExists(boolean headerExists) {
config.setHeaderExists(headerExists);
}

public void setHeaderRow(int headerRow) {
config.setHeaderRow(headerRow);
}

public void setObjectClassDefinition(File objectClassDefinition) {
this.objectClassDefinition = objectClassDefinition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ private Map<String, Column> initHeader(File csvFile) {
Iterator<CSVRecord> iterator = parser.iterator();

CSVRecord record = null;
while (iterator.hasNext()) {
for (int i = 1; i <= configuration.getHeaderRow() && iterator.hasNext(); i++) {
record = iterator.next();
if (!isRecordEmpty(record)) {
if (i == configuration.getHeaderRow() && !isRecordEmpty(record)) {
break;
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public Uid create(ObjectClass oc, Set<Attribute> set, OperationOptions oo) {
// we don't want to skip header in any case, but if it's there just
// write it to tmp file as "standard" record. We can't handle first row
// as header in case there are more columns with the same name.
if (configuration.isHeaderExists() && iterator.hasNext()) {
for (int i = 1; i <= configuration.getHeaderRow() && iterator.hasNext(); i++) {
CSVRecord record = iterator.next();
printer.printRecord(record);
}
Expand Down Expand Up @@ -389,7 +389,7 @@ public FilterTranslator<String> createFilterTranslator(ObjectClass oc, Operation
}

private boolean skipRecord(CSVRecord record) {
if (configuration.isHeaderExists() && record.getRecordNumber() == 1) {
if (configuration.isHeaderExists() && record.getRecordNumber() <= configuration.getHeaderRow()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ObjectClassHandlerConfiguration {
private boolean trailingDelimiter = false;
private boolean trim = false;
private boolean headerExists = true;
private int headerRow = 1;

private String uniqueAttribute;
private String nameAttribute;
Expand Down Expand Up @@ -71,6 +72,7 @@ public ObjectClassHandlerConfiguration(ObjectClass oc, Map<String, Object> value
setTrailingDelimiter(Util.getSafeValue(values, "trailingDelimiter", false, Boolean.class));
setTrim(Util.getSafeValue(values, "trim", false, Boolean.class));
setHeaderExists(Util.getSafeValue(values, "headerExists", true, Boolean.class));
setHeaderRow(Util.getSafeValue(values, "headerRow", 1, Integer.class));

setUniqueAttribute(Util.getSafeValue(values, "uniqueAttribute", null, String.class));
setNameAttribute(Util.getSafeValue(values, "nameAttribute", null, String.class));
Expand Down Expand Up @@ -119,6 +121,14 @@ public void setHeaderExists(boolean headerExists) {
this.headerExists = headerExists;
}

public int getHeaderRow() {
return headerRow;
}

public void setHeaderRow(int headerRow) {
this.headerRow = headerRow;
}

public ObjectClass getObjectClass() {
return objectClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ UI_CSV_OBJECT_CLASS_DEFINITION=Object class definition
UI_CSV_OBJECT_CLASS_DEFINITION_HELP=File which contains definitions for other object classes.
UI_CSV_HEADER_EXISTS=Header exists
UI_CSV_HEADER_EXISTS_HELP=Whether header exists in csv file.
UI_CSV_HEADER_ROW=Header row
UI_CSV_HEADER_ROW_HELP=Which row the header is on in the csv. Default value is 1.
UI_CSV_TMP_FOLDER=Tmp folder
UI_CSV_TMP_FOLDER_HELP=Folder where csv connector can write temporary files. By default the same folder as where csv file resides.
UI_CSV_READ_ONLY=Read only
Expand Down