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