[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / controller / AppsOSController.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
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.portalapp.portal.controller;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.json.JSONObject;
28 import org.openecomp.portalapp.portal.domain.EPUser;
29 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
30 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
31 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
32 import org.openecomp.portalapp.portal.service.AdminRolesService;
33 import org.openecomp.portalapp.portal.service.EPAppService;
34 import org.openecomp.portalapp.portal.service.PersUserAppService;
35 import org.openecomp.portalapp.portal.service.UserService;
36 import org.openecomp.portalapp.util.EPUserUtils;
37 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.web.bind.annotation.PathVariable;
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.RestController;
45
46 @RestController
47 @org.springframework.context.annotation.Configuration
48 @EnableAspectJAutoProxy
49 @EPAuditLog
50 public class AppsOSController extends AppsController {
51         
52         static final String FAILURE = "failure";
53         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsOSController.class);
54
55         @Autowired
56         AdminRolesService adminRolesService;
57         @Autowired
58         EPAppService appService;
59         @Autowired
60         PersUserAppService persUserAppService;
61         @Autowired
62         UserService userService;
63
64         
65         
66         /**
67          * Create new application's contact us details.
68          * 
69          * @param contactUs
70          * @return
71          */
72         @RequestMapping(value = "/portalApi/saveNewUser", method = RequestMethod.POST, produces = "application/json")
73         public PortalRestResponse<String> saveNewUser(HttpServletRequest request,@RequestBody EPUser newUser) {
74                 EPUser user = EPUserUtils.getUserSession(request);
75                 if (newUser == null)
76                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
77                                         "New User cannot be null or empty");
78                 
79                 if (!(adminRolesService.isSuperAdmin(user) || adminRolesService.isAccountAdmin(user))){
80                         if(!user.getLoginId().equalsIgnoreCase(newUser.getLoginId()))
81                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, FAILURE,
82                                                 "UnAuthorized");
83                 }
84                         
85         String checkDuplicate = request.getParameter("isCheck");
86                 String saveNewUser = FAILURE;
87                 try {
88                         saveNewUser = userService.saveNewUser(newUser,checkDuplicate);
89                 } catch (Exception e) {
90                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, saveNewUser, e.getMessage());
91                 }
92                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, saveNewUser, "");
93         }
94         
95         @RequestMapping(value = { "/portalApi/currentUserProfile/{loginId}" }, method = RequestMethod.GET, produces = "application/json")
96         public String getCurrentUserProfile(HttpServletRequest request, @PathVariable("loginId") String loginId) {
97                 
98                 Map<String,String> map = new HashMap<String,String>();
99                 EPUser user = null;
100                 try {
101                          user = (EPUser) userService.getUserByUserId(loginId).get(0);
102                          map.put("firstName", user.getFirstName());
103                      map.put("lastName", user.getLastName());
104                      map.put("email", user.getEmail());
105                          map.put("loginId", user.getLoginId());
106                          map.put("loginPwd",user.getLoginPwd());
107                          map.put("middleInitial",user.getMiddleInitial());
108                 } catch (Exception e) {
109                         logger.error(EELFLoggerDelegate.errorLogger, "Failed to get user info", e);
110                 }
111
112                 JSONObject j = new JSONObject(map);;
113                 return j.toString();
114         }
115
116 }