package uk.ac.ebi.quickgo.annotation.validation.loader;

import uk.ac.ebi.quickgo.annotation.validation.model.DBXRefEntity;

import java.util.regex.Pattern;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.batch.item.file.transform.IncorrectTokenCountException;
import org.springframework.validation.BindException;

import static uk.ac.ebi.quickgo.annotation.validation.loader.DBXrefEntityColumns.*;
import static uk.ac.ebi.quickgo.annotation.validation.loader.DBXrefEntityColumns.numColumns;

/**
 * @author Tony Wardell
 * Date: 07/11/2016
 * Time: 18:12
 * Created with IntelliJ IDEA.
 */
public class StringToDbXrefEntityMapper implements FieldSetMapper<DBXRefEntity> {

    @Override public DBXRefEntity mapFieldSet(FieldSet fieldSet) throws BindException {
        if (fieldSet == null) {
            throw new IllegalArgumentException("Provided field set is null");
        }

        if (fieldSet.getFieldCount() < numColumns()) {
            throw new IncorrectTokenCountException("Incorrect number of columns, expected: " + numColumns() + "; " +
                    "found: " + fieldSet.getFieldCount(), numColumns(), fieldSet.getFieldCount());
        }

        DBXRefEntity DBXRefEntity = new DBXRefEntity();
        DBXRefEntity.database = trimIfNotNull(fieldSet.readString(COLUMN_DB.getPosition()));
        DBXRefEntity.entityType = trimIfNotNull(fieldSet.readString(COLUMN_ENTITY_TYPE_ID.getPosition()));
        DBXRefEntity.databaseURL = trimIfNotNull(fieldSet.readString(COLUMN_URL_SYNTAX.getPosition()));
        DBXRefEntity.entityTypeName = trimIfNotNull(fieldSet.readString(COLUMN_ENTITY_TYPE_NAME.getPosition()));
        DBXRefEntity.idValidationPattern = Pattern.compile(replaceEscapeIfExists(trimIfNotNull(fieldSet.readString
                (COLUMN_LOCAL_ID_SYNTAX.getPosition()))));

        return DBXRefEntity;

    }

    private String trimIfNotNull(String value) {
        return value == null ? null : value.trim();
    }

    private String replaceEscapeIfExists(String value){
        return value == null ? null : value.replace("\\\\", "\\");
    }
}

@@ -599,16 +599,214 @@
 s;%0A%0A/**%0A
+ * Class responsible for mapping a %7B@link FieldSet%7D that contains data related to Database Cross references%0A * to a corresponding instance of %7B@link DBXRefEntity%7D encapsulating this information.%0A *%0A
  * @auth

Add javadoc.