Notes
![]() ![]() Notes - notes.io |
/**
* @author aman.todi
* DAO implementation for EmployeeDetails
*/
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.cxf.common.util.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate5.HibernateTemplate;
import com.highradius.common.util.HRCLog;
import com.highradius.common.util.HRCLogFactory;
import com.highradius.training.dao.EmployeeDetailsDao;
import bbsr.highradius.training.model.employee.EmployeeDetails;
public class EmployeeDetailsDaoImpl implements EmployeeDetailsDao{
private static final HRCLog LOGGER = HRCLogFactory.getLog(EmployeeDetailsDaoImpl.class);
private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public Map<String, Object> getEmployeeDetails(String advanceSearchWhereQuery, Integer startIndex, Integer rowsToFetch) {
LOGGER.debug("Inside getEmployeeDetails() method called from EmployeeDetailsDaoImpl");
StringBuffer query = new StringBuffer("FROM EmployeeDetails WHERE ");
if (!StringUtils.isEmpty(advanceSearchWhereQuery)) {
query.append(advanceSearchWhereQuery);
} else {
query.append("isDeleted = 0");
}
LOGGER.debug("Query : " + query.toString());
try {
return getRowsAndCountForQuery(query.toString(), startIndex, rowsToFetch, true);
} catch (Exception e) {
LOGGER.error("Error in getEmployeeDetails()", e);
}
return null;
}
@Override
public boolean addNewEmployeeDetails(EmployeeDetails employeeObj) {
LOGGER.debug("Inside addNewEmployeeDetails() method called from EmployeeDetailsDaoImpl");
try {
getSession().saveOrUpdate(employeeObj);
return true;
} catch (Exception e) {
LOGGER.error("Error while saving entry : ",e);
}
return false;
}
protected Map<String, Object> getRowsAndCountForQuery(String intialQuery, int startIndex, int rowsToFetch, boolean isPaginated) {
List rows = getRowsForQuery(intialQuery, startIndex, rowsToFetch, isPaginated);
int count = getRowCountForQuery(intialQuery);
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("rows", rows);
resultMap.put("count", count);
return resultMap;
}
protected List getRowsForQuery(String intialQuery, int startIndex, int rowsToFetch, boolean isPaginated) {
LOGGER.debug("Query is: " + intialQuery);
Query query = getSession().createQuery(intialQuery);
if (isPaginated) {
LOGGER.debug(
"limiting rows as isPaginated is true. startIndex: " + startIndex + " rowsToFetch: " + rowsToFetch);
query.setFirstResult(startIndex);
query.setMaxResults(rowsToFetch);
}
LOGGER.debug("Before Executing Query " + query.toString());
List list = query.list();
// AcctDocHeader acc1 = (AcctDocHeader)list.get(0);
LOGGER.debug("After Executing Query ");
return list;
}
private Session getSession() {
return hibernateTemplate.getSessionFactory().getCurrentSession();
}
protected int getRowCountForQuery(String intialQuery) {
int rowCount = 0;
try {
String countQuery = "select count(*) as count " + removeSelectNOrderByClause(intialQuery);
LOGGER.debug("CountQuery is: " + countQuery);
Query query = getSession().createQuery(countQuery);
Object resultCount = (Object) query.list().get(0);
rowCount = Integer.parseInt(String.valueOf(resultCount));
}
catch (Exception e){
LOGGER.error(e);
}
LOGGER.debug("Count returned is: " + rowCount);
return rowCount;
}
protected static String removeSelectNOrderByClause(String initialQuery){
String finalQuery=initialQuery;
finalQuery = removeSelectQuery(finalQuery);
finalQuery = removeOrderByClause(finalQuery);
return finalQuery;
}
private static String removeSelectQuery(String initialQuery){
String finalQuery = initialQuery;
Pattern p = Pattern.compile("\s*select.*\s+from\s+(.*)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(finalQuery);
if(m.matches()){
if(m.group(1).length()!=0){
finalQuery = " from " + m.group(1);
}
}
return finalQuery;
}
private static String removeOrderByClause(String intialQuery) {
int index = intialQuery.toLowerCase().indexOf("order by");
if(index != -1){
return intialQuery.substring(0, index);
}
return intialQuery;
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team