Suggested Pages

Tuesday, May 26, 2015

JAX-RS POST Example

Example of Rest Service with POST method


@EJB
private DeviceRepository deviceRepository;

@EJB
private ContactRepository contactRepository;


@Path(value = "/devices/{deviceID}/contacts")
@POST
@Consumes({ MediaType.APPLICATION_JSON })
public ContactResponse saveContact(@PathParam(value = "deviceID") String deviceID, ContactRequest contactRequest) {
  try {
    Device device= deviceRepository.findById(deviceID);
    Contact contact=createContact(contactRequest,device);
    contact=contactRepository.save(contact)
    return createResponse(contact);
  } catch (Exception e) {
    throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
  } 
 }

JPA Example of Named Query

Contact.java



@NamedQueries(value = @NamedQuery(name = "findContactByMail", query = "SELECT c FROM Contact c WHERE c.mail= :mail AND c.location= :location"))
@Entity
@Table(name = "CONTACT")
public class Contact {

 @Id
 @Column(name = "ID")
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;

 @Column(name = "MAIL")
 private String mail;

 @Column(name = "LOCATION")
 private String location;

        ....
}

Client.java

   
   @PersistenceContext
   private EntityManager entityManager;


   public Contact findContactByMail(String mail, String location){

     TypedQuery findContactByMail = entityManager.createNamedQuery("findContactByMail",Contact.class);
     findContactByMail.setParameter("mail", mail);
     findContactByMail.setParameter("location", location);

       try {
            return findContactByMail.getSingleResult();
       } 
       catch (NoResultException e) {
            return null;
       }
    }
    ....
}

JPA multiple Unique Constraint

BeanExample



@Entity
@Table(name = "BEAN_TABLE", uniqueConstraints = @UniqueConstraint(columnNames = { "FIRST_COLUMN", "SECOND_COLUMN" }))
public class BeanExample {

 
 private static final long serialVersionUID = 1L;

 @Id
 @Column(name = "ID")
 private String id = UUID.randomUUID().toString();

 
 @Column(name="FIRST_COLUMN")
 private String firstColumn;


 @Column(name="SECOND_COLUMN")
 private String secondColumn;

 ....
}

Wednesday, May 20, 2015

Design Pattern Builder - Example

Notification.java


import java.util.Date;

/**
 * The Class Notification.
 */
public class Notification {

 /** The system. */
 int SYSTEM = 1;

 /** The application. */
 int APPLICATION = 2;

 /** The builder. */
 private Builder builder;

 /**
  * Instantiates a new notification.
  * 
  * @param builder
  *            the builder
  */
 private Notification(Builder builder) {
  this.builder = builder;
 }

 /**
  * Gets the code.
  * 
  * @return the code
  */
 public int getCode() {
  return builder.code;
 }

 /**
  * Gets the priority.
  * 
  * @return the priority
  */
 public int getPriority() {
  return builder.priority;
 }

 /**
  * Gets the info.
  * 
  * @return the info
  */
 public String getInfo() {
  return builder.info;
 }

 /**
  * Gets the short description.
  * 
  * @return the short description
  */
 public String getShortDescription() {
  return builder.shortDescription;
 }

 /**
  * Gets the long description.
  * 
  * @return the long description
  */
 public String getLongDescription() {
  return builder.longDescription;
 }

 /**
  * Gets the date.
  * 
  * @return the date
  */
 public Date getDate() {
  return builder.date;
 }

 /**
  * The Class Builder.
  */
 static class Builder {

  /** The date. */
  private Date date = new Date();

  /** The long description. */
  private String longDescription;

  /** The short description. */
  private String shortDescription;

  /** The info. */
  private String info;

  /** The priority. */
  private int priority = 0;

  /** The code. */
  private final int code;

  /**
   * Instantiates a new builder.
   * 
   * @param code
   *            the code
   */
  public Builder(int code) {
   this.code = code;
  }

  /**
   * Sets the short description.
   * 
   * @param shortDescription
   *            the short description
   * @return the builder
   */
  public Builder setShortDescription(String shortDescription) {
   this.shortDescription = shortDescription;
   return this;
  }

  /**
   * Sets the date.
   * 
   * @param date
   *            the date
   * @return the builder
   */
  public Builder setDate(Date date) {
   this.date = date;
   return this;
  }

  /**
   * Sets the info.
   * 
   * @param info
   *            the info
   * @return the builder
   */
  public Builder setInfo(String info) {
   this.info = info;
   return this;
  }

  /**
   * Sets the long description.
   * 
   * @param longDescription
   *            the long description
   * @return the builder
   */
  public Builder setLongDescription(String longDescription) {
   this.longDescription = longDescription;
   return this;
  }

  /**
   * Sets the priority.
   * 
   * @param priority
   *            the priority
   * @return the builder
   */
  public Builder setPriority(int priority) {
   this.priority = priority;
   return this;
  }
  
   /**
   * Builds the.
   *
   * @return the notification
   */
  public Notification build() {
   Notification notification = new Notification(this);
   return notification;
  }

 }
}

Suggested Pages