- LoggedIn method will check whether user is logged in konakart or not. It will return konakart userId if user is logged in.
protected int loggedIn(HttpServletRequest request, HttpServletResponse response,KKAppEng kkAppEng, String forwardAfterLogin)
throws KKException, KKAppException {
// check if User Logged in LR
User liferayUser = getLoggedInLRUser(request, response);
// if user not logged in LR then return -1
if (liferayUser == null) {
return -1;
}
if ((liferayUser.getEmailAddress() == null) || (liferayUser.getEmailAddress().length() == 0)) {
return -1;
}
CustomerIf customerInKK = getCustomerFromKKByEmailId(liferayUser.getEmailAddress(), kkAppEng);
if (customerInKK == null) {
int custId = -1;
custId = registerCustomerInKK(liferayUser, kkAppEng);
if (custId < 0) {
return -1;
}
customerInKK = getCustomerFromKKById(custId, kkAppEng);
}
forceLoginInKK(kkAppEng, customerInKK);
return loggedIn(request, response, kkAppEng, forwardAfterLogin, /* checkXSRF */true, /* xsrfToken */null);
}
- This method will check whether user is logged in liferay or not. It will return liferay user profile if user is logged in liferay.
private User getLoggedInLRUser(HttpServletRequest request, HttpServletResponse response) {
Principal localPrincipal = request.getUserPrincipal();
User tempLRUser = null;
if(localPrincipal == null) {
return tempLRUser;
}
tempLRUser = getUserProfile(localPrincipal.getName());
return tempLRUser;
}
private User getUserProfile(String userId) {
User localUser = null;
if (userId == null) {
return localUser;
}
try {
localUser = UserLocalServiceUtil.getUser(Long.parseLong(userId));
} catch (NumberFormatException e) {
log.debug(e.getMessage(), e);
} catch (PortalException e) {
log.debug(e.getMessage(), e);
} catch (SystemException e) {
log.debug(e.getMessage(), e);
}
if ((localUser == null) && (log.isDebugEnabled())) {
log.warn("User with id " + userId + " could not be found");
}
return localUser;
}
- This method will return customer based on user email address.
private CustomerIf getCustomerFromKKByEmailId(String userEmailAddress, KKAppEng kkAppEng) {
KKEngIf engIf = null;
CustomerIf cust = null;
try {
engIf = kkAppEng.getEng();
MgrFactory mgrFactory = new MgrFactory(engIf);
CustomerMgrIf mgr = mgrFactory.getCustMgr(true);
cust = mgr.getCustomerForEmail(userEmailAddress);
} catch (Exception e) {
log.debug(e.getMessage(), e);
}
return cust;
}
- This method will register/create user in konakart if user does not exist in konakart.
private int registerCustomerInKK(User liferayUser, KKAppEng kkAppEng) {
int tempCustId = -1;
CustomerRegistrationIf cr = new CustomerRegistration();
try {
fillUpKKCustomerRegistration(liferayUser, cr, kkAppEng);
tempCustId = kkAppEng.getEng().registerCustomer(cr);
} catch (KKException e) {
log.debug(e.getMessage(), e);
} catch (PortalException e) {
log.debug(e.getMessage(), e);
} catch (SystemException e) {
log.debug(e.getMessage(), e);
}
return tempCustId;
}
- This method will fill user details in konakart db.
private void fillUpKKCustomerRegistration(User liferayUser, CustomerRegistrationIf cr, KKAppEng kkAppEng)
throws PortalException, SystemException, KKException {
cr.setFirstName(liferayUser.getFirstName());
cr.setLastName(liferayUser.getLastName() == null ? "" : liferayUser.getLastName());
cr.setGender(liferayUser.isMale() == true ? "m" : "f");
Calendar calendar = new GregorianCalendar();
Date bDay = liferayUser.getBirthday();
if(bDay != null){
calendar.setTime(bDay);
}
cr.setBirthDate(calendar);
cr.setEmailAddr(liferayUser.getEmailAddress());
cr.setStreetAddress("23 Halden Close");
cr.setCity("Newcastle");
cr.setPostcode("ST5 ORT");
cr.setTelephoneNumber("9825098250");
cr.setPassword("password");
/* Optional attributes */
cr.setCompany("ABC Ltd.");
// Country Id needs to be set with the id of a valid country from the Countries table
cr.setCountryId(getCountryIdByName("United Kingdom", kkAppEng));
cr.setFaxNumber("01782 639707");
// Newsletter should be set to "0" (don't receive) or "1" (receive)
cr.setNewsletter("0");
cr.setSuburb("May Bank");
cr.setState("Staff");
}
- This method will return customer/user object based on customer/user konakart id.
private CustomerIf getCustomerFromKKById(int custId, KKAppEng kkAppEng) {
KKEngIf engIf = null;
CustomerIf cust = null;
try {
engIf = kkAppEng.getEng();
MgrFactory mgrFactory = new MgrFactory(engIf);
CustomerMgrIf mgr = mgrFactory.getCustMgr(true);
cust = mgr.getCustomerForId(custId);
} catch (Exception e) {
log.debug(e.getMessage(), e);
}
return cust;
}
- This method will login user if user is not logged in konakart.
private void forceLoginInKK(KKAppEng kkAppEng, CustomerIf customerInKK) throws KKException, KKAppException {
String sessionId = kkAppEng.getCustomerMgr().login(customerInKK.getEmailAddr(), "password");
kkAppEng.setSessionId(sessionId);
}
Download :