2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.workflow.controllers;
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;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
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;
65 import com.fasterxml.jackson.databind.DeserializationFeature;
66 import com.fasterxml.jackson.databind.JsonNode;
67 import com.fasterxml.jackson.databind.ObjectMapper;
70 * Created by Ikram on 02/15/2016.
74 public class WorkflowController extends RestrictedBaseController {
76 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowController.class);
79 private WorkflowService workflowService;
81 @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
82 public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
85 ObjectMapper mapper = new ObjectMapper();
86 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87 JsonNode root = mapper.readTree(request.getReader());
89 WorkflowSchedule domainCronJobData = new WorkflowSchedule();
90 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
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(e.getMessage());
111 @RequestMapping(value = { "workflows/list" }, method = RequestMethod.GET, produces = "application/json")
113 public String getWorkflowList() {
114 ObjectMapper mapper = new ObjectMapper();
115 List<Workflow> workflows = workflowService.getAllWorkflows();
116 List<WorkflowLite> workflowLites = new ArrayList<>();
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());
135 workflowLites.add(wfl);
138 return mapper.writeValueAsString(workflowLites);
139 } catch (Exception e) {
140 logger.error(EELFLoggerDelegate.errorLogger, "getWorkflowList failed", e);
145 @RequestMapping(value = "workflows/addWorkflow", method = RequestMethod.POST, consumes = "application/json")
147 public Workflow addWorkflow(@RequestBody Workflow workflow, HttpServletRequest request) {
148 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
149 return workflowService.addWorkflow(workflow, loginId);
152 @RequestMapping(value = "workflows/editWorkflow", method = RequestMethod.POST, consumes = "application/json")
154 public Workflow editWorkflow(@RequestBody WorkflowLite workflow, HttpServletRequest request) {
155 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
156 return workflowService.editWorkflow(workflow, loginId);
159 @RequestMapping(value = { "workflows/removeWorkflow" }, method = RequestMethod.POST, consumes = "application/json")
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}";
168 @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
170 public void removeAllWorkflows() {
171 throw new UnsupportedOperationException();
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);