Statistics
| Branch: | Revision:

m2u-upass-core / src / my / com / upass / maybank / MaybankFacadeImpl.java @ 51:74be74b4d46a

History | View | Annotate | Download (11.3 KB)

1
package my.com.upass.maybank;
2

    
3
import java.text.ParseException;
4
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.HashMap;
7
import java.util.LinkedList;
8
import java.util.List;
9
import java.util.Map;
10

    
11
import javax.jws.WebParam;
12
import javax.jws.WebResult;
13
import javax.jws.WebService;
14

    
15
import my.com.upass.MinimalConstants;
16
import my.com.upass.UPassControllerV2;
17
import my.com.upass.UPassException;
18
import my.com.upass.generic.hibernate.GenericDAOHibernate;
19
import my.com.upass.generic.hibernate.HibernateUtils;
20
import my.com.upass.maybank.entities.IbccUser;
21
import my.com.upass.maybank.entities.Im2uUser;
22
import my.com.upass.maybank.entities.M2uUser;
23
import my.com.upass.maybank.entities.StockUser;
24
import my.com.upass.maybank.entities.TicketingUser;
25
import my.com.upass.maybank.entities.UserProfile;
26
import my.com.upass.pojo.ClientApp;
27
import my.com.upass.pojo.MinimalUserBean;
28
import my.com.upass.pojo.UserAppAccess;
29

    
30
import org.apache.log4j.Logger;
31
import org.hibernate.Session;
32

    
33
/**
34
 * @author Hadi
35
 * 
36
 */
37
@WebService(
38
                serviceName = "MaybankFacade",
39
                endpointInterface = "my.com.upass.maybank.MaybankFacade")
40
//
41
public class MaybankFacadeImpl
42
                extends MinimalMaybankFacadeImpl
43
                implements MaybankFacade {
44

    
45
        private static final Logger LOGGER = Logger.getLogger(MaybankFacadeImpl.class);
46

    
47
        private final UPassControllerV2 upcV2;
48

    
49
        //
50

    
51
        public MaybankFacadeImpl() {
52
                upcV2 = new UPassControllerV2();
53
        }
54

    
55
        @Override
56
        public int newAdminUser(
57
                        String appAccessId, String hashedSecretKey,
58
                        String username, String hashedPassword) {
59

    
60
                final IbccUser ibccUser = new IbccUser();
61
                // ibccUser.setPanCc(panCc);
62

    
63
                final MinimalUserBean minUser = new MinimalUserBean();
64
                minUser.setUsername(username);
65

    
66
                minUser.setHashedPassword(hashedPassword);
67
                ibccUser.setMinUser(minUser);
68

    
69
                return newAdminUser(appAccessId, hashedSecretKey, ibccUser);
70
        }
71

    
72
        @Override
73
        public int newPublicUser(
74
                        String appAccessId, String hashedSecretKey,
75
                        String username, String hashedPassword, String panCc) {
76

    
77
                final IbccUser ibccUser = new IbccUser();
78
                ibccUser.setPanCc(panCc);
79

    
80
                final MinimalUserBean minUser = new MinimalUserBean();
81
                minUser.setUsername(username);
82
                minUser.setHashedPassword(hashedPassword);
83
                ibccUser.setMinUser(minUser);
84

    
85
                return newUser(appAccessId, hashedSecretKey, ibccUser);
86
        }
87

    
88
        @Override
89
        public int newTicketingUser(
90
                        String appAccessId, String hashedSecretKey, 
91
                        String username, String hashedPassword, 
92
                        String fullName, String firstName, String lastName, String payeeCode) {
93

    
94
                final TicketingUser ticketingUser = new TicketingUser();
95
                ticketingUser.setFullName(fullName);
96
                ticketingUser.setFirstName(firstName);
97
                ticketingUser.setLastName(lastName);
98
                ticketingUser.setPayeeCode(payeeCode);
99
                
100
                final MinimalUserBean minUser = new MinimalUserBean();
101
                minUser.setUsername(username);
102
                minUser.setHashedPassword(hashedPassword);
103
                ticketingUser.setMinUser(minUser);
104
                
105
                return newUser(appAccessId, hashedSecretKey, ticketingUser);
106
        }
107
        /**
108
         * @deprecated
109
         * @see #lookupUsername(String, String, String)
110
         */
111
        @Override
112
        public Response lookupUsername_internal(
113
                        String appAccessId, String hashedSecretKey, String username) {
114

    
115
                throw new java.lang.UnsupportedOperationException();
116
        }
117

    
118
        @Override
119
        public ResponseElement lookupUsername(
120
                        String appAccessId, String hashedSecretKey, String username) {
121

    
122
                final Response response = super.lookupUsername_internal(appAccessId, hashedSecretKey, username);
123
                return new ResponseElement(response);
124
        }
125

    
126
        /**
127
         * @deprecated
128
         * @see #lookupPublicUsername(String, String, String)
129
         */
130
        @Override
131
        public Response lookupPublicUsername_internal(
132
                        String appAccessId, String hashedSecretKey, String username) {
133

    
134
                throw new java.lang.UnsupportedOperationException();
135
        }
136

    
137
        @Override
138
        public ResponseElement lookupPublicUsername(
139
                        String appAccessId, String hashedSecretKey, String username) {
140

    
141
                final Response response = super.lookupPublicUsername_internal(appAccessId, hashedSecretKey, username);
142
                return new ResponseElement(response);
143
        }
144

    
145
        /**
146
         * @deprecated
147
         * @see #lookupPan1(String, String, String)
148
         */
149
        @Override
150
        public Response lookupPan1_internal(
151
                        String appAccessId, String hashedSecretKey, String pan1) {
152

    
153
                throw new java.lang.UnsupportedOperationException();
154
        }
155

    
156
        @Override
157
        public ResponseElement lookupPan1(
158
                        String appAccessId, String hashedSecretKey, String pan1) {
159

    
160
                final Response response = super.lookupPan1_internal(appAccessId, hashedSecretKey, pan1);
161
                return new ResponseElement(response);
162
        }
163

    
164
        /**
165
         * @deprecated
166
         * @see #lookupPan2(String, String, String)
167
         */
168
        @Override
169
        public Response lookupPan2_internal(
170
                        String appAccessId, String hashedSecretKey, String pan1) {
171

    
172
                throw new java.lang.UnsupportedOperationException();
173
        }
174

    
175
        @Override
176
        public ResponseElement lookupPan2(
177
                        String appAccessId, String hashedSecretKey, String pan2) {
178

    
179
                final Response response = super.lookupPan2_internal(appAccessId, hashedSecretKey, pan2);
180
                return new ResponseElement(response);
181
        }
182

    
183
        /**
184
         * @deprecated
185
         * @see #lookupPanCc(String, String, String)
186
         */
187
        @Override
188
        public Response lookupPanCc_internal(
189
                        String appAccessId, String hashedSecretKey, String panCc) {
190

    
191
                throw new java.lang.UnsupportedOperationException();
192
        }
193

    
194
        @Override
195
        public ResponseElement lookupPanCc(
196
                        String appAccessId, String hashedSecretKey, String panCc) {
197

    
198
                final Response response = super.lookupPanCc_internal(appAccessId, hashedSecretKey, panCc);
199
                return new ResponseElement(response);
200
        }
201

    
202
        @Override
203
        public int deleteUser(
204
                        String appAccessId, String hashedSecretKey,
205
                        String username) {
206

    
207
                int rc = MinimalConstants.ERR_UNKNOWN;
208
                Session session = null;
209
                try {
210
                        session = HibernateUtils.currentSession();
211
                        session.beginTransaction();
212

    
213
                        rc = upcV2.deleteUserWithTheProfile(appAccessId, hashedSecretKey, username, session);
214

    
215
                        if (rc == MinimalConstants.ERR_SUCCESS)
216
                                session.getTransaction().commit();
217
                        else
218
                                GenericDAOHibernate.rollbackTransactionIfAny(session);
219

    
220
                } catch (UPassException e) {
221
                        rc = e.getErrorCode();
222
                        e.printStackTrace();
223
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
224

    
225
                } catch (Exception e) {
226
                        LOGGER.error(e, e);
227
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
228

    
229
                } finally {
230
                        GenericDAOHibernate.closeSessionIfAny(session);
231
                }
232
                return rc;
233
        }
234

    
235
        @Override
236
        public ResponseListElement searchUserByFilter(
237
                        String appAccessId, String hashedSecretKey,
238
                        Map<String, String> searchFilter) {
239

    
240
                ResponseListElement res = new ResponseListElement();
241
                final List<Map<String, String>> items = new LinkedList<Map<String, String>>();
242

    
243
                Session session = null;
244
                try {
245
                        session = HibernateUtils.currentSession();
246

    
247
                        List<UserProfile> examples = new ArrayList<UserProfile>(5);
248
                        for (Class<? extends UserProfile> clazz : Arrays.asList(
249
                                        IbccUser.class, Im2uUser.class, M2uUser.class, StockUser.class, TicketingUser.class)) {
250

    
251
                                final UserProfile example = clazz.newInstance();
252
                                example.mapToProperties(searchFilter);
253
                                examples.add(example);
254
                        }
255
                        List<UserProfile> profiles = upcV2.listProfilesByExamples(
256
                                        appAccessId, hashedSecretKey, examples, session);
257

    
258
                        for (UserProfile profile : profiles)
259
                                items.add(profile.propertiesToMap());
260

    
261
                        res.setItems(items);
262
                        res.setCode(MinimalConstants.ERR_SUCCESS);
263

    
264
                } catch (UPassException e) {
265
                        LOGGER.info(e, e);
266
                        res.setCode(e.getErrorCode());
267

    
268
                } catch (ParseException e) {
269
                        LOGGER.error(e, e);
270
                        res.setCode(MinimalConstants.ERR_INVALID_INPUT);
271

    
272
                } catch (Exception e) {
273
                        LOGGER.error(e, e);
274
                        res.setCode(MinimalConstants.ERR_UNKNOWN);
275

    
276
                } finally {
277
                        GenericDAOHibernate.closeSessionIfAny(session);
278
                }
279
                return res;
280
        }
281

    
282
        @Override
283
        public int changeIdNo(
284
                        String appAccessId, String hashedSecretKey,
285
                        String username, String idNo) {
286

    
287
                Session session = null;
288
                try {
289
                        session = HibernateUtils.currentSession();
290
                        session.beginTransaction();
291

    
292
                        UserProfile profile = upcV2.findProfile(
293
                                        appAccessId, hashedSecretKey, username, ClientApp.APP_ID_ONLINE_STOCK, session);
294

    
295
                        if (profile instanceof StockUser) {
296
                                StockUser stockUser = (StockUser) profile;
297
                                stockUser.setIdNo(idNo);
298

    
299
                                int rc = upcV2.updateProfileShallowly(
300
                                                appAccessId, hashedSecretKey, stockUser, session);
301

    
302
                                if (rc == MinimalConstants.ERR_SUCCESS)
303
                                        session.getTransaction().commit();
304
                                else
305
                                        GenericDAOHibernate.rollbackTransactionIfAny(session);
306

    
307
                                return rc;
308

    
309
                        } else {
310
                                GenericDAOHibernate.rollbackTransactionIfAny(session);
311
                                return MinimalConstants.ERR_INVALID_INPUT;
312
                        }
313
                } catch (UPassException e) {
314
                        LOGGER.info(e, e);
315
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
316
                        return e.getErrorCode();
317

    
318
                } catch (Exception e) {
319
                        LOGGER.error(e, e);
320
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
321
                        return MinimalConstants.ERR_UNKNOWN;
322

    
323
                } finally {
324
                        GenericDAOHibernate.closeSessionIfAny(session);
325
                }
326
        }
327

    
328
        @Override
329
        public CountResponseElement getFailedLoginsCount(
330
                        String appAccessId, String hashedSecretKey, String username) {
331

    
332
                CountResponse response = super.getFailedLoginsCount(appAccessId, hashedSecretKey, username);
333
                return new CountResponseElement(response);
334
        }
335

    
336
        @Override
337
        public int updateUserAttributes(
338
                        String appAccessId, String hashedSecretKey,
339
                        String username, Map<String, String> attributes) {
340

    
341
                Session session = null;
342
                try {
343
                        session = HibernateUtils.currentSession();
344
                        session.beginTransaction();
345

    
346
                        UserProfile profile = upcV2.findProfile(
347
                                        appAccessId, hashedSecretKey, username, ClientApp.APP_ID_M2U, session);
348

    
349
                        if (profile != null) {
350
                                final HashMap<String, String> tempMap = new HashMap<String, String>();
351
                                tempMap.putAll(profile.propertiesToMap());
352

    
353
                                attributes.remove("username");
354
                                tempMap.putAll(attributes);
355

    
356
                                profile.mapToProperties(tempMap);
357

    
358
                                int rc = upcV2.updateProfileShallowly(
359
                                                appAccessId, hashedSecretKey, profile, session);
360

    
361
                                if (rc == MinimalConstants.ERR_SUCCESS) {
362
                                        session.getTransaction().commit();
363

    
364
                                } else {
365
                                        LOGGER.warn("Unable to update user profile.");
366
                                        GenericDAOHibernate.rollbackTransactionIfAny(session);
367
                                }
368
                                return rc;
369

    
370
                        } else {
371
                                GenericDAOHibernate.rollbackTransactionIfAny(session);
372
                                return MinimalConstants.ERR_INVALID_INPUT;
373
                        }
374
                } catch (UPassException e) {
375
                        LOGGER.info(e, e);
376
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
377
                        return e.getErrorCode();
378

    
379
                } catch (Exception e) {
380
                        LOGGER.error(e, e);
381
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
382
                        return MinimalConstants.ERR_UNKNOWN;
383

    
384
                } finally {
385
                        GenericDAOHibernate.closeSessionIfAny(session);
386
                }
387
        }
388

    
389
        // Helper methods
390

    
391
        protected int newAdminUser(
392
                        String appAccessId, String hashedSecretKey, UserProfile profile) {
393

    
394
                int rc = MinimalConstants.ERR_UNKNOWN;
395
                Session session = null;
396
                try {
397
                        session = HibernateUtils.currentSession();
398
                        session.beginTransaction();
399

    
400
                        rc = upcV2.addUser(
401
                                        appAccessId, hashedSecretKey, profile.getMinUser(), UserAppAccess.TYPE_ADMIN, session);
402

    
403
                        if (rc == MinimalConstants.ERR_SUCCESS) {
404
                                rc = upcV2.updateProfileShallowly(appAccessId, hashedSecretKey, profile, session);
405

    
406
                                if (rc == MinimalConstants.ERR_SUCCESS)
407
                                        session.getTransaction().commit();
408
                                else
409
                                        GenericDAOHibernate.rollbackTransactionIfAny(session);
410

    
411
                        } else
412
                                GenericDAOHibernate.rollbackTransactionIfAny(session);
413

    
414
                } catch (Exception e) {
415
                        GenericDAOHibernate.rollbackTransactionIfAny(session);
416
                        LOGGER.error(e, e);
417

    
418
                } finally {
419
                        GenericDAOHibernate.closeSessionIfAny(session);
420
                }
421
                return rc;
422
        }
423
}