package uk.ac.ebi.atlas.model.resource;

import com.google.common.base.Preconditions;
import org.apache.commons.lang.StringUtils;
import uk.ac.ebi.atlas.solr.BioentityProperty;
import uk.ac.ebi.atlas.species.Species;

import java.nio.file.Path;
import java.util.stream.IntStream;
import java.util.stream.Stream;


public abstract class BioentityPropertyFile extends AtlasResource<Stream<BioentityProperty>> {


    public final Species species;

    protected Stream<BioentityProperty> lineOfProperties(String bioentityIdentifier, String[] propertyNames, String[] propertyValues) {
        Preconditions.checkState(propertyNames.length == propertyValues.length);

        return IntStream
                .range(0, propertyNames.length)
                .boxed()
                .flatMap(index ->
                        Stream.of(propertyValues[index].split("@@"))
                                .filter(StringUtils::isNotEmpty)
                                .map(propertyValue -> bioentityProperty(bioentityIdentifier, propertyNames[index], propertyValue))
                );

    }

    protected BioentityProperty bioentityProperty(String bioentityIdentifier, String propertyName, String propertyValue) {
        return new BioentityProperty(bioentityIdentifier, species.getEnsemblName(), propertyName, cleanUpPropertyValue(propertyValue));
    }

    String stuffInCurlyBracketsAtTheEnd = "\\{.*\\}$";
    String stuffInSquareBracketsAtTheEnd = "\\[.*\\]$";

    String cleanUpPropertyValue(String propertyValue) {
        return propertyValue.replaceFirst(stuffInCurlyBracketsAtTheEnd, "").replaceFirst(stuffInSquareBracketsAtTheEnd, "").trim();
    }


    public BioentityPropertyFile(Path path, Species species) {
        super(path);
        this.species = species;
    }

}

@@ -306,17 +306,16 @@
 tream;%0A%0A
-%0A
 public a
@@ -402,17 +402,16 @@
 ty%3E%3E %7B%0A%0A
-%0A
     publ
@@ -435,16 +435,187 @@
 species;
+%0A    private static final String STUFF_IN_CURLY_BRACKETS_AT_THE_END = %22%5C%5C%7B.*%5C%5C%7D$%22;%0A    private static final String STUFF_IN_SQUARE_BRACKETS_AT_THE_END = %22%5C%5C%5BSource.*%5C%5C%5D$%22;
 %0A%0A    pr
@@ -1531,283 +1531,183 @@
 ing 
-stuffInCurlyBracketsAtTheEnd = %22%5C%5C%7B.*%5C%5C%7D$%22;%0A    String stuffInSquareBracketsAtTheEnd = %22%5C%5C%5B.*%5C%5C%5D$%22;%0A%0A    String cleanUpPropertyValue(String propertyValue) %7B%0A        return propertyValue.replaceFirst(stuffInCurlyBracketsAtTheEnd, %22%22).replaceFirst(stuffInSquareBracketsAtTheEnd
+cleanUpPropertyValue(String propertyValue) %7B%0A        return propertyValue.replaceFirst(STUFF_IN_CURLY_BRACKETS_AT_THE_END, %22%22).replaceFirst(STUFF_IN_SQUARE_BRACKETS_AT_THE_END
 , %22%22
@@ -1723,17 +1723,16 @@
 %0A    %7D%0A%0A
-%0A
     publ

Fix regexp that cleans values Some bioentity IDs descriptions (e.g. A. thaliana AT3G06483) start with square brackets and the previous regexp would leave the value empty and therefore the bioentities index build would fail; I don’t know if this is a problem with the data...