2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 * https://creativecommons.org/licenses/by/4.0/
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.
36 * ============LICENSE_END============================================
41 package org.onap.portal.controller;
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;
67 @RequestMapping("/auxapi")
68 public class LanguageController {
70 private static final Logger LOGGER = LoggerFactory.getLogger(LanguageController.class);
72 private final FnLanguageService languageService;
73 private final FnUserService fnUserService;
75 private final FnUserMapper fnUserMapper;
76 private final FnLanguageMapper fnLanguageMapper;
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;
88 @GetMapping(value = "/language", produces = MediaType.APPLICATION_JSON_VALUE)
89 public List<FnLanguageDto> getLanguageList(final Principal principal) {
90 return fnLanguageMapper.fnLanguageListToDtoList(languageService.getLanguages(principal));
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);
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);
109 response.setMessage("FAILURE");
110 response.setResponse("User for id: " + userId + " do not exist");
111 response.setStatus(PortalRestStatusEnum.ERROR);
113 } catch (Exception e) {
114 LOGGER.error("Exception in setUpUserLanguage", e);
115 response.setMessage("FAILURE");
116 response.setResponse(e.toString());
117 response.setStatus(PortalRestStatusEnum.ERROR);
123 @GetMapping(value = "/languageSetting/user/{loginId}", produces = MediaType.APPLICATION_JSON_VALUE)
124 public FnLanguageDto getUserLanguage(final Principal principal, @PathVariable("loginId") final Long loginId) {
125 if (fnUserService.existById(loginId)) {
126 return fnLanguageMapper.fnLanguageToDto(Optional.of(fnUserService.getUser(loginId).get().getLanguageId()).orElse(new FnLanguage()));
128 return new FnLanguageDto();
131 @PostMapping(value = "/language", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
132 public PortalRestResponse<String> saveLanguage(final Principal principal,
133 @RequestBody final FnLanguage fnLanguage) {
134 PortalRestResponse<String> response = new PortalRestResponse<>();
136 response.setMessage("SUCCESS");
137 response.setResponse(languageService.save(principal, fnLanguage).toString());
138 response.setStatus(PortalRestStatusEnum.OK);
139 } catch (Exception e) {
140 LOGGER.error("Exception in saveLanguage", e);
141 response.setMessage("FAILURE");
142 response.setResponse(e.getMessage());
143 response.setStatus(PortalRestStatusEnum.ERROR);