90ea06804600d9311619b0c99ed4654f07c642a2
[portal.git] / portal-BE / src / main / java / org / onap / portal / controller / LanguageController.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import java.security.Principal;
44 import java.util.List;
45 import java.util.Optional;
46 import org.onap.portal.domain.db.fn.FnLanguage;
47 import org.onap.portal.domain.db.fn.FnUser;
48 import org.onap.portal.domain.dto.PortalRestResponse;
49 import org.onap.portal.domain.dto.PortalRestStatusEnum;
50 import org.onap.portal.domain.dto.fn.FnLanguageDto;
51 import org.onap.portal.domain.mapper.FnLanguageMapper;
52 import org.onap.portal.domain.mapper.FnUserMapper;
53 import org.onap.portal.service.fn.FnLanguageService;
54 import org.onap.portal.service.fn.FnUserService;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.http.MediaType;
59 import org.springframework.web.bind.annotation.GetMapping;
60 import org.springframework.web.bind.annotation.PathVariable;
61 import org.springframework.web.bind.annotation.PostMapping;
62 import org.springframework.web.bind.annotation.RequestBody;
63 import org.springframework.web.bind.annotation.RequestMapping;
64 import org.springframework.web.bind.annotation.RestController;
65
66 @RestController
67 @RequestMapping("/auxapi")
68 public class LanguageController {
69
70        private static final Logger LOGGER = LoggerFactory.getLogger(LanguageController.class);
71
72        private final FnLanguageService languageService;
73        private final FnUserService fnUserService;
74
75        private final FnUserMapper fnUserMapper;
76        private final FnLanguageMapper fnLanguageMapper;
77
78        @Autowired
79        public LanguageController(final FnLanguageService languageService,
80                final FnUserService fnUserService, final FnUserMapper fnUserMapper,
81                final FnLanguageMapper fnLanguageMapper) {
82               this.languageService = languageService;
83               this.fnUserService = fnUserService;
84               this.fnUserMapper = fnUserMapper;
85               this.fnLanguageMapper = fnLanguageMapper;
86        }
87
88        @GetMapping(value = "/language", produces = MediaType.APPLICATION_JSON_VALUE)
89        public List<FnLanguageDto> getLanguageList(final Principal principal) {
90               return fnLanguageMapper.fnLanguageListToDtoList(languageService.getLanguages(principal));
91        }
92
93        @PostMapping(value = "/languageSetting/user/{loginId}")
94        public PortalRestResponse<String> setUpUserLanguage(Principal principal, @RequestBody FnLanguage fnLanguage,
95                @PathVariable("loginId") Long userId) {
96               PortalRestResponse<String> response = new PortalRestResponse<>();
97               LOGGER.info("User " + principal.getName() + " try to setUpUserLanguage fnUser with id " + userId);
98               try {
99                      if (fnUserService.existById(userId)) {
100                             LOGGER.info("User " + principal.getName() + " found fnUser with id " + userId);
101                             @SuppressWarnings("OptionalGetWithoutIsPresent")
102                             FnUser user = fnUserService.getUser(userId).get();
103                             user.setLanguageId(fnLanguage);
104                             fnUserService.saveFnUser(principal, user);
105                             //response.setResponse(fnUserMapper.fnUserToFnUserDto(user).toString());
106                             response.setMessage("SUCCESS");
107                             response.setStatus(PortalRestStatusEnum.OK);
108                      } else {
109                             response.setMessage("FAILURE");
110                             response.setResponse("User for id: " + userId + " do not exist");
111                             response.setStatus(PortalRestStatusEnum.ERROR);
112                      }
113               } catch (Exception e) {
114                      response.setMessage("FAILURE");
115                      response.setResponse(e.toString());
116                      response.setStatus(PortalRestStatusEnum.ERROR);
117                      return response;
118               }
119               return response;
120        }
121
122        @GetMapping(value = "/languageSetting/user/{loginId}", produces = MediaType.APPLICATION_JSON_VALUE)
123        public FnLanguageDto getUserLanguage(final Principal principal, @PathVariable("loginId") final Long loginId) {
124               if (fnUserService.existById(loginId)) {
125                      return fnLanguageMapper.fnLanguageToDto(Optional.of(fnUserService.getUser(loginId).get().getLanguageId()).orElse(new FnLanguage()));
126               }
127               return new FnLanguageDto();
128        }
129
130        @PostMapping(value = "/language", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
131        public PortalRestResponse<String> saveLanguage(final Principal principal,
132                @RequestBody final FnLanguage fnLanguage) {
133               PortalRestResponse<String> response = new PortalRestResponse<>();
134               try {
135                      response.setMessage("SUCCESS");
136                      response.setResponse(languageService.save(principal, fnLanguage).toString());
137                      response.setStatus(PortalRestStatusEnum.OK);
138               } catch (Exception e) {
139                      response.setMessage("FAILURE");
140                      response.setResponse(e.getMessage());
141                      response.setStatus(PortalRestStatusEnum.ERROR);
142                      return response;
143               }
144               return response;
145        }
146
147 }