Skip to content

Commit

Permalink
initial import of demo application
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Preusker committed Jan 27, 2013
1 parent f835784 commit 9601972
Show file tree
Hide file tree
Showing 39 changed files with 26,163 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Expand Up @@ -4,3 +4,12 @@
*.jar
*.war
*.ear

build.properties
target

.project
.settings
.classpath

src/main/webapp/app/.DS_Store
22 changes: 22 additions & 0 deletions build.xml
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="crm-demo" default="deploy">

<property file="build.properties" />
<property name="target.dir" value="target" />

<condition property="mvn.executable" value="mvn.bat" else="mvn">
<os family="windows" />
</condition>

<target name="package.mvn">
<exec executable="${mvn.executable}" dir="." failonerror="true">
<env key="MAVEN_OPTS" value="-Xmx1024m -Xms512m" />
<arg line="clean package" />
</exec>
</target>

<target name="deploy" depends="package.mvn">
<copy file="${target.dir}/${ant.project.name}.war" todir="${deploy.dir}" />
</target>

</project>
9 changes: 9 additions & 0 deletions src/main/java/com/mycompany/boundary/CrmDemoApplication.java
@@ -0,0 +1,9 @@
package com.mycompany.boundary;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/rest")
public class CrmDemoApplication extends Application {

}
76 changes: 76 additions & 0 deletions src/main/java/com/mycompany/boundary/CustomerResource.java
@@ -0,0 +1,76 @@
package com.mycompany.boundary;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import com.mycompany.control.CustomerService;
import com.mycompany.entity.Customer;

@Path("/customer")
@Stateless
public class CustomerResource {

@Inject
private CustomerService customerService;

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getCustomers(@QueryParam("firstName") String firstName,
@QueryParam("name") String name) {
List<Customer> customers;
if (firstName != null && name != null) {
customers = customerService.findCustomer(firstName, name);
} else {
customers = customerService.findAllCustomers();
}
return Response.ok().entity(customers).build();
}

@POST
@Path("/")
public Response saveCustomer(@Context UriInfo uriInfo, Customer customer)
throws URISyntaxException {
customerService.saveCustomer(customer);
return Response.created(
new URI(uriInfo.getRequestUri() + "/" + customer.getId())).build();
}

@GET
@Path("/{customerId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getCustomer(@PathParam(value = "customerId") String customerId) {
Customer customer = customerService.findCustomerById(Long
.parseLong(customerId));

if (customer != null) {
return Response.ok().entity(customer).build();
} else {
return Response.status(Status.NOT_FOUND).build();
}
}

@PUT
@Path("/{customerId}")
public Response updateCustomer(Customer customer) {
customerService.updateCustomer(customer);
return Response.status(Status.ACCEPTED).build();
}

}
45 changes: 45 additions & 0 deletions src/main/java/com/mycompany/control/CustomerService.java
@@ -0,0 +1,45 @@
package com.mycompany.control;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import com.mycompany.entity.Customer;

public class CustomerService {

@PersistenceContext
private EntityManager entityManager;

public void saveCustomer(Customer customer) {
entityManager.persist(customer);
}

public List<Customer> findAllCustomers() {
TypedQuery<Customer> query = entityManager.createQuery(
"SELECT e FROM Customer e", Customer.class);
return (List<Customer>) query.getResultList();
}

public Customer findCustomerById(Long id) {
return entityManager.find(Customer.class, id);
}

public void updateCustomer(Customer customer) {
entityManager.merge(customer);
}

public List<Customer> findCustomer(String firstName, String lastName) {
TypedQuery<Customer> query = entityManager.createQuery(
"SELECT e FROM Customer e where e.firstName LIKE ? AND e.name LIKE ?",
Customer.class);

query.setParameter(1, "%" + firstName + "%");
query.setParameter(2, "%" + lastName + "%");

return query.getResultList();
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/mycompany/entity/Company.java
@@ -0,0 +1,52 @@
package com.mycompany.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* Company entity, representing a company object that can be persisted to a
* relational database and, at the same time, be used as a transfer object for
* company entities that are used in the user interface.
*
* @author Nils Preusker - n.preusker@gmail.com
*/
@Entity
public class Company {

@Id
@GeneratedValue
private Long id;

private String name;

/**
* Default constructor for JAX-RS (object <> JSON serialization)
*/
public Company() {
}

public Company(String name) {
super();
this.name = name;
}

// -------- getters and setters

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
159 changes: 159 additions & 0 deletions src/main/java/com/mycompany/entity/Customer.java
@@ -0,0 +1,159 @@
package com.mycompany.entity;

import java.util.Date;
import java.util.Locale;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
* Customer entity, representing a customer object that can be persisted to a
* relational database and, at the same time, be used as a transfer object for
* customer entities that are used in the user interface.
*
* @author Nils Preusker - n.preusker@gmail.com
*/
@Entity
public class Customer {

@Id
@GeneratedValue
private Long id;

private String firstName;

private String lastName;

private String email;

private String phone;

private String fax;

private Sex sex;

private String country;

private Locale locale;

private Date createDate;

@ManyToOne(cascade = { CascadeType.ALL })
private Company company;

/**
* Default constructor for JAX-RS (object <> JSON serialization)
*/
public Customer() {
}

public Customer(String firstName, String lastName, String email,
String phone, String fax, Sex sex, String country, Locale locale,
Date createDate, Company company) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.fax = fax;
this.sex = sex;
this.country = country;
this.locale = locale;
this.createDate = createDate;
this.company = company;
}

// -------- getters and setters

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getFax() {
return fax;
}

public void setFax(String fax) {
this.fax = fax;
}

public Sex getSex() {
return sex;
}

public void setSex(Sex sex) {
this.sex = sex;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public Locale getLocale() {
return locale;
}

public void setLocale(Locale locale) {
this.locale = locale;
}

public Date getCreateDate() {
return createDate;
}

public void setCreateDate(Date createDate) {
this.createDate = createDate;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/mycompany/entity/Sex.java
@@ -0,0 +1,5 @@
package com.mycompany.entity;

public enum Sex {
male, female, unknown
}
Empty file.

0 comments on commit 9601972

Please sign in to comment.