package fi.helsinki.lib.simplerest;

import com.google.gson.Gson;
import fi.helsinki.lib.simplerest.options.GetOptions;
import fi.helsinki.lib.simplerest.stubs.StubItem;
import org.dspace.content.ItemIterator;
import org.dspace.core.Context;
import org.dspace.content.Item;

import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;

/**
 * Created by moubarik on 2.6.15.
 */

public class HandleResource extends BaseResource{

    private static Logger log = Logger.getLogger(HandleResource.class);

    private String handle;
    private Item item;
    private Context context;

    public HandleResource(){
        this.handle = "";
        this.item = null;
        try{
            this.context = new Context();
        }catch(SQLException e){
            log.log(Priority.FATAL, e);
        }
    }

    static public String relativeUrl(String handle) {return "handle/10024/"+handle; }

    @Override
    protected void doInit() throws ResourceException {
        String s = (String)getRequest().getAttributes().get("handle");
        this.handle = s;
        this.handle = this.handle.replaceAll("\\+", "/");
    }

    @Get
    public String toJson(){
        GetOptions.allowAccess(getResponse());
        ItemIterator it = null;
        try{
            it = Item.findByMetadataField(context, "dc", "identifier", "uri", "http://fennougrica.kansalliskirjasto.fi/handle/"+this.handle);
        }catch(Exception e){
            if(context != null){
                context.abort();
            }
            log.log(Priority.INFO, e);
        }

        try{
            if(it.hasNext()){
                this.item = it.next();
            }else{
                return "{\"error\": \"Item not found\", \"status\": 404 }";
            }
        }catch(SQLException e){
            if(context != null){
                context.abort();
            }
            log.log(Priority.INFO, e);
        }


        StubItem stub = null;
        try{
            stub = new StubItem(this.item);
        }catch(SQLException e){
            if(context != null){
                context.abort();
            }
            log.log(Priority.INFO, e);
        }

        try{
            if(context != null){
                context.complete();
            }
        }catch (SQLException e){
            log.log(Priority.ERROR, e);
        }

        Gson gson = new Gson();

        return gson.toJson(stub);

    }

}

@@ -294,24 +294,48 @@
 LException;%0A
+import java.util.Date;%0A%0A
 import org.a
@@ -388,16 +388,286 @@
 iority;%0A
+import org.restlet.data.MediaType;%0Aimport org.restlet.data.Status;%0Aimport org.restlet.engine.util.DateUtils;%0Aimport org.restlet.representation.EmptyRepresentation;%0Aimport org.restlet.representation.Representation;%0Aimport org.restlet.representation.StringRepresentation;%0A
 import o
@@ -737,16 +737,48 @@
 ception;
+%0Aimport org.restlet.util.Series;
 %0A%0A/**%0A *
@@ -1016,16 +1016,50 @@
 context;
+%0A    private Date ifModifiedSince;
 %0A%0A    pu
@@ -1586,16 +1586,267 @@
 %22, %22/%22);
+%0A%0A        Series headers = (Series)getRequestAttributes().get(%22org.restlet.http.headers%22);%0A        String date = headers.getFirstValue(%22If-Modified-Since%22);%0A        if(date != null) %7B%0A            this.ifModifiedSince = DateUtils.parse(date);%0A        %7D
 %0A    %7D%0A%0A
@@ -1865,22 +1865,30 @@
  public 
-String
+Representation
  toJson(
@@ -2407,16 +2407,41 @@
  return 
+new StringRepresentation(
 %22%7B%5C%22erro
@@ -2483,16 +2483,45 @@
 : 404 %7D%22
+, MediaType.APPLICATION_JSON)
 ;%0A      
@@ -2690,16 +2690,94 @@
      %7D%0A%0A
+        GetOptions.lastModified(getResponse(), this.item.getLastModified());%0A%0A
 %0A       
@@ -2855,16 +2855,302 @@
 s.item);
+%0A%0A            if(this.ifModifiedSince != null) %7B%0A                if (this.ifModifiedSince.after(stub.getLastModified())) %7B%0A                    getResponse().setStatus(Status.REDIRECTION_NOT_MODIFIED);%0A                    return new EmptyRepresentation();%0A                %7D%0A            %7D
 %0A       
@@ -3536,24 +3536,78 @@
 urn 
-gson.toJson(stub
+new StringRepresentation(gson.toJson(stub), MediaType.APPLICATION_JSON
 );%0A%0A

Handle search supports modified headers now