no.abmu.questionnarie.domain.Account.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.questionnarie.domain.Account.java

Source

/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2006 ABM-utvikling                        *
 *                                                                          *
 * This program is free software; you can redistribute it and/or modify it  *
 * under the terms of the GNU General Public License as published by the    *
 * Free Software Foundation; either version 2 of the License, or (at your   *
 * option) any later version.                                               *
 *                                                                          *
 * This program is distributed in the hope that it will be useful, but      *
 * WITHOUT ANY WARRANTY; without even the implied warranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
 * Public License for more details. http://www.gnu.org/licenses/gpl.html    *
 *                                                                          *
 ****************************************************************************
 */
package no.abmu.questionnarie.domain;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.validator.NotNull;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Erik Romson, erik@zenior.no
 * @author $Author:$
 * @version $Rev:$
 *          $Date:$
 *          copyright ABM-Utvikling
 * @hibernate.class table="FINANCE_ACCOUNT"
 * @hibernate.cache usage="nonstrict-read-write"
 * @assoc 1..* - 1..* no.abmu.finances.domain.Post
 */

@javax.persistence.Entity
//@Table(schema="QUESTIONNARIE")
//@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "QUESTIONNARIE_ACCOUNT")
public class Account implements no.abmu.common.domain.Entity {

    private static final Log logger = (Log) LogFactory.getLog(Account.class);

    public Long id;
    private String name;
    private String description;
    private Set<Post> posts = new HashSet<Post>();
    private Set<MainReportSchema> mainReportSchemas = new HashSet<MainReportSchema>();
    private Set<SubReportSchema> subReportSchemas = new HashSet<SubReportSchema>();

    /**
     * @return
     * @hibernate.id generator-class="increment"
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    /**
     * @hibernate.property not-null="true"
     * unique="true"
     */
    @NotNull
    @Column(unique = true)
    public String getName() {
        return name;
    }

    /**
     * @hibernate.property
     */
    public String getDescription() {
        return description;
    }

    /**
     * @hibernate.set lazy="false"
     * table="FINANCE_ACCOUNT_POST"
     * cascade="save-update"
     * @hibernate.key column="FK_ACCOUNT_ID"
     * @hibernate.many-to-many class="no.abmu.finances.domain.Post"
     * column="FK_POST_ID"
     * @hibernate.cache usage="nonstrict-read-write"
     * @hibernate.collection-key column="FK_ACCOUNT_ID"
     * @hibernate.collection-many-to-many class="no.abmu.finances.domain.Post"
     * column="FK_POST_ID"
     */
    @ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER /* TODO FETCH */
    )
    @JoinTable(
            //            schema = "QUESTIONNARIE"
            name = "QUESTIONNARIE_ACCOUNT_POST", joinColumns = {
                    @JoinColumn(name = "ACCOUNT_ID") }, inverseJoinColumns = { @JoinColumn(name = "POST_ID") })
    public Set<Post> getPosts() {
        return posts;
    }

    public void addPost(Post post) {
        post.addAccount(this);
        posts.add(post);
    }

    /**
     * @hibernate.set lazy="true"
     * inverse="true"
     * cascade="save-update"
     * table="FINANCE_REPORT_SCHEMA_ACCOUNT"
     * @hibernate.key column="FK_ACCOUNT_ID"
     * @hibernate.many-to-many class="no.abmu.finances.domain.ReportSchema"
     * column="FK_REPORT_SCHEMA_ID"
     * @hibernate.cache usage="nonstrict-read-write"
     * @hibernate.collection-key column="FK_ACCOUNT_ID"
     * @hibernate.collection-many-to-many class="no.abmu.finances.domain.ReportSchema"
     * column="FK_REPORT_SCHEMA_ID"
     */
    //    @ManyToMany(cascade = {CascadeType.ALL})
    //    @ForeignKey(name = "FK_REPORT_SCHEMA_ID", inverseName = "FK_REPORT_SCHEMA_ID")
    //    @JoinTable(
    //            name = "QUESTIONNARIE_ACCOUNT_REPORTSCHEMA"
    ////            schema = "QUESTIONNARIE"
    //    )
    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER /* TODO FETCH */
    )
    @JoinTable(
            //            schema = "QUESTIONNARIE"
            name = "QUESTIONNARIE_ACCOUNT_MAIN_REPORT_SCHEMA", joinColumns = {
                    @JoinColumn(name = "ACCOUNT_ID") }, inverseJoinColumns = { @JoinColumn(name = "RS_ID") })
    //    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public Set<MainReportSchema> getMainReportSchemas() {
        return mainReportSchemas;
    }

    @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER /* TODO FETCH */
    )
    @JoinTable(
            //            schema = "QUESTIONNARIE"
            name = "QUESTIONNARIE_ACCOUNT_SUB_REPORT_SCHEMA", joinColumns = {
                    @JoinColumn(name = "ACCOUNT_ID") }, inverseJoinColumns = { @JoinColumn(name = "RS_ID") })
    //    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public Set<SubReportSchema> getSubReportSchemas() {
        return subReportSchemas;
    }

    public void addReportSchema(ReportSchema reportSchema) {
        if (reportSchema instanceof MainReportSchema) {
            mainReportSchemas.add((MainReportSchema) reportSchema);
        } else {
            subReportSchemas.add((SubReportSchema) reportSchema);
        }
    }

    @Transient
    private Set<ReportSchema> getReportSchemas() {
        Set<ReportSchema> retSet = new HashSet<ReportSchema>();
        retSet.addAll(mainReportSchemas);
        retSet.addAll(subReportSchemas);
        return retSet;
    }

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

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

    public void setDescription(String description) {
        this.description = description;
    }

    private void setPosts(Set<Post> posts) {
        this.posts = posts;
    }

    private void setMainReportSchemas(Set<MainReportSchema> reportSchemas) {
        this.mainReportSchemas = reportSchemas;
    }

    private void setSubReportSchemas(Set<SubReportSchema> reportSchemas) {
        this.subReportSchemas = reportSchemas;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final Account account = (Account) o;

        if (description != null ? !description.equals(account.description) : account.description != null) {
            return false;
        }
        if (id != null ? !id.equals(account.id) : account.id != null) {
            return false;
        }
        if (!name.equals(account.name)) {
            return false;
        }

        return true;
    }

    public int hashCode() {
        int result;
        result = name.hashCode();
        result = 29 * result + (description != null ? description.hashCode() : 0);
        return result;
    }
}