Java tutorial
package com.stoxx.service.dao.userprofile; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.stoxx.service.dao.StoxxBaseDAO; import com.stoxx.service.domain.PromoCodeDetails; import com.stoxx.service.domain.userprofile.UserProfileDetails; import com.stoxx.service.exception.STOXXException; public class UserProfileDAOImpl extends StoxxBaseDAO implements UserProfileDAO, Serializable { /** * */ private static final long serialVersionUID = -5687873887453258204L; private static final String unchecked = "unchecked"; private static final Logger log = Logger.getLogger(UserProfileDAOImpl.class); public void savePromoDetails(PromoCodeDetails promoCodeDetails) { hibernateTemplate.saveOrUpdate(promoCodeDetails); } public UserProfileDetails getUserProfile(String emailAddress) { UserProfileDetails userProfileDetails = new UserProfileDetails(); String queryString = null; queryString = "from UserProfileDetails ud where ud.emailAddress=?"; @SuppressWarnings("unchecked") List<UserProfileDetails> userProfileList = (List<UserProfileDetails>) listAllObjects(queryString, emailAddress.trim().toLowerCase()); if (Validator.isNotNull(userProfileList)) { if (userProfileList.size() > 0) { userProfileDetails = userProfileList.get(0); } } return userProfileDetails; } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { STOXXException.class, Exception.class }) public void updateUserProfile(UserProfileDetails userProfileDetails) throws STOXXException { hibernateTemplate.update(userProfileDetails); } public void saveUserProfile(UserProfileDetails userProfileDetails) throws STOXXException { hibernateTemplate.save(userProfileDetails); } public Boolean checkUserExists(String emailAddress) throws STOXXException { Long count = null; Session session = null; try { String promoCodeQueryString = "select count(*) from UserProfileDetails where emailAddress=?"; session = sessionFactory.openSession(); Query query = session.createQuery(promoCodeQueryString); query.setString(0, emailAddress); count = (Long) query.uniqueResult(); } catch (Exception e) { log.error(e.getMessage(), e); } finally { try { if (null != session) { session.close(); } } catch (Exception e) { log.error(e.getMessage(), e); } } return (count.intValue() > 0); } public int checkUserExistsForComapny(List<String> emailAddress) throws STOXXException { Long count = null; int licenseeCount = 0; Session session = null; StringBuilder queryStringInParam = new StringBuilder( "select count(*) from UserProfileDetails where emailAddress in ("); try { if (Validator.isNotNull(emailAddress) && emailAddress.size() > 0) { for (String catName : emailAddress) { queryStringInParam.append("'").append(catName.toLowerCase()).append("',"); } queryStringInParam.delete(queryStringInParam.lastIndexOf(StringPool.COMMA), queryStringInParam.length()); String append = ") and (status!=0 or status!=4)"; queryStringInParam.append(append); log.info("queryStringInParam ==> " + queryStringInParam.toString()); session = sessionFactory.openSession(); Query query = session.createQuery(queryStringInParam.toString()); count = (Long) query.uniqueResult(); log.info("license count already used in service before " + count); licenseeCount = emailAddress.size() - count.intValue(); log.info("new license count in service before " + licenseeCount); } } catch (Exception e) { log.error(e.getMessage() + "in checkUserExistsForComapny", e); } finally { try { if (null != session) { session.close(); } } catch (Exception e) { log.error(e.getMessage(), e); } } return licenseeCount; } @Override public void deleteUserProfile(UserProfileDetails userProfileDetails) throws STOXXException { hibernateTemplate.delete(userProfileDetails); } /** * Returns all users of a sales entry, but not users created for email * address change. */ @SuppressWarnings(unchecked) @Override public List<UserProfileDetails> getLicensedUsers(long salesEntryId) throws STOXXException { String queryString = null; queryString = "from UserProfileDetails ud where ud.salesEntryId=? and (status != 0 or status is null)"; return (List<UserProfileDetails>) listAllObjects(queryString, salesEntryId); } @SuppressWarnings(unchecked) @Override public List<String> getLicensedUsersEmailAddress(long salesEntryId) throws STOXXException { List<UserProfileDetails> userProfileDetails = null; List<String> licensedUsersEmailAddressList = new ArrayList<String>(); String queryString = null; queryString = "from UserProfileDetails ud where ud.salesEntryId=?"; userProfileDetails = (List<UserProfileDetails>) listAllObjects(queryString, salesEntryId); if (Validator.isNotNull(userProfileDetails)) { for (UserProfileDetails userDetails : userProfileDetails) { licensedUsersEmailAddressList.add(userDetails.getEmailAddress()); } } return licensedUsersEmailAddressList; } @SuppressWarnings("unchecked") @Override public Boolean getStausForMigratedUsers(String emailAddress) throws STOXXException { Boolean isUserMigrated = Boolean.FALSE; List<UserProfileDetails> userProfileDetails = null; String queryString = "from UserProfileDetails where emailAddress= ?"; userProfileDetails = (List<UserProfileDetails>) listAllObjects(queryString, emailAddress.trim().toLowerCase()); if (Validator.isNotNull(userProfileDetails) && userProfileDetails.size() > 0) { int status = userProfileDetails.get(0).getStatus(); if (Validator.isNotNull(status) && status == 3) { isUserMigrated = Boolean.TRUE; } } return isUserMigrated; } @SuppressWarnings("unchecked") @Override public List<String> getTranslatorUsersByTranslatorId(long translatorAgencyId) { List<UserProfileDetails> userProfileDetails = null; List<String> translatorUsersEmailAddressList = new ArrayList<String>(); String queryString = null; Long longObj = Long.valueOf(translatorAgencyId); queryString = "from UserProfileDetails ud where ud.translatorAgencyId= ?"; userProfileDetails = (List<UserProfileDetails>) listAllObjects(queryString, Integer.valueOf(longObj.toString()).intValue()); if (Validator.isNotNull(userProfileDetails)) { for (UserProfileDetails userDetails : userProfileDetails) { translatorUsersEmailAddressList.add(userDetails.getEmailAddress()); } } return translatorUsersEmailAddressList; } @Override public List<UserProfileDetails> getLicensedUsersNotFromDomains(long salesEntryId, List<String> domains) throws STOXXException { List<UserProfileDetails> returnList; if (domains != null && !domains.isEmpty()) { StringBuffer query = new StringBuffer("from UserProfileDetails ud where salesEntryId = ? and not ("); for (int counter = 0; counter < domains.size(); counter++) { if (counter > 0) { query = query.append(" or "); } query = query.append(" emailaddress like '%"); query = query.append(domains.get(counter)); query = query.append("'"); } query = query.append(" )"); returnList = (List<UserProfileDetails>) listAllObjects(query.toString(), salesEntryId); } else { returnList = getLicensedUsers(salesEntryId); } return returnList; } @Override public List<UserProfileDetails> getLicensedUsersOfDomain(long salesEntryId, String domain) throws STOXXException { String queryString = "from UserProfileDetails ud where salesEntryId = ? and emailaddress like '%" + domain + "'"; return (List<UserProfileDetails>) listAllObjects(queryString, salesEntryId); } @Override public List<UserProfileDetails> getStaffUsersPerDepartment(String deptName, String userType) { String queryString = "from UserProfileDetails ud where status = 1 and usertype = '" + userType + "' and department like '" + deptName + "'"; return (List<UserProfileDetails>) listAllObjects(queryString); } }