org.molgenis.data.csv.CsvRepository Maven / Gradle / Ivy
Show all versions of molgenis-data-csv Show documentation
package org.molgenis.data.csv;
import static java.util.Objects.requireNonNull;
import static org.molgenis.data.meta.AttributeType.STRING;
import com.google.common.collect.Iterables;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import org.molgenis.data.Entity;
import org.molgenis.data.RepositoryCapability;
import org.molgenis.data.file.processor.CellProcessor;
import org.molgenis.data.meta.model.Attribute;
import org.molgenis.data.meta.model.AttributeFactory;
import org.molgenis.data.meta.model.EntityType;
import org.molgenis.data.meta.model.EntityTypeFactory;
import org.molgenis.data.support.AbstractRepository;
import org.springframework.util.StringUtils;
/**
* Repository implementation for csv files.
*
* The filename without the extension is considered to be the entityname
*/
public class CsvRepository extends AbstractRepository {
private final File file;
private final EntityTypeFactory entityTypeFactory;
private final AttributeFactory attrMetaFactory;
private final String sheetName;
private List cellProcessors;
private EntityType entityType;
private Character separator = null;
public CsvRepository(
File file,
EntityTypeFactory entityTypeFactory,
AttributeFactory attrMetaFactory,
@Nullable @CheckForNull List cellProcessors,
Character separator) {
this(
file,
entityTypeFactory,
attrMetaFactory,
StringUtils.stripFilenameExtension(file.getName()),
cellProcessors);
this.separator = separator;
}
public CsvRepository(
File file,
EntityTypeFactory entityTypeFactory,
AttributeFactory attrMetaFactory,
@Nullable @CheckForNull List cellProcessors) {
this(
file,
entityTypeFactory,
attrMetaFactory,
StringUtils.stripFilenameExtension(file.getName()),
cellProcessors);
}
public CsvRepository(
File file,
EntityTypeFactory entityTypeFactory,
AttributeFactory attrMetaFactory,
String sheetName,
@Nullable @CheckForNull List cellProcessors) {
this.file = file;
this.entityTypeFactory = requireNonNull(entityTypeFactory);
this.attrMetaFactory = requireNonNull(attrMetaFactory);
this.sheetName = sheetName;
this.cellProcessors = cellProcessors;
}
@NotNull
@Override
public Iterator iterator() {
return new CsvIterator(file, sheetName, cellProcessors, separator, getEntityType());
}
public EntityType getEntityType() {
if (entityType == null) {
entityType = entityTypeFactory.create(sheetName);
for (String attrName :
new CsvIterator(file, sheetName, null, separator).getColNamesMap().keySet()) {
Attribute attr = attrMetaFactory.create().setName(attrName).setDataType(STRING);
entityType.addAttribute(attr);
}
}
return entityType;
}
public void addCellProcessor(CellProcessor cellProcessor) {
if (cellProcessors == null) cellProcessors = new ArrayList<>();
cellProcessors.add(cellProcessor);
}
@Override
public Set getCapabilities() {
return Collections.emptySet();
}
@Override
public long count() {
return Iterables.size(this);
}
}