ca2b38858daf7f9d5f9793a778eaaf6298429e20
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.workflow.controllers;
21
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;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
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;
46
47 import com.fasterxml.jackson.databind.DeserializationFeature;
48 import com.fasterxml.jackson.databind.JsonNode;
49 import com.fasterxml.jackson.databind.ObjectMapper;
50
51 /**
52  * Created by Ikram on 02/15/2016.
53  */
54 @Controller
55 @RequestMapping("/")
56 public class WorkflowController extends RestrictedBaseController {
57
58         @Autowired
59         private WorkflowService workflowService;
60         // @Autowired
61         // private CronJobService cronJobService;
62
63         @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
64         public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
65
66                 try {
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());
71
72                         WorkflowSchedule domainCronJobData = new WorkflowSchedule();
73                         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
74
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());
79                         domainCronJobData
80                                         .setStartDateTime(dateFormat.parse(root.get("cronJobDataObj").get("startDateTime").textValue()));
81                         domainCronJobData
82                                         .setEndDateTime(dateFormat.parse(root.get("cronJobDataObj").get("endDateTime").textValue()));
83                         domainCronJobData.setRecurrence(root.get("cronJobDataObj").get("recurrence").textValue());
84
85                         workflowService.saveCronJob(domainCronJobData);
86
87                         // response.getWriter().write("hello".toString());
88
89                 } catch (Exception e) {
90                         response.setCharacterEncoding("UTF-8");
91                         request.setCharacterEncoding("UTF-8");
92                         PrintWriter out = response.getWriter();
93                         out.write(e.getMessage());
94
95                 }
96
97         }
98
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>();
104
105                 try {
106
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());
122
123                                 workflowLites.add(wfl);
124                         }
125
126                         return mapper.writeValueAsString(workflowLites);
127                 } catch (Exception e) {
128                         // TODO Auto-generated catch block
129                         e.printStackTrace();
130                 }
131                 return "";
132         }
133
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);
139         }
140
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);
146         }
147
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) {
153
154                 // System.out.println("Removing ... " + workflowId);
155
156                 workflowService.deleteWorkflow(workflowId);
157
158                 response.setCharacterEncoding("UTF-8");
159                 response.setContentType("application / json");
160                 PrintWriter out = null;
161                 try {
162                         request.setCharacterEncoding("UTF-8");
163                         out = response.getWriter();
164                 } catch (Exception e) {
165                         e.printStackTrace();
166                 }
167
168                 JSONObject j = new JSONObject("{removed: 123}");
169                 out.write(j.toString());
170
171         }
172
173         @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
174         public @ResponseBody void removeAllWorkflows() {
175                 // workflowService.deleteAll();
176         }
177
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);
182         }
183 }