2566868e7730aec4076e5cb95c40406ab53b6ae5
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.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;
65
66 import com.fasterxml.jackson.databind.DeserializationFeature;
67 import com.fasterxml.jackson.databind.JsonNode;
68 import com.fasterxml.jackson.databind.ObjectMapper;
69
70 /**
71  * Created by Ikram on 02/15/2016.
72  */
73 @Controller
74 @RequestMapping("/")
75 public class WorkflowController extends RestrictedBaseController {
76  
77         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WorkflowController.class);
78
79         @Autowired
80         private WorkflowService workflowService;
81         // @Autowired
82         // private CronJobService cronJobService;
83
84         @RequestMapping(value = { "workflows/saveCronJob" }, method = RequestMethod.POST)
85         public void saveCronJob(HttpServletRequest request, HttpServletResponse response) throws Exception {
86
87                 try {
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());
92
93                         WorkflowSchedule domainCronJobData = new WorkflowSchedule();
94                         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
95
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());
100                         domainCronJobData
101                                         .setStartDateTime(dateFormat.parse(root.get("cronJobDataObj").get("startDateTime").textValue()));
102                         domainCronJobData
103                                         .setEndDateTime(dateFormat.parse(root.get("cronJobDataObj").get("endDateTime").textValue()));
104                         domainCronJobData.setRecurrence(root.get("cronJobDataObj").get("recurrence").textValue());
105
106                         workflowService.saveCronJob(domainCronJobData);
107
108                         // response.getWriter().write("hello".toString());
109
110                 } catch (Exception e) {
111                         response.setCharacterEncoding("UTF-8");
112                         request.setCharacterEncoding("UTF-8");
113                         PrintWriter out = response.getWriter();
114                         out.write(e.getMessage());
115
116                 }
117
118         }
119
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>();
125
126                 try {
127
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());
143
144                                 workflowLites.add(wfl);
145                         }
146
147                         return mapper.writeValueAsString(workflowLites);
148                 } catch (Exception e) {
149                         // TODO Auto-generated catch block
150                         e.printStackTrace();
151                 }
152                 return "";
153         }
154
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);
160         }
161
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);
167         }
168
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) {
174
175                 // System.out.println("Removing ... " + workflowId);
176
177                 workflowService.deleteWorkflow(workflowId);
178
179                 response.setCharacterEncoding("UTF-8");
180                 response.setContentType("application / json");
181                 PrintWriter out = null;
182                 try {
183                         request.setCharacterEncoding("UTF-8");
184                         out = response.getWriter();
185                 } catch (Exception e) {
186                         logger.error(EELFLoggerDelegate.errorLogger, "removeWorkflow failed", e);
187                 }
188
189                 JSONObject j = new JSONObject("{removed: 123}");
190                 out.write(j.toString());
191
192         }
193
194         @RequestMapping(value = "workflows/removeAllWorkflows", method = RequestMethod.DELETE)
195         public @ResponseBody void removeAllWorkflows() {
196                 // workflowService.deleteAll();
197         }
198
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);
203         }
204 }