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.json.JSONObject;
51 import org.onap.portalsdk.core.controller.RestrictedBaseController;
52 import org.onap.portalsdk.core.domain.User;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.workflow.domain.WorkflowSchedule;
55 import org.onap.portalsdk.workflow.models.Workflow;
56 import org.onap.portalsdk.workflow.models.WorkflowLite;
57 import org.onap.portalsdk.workflow.services.WorkflowService;
58 import org.springframework.beans.factory.annotation.Autowired;
59 import org.springframework.stereotype.Controller;
60 import org.springframework.web.bind.annotation.RequestBody;
61 import org.springframework.web.bind.annotation.RequestMapping;
62 import org.springframework.web.bind.annotation.RequestMethod;
63 import org.springframework.web.bind.annotation.ResponseBody;
64 import org.springframework.web.servlet.ModelAndView;
66 import com.fasterxml.jackson.databind.DeserializationFeature;
67 import com.fasterxml.jackson.databind.JsonNode;
68 import com.fasterxml.jackson.databind.ObjectMapper;
71 * Created by Ikram on 02/15/2016.
75 public class WorkflowController extends RestrictedBaseController {
77 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowController.class);
80 private WorkflowService workflowService;
82 // private CronJobService cronJobService;
84 @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
85 public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
88 // System.out.println("inside save cron job...");
89 ObjectMapper mapper = new ObjectMapper();
90 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
91 JsonNode root = mapper.readTree(request.getReader());
93 WorkflowSchedule domainCronJobData = new WorkflowSchedule();
94 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
96 domainCronJobData.setCronDetails(root.get("cronJobDataObj").get("startDateTime_CRON").textValue());
97 domainCronJobData.setWorkflowKey(root.get("cronJobDataObj").get("workflowKey").textValue());
98 domainCronJobData.setArguments(root.get("cronJobDataObj").get("workflow_arguments").textValue());
99 domainCronJobData.setServerUrl(root.get("cronJobDataObj").get("workflow_server_url").textValue());
101 .setStartDateTime(dateFormat.parse(root.get("cronJobDataObj").get("startDateTime").textValue()));
103 .setEndDateTime(dateFormat.parse(root.get("cronJobDataObj").get("endDateTime").textValue()));
104 domainCronJobData.setRecurrence(root.get("cronJobDataObj").get("recurrence").textValue());
106 workflowService.saveCronJob(domainCronJobData);
108 // response.getWriter().write("hello".toString());
110 } catch (Exception e) {
111 response.setCharacterEncoding("UTF-8");
112 request.setCharacterEncoding("UTF-8");
113 PrintWriter out = response.getWriter();
114 out.write(e.getMessage());
120 @RequestMapping(value = { "workflows/list" }, method = RequestMethod.GET, produces = "application/json")
121 public @ResponseBody String getWorkflowList() {
122 ObjectMapper mapper = new ObjectMapper();
123 List<Workflow> workflows = workflowService.getAllWorkflows();
124 List<WorkflowLite> workflowLites = new ArrayList<WorkflowLite>();
128 for (Workflow workflow : workflows) {
129 WorkflowLite wfl = new WorkflowLite();
130 wfl.setId(workflow.getId());
131 wfl.setName(workflow.getName());
132 wfl.setDescription(workflow.getDescription());
133 wfl.setActive(workflow.getActive() == null ? "" : workflow.getActive().toString());
134 wfl.setCreated(workflow.getCreated() == null ? "" : workflow.getCreated().toString());
135 wfl.setCreatedBy(workflow.getCreatedBy() == null ? ""
136 : workflow.getCreatedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
137 wfl.setModifiedBy(workflow.getModifiedBy() == null ? ""
138 : workflow.getModifiedBy().getFirstName() + " " + workflow.getCreatedBy().getLastName());
139 wfl.setLastUpdated(workflow.getLastUpdated() == null ? "" : workflow.getLastUpdated().toString());
140 wfl.setWorkflowKey(workflow.getWorkflowKey());
141 wfl.setRunLink(workflow.getRunLink());
142 wfl.setSuspendLink(workflow.getSuspendLink());
144 workflowLites.add(wfl);
147 return mapper.writeValueAsString(workflowLites);
148 } catch (Exception e) {
149 // TODO Auto-generated catch block
155 @RequestMapping(value = "workflows/addWorkflow", method = RequestMethod.POST, consumes = "application/json")
156 public @ResponseBody Workflow addWorkflow(@RequestBody Workflow workflow, HttpServletRequest request,
157 HttpServletResponse response) {
158 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
159 return workflowService.addWorkflow(workflow, loginId);
162 @RequestMapping(value = "workflows/editWorkflow", method = RequestMethod.POST, consumes = "application/json")
163 public @ResponseBody Workflow editWorkflow(@RequestBody WorkflowLite workflow, HttpServletRequest request,
164 HttpServletResponse response) {
165 String loginId = ((User) (request.getSession().getAttribute("user"))).getLoginId();
166 return workflowService.editWorkflow(workflow, loginId);
169 // @RequestMapping(value = "workflows/removeWorkflow", method =
170 // RequestMethod.DELETE)
171 @RequestMapping(value = { "workflows/removeWorkflow" }, method = RequestMethod.POST, consumes = "application/json")
172 public @ResponseBody void removeWorkflow(@RequestBody Long workflowId, HttpServletRequest request,
173 HttpServletResponse response) {
175 // System.out.println("Removing ... " + workflowId);
177 workflowService.deleteWorkflow(workflowId);
179 response.setCharacterEncoding("UTF-8");
180 response.setContentType("application / json");
181 PrintWriter out = null;
183 request.setCharacterEncoding("UTF-8");
184 out = response.getWriter();
185 } catch (Exception e) {
186 logger.error(EELFLoggerDelegate.errorLogger, "removeWorkflow failed", e);
189 JSONObject j = new JSONObject("{removed: 123}");
190 out.write(j.toString());
194 @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
195 public @ResponseBody void removeAllWorkflows() {
196 // workflowService.deleteAll();
199 @RequestMapping(value = { "/workflows" }, method = RequestMethod.GET)
200 public ModelAndView getWorkflowPartialPage() {
201 Map<String, Object> model = new HashMap<String, Object>();
202 return new ModelAndView(getViewName(), "workflows", model);