org.myorganisation.myproject.quickstart.backend.model.Person.java Source code

Java tutorial

Introduction

Here is the source code for org.myorganisation.myproject.quickstart.backend.model.Person.java

Source

/*
 * #%L
 * Nazgul Project: quickstart-model
 * %%
 * Copyright (C) 2015 MyOrganisation
 * %%
 * Licensed under the jGuru Europe AB license (the "License"), based
 * on Apache License, Version 2.0; you may not use this file except
 * in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * 
 *       http://www.jguru.se/licenses/jguruCorporateSourceLicense-2.0.txt
 * 
 * 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.
 * #L%
 */
package org.myorganisation.myproject.quickstart.backend.model;

import org.apache.commons.lang3.Validate;
import org.joda.time.DateTime;
import se.jguru.nazgul.core.persistence.model.NazgulEntity;
import se.jguru.nazgul.tools.validation.api.exception.InternalStateValidationException;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import java.util.Calendar;

/**
 * Person entity, normally written into a database using JPA.
 * Also JAXB-annotated, implying that each Person object can be converted to XML or JSON using JAXB.
 *
 * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
 */
@Entity
@Access(AccessType.FIELD)
@NamedQueries({ @NamedQuery(name = "Person.getAllPersons", query = "select a from Person a order by a.firstName"),
        @NamedQuery(name = "Person.getPersonsMatching", query = "select p from Person p "
                + "where p.firstName like :firstName and p.lastName like :lastName") })
@XmlType(namespace = Model.NAMESPACE, propOrder = { "firstName", "lastName", "birthday" })
@XmlAccessorType(XmlAccessType.FIELD)
public class Person extends NazgulEntity {

    // Internal state
    @Basic(optional = false)
    @Column(nullable = false)
    @XmlElement(required = true, nillable = false)
    private String firstName;

    @Basic(optional = false)
    @Column(nullable = false)
    @XmlElement(required = true, nillable = false)
    private String lastName;

    @Basic(optional = false)
    @Column(nullable = false)
    @Temporal(TemporalType.DATE)
    @XmlAttribute(required = true)
    private Calendar birthday;

    /**
     * JPA/JAXB-friendly constructor.
     */
    public Person() {
    }

    /**
     * Compound constructor creating a Person wrapping the supplied data.
     *
     * @param firstName The first name of this Person.
     * @param lastName  The last name of this Person.
     * @param birthday  The birthday of this Person.
     */
    public Person(final String firstName, final String lastName, final DateTime birthday) {

        this.firstName = firstName;
        this.lastName = lastName;
        this.birthday = birthday.toGregorianCalendar();
    }

    /**
     * @return The first name of this Person.
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @return The last name of this Person.
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the first name of this Person.
     *
     * @param firstName The new First Name of this Person. Cannot be null or empty.
     */
    public void setFirstName(final String firstName) {

        // Check sanity
        Validate.notEmpty(firstName, "Cannot handle null or empty 'firstName' argument.");

        // Assign internal state
        this.firstName = firstName;
    }

    /**
     * Sets the last name of this Person.
     *
     * @param lastName The new Last Name of this Person. Cannot be null or empty.
     */
    public void setLastName(final String lastName) {

        // Check sanity
        Validate.notEmpty(lastName, "Cannot handle null or empty 'lastName' argument.");

        // Assign internal state
        this.lastName = lastName;
    }

    /**
     * @return The birthday of this Person.
     */
    public DateTime getBirthday() {
        return new DateTime(birthday);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void validateEntityState() throws InternalStateValidationException {

        InternalStateValidationException.create().notNull(firstName, "firstName").notNull(lastName, "lastName")
                .notNull(birthday, "birthday").endExpressionAndValidate();
    }

    /**
     * Convenience factory method which can set the JPA ID in addition to the Person's data.
     * As the JPA ID is normally automatically assigned by the database, the JPA ID is normally
     * retrieved <strong>from</strong> the database following a JPA persist operation.
     *
     * @param jpaId     The JPA ID to set.
     * @param firstName The first name of this Person.
     * @param lastName  The last name of this Person.
     * @param birthday  The birthday of this Person.
     * @return A fully created Person entity.
     */
    public static Person create(final long jpaId, final String firstName, final String lastName,
            final DateTime birthday) {

        final Person toReturn = new Person(firstName, lastName, birthday);
        toReturn.setId(jpaId);
        return toReturn;
    }
}