LanguageController tests up
[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.service.fn.FnLanguageService;
51 import org.onap.portal.service.fn.FnUserService;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.http.MediaType;
56 import org.springframework.web.bind.annotation.GetMapping;
57 import org.springframework.web.bind.annotation.PathVariable;
58 import org.springframework.web.bind.annotation.PostMapping;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RestController;
62
63 @RestController
64 @RequestMapping("/auxapi")
65 public class LanguageController {
66
67        private static final Logger LOGGER = LoggerFactory.getLogger(LanguageController.class);
68
69        private final FnLanguageService languageService;
70        private final FnUserService fnUserService;
71
72        @Autowired
73        public LanguageController(final FnLanguageService languageService,
74                final FnUserService fnUserService) {
75               this.languageService = languageService;
76               this.fnUserService = fnUserService;
77        }
78
79        @GetMapping(value = "/language", produces = MediaType.APPLICATION_JSON_VALUE)
80        public List<FnLanguage> getLanguageList(final Principal principal) {
81               return languageService.getLanguages(principal);
82        }
83
84        @PostMapping(value = "/languageSetting/user/{loginId}")
85        public PortalRestResponse<String> setUpUserLanguage(Principal principal, @RequestBody FnLanguage fnLanguage,
86                @PathVariable("loginId") Long userId) {
87               PortalRestResponse<String> response = new PortalRestResponse<>();
88               try {
89                      if (fnUserService.getUser(userId).isPresent()) {
90                             FnUser user = fnUserService.getUser(userId).get();
91                             user.setLanguageId(fnLanguage);
92                             fnUserService.saveFnUser(principal, user);
93                      }
94                      response.setMessage("SUCCESS");
95                      response.setStatus(PortalRestStatusEnum.OK);
96               } catch (Exception e) {
97                      response.setMessage("FAILURE");
98                      response.setResponse(e.getMessage());
99                      response.setStatus(PortalRestStatusEnum.ERROR);
100                      return response;
101               }
102               return response;
103        }
104
105        @GetMapping(value = "/languageSetting/user/{loginId}", produces = MediaType.APPLICATION_JSON_VALUE)
106        public FnLanguage getUserLanguage(@PathVariable("loginId") final Long loginId) {
107               if (fnUserService.getUser(loginId).isPresent()) {
108                      return Optional.of(fnUserService.getUser(loginId).get().getLanguageId()).orElse(new FnLanguage());
109               }
110               return new FnLanguage();
111        }
112
113        @PostMapping(value = "/language", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
114        public PortalRestResponse<String> saveLanguage(final Principal principal,
115                @RequestBody final FnLanguage fnLanguage) {
116               PortalRestResponse<String> response = new PortalRestResponse<>();
117               try {
118                      response.setMessage("SUCCESS");
119                      response.setResponse(languageService.save(principal, fnLanguage).toString());
120                      response.setStatus(PortalRestStatusEnum.OK);
121               } catch (Exception e) {
122                      response.setMessage("FAILURE");
123                      response.setResponse(e.getMessage());
124                      response.setStatus(PortalRestStatusEnum.ERROR);
125                      return response;
126               }
127               return response;
128        }
129
130 }