Java tutorial
/*$Id: QuestionnaireData.java 15586 2010-04-29 19:33:38Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2009 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.questionnaire.domain.data; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Transient; import no.abmu.organisationregister.domain.OrganisationUnit; import no.abmu.questionnaire.domain.constants.DbSchemaNameConst; import no.abmu.questionnaire.domain.metadata.Questionnaire; import no.abmu.questionnaire.domain.metadata.Schema; import no.abmu.questionnaire.domain.metadata.SubSchema; import no.abmu.util.test.Assert; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.annotations.Index; import org.hibernate.annotations.MapKey; /** * QuestionnaireData. * * @author Aase Mestad * @author $Author: jens $ * @version $Rev: 15586 $ * @date $Date: 2010-04-29 21:33:38 +0200 (Thu, 29 Apr 2010) $ * @copyright ABM-Utvikling */ @SuppressWarnings("serial") @Entity public class QuestionnaireData extends SchemaDataImpl { private static final Log logger = (Log) LogFactory.getLog(QuestionnaireData.class); private Questionnaire questionnaire; private OrganisationUnit organisationUnit; private Map<String, SubSchemaDataList> schemaDataListMap = new HashMap<String, SubSchemaDataList>(); public QuestionnaireData() { } public QuestionnaireData(Questionnaire questionnaire) { super(questionnaire); setQuestionnaire(questionnaire); } @ManyToOne(targetEntity = OrganisationUnit.class) @JoinColumn(name = "FK_ORGUNIT_ID") @Index(name = "QD_FK_ORGUNIT_ID_idx") public OrganisationUnit getOrganisationUnit() { return organisationUnit; } public void setOrganisationUnit(OrganisationUnit organisationUnit) { this.organisationUnit = organisationUnit; } @ManyToOne(targetEntity = Questionnaire.class, fetch = FetchType.EAGER) @JoinColumn(name = "FK_QUESTIONNAIRE_ID") public Questionnaire getQuestionnaire() { return questionnaire; } public void setQuestionnaire(Questionnaire questionnaire) { this.questionnaire = questionnaire; } /** * getReportDataList. * * @hibernate.map table="FINANCE_REPORT_DATA_LIST" cascade="all" lazy="false" * @hibernate.key column="FK_SUBREPDATA_ID" * @hibernate.index column="code" type="string" * @hibernate.one-to-many class="no.abmu.finances.domain.SubReportDataList" lazy="false" * @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(targetEntity = SubSchemaDataList.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinTable(name = "QUESTIONNAIRE_SUBSCHEMA_DATA_LIST", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) @MapKey(columns = { @Column(name = "key") }, targetElement = String.class) public Map<String, SubSchemaDataList> getSchemaDataListMap() { return schemaDataListMap; } public void setSchemaDataListMap(Map<String, SubSchemaDataList> schemaDataListMap) { this.schemaDataListMap = schemaDataListMap; } public void setSubSchemaDataList(Collection<SubSchemaDataList> subSchemaDataLists) { for (SubSchemaDataList subSchemaDataList : subSchemaDataLists) { schemaDataListMap.put(subSchemaDataList.getKey(), subSchemaDataList); } } /** * returns all added sub reports names. * * @return */ @Transient public String[] getSubSchemaDataNames() { return schemaDataListMap.keySet().toArray(new String[schemaDataListMap.size()]); } /** * 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<SubSchemaData> getSubSchemaDataList(String name) { SubSchemaDataList dataList = schemaDataListMap.get(name); if (dataList == null) { return null; } return dataList.getSubSchemaDatas(); } /** * Creates a new SubSchemaData if the schema of the questionnaire have an * available SubSchema with the right name. * * @param name name of the subSchema that represents the metadata of the SubSchemaData. * @return new SubSchemaData instantiated with the right subSchema */ public SubSchemaData createSubSchemaData(String subSchemaName) { Assert.checkRequiredArgument("subSchemaName", subSchemaName); SubSchema subSchema = questionnaire.getSubSchema(subSchemaName); if (subSchema == null) { String errorMessage = "Unknown subSchema=[" + subSchemaName + "]"; logger.error(errorMessage); throw new IllegalArgumentException(errorMessage); } SubSchemaData subSchemaData = new SubSchemaData(subSchema); subSchemaData.initializeFieldData(); return subSchemaData; } /** * Creates a new SubSchemaData if the schema of the questionnaire have an * available SubSchema with the right name. * * @param name name of the subSchema that represents the metadata of the SubSchemaData. * @return new SubSchemaData instantiated with the right subSchema */ public SubSchemaData createAndAddSubSchemaData(String subSchemaName) { Assert.checkRequiredArgument("subSchemaName", subSchemaName); SubSchemaData subSchemaData = createSubSchemaData(subSchemaName); addSubSchemaData(subSchemaData); return subSchemaData; } /** * Creates a new SubSchemaData if the schema of the questionnaire have an * available SubSchema with the right name. * * @param name name of the subSchema that represents the metadata of the SubSchemaData. * @return new SubSchemaData instantiated with the right subSchema */ public void addSubSchemaData(SubSchemaData subSchemaData) { Assert.checkRequiredArgument("subSchemaData", subSchemaData); Schema schema = subSchemaData.getSchema(); if (!(schema instanceof SubSchema)) { String errorMessage = "Schema is not of type SubSchema"; logger.equals(errorMessage); throw new IllegalArgumentException(errorMessage); } SubSchema subSchema = (SubSchema) schema; String subSchemaName = subSchema.getSubSchemaName(); SubSchemaDataList subSchemaDataList = getOrCreateSubSchemaDataList(subSchemaName); subSchemaDataList.addSubSchemaData(subSchemaData); } /** * Helper method that traverses the subSchemaList of this objects associated * schema (questionnaire) and returns the subSchema associated with name. * * @param name the name of the subSchema to return. * @return the right subSchema from the available subschemas of questionnaire */ /* @Transient private SubSchema getSubSchemaFromSubSchemaList(String name) { Set<SubSchema> subSchemaList = ((Questionnaire) getSchema()).getSubSchema(); for (SubSchema subSchema : subSchemaList) { if (subSchema.getSubSchemaName().equals(name)) { return subSchema; } } return null; } */ /** * Helper method that returns an already existing subSchemaDataList with a * given name or creates a new one and adds to the schemaDataListMap of * the questionnaire. * * @param name name of the schemaDataList with the given subSchema * @return new subSchemaDataList with the given name. */ @Transient private SubSchemaDataList getOrCreateSubSchemaDataList(String name) { SubSchemaDataList dataList = schemaDataListMap.get(name); if (dataList == null) { dataList = new SubSchemaDataList(name); schemaDataListMap.put(name, dataList); } return dataList; } }