5aa556d16fcd4a1f473ccca2afce324ea919ba8a
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.workflow.controllers;
39
40 import java.io.PrintWriter;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49
50 import org.onap.portalsdk.core.controller.RestrictedBaseController;
51 import org.onap.portalsdk.core.domain.User;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.workflow.domain.WorkflowSchedule;
54 import org.onap.portalsdk.workflow.models.Workflow;
55 import org.onap.portalsdk.workflow.models.WorkflowLite;
56 import org.onap.portalsdk.workflow.services.WorkflowService;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.stereotype.Controller;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.bind.annotation.ResponseBody;
63 import org.springframework.web.servlet.ModelAndView;
64
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.fasterxml.jackson.databind.JsonNode;
67 import com.fasterxml.jackson.databind.ObjectMapper;
68
69 /**
70  * Created by Ikram on 02/15/2016.
71  */
72 @Controller
73 @RequestMapping("/")
74 public class WorkflowController extends RestrictedBaseController {
75
76         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowController.class);
77
78         @Autowired
79         private WorkflowService workflowService;
80
81         @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
82         public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
83
84                 try {
85                         ObjectMapper mapper = new ObjectMapper();
86                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87                         JsonNode root = mapper.readTree(request.getReader());
88
89                         WorkflowSchedule domainCronJobData = new WorkflowSchedule();
90                         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
91
92                         final JsonNode cronJobDataObj = root.get("cronJobDataObj");
93                         domainCronJobData.setCronDetails(cronJobDataObj.get("startDateTime_CRON").textValue());
94                         domainCronJobData.setWorkflowKey(cronJobDataObj.get("workflowKey").textValue());
95                         domainCronJobData.setArguments(cronJobDataObj.get("workflow_arguments").textValue());
96                         domainCronJobData.setServerUrl(cronJobDataObj.get("workflow_server_url").textValue());
97                         domainCronJobData.setStartDateTime(dateFormat.parse(cronJobDataObj.get("startDateTime").textValue()));
98                         domainCronJobData.setEndDateTime(dateFormat.parse(cronJobDataObj.get("endDateTime").textValue()));
99                         domainCronJobData.setRecurrence(cronJobDataObj.get("recurrence").textValue());
100                         workflowService.saveCronJob(domainCronJobData);
101                 } catch (Exception e) {
102                         logger.error(EELFLoggerDelegate.errorLogger, "saveCronJob failed", e);
103                         response.setCharacterEncoding("UTF-8");
104                         request.setCharacterEncoding("UTF-8");
105                         PrintWriter out = response.getWriter();
106                         out.write("An error occurred while saving the CronJob : saveCronJob()");
107                 }
108
109         }
110
111         @RequestMapping(value = { "workflows/list" }, method = RequestMethod.GET, produces = "application/json")
112         @ResponseBody
113         public String getWorkflowList() {
114                 ObjectMapper mapper = new ObjectMapper();
115                 List<Workflow> workflows = workflowService.getAllWorkflows();
116                 List<WorkflowLite> workflowLites = new ArrayList<>();
117
118                 try {
119                         for (Workflow workflow : workflows) {
120                                 WorkflowLite wfl = new WorkflowLite();
121                                 wfl.setId(workflow.getId());
122                                 wfl.setName(workflow.getName());
123                                 wfl.setDescription(workflow.getDescription());
124                                 wfl.setActive(workflow.getActive() == null ? "" : workflow.getActive().toString());
125                                 wfl.setCreated(workflow.getCreated() == null ? "" : workflow.getCreated().toString());
126                                 wfl.setCreatedBy(workflow.getCreatedBy() == null ? ""
127                                                 : workflow.getCreatedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
128                                 wfl.setModifiedBy(workflow.getModifiedBy() == null ? ""
129                                                 : workflow.getModifiedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
130                                 wfl.setLastUpdated(workflow.getLastUpdated() == null ? "" : workflow.getLastUpdated().toString());
131                                 wfl.setWorkflowKey(workflow.getWorkflowKey());
132                                 wfl.setRunLink(workflow.getRunLink());
133                                 wfl.setSuspendLink(workflow.getSuspendLink());
134
135                                 workflowLites.add(wfl);
136                         }
137
138                         return mapper.writeValueAsString(workflowLites);
139                 } catch (Exception e) {
140                         logger.error(EELFLoggerDelegate.errorLogger, "getWorkflowList failed", e);
141                 }
142                 return "";
143         }
144
145         @RequestMapping(value = "workflows/addWorkflow", method = RequestMethod.POST, consumes = "application/json")
146         @ResponseBody
147         public Workflow addWorkflow(@RequestBody Workflow workflow, HttpServletRequest request) {
148                 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
149                 return workflowService.addWorkflow(workflow, loginId);
150         }
151
152         @RequestMapping(value = "workflows/editWorkflow", method = RequestMethod.POST, consumes = "application/json")
153         @ResponseBody
154         public Workflow editWorkflow(@RequestBody WorkflowLite workflow, HttpServletRequest request) {
155                 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
156                 return workflowService.editWorkflow(workflow, loginId);
157         }
158
159         @RequestMapping(value = { "workflows/removeWorkflow" }, method = RequestMethod.POST, consumes = "application/json")
160         @ResponseBody
161         public String removeWorkflow(@RequestBody Long workflowId, HttpServletRequest request, HttpServletResponse response) {
162                 workflowService.deleteWorkflow(workflowId);
163                 response.setCharacterEncoding("UTF-8");
164                 response.setContentType("application / json");
165                 return "{removed: 123}";
166         }
167
168         @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
169         @ResponseBody
170         public void removeAllWorkflows() {
171                 throw new UnsupportedOperationException();
172         }
173
174         @RequestMapping(value = { "/workflows" }, method = RequestMethod.GET)
175         public ModelAndView getWorkflowPartialPage() {
176                 Map<String, Object> model = new HashMap<>();
177                 return new ModelAndView(getViewName(), "workflows", model);
178         }
179 }