Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.matel.pg.dao.impl; import com.matel.dao.UserDAO; import com.matel.pg.dao.CompanyDAO; import com.matel.pg.dao.ProductDAO; import com.matel.pg.domain.Company; import com.matel.pg.mock.domain.Customer; import com.matel.pg.mock.domain.MockCompany; import com.matel.pg.mock.domain.SalesItem; import java.math.BigDecimal; import org.apache.commons.beanutils.BeanUtils; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Transactional @Repository @Component("companyDAO") public class CompanyDAOImpl implements CompanyDAO { @Autowired SessionFactory sessionFactory; @Autowired ProductDAO productDAO; @Autowired UserDAO userDAO; @Override public Company findByRequestToken(String requestToken) { Company company = null; try { Session session = sessionFactory.getCurrentSession(); Criteria criteria = session.createCriteria(Company.class); criteria.add(Restrictions.eq("requestToken", requestToken)); criteria.setMaxResults(1); company = (Company) criteria.uniqueResult(); } catch (Exception e) { e.printStackTrace(); } return company; } @Override public Company findOne(long parseLong) { Company company = null; try { Session session = sessionFactory.getCurrentSession(); company = (Company) session.get(Company.class, parseLong); } catch (Exception e) { e.printStackTrace(); } return company; } @Override public void save(Company company) { try { Session session = sessionFactory.getCurrentSession(); session.merge(company); } catch (HibernateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } @Override public MockCompany getMockCompany() { MockCompany mockCompany = new MockCompany(); try { Company c = findOne(Long.parseLong("1")); BeanUtils.copyProperties(mockCompany, c); mockCompany.setProducts(productDAO.findAll()); mockCompany.setUsers(userDAO.findAll()); loadMockData(mockCompany); } catch (Exception e) { e.printStackTrace(); } return mockCompany; } private void loadMockData(MockCompany mockCompany) { // Preaper Customer final Customer customer1 = new Customer("Aashi", "Mittal", "aashi.mittal@example.com", "555-555-5555"); customer1.setCity("Middlefield"); customer1.setPostalCode("98756"); customer1.setCountry("United States"); customer1.setLine1("333 Easy Street"); customer1.setCountrySubDivisionCode("CA"); customer1.setCompany(mockCompany); mockCompany.getCustomers().add(customer1); // Prepare SalesItem Objects SalesItem item1 = new SalesItem("Calendar, Calculator, Sec, Pen", "Calendar, Calculator, Sec, Pen", "588", null); item1.setCompany(mockCompany); item1.setQtyOnHand(new BigDecimal(1)); SalesItem item2 = new SalesItem("Calendar, Calculator, Sec, Pen 1", "Calendar, Calculator, Sec, Pen", "588", null); item2.setCompany(mockCompany); item2.setQtyOnHand(new BigDecimal(1)); mockCompany.getSalesItems().add(item1); mockCompany.getSalesItems().add(item2); System.out.println(mockCompany.getSalesItems().size()); } }