package reciter.pubmed.retriever;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Builder
@Getter
@Setter
public class PubMedQuery {
    private static SimpleDateFormat dt = new SimpleDateFormat("yyyy/MM/dd");

    @JsonProperty("author")
    private String author;

    @JsonProperty("start")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd")
    private Date start;

    @JsonProperty("end")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd")
    private Date end;

    @JsonProperty("strategy-query")
    private String strategyQuery;

    @Override
    public String toString() {
//        return author + "[au]" + " AND " + dt.format(start) + ":" + dt.format(end) + "[DP]";
        List<String> parts = new ArrayList<String>();
        if (author != null) {
            //parts.add(author + " [au]");
        	parts.add(author);
        }
        //Switched from [DP] to [EDAT] for better capture of pubs : Date of publication - Date added to Entrez
        if (start != null && end != null) {
            parts.add(dt.format(start) + ":" + dt.format(end) + "[EDAT]");
        }
        if (strategyQuery != null && !strategyQuery.isEmpty()) {
            parts.add(strategyQuery);
        }

        return StringUtils.join(parts, " AND ");
    }
}

@@ -1179,29 +1179,27 @@
   //
-Switched from
+Added both
  %5BDP%5D 
-to
+and
  %5BED
@@ -1340,16 +1340,23 @@
 rts.add(
+%22((%22 + 
 dt.forma
@@ -1397,16 +1397,80 @@
 %22%5BEDAT%5D%22
+ + %22) OR (%22 + dt.format(start) + %22:%22 + dt.format(end) + %22%5BDP%5D))%22
 );%0A     

use both DP and EDAT for date search