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

Java tutorial

Introduction

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

Source

/*$Id: WhoExtinguishedFieldData.java 15311 2010-03-08 09:19:19Z jens $*/
/*
 ****************************************************************************
 *                                                                          *
 *                   (c) Copyright 2010 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.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Transient;

import no.abmu.common.constants.LocaleTypeNameConst;
import no.abmu.questionnaire.domain.metadata.Field;
import no.abmu.questionnaire.domain.types.WhoExtinguished;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Field for handling yes/no/partly-fields.
 *
 * @author Aase Mestad
 * @author $Author: jens $
 * @version $Rev: 15311 $
 * @date $Date: 2010-03-08 10:19:19 +0100 (Mon, 08 Mar 2010) $
 * @copyright ABM-Utvikling
 */
@SuppressWarnings("serial")
@Entity
@DiscriminatorValue("WhoExtinguished")
public class WhoExtinguishedFieldData extends FieldDataImpl {

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

    private Locale locale;
    private WhoExtinguished whoExtinguishedValue;

    public WhoExtinguishedFieldData() {
    }

    public WhoExtinguishedFieldData(Field field) {
        super(field);
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "enumValue")
    public WhoExtinguished getWhoExtinguishedValue() {
        return whoExtinguishedValue;
    }

    public void setWhoExtinguishedValue(WhoExtinguished value) {
        logger.info("SetValue setWhoExtinguishedValue value=[" + value + "]");

        if (value == null) {
            if (this.whoExtinguishedValue != null) {
                this.whoExtinguishedValue = null;
            }
            return;
        }

        if (value.equals(this.whoExtinguishedValue)) {
            return;
        }

        this.whoExtinguishedValue = value;
    }

    /*  ------   All the following method are transient.  ------  */

    @Transient
    public WhoExtinguished getValue() {
        return whoExtinguishedValue;
    }

    public void setValue(Object value) {
        logger.info("SetValue object value=[" + value + "]");
        if (value == null) {
            if (this.whoExtinguishedValue != null) {
                this.whoExtinguishedValue = null;
            }
            return;
        }

        if (value instanceof WhoExtinguished) {
            setWhoExtinguishedValue((WhoExtinguished) value);
            return;
        }

        if (value instanceof String) {
            String stringValue = (String) value;
            setValueAsString(stringValue);
            return;
        }

        logger.warn("Unknown dataType for " + this.toString() + " Code=[" + this.getCode()
                + "] DataType received was " + value.getClass().getName());

    }

    @Override
    @Transient
    public Object getUntypedValue() {
        return whoExtinguishedValue;
    }

    public void setValueAsString(String value) {
        logger.info("SetValue String value=[" + value + "]");
        if (value == null) {
            if (this.whoExtinguishedValue != null) {
                this.whoExtinguishedValue = null;
            }
            return;
        }
        String trimmedStringValue = value.trim();
        if (trimmedStringValue.isEmpty()) {
            if (this.whoExtinguishedValue != null) {
                this.whoExtinguishedValue = null;
            }
            return;
        }

        WhoExtinguished whoExtinguished = WhoExtinguished.getInstance(trimmedStringValue);
        setWhoExtinguishedValue(whoExtinguished);
    }

    @Transient
    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    @Transient
    public String getValueAsString() {
        Locale currentLocale = this.locale;

        if (currentLocale == null) {
            currentLocale = LocaleTypeNameConst.BOKMAAL;
        }
        return this.whoExtinguishedValue.toString(currentLocale);
    }

    @Override
    public boolean acceptValue(Object value) {
        return (value instanceof WhoExtinguished);
    }

    @Transient
    public static List<WhoExtinguishedFieldData> getWhoExtinguishedReferenceList(Locale currentLocale) {
        List<WhoExtinguished> whoExtinguishedList = WhoExtinguished.getEnumAsList();
        List<WhoExtinguishedFieldData> whoExtinguishedFieldDatas = new ArrayList<WhoExtinguishedFieldData>();
        for (WhoExtinguished whoExtinguished : whoExtinguishedList) {
            WhoExtinguishedFieldData fieldData = new WhoExtinguishedFieldData();
            fieldData.setWhoExtinguishedValue(whoExtinguished);
            fieldData.setLocale(currentLocale);
            whoExtinguishedFieldDatas.add(fieldData);
        }
        return whoExtinguishedFieldDatas;
    }

}