Replace ecomp references
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / service / UserServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.net.HttpURLConnection;
44 import java.net.URL;
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.onap.portalapp.portal.domain.EPUser;
51 import org.onap.portalapp.portal.service.UserService;
52 import org.onap.portalapp.portal.service.UserServiceImpl;
53 import org.onap.portalapp.portal.utils.EPSystemProperties;
54 import org.onap.portalsdk.core.FusionObject.Utilities;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
57 import org.onap.portalsdk.core.service.DataAccessService;
58 import org.onap.portalsdk.core.util.SystemProperties;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.stereotype.Service;
61 import org.springframework.transaction.annotation.Transactional;
62
63 @Service("userService")
64 @Transactional
65 public class UserServiceImpl implements UserService {
66
67         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
68
69         @Autowired
70         private DataAccessService dataAccessService;
71
72         public DataAccessService getDataAccessService() {
73                 return dataAccessService;
74         }
75
76         public void setDataAccessService(DataAccessService dataAccessService) {
77                 this.dataAccessService = dataAccessService;
78         }
79
80         @SuppressWarnings("rawtypes")
81         @Override
82         public List getUserByUserId(String userId) {
83
84                 if (SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
85                         List<EPUser> users = new ArrayList<EPUser>();
86                         List<EPUser> filterdUsers = new ArrayList<EPUser>();
87                         BufferedReader in = null;
88                         HttpURLConnection con = null;
89                         try {
90                                 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
91                                 URL obj = new URL(url);
92
93                                 con = (HttpURLConnection) obj.openConnection();
94
95                                 // optional default is GET
96                                 con.setRequestMethod("GET");
97                                 con.setConnectTimeout(3000);
98                                 con.setReadTimeout(8000);
99
100                                 StringBuffer response = new StringBuffer();
101
102                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
103                                 String inputLine;
104                                 while ((inputLine = in.readLine()) != null)
105                                         response.append(inputLine);
106                                 JSONObject jObject = new JSONObject(response.toString()); // json
107                                 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
108                                 for (int i = 0; i < jsonUsers.length(); i++) {
109                                         JSONObject eachObject = jsonUsers.getJSONObject(i);
110                                         EPUser eachUser = new EPUser();
111                                         eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
112                                         eachUser.setFirstName(eachObject.get("givenName").toString());
113                                         eachUser.setLastName(eachObject.get("familyName").toString());
114                                         eachUser.setEmail(eachObject.get("email").toString());
115                                         users.add(eachUser);
116                                 }
117
118                                 for (int i = 0; i < users.size(); i++) {
119
120                                         if (Utilities.nvl(userId).length() > 0) {
121                                                 if (!userId.equalsIgnoreCase(users.get(i).getOrgUserId())) {
122                                                         continue;
123                                                 }
124                                         }
125                                         filterdUsers.add(users.get(i));
126
127                                 }
128
129                         } catch (Exception e) {
130                                 logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId failed", e);
131                         } finally {
132                                 try {
133                                         in.close();
134                                         con.disconnect();
135                                 } catch (IOException e) {
136                                         logger.error(EELFLoggerDelegate.errorLogger, "getUserByUserId 2 failed", e);
137                                 }
138                         }
139
140                         return filterdUsers;
141
142                 } else {
143
144                         List list = null;
145                         StringBuffer criteria = new StringBuffer();
146                         criteria.append(" where org_user_id = '").append(userId).append("'");
147                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
148                         return (list == null || list.size() == 0) ? null : list;
149
150                 }
151
152         }
153
154         @SuppressWarnings("rawtypes")
155         @Override
156         public List getUserByFirstLastName(String firstName, String lastName) {
157
158                 if (!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")) {
159
160                         List list = null;
161                         StringBuffer criteria = new StringBuffer();
162                         if (firstName != null)
163                                 criteria.append(" where first_name = '").append(firstName).append("'");
164                         if (lastName != null)
165                                 criteria.append(" where last_name = '").append(lastName).append("'");
166                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
167                         return (list == null || list.size() == 0) ? null : list;
168
169                 } else {
170
171                         List<EPUser> users = new ArrayList<EPUser>();
172                         List<EPUser> filterdUsers = new ArrayList<EPUser>();
173                         BufferedReader in = null;
174                         HttpURLConnection con = null;
175                         try {
176                                 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
177                                 URL obj = new URL(url);
178
179                                 con = (HttpURLConnection) obj.openConnection();
180
181                                 // optional default is GET
182                                 con.setRequestMethod("GET");
183                                 con.setConnectTimeout(3000);
184                                 con.setReadTimeout(8000);
185
186                                 StringBuffer response = new StringBuffer();
187
188                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
189                                 String inputLine;
190                                 while ((inputLine = in.readLine()) != null)
191                                         response.append(inputLine);
192                                 JSONObject jObject = new JSONObject(response.toString()); // json
193                                 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
194                                 for (int i = 0; i < jsonUsers.length(); i++) {
195                                         JSONObject eachObject = jsonUsers.getJSONObject(i);
196                                         EPUser eachUser = new EPUser();
197                                         eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
198                                         eachUser.setFirstName(eachObject.get("givenName").toString());
199                                         eachUser.setLastName(eachObject.get("familyName").toString());
200                                         eachUser.setEmail(eachObject.get("email").toString());
201                                         users.add(eachUser);
202                                 }
203
204                                 for (int i = 0; i < users.size(); i++) {
205
206                                         if (Utilities.nvl(firstName).length() > 0) {
207                                                 if (!firstName.equalsIgnoreCase(users.get(i).getFirstName())) {
208                                                         continue;
209                                                 }
210                                         }
211                                         if (Utilities.nvl(lastName).length() > 0) {
212                                                 if (!lastName.equalsIgnoreCase(users.get(i).getLastName())) {
213                                                         continue;
214                                                 }
215                                         }
216
217                                         filterdUsers.add(users.get(i));
218
219                                 }
220
221                         } catch (Exception e) {
222                                 logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName failed", e);
223                         } finally {
224                                 try {
225                                         in.close();
226                                         con.disconnect();
227                                 } catch (IOException e) {
228                                         logger.error(EELFLoggerDelegate.errorLogger, "getUserByFirstLastName 2 failed", e);
229                                 }
230                         }
231
232                         return filterdUsers;
233                 }
234
235         }
236
237         public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception {
238                 try {
239                         List list = null;
240                         StringBuffer criteria = new StringBuffer();
241                         criteria.append(" where org_user_id = '").append(newUser.getLoginId()).append("'");
242                         list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
243                         if (list == null || list.size() == 0) {
244                                 newUser.setActive(true);
245                                 newUser.setOrgUserId(newUser.getLoginId());
246                                 newUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
247                                 getDataAccessService().saveDomainObject(newUser, null);
248                         } else {
249                                 if (checkDuplicate.equals("Yes")) {
250                                         // userId already exist in database
251                                         return "Record already exist";
252                                 } else {
253
254                                         EPUser oldUser = (EPUser) list.get(0);
255                                         oldUser.setFirstName(newUser.getFirstName());
256                                         oldUser.setLastName(newUser.getLastName());
257                                         oldUser.setMiddleInitial(newUser.getMiddleInitial());
258                                         if (!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
259                                                 oldUser.setLoginPwd(CipherUtil.encryptPKC(newUser.getLoginPwd()));
260                                         else
261                                                 oldUser.setLoginPwd(newUser.getLoginPwd());
262                                         getDataAccessService().saveDomainObject(oldUser, null);
263
264                                 }
265
266                         }
267
268                 } catch (Exception e) {
269                         logger.error(EELFLoggerDelegate.errorLogger, "saveNewUser failed", e);
270                         throw new Exception(e);
271                 }
272                 return "success";
273         };
274
275 }