package nl.opengeogroep.safetymaps.server.stripes;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ErrorResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.action.StrictBinding;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import nl.b3p.web.stripes.ErrorMessageResolution;
import nl.opengeogroep.safetymaps.server.db.DB;
import static nl.opengeogroep.safetymaps.server.db.JSONUtils.rowToJson;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;

import static nl.opengeogroep.safetymaps.server.db.DB.ROLE_ADMIN;
import static nl.opengeogroep.safetymaps.server.db.DB.ROLE_KLADBLOKCHAT_EDITOR;
import static nl.opengeogroep.safetymaps.server.db.DB.ROLE_KLADBLOKCHAT_VIEWER;

/**
 *
 * @author Bart Verhaar
 */
@StrictBinding
@UrlBinding("/viewer/api/kladblok/{incident}.json")
public class KladblokActionBean implements ActionBean {
    private ActionBeanContext context;

    @Validate
    private String incident;

    @Validate
    private String row;

    @Validate
    private String vehicle;

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public String getIncident() {
        return incident;
    }

    public void setIncident(String incident) {
        this.incident = incident;
    }

    public String getRow() {
        return row;
    }

    public void setRow(String row) {
        this.row = row;
    }

    public String getVehicle() {
        return vehicle;
    }

    public void setVehicle(String vehicle) {
        this.vehicle = vehicle;
    }

    @DefaultHandler
    public Resolution defaultHander() throws Exception {
        if("POST".equals(context.getRequest().getMethod())) {
            return save();
        } else {
            return load();
        }
    }

    public Resolution load() throws Exception {
        HttpServletRequest request = getContext().getRequest();
        JSONArray response = new JSONArray();

        if(!request.isUserInRole(ROLE_ADMIN) && !request.isUserInRole(ROLE_KLADBLOKCHAT_VIEWER) && !request.isUserInRole(ROLE_KLADBLOKCHAT_EDITOR)) {
            return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);
        }

        try {
            List<Map<String,Object>> results = DB.qr().query("select DTG, Inhoud from safetymaps.kladblok where incident = ?", new MapListHandler(), incident);

            for (Map<String, Object> resultRow : results) {
                response.put(rowToJson(resultRow, false, false));
            }

            return new StreamingResolution("application/json", response.toString());
        } catch(Exception e) {
            return new ErrorMessageResolution(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error: " + e.getClass() + ": " + e.getMessage());
        }
    }

    public Resolution save() throws Exception {
        HttpServletRequest request = getContext().getRequest();
        
        Date today = Calendar.getInstance().getTime(); 
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Object[] qparams = new Object[] {
            incident,
            df.format(today),
            "(" + vehicle + ") " + StringEscapeUtils.escapeJava(row)
        };

        if(!request.isUserInRole(ROLE_ADMIN) && !request.isUserInRole(ROLE_KLADBLOKCHAT_EDITOR)) {
            return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);
        }

        try {
            DB.qr().insert("insert into safetymaps.kladblok (incident, DTG, Inhoud) values (?,?,?)", new MapListHandler(), qparams);
            return new ErrorMessageResolution(200, "");
        } catch(Exception e) {
            return new ErrorMessageResolution(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error: " + e.getClass() + ": " + e.getMessage());
        }
    }
}
@@ -3588,24 +3588,25 @@
 uest();%0A
+%0A
         
 %0A       
@@ -3597,17 +3597,250 @@
         
-%0A
+if(!request.isUserInRole(ROLE_ADMIN) && !request.isUserInRole(ROLE_KLADBLOKCHAT_EDITOR)) %7B%0A            return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);%0A        %7D%0A%0A        if(row.length() %3E 0 && row.length() %3C= 500) %7B%0A    
         
@@ -3887,16 +3887,20 @@
 ime(); %0A
+    
         
@@ -3969,16 +3969,20 @@
         
+   
+ 
 Object%5B%5D
@@ -4007,16 +4007,20 @@
 ect%5B%5D %7B%0A
+    
         
@@ -4045,16 +4045,20 @@
         
+    
 df.forma
@@ -4079,16 +4079,20 @@
         
+ 
+   
 %22(%22 + ve
@@ -4152,210 +4152,38 @@
     
-%7D;%0A%0A        if(!request.isUserInRole(ROLE_ADMIN) && !request.isUserInRole(ROLE_KLADBLOKCHAT_EDITOR)) %7B%0A            return new ErrorResolution(HttpServletResponse.SC_FORBIDDEN);%0A        %7D%0A%0A        try %7B%0A
+    %7D;%0A%0A            try %7B%0A    
     
@@ -4315,32 +4315,36 @@
 s);%0A            
+    
 return new Error
@@ -4371,32 +4371,36 @@
 0, %22%22);%0A        
+  
+  
 %7D catch(Exceptio
@@ -4398,32 +4398,36 @@
 (Exception e) %7B%0A
+    
             retu
@@ -4545,32 +4545,226 @@
 .getMessage());%0A
+            %7D%0A        %7D else %7B%0A            return new ErrorMessageResolution(HttpServletResponse.SC_FORBIDDEN, %22Notepad row must contain at least 1 character and no more then 500 characters.%22);%0A
         %7D%0A    %7D%0A

Add check on notepad row length