2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.workflow.controllers;
22 import java.io.PrintWriter;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
32 import org.json.JSONObject;
33 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
34 import org.openecomp.portalsdk.core.domain.User;
35 import org.openecomp.portalsdk.workflow.domain.WorkflowSchedule;
36 import org.openecomp.portalsdk.workflow.models.Workflow;
37 import org.openecomp.portalsdk.workflow.models.WorkflowLite;
38 import org.openecomp.portalsdk.workflow.services.WorkflowService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.bind.annotation.ResponseBody;
45 import org.springframework.web.servlet.ModelAndView;
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
52 * Created by Ikram on 02/15/2016.
56 public class WorkflowController extends RestrictedBaseController {
59 private WorkflowService workflowService;
61 // private CronJobService cronJobService;
63 @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
64 public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
67 // System.out.println("inside save cron job...");
68 ObjectMapper mapper = new ObjectMapper();
69 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
70 JsonNode root = mapper.readTree(request.getReader());
72 WorkflowSchedule domainCronJobData = new WorkflowSchedule();
73 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
75 domainCronJobData.setCronDetails(root.get("cronJobDataObj").get("startDateTime_CRON").textValue());
76 domainCronJobData.setWorkflowKey(root.get("cronJobDataObj").get("workflowKey").textValue());
77 domainCronJobData.setArguments(root.get("cronJobDataObj").get("workflow_arguments").textValue());
78 domainCronJobData.setServerUrl(root.get("cronJobDataObj").get("workflow_server_url").textValue());
80 .setStartDateTime(dateFormat.parse(root.get("cronJobDataObj").get("startDateTime").textValue()));
82 .setEndDateTime(dateFormat.parse(root.get("cronJobDataObj").get("endDateTime").textValue()));
83 domainCronJobData.setRecurrence(root.get("cronJobDataObj").get("recurrence").textValue());
85 workflowService.saveCronJob(domainCronJobData);
87 // response.getWriter().write("hello".toString());
89 } catch (Exception e) {
90 response.setCharacterEncoding("UTF-8");
91 request.setCharacterEncoding("UTF-8");
92 PrintWriter out = response.getWriter();
93 out.write(e.getMessage());
99 @RequestMapping(value = { "workflows/list" }, method = RequestMethod.GET, produces = "application/json")
100 public @ResponseBody String getWorkflowList() {
101 ObjectMapper mapper = new ObjectMapper();
102 List<Workflow> workflows = workflowService.getAllWorkflows();
103 List<WorkflowLite> workflowLites = new ArrayList<WorkflowLite>();
107 for (Workflow workflow : workflows) {
108 WorkflowLite wfl = new WorkflowLite();
109 wfl.setId(workflow.getId());
110 wfl.setName(workflow.getName());
111 wfl.setDescription(workflow.getDescription());
112 wfl.setActive(workflow.getActive() == null ? "" : workflow.getActive().toString());
113 wfl.setCreated(workflow.getCreated() == null ? "" : workflow.getCreated().toString());
114 wfl.setCreatedBy(workflow.getCreatedBy() == null ? ""
115 : workflow.getCreatedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
116 wfl.setModifiedBy(workflow.getModifiedBy() == null ? ""
117 : workflow.getModifiedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
118 wfl.setLastUpdated(workflow.getLastUpdated() == null ? "" : workflow.getLastUpdated().toString());
119 wfl.setWorkflowKey(workflow.getWorkflowKey());
120 wfl.setRunLink(workflow.getRunLink());
121 wfl.setSuspendLink(workflow.getSuspendLink());
123 workflowLites.add(wfl);
126 return mapper.writeValueAsString(workflowLites);
127 } catch (Exception e) {
128 // TODO Auto-generated catch block
134 @RequestMapping(value = "workflows/addWorkflow", method = RequestMethod.POST, consumes = "application/json")
135 public @ResponseBody Workflow addWorkflow(@RequestBody Workflow workflow, HttpServletRequest request,
136 HttpServletResponse response) {
137 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
138 return workflowService.addWorkflow(workflow, loginId);
141 @RequestMapping(value = "workflows/editWorkflow", method = RequestMethod.POST, consumes = "application/json")
142 public @ResponseBody Workflow editWorkflow(@RequestBody WorkflowLite workflow, HttpServletRequest request,
143 HttpServletResponse response) {
144 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
145 return workflowService.editWorkflow(workflow, loginId);
148 // @RequestMapping(value = "workflows/removeWorkflow", method =
149 // RequestMethod.DELETE)
150 @RequestMapping(value = { "workflows/removeWorkflow" }, method = RequestMethod.POST, consumes = "application/json")
151 public @ResponseBody void removeWorkflow(@RequestBody Long workflowId, HttpServletRequest request,
152 HttpServletResponse response) {
154 // System.out.println("Removing ... " + workflowId);
156 workflowService.deleteWorkflow(workflowId);
158 response.setCharacterEncoding("UTF-8");
159 response.setContentType("application / json");
160 PrintWriter out = null;
162 request.setCharacterEncoding("UTF-8");
163 out = response.getWriter();
164 } catch (Exception e) {
168 JSONObject j = new JSONObject("{removed: 123}");
169 out.write(j.toString());
173 @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
174 public @ResponseBody void removeAllWorkflows() {
175 // workflowService.deleteAll();
178 @RequestMapping(value = { "/workflows" }, method = RequestMethod.GET)
179 public ModelAndView getWorkflowPartialPage() {
180 Map<String, Object> model = new HashMap<String, Object>();
181 return new ModelAndView(getViewName(), "workflows", model);