net.eusashead.hateoas.springhalbuilder.model.Order.java Source code

Java tutorial

Introduction

Here is the source code for net.eusashead.hateoas.springhalbuilder.model.Order.java

Source

package net.eusashead.hateoas.springhalbuilder.model;

/*
 * #[license]
 * Spring HalBuilder Example Webapp
 * %%
 * Copyright (C) 2013 Eusa's Head
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * %[license]
 */

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import net.eusashead.hateoas.hal.adapter.annotation.RepresentationWriterAdapter;
import net.eusashead.hateoas.springhalbuilder.representation.OrderRepresentationWriter;

import com.fasterxml.jackson.annotation.JsonIgnore;

@RepresentationWriterAdapter(OrderRepresentationWriter.class)
@Entity
@Table(name = "ORDERS", schema = "PUBLIC")
public class Order implements Serializable {

    private static final long serialVersionUID = -2638341954941154737L;

    @Id
    @Column(name = "ORDER_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer orderId;

    @Basic
    @Column(name = "DATE")
    private Date date;

    @Version
    @Column(name = "VERSION")
    private Integer version;

    @Column(name = "CREATED")
    @Temporal(TemporalType.DATE)
    private Date created;

    @Column(name = "UPDATED")
    @Temporal(TemporalType.DATE)
    private Date updated;

    @ManyToOne(targetEntity = Customer.class)
    @JoinColumns({ @JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID", nullable = false) })
    private Customer customer;

    @OneToMany(targetEntity = LineItem.class, mappedBy = "order")
    private List<LineItem> lineItems = new ArrayList<LineItem>();

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @PreUpdate
    @PrePersist
    public void updateTimeStamps() {
        Date now = new Date();
        this.updated = now;
        if (created == null) {
            created = now;
        }
    }

    @Transient
    @JsonIgnore
    public BigDecimal getTotal() {
        double total = 0d;
        for (LineItem item : lineItems) {
            total += item.getSubTotal().doubleValue();
        }
        return BigDecimal.valueOf(total);
    }

}