This is an automated email from the ASF dual-hosted git repository.

lahirujayathilake pushed a commit to branch agent-framewok-refactoring
in repository https://gitbox.apache.org/repos/asf/airavata.git


The following commit(s) were added to refs/heads/agent-framewok-refactoring by 
this push:
     new 0eeffef158 support airavata experiment plan services
0eeffef158 is described below

commit 0eeffef158cee73b04fa8370607eb982e11cd5c3
Author: lahiruj <[email protected]>
AuthorDate: Mon Dec 9 03:17:25 2024 -0500

    support airavata experiment plan services
---
 .../service/controllers/PlanController.java        | 91 ++++++++++++++++++++++
 .../agent/connection/service/db/entity/Plan.java   | 55 +++++++++++++
 .../agent/connection/service/db/repo/PlanRepo.java | 13 ++++
 .../connection/service/handlers/PlanHandler.java   | 35 +++++++++
 4 files changed, 194 insertions(+)

diff --git 
a/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/controllers/PlanController.java
 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/controllers/PlanController.java
new file mode 100644
index 0000000000..939ff3ae7c
--- /dev/null
+++ 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/controllers/PlanController.java
@@ -0,0 +1,91 @@
+package org.apache.airavata.agent.connection.service.controllers;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.airavata.agent.connection.service.UserContext;
+import org.apache.airavata.agent.connection.service.db.entity.Plan;
+import org.apache.airavata.agent.connection.service.handlers.PlanHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/api/v1/plan")
+public class PlanController {
+
+    private final static Logger logger = 
LoggerFactory.getLogger(PlanController.class);
+
+    private final PlanHandler planHandler;
+    private final ObjectMapper objectMapper;
+
+    public PlanController(PlanHandler planHandler, ObjectMapper objectMapper) {
+        this.planHandler = planHandler;
+        this.objectMapper = objectMapper;
+    }
+
+    @PostMapping
+    public ResponseEntity<Plan> savePlan(@RequestBody JsonNode incomingData) {
+        try {
+            String planId = incomingData.get("id").asText();
+
+            String dataAsString = 
objectMapper.writeValueAsString(incomingData);
+
+            Plan plan = new Plan();
+            plan.setId(planId);
+            plan.setUserId(UserContext.username());
+            plan.setGatewayId(UserContext.gatewayId());
+            plan.setData(dataAsString);
+
+            Plan savedPlan = planHandler.savePlan(plan);
+            return ResponseEntity.ok(savedPlan);
+
+        } catch (Exception e) {
+            logger.error("Error while generating the plan data string", e);
+            return 
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
+        }
+    }
+
+    @GetMapping("/user")
+    public ResponseEntity<List<Plan>> getPlansByUserId() {
+        String userId = UserContext.username();
+        List<Plan> plans = planHandler.getAllPlansByUserId(userId);
+        return ResponseEntity.ok(plans);
+    }
+
+    @GetMapping("/{planId}")
+    public ResponseEntity<Plan> getPlanById(@PathVariable String planId) {
+        Plan plan = planHandler.getPlanById(planId);
+        return ResponseEntity.ok(plan);
+    }
+
+    @PutMapping("/{planId}")
+    public ResponseEntity<Plan> updatePlan(@PathVariable String planId, 
@RequestBody JsonNode incomingData) {
+        try {
+            Plan existingPlan = planHandler.getPlanById(planId);
+
+            if (existingPlan == null) {
+                logger.error("Couldn't find a plan with id: {} to update", 
planId);
+                return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
+            }
+
+            String dataAsString = 
objectMapper.writeValueAsString(incomingData);
+            existingPlan.setData(dataAsString);
+            Plan updatedPlan = planHandler.savePlan(existingPlan);
+            return ResponseEntity.ok(updatedPlan);
+
+        } catch (Exception e) {
+            logger.error("Error while generating the plant data to update the 
plan with the id: {}", planId);
+            return 
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
+        }
+    }
+}
diff --git 
a/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/entity/Plan.java
 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/entity/Plan.java
new file mode 100644
index 0000000000..14b716cad6
--- /dev/null
+++ 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/entity/Plan.java
@@ -0,0 +1,55 @@
+package org.apache.airavata.agent.connection.service.db.entity;
+
+import jakarta.persistence.Column;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Id;
+
+@Entity(name = "PLAN")
+public class Plan {
+
+    @Id
+    @Column(name = "PLAN_ID", nullable = false)
+    private String id;
+
+    @Column(name = "USER_ID", nullable = false)
+    private String userId;
+
+    @Column(name = "GATEWAY_ID", nullable = false)
+    private String gatewayId;
+
+    @Column(name = "DATA", columnDefinition = "TEXT")
+    private String data;
+
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getUserId() {
+        return userId;
+    }
+
+    public void setUserId(String userId) {
+        this.userId = userId;
+    }
+
+    public String getGatewayId() {
+        return gatewayId;
+    }
+
+    public void setGatewayId(String gatewayId) {
+        this.gatewayId = gatewayId;
+    }
+
+    public String getData() {
+        return data;
+    }
+
+    public void setData(String data) {
+        this.data = data;
+    }
+}
diff --git 
a/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/repo/PlanRepo.java
 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/repo/PlanRepo.java
new file mode 100644
index 0000000000..d8dc60c445
--- /dev/null
+++ 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/db/repo/PlanRepo.java
@@ -0,0 +1,13 @@
+package org.apache.airavata.agent.connection.service.db.repo;
+
+import org.apache.airavata.agent.connection.service.db.entity.Plan;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface PlanRepo extends JpaRepository<Plan, String> {
+
+    List<Plan> findByUserId(String userId);
+}
diff --git 
a/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/handlers/PlanHandler.java
 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/handlers/PlanHandler.java
new file mode 100644
index 0000000000..283196a660
--- /dev/null
+++ 
b/modules/agent-framework/agent-service/src/main/java/org/apache/airavata/agent/connection/service/handlers/PlanHandler.java
@@ -0,0 +1,35 @@
+package org.apache.airavata.agent.connection.service.handlers;
+
+import org.apache.airavata.agent.connection.service.db.entity.Plan;
+import org.apache.airavata.agent.connection.service.db.repo.PlanRepo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class PlanHandler {
+
+    private final static Logger logger = 
LoggerFactory.getLogger(PlanHandler.class);
+
+    private final PlanRepo planRepo;
+
+    public PlanHandler(PlanRepo planRepo) {
+        this.planRepo = planRepo;
+    }
+
+    public Plan savePlan(Plan plan) {
+        Plan savedPlan = planRepo.save(plan);
+        logger.info("Created the plan with the id: {}", plan.getId());
+        return savedPlan;
+    }
+
+    public List<Plan> getAllPlansByUserId(String userId) {
+        return planRepo.findByUserId(userId);
+    }
+
+    public Plan getPlanById(String planId) {
+        return planRepo.findById(planId).orElseThrow(() -> new 
RuntimeException("Plan not found: " + planId));
+    }
+}

Reply via email to