Java tutorial
/* **************************************************************************** * * * (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.CollectionOfElements; import org.hibernate.annotations.ForeignKey; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.proxy.HibernateProxy; import javax.persistence.*; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /** * @author Erik Romson, erik@zenior.no * @author $Author:$ * @version $Rev:$ * @date $Date:$ * @copyright ABM-Utvikling * @hibernate.subclass discriminator-value="main_data" * @hibernate.cache usage="nonstrict-read-write" * @navassoc 1 - * no.abmu.finances.domain.SubReportDataList */ @javax.persistence.Entity @javax.persistence.Table(name = "QUESTIONNARIE_MAIN_REPORT_DATA") public class MainReportData extends ReportData { private static final Log logger = (Log) LogFactory.getLog(MainReportData.class); private Map<String, SubReportDataList> reportDataList = new HashMap<String, SubReportDataList>(); private ReportDataOrgUnitMapping orgUnitMapping; private MainReportData() { super(); } public MainReportData(ReportSchema reportSchema) { super(reportSchema); } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return super.getId(); } @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER /* TODO FETCH */) @MapKey(name = "code") @JoinColumn(name = "mainreportdata_fk") public Map<String, PostData> getPostData() { return super.getPostData(); } //cascade = {CascadeType.ALL} @ManyToOne() @Fetch(value = FetchMode.JOIN) // @ForeignKey(name = "FK_REPORT_SCHEMA_ID", inverseName = "FK_REPORT_SCHEMA_ID") public MainReportSchema getReportSchema() { return (MainReportSchema) super.getReportSchema(); } /** * @hibernate.map table="FINANCE_REPORT_DATA_LIST" cascade="all" * @hibernate.key column="FK_SUBREPDATA_ID" * @hibernate.index column="code" * type="string" * @hibernate.one-to-many class="no.abmu.finances.domain.SubReportDataList" * @hibernate.cache usage="nonstrict-read-write" * @hibernate.collection-key column="FK_SUBREPDATA_ID" * @hibernate.collection-index column="code" * type="string" * @hibernate.collection-one-to-many class="no.abmu.finances.domain.SubReportDataList" * @ hibernate.collection-cache usage="nonstrict-read-write" */ @OneToMany(cascade = CascadeType.ALL) @MapKey(name = "key") @JoinColumn(name = "mrd_fk") public Map<String, SubReportDataList> getReportDataList() { return reportDataList; } private void setReportDataList(Map<String, SubReportDataList> reportDataList) { this.reportDataList = reportDataList; } @ManyToOne(cascade = { CascadeType.ALL }) @ForeignKey(name = "FK_ORGU_MAPPING_ID") public ReportDataOrgUnitMapping getOrgUnitMapping() { return orgUnitMapping; } protected void setOrgUnitMapping(ReportDataOrgUnitMapping orgUnitMapping) { this.orgUnitMapping = orgUnitMapping; } public SubReportData createSubReportData(String name) { // ReportSchema reportSchema=getReportSchema(); // if (reportSchema instanceof HibernateProxy){ // ((HibernateProxy)reportSchema) // } Set subReportSchemas = ((MainReportSchema) getReportSchema()).getSubReportSchemas(); SubReportSchema subReportSchema = null; for (Object subReportSchema1 : subReportSchemas) { SubReportSchema srSchema = (SubReportSchema) subReportSchema1; if (srSchema.getName().equals(name)) { subReportSchema = srSchema; break; } } if (subReportSchema == null) { throw new IllegalArgumentException("There was no subreport schema called " + name); } SubReportDataList dataList = (SubReportDataList) reportDataList.get(name); if (dataList == null) { dataList = new SubReportDataList(name); reportDataList.put(name, dataList); } SubReportData subReportData = new SubReportData(subReportSchema); dataList.addSubReportData(subReportData); return subReportData; } /** * returns a list of subreport datas for the subschema that has name * * @param name * @return umodifiable list or null if there is none */ @Transient public List getSubReportDataList(String name) { SubReportDataList dataList = (SubReportDataList) reportDataList.get(name); if (dataList == null) { return null; } return dataList.getSubReportDatas(); } /** * sets a list of sub report data * * @param name * @param subReportDataList the list to set * @throws IllegalArgumentException if there was no list with name */ public void setSubReportDataList(String name, List subReportDataList) { SubReportDataList dataList = (SubReportDataList) reportDataList.get(name); if (dataList == null) { throw new IllegalArgumentException("There is no subreport list with name " + name); } dataList.setSubReportDatas(subReportDataList); } /** * returns all added sub reports names * * @return */ @Transient public String[] getSubReportDataNames() { return (String[]) reportDataList.keySet().toArray(new String[reportDataList.size()]); } /** * returns all possible sub report schema names */ @Transient public String[] getSubReportSchemaNames() { Set<String> retNames = new HashSet<String>(); Iterator it = ((MainReportSchema) getReportSchema()).getSubReportSchemas().iterator(); while (it.hasNext()) { SubReportSchema subReportSchema = (SubReportSchema) it.next(); retNames.add(subReportSchema.getName()); } return (String[]) retNames.toArray(new String[retNames.size()]); } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final MainReportData that = (MainReportData) o; if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) { return false; } if (getReportSchema() != null ? !getReportSchema().equals(that.getReportSchema()) : that.getReportSchema() != null) { return false; } return true; } public int hashCode() { return (getReportSchema() != null ? getReportSchema().hashCode() : 0); } }