[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-os / 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.EPSystemProperties;
34 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
35 import org.openecomp.portalsdk.core.FusionObject.Utilities;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.service.DataAccessService;
38 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
43
44 @Service("userService")
45 @Transactional
46 public class UserServiceImpl implements UserService {
47         
48         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
49
50         @Autowired
51         private DataAccessService  dataAccessService;
52         
53         public DataAccessService getDataAccessService() {
54                 return dataAccessService;
55         }
56
57         public void setDataAccessService(DataAccessService dataAccessService) {
58                 this.dataAccessService = dataAccessService;
59         }
60         
61         @SuppressWarnings("rawtypes")
62         @Override
63         public List getUserByUserId(String userId) {
64                 
65                 
66                 if(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")){
67                         List<EPUser> users=new ArrayList<EPUser>();
68                     List<EPUser> filterdUsers=new ArrayList<EPUser>();
69                     BufferedReader in = null;
70                     HttpURLConnection con = null;
71                     try{
72                         String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
73                                 URL obj = new URL(url);
74
75                                 con = (HttpURLConnection) obj.openConnection();
76
77                                 // optional default is GET
78                                 con.setRequestMethod("GET");
79                                 con.setConnectTimeout(3000);
80                                 con.setReadTimeout(8000);
81
82                                 StringBuffer response = new StringBuffer();
83
84                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
85                                 String inputLine;
86                                 while ((inputLine = in.readLine()) != null)
87                                                                 response.append(inputLine);
88                             JSONObject jObject  = new JSONObject(response.toString()); // json
89                             JSONArray  jsonUsers = jObject.getJSONArray("response"); // get data 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());// getString("id"));
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                                  
102                                 if(Utilities.nvl(userId).length() > 0){
103                                                 if(!userId.equalsIgnoreCase(users.get(i).getOrgUserId())){                              
104                                                         continue;
105                                                 }
106                                         }
107                                         filterdUsers.add(users.get(i));
108                                  
109                              }
110                 
111                     }catch (Exception e){
112                                 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
113                     }finally{
114                         try {
115                                         in.close();
116                                         con.disconnect();
117                                 } catch (IOException e) {
118                                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
119                                 }
120                     }
121                     
122                         return filterdUsers;
123                         
124                 }else{
125                         
126                         List      list     = null;
127                 StringBuffer criteria = new StringBuffer();
128                 criteria.append(" where org_user_id = '").append(userId).append("'");
129                 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
130                 return (list == null || list.size() == 0) ? null : list;
131                 
132                 }
133                 
134         }
135
136         @SuppressWarnings("rawtypes")
137         @Override
138         public List getUserByFirstLastName(String firstName, String lastName) {
139                 
140                 if(!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")){
141                         
142                         List      list     = null;
143                     StringBuffer criteria = new StringBuffer();
144                     if(firstName!=null)
145                     criteria.append(" where first_name = '").append(firstName).append("'");
146                     if(lastName!=null)
147                     criteria.append(" where last_name = '").append(lastName).append("'");
148                     list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
149                     return (list == null || list.size() == 0) ? null : list;
150                 
151                 }else{
152                         
153                         List<EPUser> users=new ArrayList<EPUser>();
154                     List<EPUser> filterdUsers=new ArrayList<EPUser>();
155                     BufferedReader in = null;
156                     HttpURLConnection con = null;
157                     try{
158                         String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
159                                 URL obj = new URL(url);
160
161                                 con = (HttpURLConnection) obj.openConnection();
162
163                                 // optional default is GET
164                                 con.setRequestMethod("GET");
165                                 con.setConnectTimeout(3000);
166                                 con.setReadTimeout(8000);
167
168                                 StringBuffer response = new StringBuffer();
169                         
170                                 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
171                                 String inputLine;
172                                 while ((inputLine = in.readLine()) != null)
173                                                                 response.append(inputLine);
174                             JSONObject jObject  = new JSONObject(response.toString()); // json
175                             JSONArray  jsonUsers = jObject.getJSONArray("response"); // get data 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                                         
200                                         filterdUsers.add(users.get(i));
201                                  
202                              }
203                 
204                     }catch (Exception e){
205                                 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
206                     }finally{
207                         try {
208                                         in.close();
209                                         con.disconnect();
210                                 } catch (IOException e) {
211                                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
212                                 }
213                     }
214                     
215                         return filterdUsers;
216                 }
217                 
218         }
219         
220     public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception{
221                 
222                 try{ 
223                         
224                          List      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, "", e);
255                         throw new Exception(e);
256                 }
257                 return "success";
258         };
259
260 }