package edu.harvard.iq.dataverse.api;

import edu.harvard.iq.dataverse.DataFile;
import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord;
import static edu.harvard.iq.dataverse.api.AbstractApiBean.error;
import edu.harvard.iq.dataverse.authorization.users.ApiToken;
import edu.harvard.iq.dataverse.externaltools.ExternalTool;
import edu.harvard.iq.dataverse.externaltools.ExternalToolHandler;
import edu.harvard.iq.dataverse.externaltools.ExternalToolServiceBean;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;

@Path("admin/externalTools")
public class ExternalTools extends AbstractApiBean {

    @GET
    public Response getExternalTools() {
        JsonArrayBuilder jab = Json.createArrayBuilder();
        externalToolService.findAll().forEach((externalTool) -> {
            JsonObjectBuilder job = Json.createObjectBuilder();
            job.add("id", externalTool.getId());
            job.add(ExternalToolHandler.DISPLAY_NAME, externalTool.getDisplayName());
            job.add(ExternalToolHandler.TOOL_URL, externalTool.getToolUrl());
            jab.add(job);
        });
        return ok(jab);
    }

    @POST
    public Response addExternalTool(String manifest) {
        try {
            ExternalTool externalTool = ExternalToolServiceBean.parseAddExternalToolManifest(manifest);
            ExternalTool saved = externalToolService.save(externalTool);
            Long toolId = saved.getId();
            actionLogSvc.log(new ActionLogRecord(ActionLogRecord.ActionType.ExternalTool, "addExternalTool").setInfo("External tool added with id " + toolId + "."));
            JsonObjectBuilder tool = Json.createObjectBuilder();
            tool.add("id", toolId);
            tool.add(ExternalToolHandler.DISPLAY_NAME, saved.getDisplayName());
            return ok(tool);
        } catch (Exception ex) {
            return error(BAD_REQUEST, ex.getMessage());
        }

    }

    @DELETE
    @Path("{id}")
    public Response deleteExternalTool(@PathParam("id") long externalToolIdFromUser) {
        boolean deleted = externalToolService.delete(externalToolIdFromUser);
        if (deleted) {
            return ok("Deleted external tool with id of " + externalToolIdFromUser);
        } else {
            return error(BAD_REQUEST, "Could not not delete external tool with id of " + externalToolIdFromUser);
        }
    }

//    /**
//     * For testing only. For each of the external tools in the database we are
//     * testing the JSON format and that any reserved words within the JSON are
//     * properly replaced with the data the user supplies such as a file id
//     * and/or API token.
//     */
//    @GET
//    @Path("test/file/{id}")
//    public Response getExternalToolHandlersByFile(@PathParam("id") Long fileIdFromUser) {
//        DataFile dataFile = fileSvc.find(fileIdFromUser);
//        if (dataFile == null) {
//            return error(BAD_REQUEST, "Could not find datafile with id " + fileIdFromUser);
//        }
//        JsonArrayBuilder tools = Json.createArrayBuilder();
//        ApiToken apiToken = new ApiToken();
//        String apiTokenString = getRequestApiKey();
//        apiToken.setTokenString(apiTokenString);
//        
//        List<ExternalTool> allTools = externalToolService.findAll();
//        
//        externalToolService.findExternalToolHandlersByFile(allTools, dataFile, apiToken)
//                .forEach((externalToolHandler) -> {
//                    tools.add(externalToolHandler.toJson());
//                });
//        return ok(tools);
//    }

}

@@ -2647,18 +2647,16 @@
  %7D%0A%0A
-//
     /**%0A
 //  
@@ -2651,18 +2651,16 @@
     /**%0A
-//
      * F
@@ -2726,26 +2726,24 @@
 base we are%0A
-//
      * testi
@@ -2809,18 +2809,16 @@
 SON are%0A
-//
      * p
@@ -2884,18 +2884,16 @@
 file id%0A
-//
      * a
@@ -2913,18 +2913,16 @@
 en.%0A
-//
      */%0A
 //  
@@ -2917,18 +2917,16 @@
      */%0A
-//
     @GET
@@ -2926,18 +2926,16 @@
    @GET%0A
-//
     @Pat
@@ -2954,18 +2954,16 @@
 /%7Bid%7D%22)%0A
-//
     publ
@@ -3040,26 +3040,24 @@
 FromUser) %7B%0A
-//
         Data
@@ -3094,34 +3094,32 @@
 ileIdFromUser);%0A
-//
         if (data
@@ -3134,18 +3134,16 @@
 null) %7B%0A
-//
         
@@ -3226,18 +3226,16 @@
 mUser);%0A
-//
         
@@ -3236,18 +3236,16 @@
       %7D%0A
-//
         
@@ -3292,26 +3292,24 @@
 yBuilder();%0A
-//
         ApiT
@@ -3336,26 +3336,24 @@
 ApiToken();%0A
-//
         Stri
@@ -3388,26 +3388,24 @@
 stApiKey();%0A
-//
         apiT
@@ -3441,29 +3441,17 @@
 tring);%0A
-//        %0A//
+%0A
         
@@ -3515,29 +3515,65 @@
 ();%0A
-//
+ 
        
- %0A//
+for (ExternalTool tool : allTools) %7B%0A    
         
 exte
@@ -3560,33 +3560,33 @@
 ) %7B%0A            
-e
+E
 xternalToolServi
@@ -3580,28 +3580,42 @@
 rnalTool
-Service.find
+Handler toolHandler = new 
 External
@@ -3629,26 +3629,14 @@
 dler
-sByFile(allTools, 
+(tool,
 data
@@ -3654,73 +3654,10 @@
 ken)
+;
 %0A
-//                .forEach((externalToolHandler) -%3E %7B%0A//        
     
@@ -3674,25 +3674,17 @@
 ols.add(
-externalT
+t
 oolHandl
@@ -3701,19 +3701,16 @@
 ));%0A
-// 
         
     
@@ -3709,21 +3709,11 @@
     
-       %7D);%0A//
+%7D%0A%0A
     
@@ -3738,10 +3738,8 @@
 s);%0A
-//
     

make getExternalToolsByFileId test in ExternalToolsIT work again #4230