Java tutorial
/*$Id: MuseumHovedOmraadeFieldData.java 15541 2010-04-20 15:45:18Z 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.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.MuseumHovedOmraade; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * MuseumHovedOmraadeFieldData. * * @author Aase Mestad * @author $Author: jens $ * @version $Rev: 15541 $ * @date $Date: 2010-04-20 17:45:18 +0200 (Tue, 20 Apr 2010) $ * @copyright ABM-Utvikling */ @SuppressWarnings("serial") @Entity //@Table(name="MUSEUM_MAINFIELD_FIELD_DATA", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) //@PrimaryKeyJoinColumn(name="id") @DiscriminatorValue("MuseumHovedOmraade") public class MuseumHovedOmraadeFieldData extends FieldDataImpl { private static final Log logger = (Log) LogFactory.getLog(MuseumHovedOmraadeFieldData.class); private Locale locale; private MuseumHovedOmraade museumHovedOmraade; public MuseumHovedOmraadeFieldData() { } public MuseumHovedOmraadeFieldData(Field field) { super(field); } @Enumerated(EnumType.STRING) @Column(name = "enumValue") public MuseumHovedOmraade getMuseumHovedOmraadeValue() { // logger.debug("getMuseumHovedOmraadeValue"); return museumHovedOmraade; } public void setMuseumHovedOmraadeValue(MuseumHovedOmraade value) { logger.debug("SetValue MuseumHovedOmraade value=[" + value + "]"); if (value != this.museumHovedOmraade) { this.museumHovedOmraade = value; } } /* ------ All the following methods are transient. ------ */ @Transient public MuseumHovedOmraade getValue() { // logger.debug("getValue"); return museumHovedOmraade; } //TODO: Needs testing @Transient public void setValue(Object value) { logger.debug("SetValue object value=[" + value + "] which is of type [" + (value == null ? "null" : value.getClass().getName()) + "]"); if (value == null) { if (this.museumHovedOmraade != null) { this.museumHovedOmraade = null; } return; } if (value instanceof MuseumHovedOmraade) { setMuseumHovedOmraadeValue((MuseumHovedOmraade) 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() { // logger.debug("getUntypedValue"); return museumHovedOmraade; } @Transient public void setValueAsString(String value) { logger.debug("setValueAsString String value=[" + value + "]"); if (value == null) { if (this.museumHovedOmraade != null) { this.museumHovedOmraade = null; } return; } String trimmedStringValue = value.trim(); if (trimmedStringValue.isEmpty()) { logger.warn("Executing setValueAsString on empty string."); if (this.museumHovedOmraade != null) { this.museumHovedOmraade = null; } return; } MuseumHovedOmraade museumHovedOmraade = MuseumHovedOmraade.getInstance(trimmedStringValue); setMuseumHovedOmraadeValue(museumHovedOmraade); } @Transient public Locale getLocale() { return locale; } @Transient public void setLocale(Locale locale) { this.locale = locale; } @Transient public String getValueAsString() { Locale currentLocale = this.locale; if (currentLocale == null) { currentLocale = LocaleTypeNameConst.BOKMAAL; } return this.museumHovedOmraade.toString(currentLocale); } @Override @Transient public boolean acceptValue(Object value) { return (value instanceof MuseumHovedOmraade); } @Transient public static List<MuseumHovedOmraadeFieldData> getMuseumHovedOmraadeReferenceList(Locale currentLocale) { List<MuseumHovedOmraade> museumHovedOmraadeList = MuseumHovedOmraade.getEnumAsList(); List<MuseumHovedOmraadeFieldData> fieldDatas = new ArrayList<MuseumHovedOmraadeFieldData>(); for (MuseumHovedOmraade museumHovedOmraade : museumHovedOmraadeList) { MuseumHovedOmraadeFieldData fieldData = new MuseumHovedOmraadeFieldData(); fieldData.setMuseumHovedOmraadeValue(museumHovedOmraade); fieldData.setLocale(currentLocale); fieldDatas.add(fieldData); } return fieldDatas; } }