NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

//daoimpl

package bbsr.highradius.training.dao.impl;

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.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.HibernateTemplate;

import com.highradius.common.util.HRCLog;
import com.highradius.common.util.HRCLogFactory;


import bbsr.highradius.training.dao.InvoiceDetailsDao;
import bbsr.highradius.training.model.employee.EmployeeDetails;
import bbsr.highradius.training.model.invoice.trn_customer;
import bbsr.highradius.training.model.invoice.trn_invoice;
import bbsr.highradius.training.model.invoice.trn_invoice_type;
import bbsr.highradius.training.model.invoice.trn_posting_key;
import net.sf.ehcache.hibernate.HibernateUtil;
import org.hibernate.Transaction;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;
import org.hibernate.HibernateException;
import java.util.Iterator;
import java.sql.Types;
import java.util.ArrayList;

import org.hibernate.criterion.Property;

public class InvoiceDetailsDaoImpl implements InvoiceDetailsDao{

private static final HRCLog LOGGER = HRCLogFactory.getLog(InvoiceDetailsDaoImpl.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 InvoiceDetailsDaoImpl");
StringBuffer query = new StringBuffer("FROM trn_invoice WHERE ");

if (!StringUtils.isEmpty(advanceSearchWhereQuery)) {
query.append(advanceSearchWhereQuery);
} else {
query.append("is_open = 1");
}

LOGGER.debug("Query : " + query.toString());
try {
return getRowsAndCountForQuery(query.toString(), startIndex, rowsToFetch, true);
} catch (Exception e) {
LOGGER.error("Error in getInvoiceDetails()", e);
}
return null;
}
@Override
public boolean addNewInvoiceDetails(trn_invoice invoiceObj,String customer_name ,String description, String invoice_type) {
LOGGER.debug("Inside addNewEmployeeDetails() method called from EmployeeDetailsDaoImpl");
String hql="FROM trn_customer WHERE customerName LIKE '"+customer_name+"'";
String hql2="FROM trn_posting_key WHERE description LIKE '"+description+"'";
String hql3="FROM trn_invoice_type WHERE invoiceType LIKE '"+invoice_type+"'";
ArrayList<trn_customer> customer=new ArrayList<trn_customer>();
trn_customer cust=new trn_customer();
ArrayList<trn_posting_key> postkey=new ArrayList<trn_posting_key>();
trn_posting_key post=new trn_posting_key();
ArrayList<trn_invoice_type> invtype=new ArrayList<trn_invoice_type>();
trn_invoice_type inv=new trn_invoice_type();


try {
Session session=getSession();
Query q1=session.createQuery(hql.toString());
customer=(ArrayList<trn_customer>) q1.list();
Iterator it1=customer.iterator();
cust=(trn_customer) it1.next();
System.out.println("Customer ID:"+cust.getPk_customer_map_id());
if(cust.getPk_customer_map_id()!=null && String.valueOf(cust.getPk_customer_map_id()).length()!=0) {
invoiceObj.setCustid(cust.getPk_customer_map_id());
}
else {
invoiceObj.setCustid(Types.NULL);
}
Query q2=session.createQuery(hql2.toString());
postkey=(ArrayList<trn_posting_key>) q2.list();
Iterator it2=postkey.iterator();
post=(trn_posting_key) it2.next();
System.out.println("Posting ID:"+post.getPk_posting_key_id());
if(post.getPk_posting_key_id()!=null && String.valueOf(post.getPk_posting_key_id()).length()!=0) {
invoiceObj.setPostid(post.getPk_posting_key_id());
}
else {
invoiceObj.setCustid(Types.NULL);
}
Query q3=session.createQuery(hql3.toString());
invtype=(ArrayList<trn_invoice_type>) q3.list();
Iterator it3=invtype.iterator();
inv=(trn_invoice_type) it3.next();
System.out.println("Invoice ID:"+inv.getPk_invoice_type_map_id());
if(inv.getPk_invoice_type_map_id()!=null && String.valueOf(inv.getPk_invoice_type_map_id()).length()!=0)
{
invoiceObj.setInvid(inv.getPk_invoice_type_map_id());
}
else {
invoiceObj.setCustid(Types.NULL);
}

Transaction transaction=null;
transaction=session.beginTransaction();

session.saveOrUpdate(invoiceObj);
transaction.commit();
session.close();
return true;
} catch (Exception e) {
LOGGER.error("Error while saving entry : ",e);
}
return false;
}
@Override
public boolean deleteInvoiceDetails(trn_invoice invoiceObj) {
LOGGER.debug("Inside deleteInvoiceDetails() method called from EmployeeDetailsDaoImpl");
Session sess=getSession();
Transaction tx=null;


try {
tx=sess.beginTransaction();
Query query=sess.createQuery("update trn_invoice set is_open=0 where pk_invoice_id=?");
query.setParameter(0,invoiceObj.getPk_invoice_id());
query.executeUpdate();

tx.commit();
sess.close();
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().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;
}
/*
* public List getCustomerId(String customer_name) { Session session =
* sessionFactory.openSession(); Transaction tx = null; tx =
* session.beginTransaction(); Criteria cr =
* session.createCriteria(trn_customer.class); // Add restriction.
* cr.add(Restrictions.eq("customer_name", customer_name)); List customer =
* cr.list();
*
* tx.commit(); session.close(); return customer; }
*/
public int getCustomerId(String user_id) {
/*Session session = null;
trn_customer user = null;
try {
session = getSession();
user = (trn_customer)session.get(trn_customer.class, "ABC Products");
Hibernate.initialize(user);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null && session.isOpen()) {
session.close();
}
}
return user.getPk_customer_map_id();
}*/
DetachedCriteria query = DetachedCriteria.forClass(trn_customer.class);
query.add(Property.forName("customer_name").eq(user_id));
Session session = getSession();
List<trn_customer> employeeList = new ArrayList<trn_customer>();
employeeList = query.getExecutableCriteria(session).list();
Iterator it = employeeList.iterator();
ArrayList idlist = new ArrayList();
while (it.hasNext()) {
trn_customer customer = (trn_customer) it.next();
idlist.add(customer.getPk_customer_map_id());

}
session.close();
return (int) idlist.get(0);
}
}



//action


package bbsr.highradius.training.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.highradius.common.util.HRCLog;
import com.highradius.common.util.HRCLogFactory;
import com.receivablesradius.deductions.action.BaseAction;
import com.receivablesradius.deductions.constants.JSONRowResult;
import com.receivablesradius.deductions.manager.impl.AdvanceSearchQueryHelperManagerImpl;

import java.util.List;
import bbsr.highradius.training.manager.InvoiceDetailsManager;
import bbsr.highradius.training.model.employee.EmployeeDetails;
import bbsr.highradius.training.model.invoice.trn_customer;
import bbsr.highradius.training.model.invoice.trn_invoice;
import bbsr.highradius.training.model.invoice.trn_invoice_type;
import bbsr.highradius.training.model.invoice.trn_posting_key;
import net.sf.ehcache.hibernate.HibernateUtil;

import org.springframework.orm.hibernate5.HibernateTemplate;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class InvoiceDetailsAction extends BaseAction {
private static final HRCLog LOGGER = HRCLogFactory.getLog(InvoiceDetailsAction.class);
private InvoiceDetailsManager invoiceDetailsManager;

private String invoice_number;
private String company_code;
private String fiscal_year;
private double invoice_total_amount;
private double invoice_due_amount;
private Date invoice_created_date;
private Date due_date;
private int discount_1_percentage;
private int discount_2_percentage;
private int discount_3_percentage;
private String debit_credit_indicator;
private String pk_invoice_id;
private String customer_name;
private String query;
private String item_number;
public String getItem_number() {
return item_number;
}

public void setItem_number(String item_number) {
this.item_number = item_number;
}

public String getQuery() {
return query;
}

public void setQuery(String query) {
this.query = query;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getInvoice_type() {
return invoice_type;
}

public void setInvoice_type(String invoice_type) {
this.invoice_type = invoice_type;
}

private String description;
private String invoice_type;






public String getPk_invoice_id() {
return pk_invoice_id;
}

public void setPk_invoice_id(String pk_invoice_id) {
this.pk_invoice_id = pk_invoice_id;
}

public InvoiceDetailsManager getInvoiceDetailsManager() {
return invoiceDetailsManager;
}

public void setInvoiceDetailsManager(InvoiceDetailsManager invoiceDetailsManager) {
this.invoiceDetailsManager = invoiceDetailsManager;
}

public String getInvoiceDetails() {
LOGGER.debug("Inside getInvoiceDetails() calls from InvoiceDetailsAction");
AdvanceSearchQueryHelperManagerImpl advanceSearchQueryHelperManagerImpl = new AdvanceSearchQueryHelperManagerImpl();
String advanceSearchWhereQuery = advanceSearchQueryHelperManagerImpl.massageQuery(null,query);
Map<String, Object> invoiceDetailsMap = new HashMap<>();
JSONRowResult result = new JSONRowResult();

try {
invoiceDetailsMap = invoiceDetailsManager.getInvoiceDetails(advanceSearchWhereQuery,getStartIndex(), getRowsToFetch());
//null check to avoid null pointer exception
if(invoiceDetailsMap != null) {
result.results = invoiceDetailsMap.get("count");
result.rows = invoiceDetailsMap.get("rows");
}
}
catch (Exception e) {
LOGGER.error("Error inside getEmployeeDetails :",e);
return ERROR;
}
Gson gson = new Gson();
String jsonResult = gson.toJson(result);
setOutput(jsonResult);
return SUCCESS;

}
public String addNewInvoiceDetails() {
LOGGER.debug("Inside getIvoiceDetails() called from EmployeeDetailsManager");
JSONRowResult result = new JSONRowResult();
try {
trn_invoice invoiceObj = new trn_invoice();
trn_customer custObj=new trn_customer();
//int L1= (int)invoiceDetailsManager.getCustomerId(customer_name);
//custObj.setPk_customer_map_id(L1);
//if(Integer.toString(L1).isEmpty()) {
//System.out.println("Empty string");
//}
//else {
//System.out.println(L1);
//}
trn_invoice_type invObj=new trn_invoice_type();
trn_posting_key postObj=new trn_posting_key();
System.out.println(pk_invoice_id);
if(pk_invoice_id!=null && pk_invoice_id.length()!=0) {
invoiceObj.setPk_invoice_id(Integer.parseInt(pk_invoice_id));
}
invoiceObj.setInvoice_number(invoice_number);
invoiceObj.setCompany_code(company_code);
invoiceObj.setFiscal_year(fiscal_year);
invoiceObj.setInvoice_total_amount(invoice_total_amount);
invoiceObj.setInvoice_due_amount(invoice_due_amount);
invoiceObj.setInvoice_created_date(invoice_created_date);
invoiceObj.setDue_date(due_date);
invoiceObj.setDiscount_1_percentage(discount_1_percentage);
invoiceObj.setDiscount_2_percentage(discount_2_percentage);
invoiceObj.setDiscount_3_percentage(discount_3_percentage);
invoiceObj.setDebit_credit_indicator(debit_credit_indicator);
invoiceObj.setItem_number(item_number);
invoiceObj.setIs_open(1);
//invoiceObj.setFk_customer_id(custObj);
/*
* custObj.setCustomer_name(customer_name);
* invoiceObj.setFk_customer_id(custObj); invObj.setInvoice_type(invoice_type);
* invoiceObj.setFk_invoice_type(invObj); postObj.setDescription(description);
* invoiceObj.setFk_posting_key(postObj);
*/
System.out.println("Data:"+invoice_number+" "+company_code+" "+fiscal_year+" "
+invoice_total_amount+" "+invoice_due_amount+" "
+invoice_created_date+" "+due_date+" "+discount_1_percentage+" "
+discount_2_percentage+" "+discount_3_percentage+" "+debit_credit_indicator+" "
+customer_name+" "+description+" "+invoice_type);

boolean status = invoiceDetailsManager.addNewInvoiceDetails(invoiceObj,customer_name,description, invoice_type);

result.success= status;
} catch (Exception e) {
LOGGER.error("Exception inside addNewInvoiceDetails() : ",e);
return ERROR;
}
Gson gson = new Gson();
String jsonResult = gson.toJson(result);
setOutput(jsonResult);
return SUCCESS;
}

public String deleteInvoiceDetails() {
JSONRowResult result=new JSONRowResult();
try {
trn_invoice invoiceObj=new trn_invoice();
invoiceObj.setPk_invoice_id(Integer.parseInt(pk_invoice_id));
boolean status = invoiceDetailsManager.deleteInvoiceDetails(invoiceObj);
result.success=status;

}catch(Exception e) {
LOGGER.error("Exception inside deleteEmployeeDetails():",e);
return ERROR;
}
Gson gson=new Gson();
String jsonResult=gson.toJson(result);
setOutput(jsonResult);
return SUCCESS;
}

public String getCustomer_name() {
return customer_name;
}

public void setCustomer_name(String customer_name) {
this.customer_name = customer_name;
}

/*
* public trn_customer getCustomerId(String user_id) { Session session = null;
* Object user = null; try { session =
* HibernateUtil.getSessionFactory().openSession(); user =
* (trn_customer)session.load(trn_customer.class, user_id);
* Hibernate.initialize(user); } catch (Exception e) { e.printStackTrace(); }
* finally { if (session != null && session.isOpen()) { session.close(); } }
* return user; } private SessionFactory sessionFactory; private
* HibernateTemplate hibernateTemplate;
*
* public SessionFactory getSessionFactory() { return sessionFactory; }
*
* public void setSessionFactory(SessionFactory sessionFactory) {
* this.sessionFactory = sessionFactory; this.hibernateTemplate = new
* HibernateTemplate(sessionFactory); }
*/
public String getInvoice_number() {
return invoice_number;
}

public void setInvoice_number(String invoice_number) {
this.invoice_number = invoice_number;
}

public String getCompany_code() {
return company_code;
}

public void setCompany_code(String company_code) {
this.company_code = company_code;
}

public String getFiscal_year() {
return fiscal_year;
}

public void setFiscal_year(String fiscal_year) {
this.fiscal_year = fiscal_year;
}

public double getInvoice_total_amount() {
return invoice_total_amount;
}

public void setInvoice_total_amount(double invoice_total_amount) {
this.invoice_total_amount = invoice_total_amount;
}

public double getInvoice_due_amount() {
return invoice_due_amount;
}

public void setInvoice_due_amount(double invoice_due_amount) {
this.invoice_due_amount = invoice_due_amount;
}

public Date getInvoice_created_date() {
return invoice_created_date;
}

public void setInvoice_created_date(Date invoice_created_date) {
this.invoice_created_date = invoice_created_date;
}

public Date getDue_date() {
return due_date;
}

public void setDue_date(Date due_date) {
this.due_date = due_date;
}

public int getDiscount_1_percentage() {
return discount_1_percentage;
}

public void setDiscount_1_percentage(int discount_1_percentage) {
this.discount_1_percentage = discount_1_percentage;
}

public int getDiscount_2_percentage() {
return discount_2_percentage;
}

public void setDiscount_2_percentage(int discount_2_percentage) {
this.discount_2_percentage = discount_2_percentage;
}

public int getDiscount_3_percentage() {
return discount_3_percentage;
}

public void setDiscount_3_percentage(int discount_3_percentage) {
this.discount_3_percentage = discount_3_percentage;
}

public String getDebit_credit_indicator() {
return debit_credit_indicator;
}

public void setDebit_credit_indicator(String debit_credit_indicator) {
this.debit_credit_indicator = debit_credit_indicator;
}

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