NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package bbsr.highradius.training.dao.impl;
import bbsr.highradius.training.model.invoice.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
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.hibernate.Transaction;
import org.springframework.orm.hibernate5.HibernateTemplate;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.highradius.common.util.HRCLog;
import com.highradius.common.util.HRCLogFactory;

import bbsr.highradius.training.dao.InvoicedetailsDAO;

public class InvoiceDAOimpl implements InvoicedetailsDAO{


private static final HRCLog LOGGER = HRCLogFactory.getLog(InvoiceDAOimpl.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> getInvoiceDetails( String advanceSearchWhereQuery,Integer startIndex, Integer rowsToFetch) {
LOGGER.debug("Inside getInvoiceDetails() method called from InvoiceDAOimpl");
StringBuffer query = new StringBuffer("FROM InvoiceTable WHERE ");
if (!StringUtils.isEmpty(advanceSearchWhereQuery)) {
query.append(advanceSearchWhereQuery);
} else {
query.append("is_open = 1 AND (approval_status ='pending' OR approval_status ='approved')");
}
LOGGER.debug("Query : " + query.toString());
try {
return getRowsAndCountForQuery(query.toString(), startIndex, rowsToFetch, true);
} catch (Exception e) {
LOGGER.error("Error in getInvoiceDetails()", e);
}
return null;
}




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().openSession();
}

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;
}





}


     
 
what is notes.io
 

Notes.io is a web-based application for 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 12 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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.