public static void main(String[] args) { TravelOffice travelOffice = new TravelOffice(); travelOffice.addTrip(new Trip()); travelOffice.addTrip(new AbroadTrip()); travelOffice.displayTrips(); Customer test1 = new Customer(); test1.setName("test1"); test1.setNip("123"); test1.setMail("EMAIL BITCH"); test1.setNewsletterAccepted(true); Customer test2 = new Customer(); test2.setName("test2"); test2.setNip("234"); test2.setMail("WA WA WEE WA"); test2.setNewsletterAccepted(true); Trip trip1 = new Trip(); Trip trip2 = new Trip(); Trip trip3 = new Trip(); travelOffice.addTrip(trip1); travelOffice.addTrip(trip2); travelOffice.addTrip(trip3); travelOffice.addCustomer(test1); travelOffice.addCustomer(test2); travelOffice.saveCustomers(); travelOffice.displayCustomers(); travelOffice.displayTrips(); travelOffice.sendNewsletter(); }
public CustomersResource() { System.out.println("[CustomerResource] Instantiated " + this); Customer customer1 = new Customer(); customer1.setId(1111); customer1.setName("CustomerOne"); customer1.setAddress("Bombay, India"); Customer customer2 = new Customer(); customer2.setId(2222); customer2.setName("CustomerTwo"); customer2.setAddress("Pune, India"); Customer customer3 = new Customer(); customer3.setId(3333); customer3.setName("CustomerThree"); customer3.setAddress("Bangalore, India"); customerMap.put(customer1.getId(), customer1); customerMap.put(customer2.getId(), customer2); customerMap.put(customer3.getId(), customer3); if (testBean != null) { System.out.println("[CustomersResource] testBean NOT NULL " + testBean); testBean.setCustomerMap(customerMap); } else { System.out.println("[CustomersResource] testBean IS NULL"); } }
public static void createTestData(EntityManager em) { em.getTransaction().begin(); // Create customer 1 Customer customer1 = new Customer(); customer1.setName("Customer1"); customer1.setId(1); // Create customer 2 Customer customer2 = new Customer(); customer2.setName("Customer2"); customer2.setId(2); // Create customer 3 Customer customer3 = new Customer(); customer3.setName("Customer3"); customer3.setId(3); // Create 3 orders Order order1 = new Order(); order1.setAddress("Address of order #1, Brazil"); order1.setId(1); Order order2 = new Order(); order2.setAddress("Address of order #2, USA"); order2.setId(2); Order order3 = new Order(); order3.setAddress("Address of order #3, Korea"); order3.setId(3); // Since we are not using Cascade, we need to manaully persist orders em.persist(order1); em.persist(order2); em.persist(order3); // The first 2 orders belong to customer 1 customer1.getOrders().add(order1); order1.setCustomer(customer1); customer1.getOrders().add(order2); order2.setCustomer(customer1); // The 3rd order belongs to customer 2 customer2.getOrders().add(order3); order3.setCustomer(customer2); em.persist(customer1); em.persist(customer2); em.persist(customer3); em.getTransaction().commit(); }
private Customer getNewCustomerWithUniqueName() { Customer c1 = getCustomerComponent().createNew(); c1.setName("A" + System.currentTimeMillis()); c1.setPhoneNumber("808-555-1212"); getCustomerComponent().persist(c1); return c1; }
public void testOneToOnePropertyRef() { Session s = openSession(); Transaction t = s.beginTransaction(); Customer c = new Customer(); c.setName("Emmanuel"); c.setCustomerId("C123-456"); c.setPersonId("P123-456"); Account a = new Account(); a.setCustomer(c); a.setPerson(c); a.setType('X'); s.persist(c); s.persist(a); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); a = (Account) s.createQuery("from Account acc join fetch acc.customer join fetch acc.person") .uniqueResult(); assertNotNull(a.getCustomer()); assertTrue(Hibernate.isInitialized(a.getCustomer())); assertNotNull(a.getPerson()); assertTrue(Hibernate.isInitialized(a.getPerson())); c = (Customer) s.createQuery("from Customer").uniqueResult(); assertSame(c, a.getCustomer()); assertSame(c, a.getPerson()); s.delete(a); s.delete(a.getCustomer()); s.delete(a.getPerson()); t.commit(); s.close(); }
@Test public void testFindIterate() { // insert 1000 customers int j = 0; for (int i = 0; i < 1000; i++) { Customer customer = new Customer(); customer.setName("Hello" + j++); customer.save(); } QueryIterator<Customer> iterate = Customer.find .query() .select("id") // .fetch("contacts", new FetchConfig().lazy(20)) .findIterate(); try { while (iterate.hasNext()) { Customer customer = iterate.next(); // do something interesting with customer // customer.getContacts().size(); System.out.println("got name " + customer.getId() + " " + customer.getName()); } } finally { // close the underlying resources held by the QueryIterator // those are: ResultSet and Connection iterate.close(); } }
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { Customer customer = new Customer(); customer.setCustId(rs.getInt("CUST_ID")); customer.setName(rs.getString("NAME")); customer.setAge(rs.getInt("AGE")); return customer; }
public static void main(String[] args) throws Exception { System.out.println("hello from Customer.java"); EntityManagerFactory factory = Persistence.createEntityManagerFactory("customerPU"); EntityManager manager = factory.createEntityManager(); Query q1 = manager.createQuery("SELECT COUNT(c) FROM Customer c"); Long count = (Long) q1.getSingleResult(); if (count == 0) { // record is empty, read from data.txst System.out.println("record empty, read from data.txt..."); // try { FileReader fr = new FileReader("data.txt"); BufferedReader br = new BufferedReader(fr); String s; while ((s = br.readLine()) != null) { // System.out.println(s); // split the string s Object[] items = s.split("\\|"); // store in string list // List<String> itemList= new ArrayList<String>(Arrays.asList(items)); // string list converted to array // Object[] itemArray = itemList.toArray(); // insert data into database table manager.getTransaction().begin(); Customer c = new Customer(); // add email c.setEmail((String) items[0]); // add pass c.setPass((String) items[1]); // add name c.setName((String) items[2]); // add address c.setAddress((String) items[3]); // add yob c.setYob((String) items[4]); // change to managed state manager.persist(c); manager.getTransaction().commit(); } fr.close(); } // display the records Query q2 = manager.createNamedQuery("Customer.findAll"); List<Customer> customers = q2.getResultList(); for (Customer c : customers) { System.out.println(c.getName() + ", " + c.getEmail()); } manager.close(); factory.close(); }
final void init() { Customer c = new Customer(); c.setName("John"); c.setId(123); customers.put(c.getId(), c); c = new Customer(); c.setName("Dan"); c.setId(113); customers.put(c.getId(), c); Order o = new Order(); o.setDescription("order 223"); o.setId(223); orders.put(o.getId(), o); }
private Customer insertCustomerNoContacts(String name) { Customer c = createCustomer("Roger", "15 Kumera Way", "Bos town", 1, "2010-04-10"); c.setName(name); c.setStatus(Customer.Status.ACTIVE); Ebean.save(c); return c; }
private Customer insertCustomerNoAddress() { Customer c = new Customer(); c.setName("Cust NoAddress"); c.setStatus(Customer.Status.NEW); c.addContact(createContact("Jack", "Black")); Ebean.save(c); return c; }
public static Customer getCustomer(int customerNumber) { Customer barbara = new Customer(); barbara.setName("Barbara White"); barbara.setAddress("3400 Richmond Parkway #3423"); barbara.setCity("Bristol"); barbara.setState("CT"); barbara.setPostalCode("06010"); if (customerNumber == 1001) { return barbara; } Customer karl = new Customer(); karl.setName("Karl Vang"); karl.setAddress("327 Franklin Street"); karl.setCity("Edina"); karl.setState("MN"); karl.setPostalCode("55435"); if (customerNumber == 1002) { return karl; } Customer ronda = new Customer(); ronda.setName("Ronda Chavan"); ronda.setAddress("518 Commanche Dr."); ronda.setCity("Greensboro"); ronda.setState("NC"); ronda.setPostalCode("27410"); if (customerNumber == 1003) { return ronda; } return null; }
@POST @Path("add") @Produces(MediaType.APPLICATION_XML) @Consumes("application/x-www-form-urlencoded") public Customer addCustomer( @FormParam("custId") int id, @FormParam("custName") String name, @FormParam("custAddress") String address) { Customer customer = new Customer(); customer.setId(id); customer.setName(name); customer.setAddress(address); customerMap.put(customer.getId(), customer); testBean.setCustomerMap(customerMap); return customer; }
public static Customer createCustomer( String name, String shippingStreet, String billingStreet, int contactSuffix, String annDate) { Customer c = new Customer(); c.setName(name); c.setStatus(Customer.Status.NEW); if (annDate == null) { annDate = "2010-04-14"; } c.setAnniversary(Date.valueOf(annDate)); if (contactSuffix > 0) { Contact jim = new Contact("Jim" + contactSuffix, "Cricket"); jim.getNotes().add(new ContactNote("ORM Lives", "And it is cool!")); c.addContact(jim); c.addContact(new Contact("Fred" + contactSuffix, "Blue")); c.addContact(new Contact("Bugs" + contactSuffix, "Bunny")); } if (shippingStreet != null) { Address shippingAddr = new Address(); shippingAddr.setLine1(shippingStreet); shippingAddr.setLine2("Sandringham"); shippingAddr.setCity("Auckland"); shippingAddr.setCountry(Ebean.getReference(Country.class, "NZ")); c.setShippingAddress(shippingAddr); } if (billingStreet != null) { Address billingAddr = new Address(); billingAddr.setLine1(billingStreet); billingAddr.setLine2("St Lukes"); billingAddr.setCity("Auckland"); billingAddr.setCountry(Ebean.getReference(Country.class, "NZ")); c.setBillingAddress(billingAddr); } return c; }
/** * Example technologies */ @Test public void showExamples() throws JsonProcessingException { if (LOG.isDebugEnabled()) { LOG.debug("Show how jackson can convert something to json"); } Customer customer = new Customer(); customer.setName("TestName"); customer.setId("12312321"); ObjectMapper mapper = new ObjectMapper(); String jsonValue = mapper.writeValueAsString(customer); if (LOG.isInfoEnabled()) { LOG.info("Json Value:" + jsonValue); } }
public Employee createEmployee(String name, Organization organization) { if (organization.getOrganizationName().equals(TypeOfOrg.Customer.getValue())) { Customer customer = new Customer(); customer.setName(name); employeeList.add(customer); return customer; } else if (organization.getOrganizationName().equals(TypeOfOrg.Supplier.getValue())) { Supplier supplier = new Supplier(); supplier.setName(name); employeeList.add(supplier); return supplier; } else if (organization.getOrganizationName().equals(TypeOfOrg.SalesSpecialist.getValue())) { SalesSpecialist salesSpecialist = new SalesSpecialist(); salesSpecialist.setName(name); employeeList.add(salesSpecialist); return salesSpecialist; } else if (organization.getOrganizationName().equals(TypeOfOrg.ShippingSpecialist.getValue())) { ShippingSpecialist shippingSpecialist = new ShippingSpecialist(); shippingSpecialist.setName(name); employeeList.add(shippingSpecialist); return shippingSpecialist; } return null; }
public void updateCustomer() { customer.setName("modified by " + getInternalId()); }
public void testDirtyButNotDirty() throws Exception { EntityManager manager = getOrCreateEntityManager(); manager.getTransaction().begin(); Employee mark = new Employee(); mark.setName("Mark"); mark.setTitle("internal sales"); mark.setSex('M'); mark.setAddress("buckhead"); mark.setZip("30305"); mark.setCountry("USA"); Customer joe = new Customer(); joe.setName("Joe"); joe.setSex('M'); joe.setAddress("San Francisco"); joe.setZip("XXXXX"); joe.setCountry("USA"); joe.setComments("Very demanding"); joe.setSalesperson(mark); Person yomomma = new Person(); yomomma.setName("mum"); yomomma.setSex('F'); manager.persist(mark); manager.persist(joe); manager.persist(yomomma); long[] ids = {mark.getId(), joe.getId(), yomomma.getId()}; manager.getTransaction().commit(); manager.getTransaction().begin(); assertEquals( manager .createQuery("select p.address, p.name from Person p order by p.name") .getResultList() .size(), 3); assertEquals( manager .createQuery("select p from Person p where p.class = Customer") .getResultList() .size(), 1); manager.getTransaction().commit(); manager.getTransaction().begin(); List customers = manager .createQuery("select c from Customer c left join fetch c.salesperson") .getResultList(); for (Iterator iter = customers.iterator(); iter.hasNext(); ) { Customer c = (Customer) iter.next(); assertEquals(c.getSalesperson().getName(), "Mark"); } assertEquals(customers.size(), 1); manager.getTransaction().commit(); manager.getTransaction().begin(); customers = manager.createQuery("select c from Customer c").getResultList(); for (Iterator iter = customers.iterator(); iter.hasNext(); ) { Customer c = (Customer) iter.next(); assertEquals(c.getSalesperson().getName(), "Mark"); } assertEquals(customers.size(), 1); manager.getTransaction().commit(); manager.getTransaction().begin(); mark = manager.find(Employee.class, new Long(ids[0])); joe = (Customer) manager.find(Customer.class, new Long(ids[1])); yomomma = manager.find(Person.class, new Long(ids[2])); mark.setZip("30306"); assertEquals( 1, manager.createQuery("select p from Person p where p.zip = '30306'").getResultList().size()); manager.remove(mark); manager.remove(joe); manager.remove(yomomma); assertTrue(manager.createQuery("select p from Person p").getResultList().isEmpty()); manager.getTransaction().commit(); manager.close(); }