org.antbear.jee.wicket.guestbook.model.Guestbook.java Source code

Java tutorial

Introduction

Here is the source code for org.antbear.jee.wicket.guestbook.model.Guestbook.java

Source

/*
 * Copyright 2011 Marcus Geiger.
 *
 * 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.
 */
package org.antbear.jee.wicket.guestbook.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import org.apache.commons.collections.list.UnmodifiableList;

/**
 * Entity representing a guestbook.
 *
 * @see http://wiki.eclipse.org/EclipseLink/UserGuide/JPA
 * @author Marcus Geiger
 */
@Entity
@NamedQueries({ @NamedQuery(name = "guestbook.findall", query = "select gb from Guestbook as gb") })
public class Guestbook implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable = false, length = 32, unique = true)
    private String name;

    @OneToMany(mappedBy = "guestbook", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @OrderBy("creationDate ASC")
    private List<Entry> entries = new ArrayList<Entry>();

    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;
    }

    @SuppressWarnings("unchecked")
    public List<Entry> getEntries() {
        return UnmodifiableList.decorate(entries); // Collections.unmodifiableList is not Serializable!
    }

    public void add(Entry entry) {
        if (entry == null)
            throw new NullPointerException("entry must not be null");

        int pos = Collections.binarySearch(entries, entry, new Entry.Comparator());
        if (pos < 0) {
            pos = -(pos + 1);
        }
        entries.add(pos, entry);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        hash += (name != null ? name.hashCode() : 0);
        hash += (entries != null ? entries.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Guestbook)) {
            return false;
        }
        Guestbook other = (Guestbook) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null && other.name != null) || (this.name != null && !this.name.equals(other.name))) {
            return false;
        }
        if ((this.entries == null && other.entries != null)
                || (this.entries != null && !this.entries.equals(other.entries))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.antbear.jee.wicket.guestbook.model.Guestbook[ id=" + id + ", name=" + name + ",entries={"
                + entries + "} ]";
    }

}