no.abmu.questionnaire.domain.data.SchemaDataImpl.java Source code

Java tutorial

Introduction

Here is the source code for no.abmu.questionnaire.domain.data.SchemaDataImpl.java

Source

/*$Id: SchemaDataImpl.java 14740 2010-02-03 09:16:34Z 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.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import no.abmu.questionnaire.domain.constants.DbSchemaNameConst;
import no.abmu.questionnaire.domain.metadata.Field;
import no.abmu.questionnaire.domain.metadata.Schema;
import no.abmu.questionnaire.domain.metadata.SchemaImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.MapKey;

/**
 * Implementing a superclass for Questionnaire and SubSchema. 
 * SchemaDataImpl holds a map that maps field codes to fieldData 
 * instances for the schema. SchemaDataImpl also have an association 
 * to the corresponding schema definition.
 *
 * @author Aase Mestad
 * @author $Author: jens $
 * @version $Rev: 14740 $
 * @date $Date: 2010-02-03 10:16:34 +0100 (Wed, 03 Feb 2010) $
 * @copyright ABM-Utvikling
 */
@SuppressWarnings("serial")
@Entity
@Inheritance
@Table(name = "SCHEMADATA", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME)
public class SchemaDataImpl extends DomainObject implements SchemaData {

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

    private Map<String, FieldData> fieldDataMap = new HashMap<String, FieldData>();

    private Schema schema;

    public SchemaDataImpl() {
    }

    public SchemaDataImpl(Schema schema) {
        this.schema = schema;
    }

    @ManyToOne(targetEntity = SchemaImpl.class)
    @Index(name = "schemaDataImpl_schema_fk_idx")
    public Schema getSchema() {
        return schema;
    }

    public void setSchema(Schema schema) {
        this.schema = schema;
    }

    /**
     * column="id"
     * @hibernate.map table="FINANCE_POST_DATA" cascade="all" outer-join="true" batch-size="50" lazy="false"
     * @hibernate.key column="coll_key"
     * 
     * @hibernate.index column="code" type="string"
     * @hibernate.one-to-many class="no.abmu.finances.domain.PostData"
     * @hibernate.cache usage="nonstrict-read-write"
     * @hibernate.collection-key column="coll_key"
     * @hibernate.collection-index column="code" type="string"
     * @hibernate.collection-one-to-many column="id" class="no.abmu.finances.domain.PostData"
     *
     *    hibernate.collection-cache usage="nonstrict-read-write"
     */

    @OneToMany(targetEntity = FieldDataImpl.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "SCHEMADATA_FIELDDATA_MAP", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME)
    @MapKey(columns = { @Column(name = "code") }, targetElement = String.class)
    public Map<String, FieldData> getFieldData() {
        return fieldDataMap;
    }

    public void setFieldData(Map<String, FieldData> fieldData) {
        this.fieldDataMap = fieldData;
    }

    @Transient
    public FieldData getFieldData(String code) {
        return fieldDataMap.get(code);
    }

    public void initializeFieldData() {
        Collection<Field> localFields = schema.getFields();
        for (Field fld : localFields) {
            String code = fld.getCode();
            createOrGetFieldData(code);
        }
    }

    public FieldData createOrGetFieldData(String code) {
        if (logger.isDebugEnabled()) {
            logger.debug("Execute createOrGetFieldData with code='" + code + "'.");
        }
        FieldData fieldData = getFieldData(code);
        Field field;
        if (fieldData == null) {
            logger.info("[FieldData] fieldData == null, for code ='" + code + "'.");
            try {
                field = schema.getField(code);
            } catch (Exception e) {
                throw new IllegalArgumentException(
                        "Exception --- There is no field for this schema with code [" + code + "].");
            }

            if (field == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("==========================================================");
                    for (Field fld : schema.getFields()) {
                        logger.debug("Field code: " + fld.getCode());
                        logger.debug("Field id: " + fld.getId());
                    }
                    logger.debug("==========================================================");
                }
                throw new IllegalArgumentException(
                        "field == null. There is no field for this schema with code [" + code + "].");
            }

            fieldData = field.createFieldData();
            addFieldData(fieldData);
        } else {
            logger.info(
                    "[FieldData] fieldData , for code ='" + code + "' has value=[" + fieldData.toString() + "].");
        }
        return fieldData;
    }

    public void addFieldData(FieldData fieldDataValue) {
        fieldDataMap.put(fieldDataValue.getCode(), fieldDataValue);
    }

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

        SchemaDataImpl that = (SchemaDataImpl) o;

        if (schema != null ? !schema.equals(that.schema) : that.schema != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return schema != null ? schema.hashCode() : 0;
    }
}