Java tutorial
/*$Id: MuseumOrganizationFormFieldData.java 15427 2010-03-15 18:21: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.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.MuseumOrganizationForm; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * MuseumOrganizationFormFieldData. * * @author Aase Mestad * @author $Author: jens $ * @version $Rev: 15427 $ * @date $Date: 2010-03-15 19:21:38 +0100 (Mon, 15 Mar 2010) $ * @copyright ABM-Utvikling */ @SuppressWarnings("serial") @Entity //@Table(name="MUSEUM_ORGANISATIONFORM_FIELD_DATA", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) //@PrimaryKeyJoinColumn(name="id") @DiscriminatorValue("MuseumOrganizationForm") public class MuseumOrganizationFormFieldData extends FieldDataImpl { private static final Log logger = (Log) LogFactory.getLog(MuseumOrganizationFormFieldData.class); private Locale locale; private MuseumOrganizationForm museumOrganizationForm; public MuseumOrganizationFormFieldData() { } public MuseumOrganizationFormFieldData(Field field) { super(field); } @Transient public MuseumOrganizationForm getValue() { return museumOrganizationForm; } //TODO: Needs testing public void setValue(Object value) { // logger.debug("SetValue object value=[" + value + "]"); if (value == null) { if (this.museumOrganizationForm != null) { this.museumOrganizationForm = null; } return; } if (value instanceof MuseumOrganizationForm) { setMuseumOrganizationFormValue((MuseumOrganizationForm) 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()); // if (value instanceof MuseumOrganizationForm) { // this.museumOrganizationForm = MuseumOrganizationForm.getInstance(value.toString()); // } } @Enumerated(EnumType.STRING) @Column(name = "enumValue") public MuseumOrganizationForm getMuseumOrganizationFormValue() { return museumOrganizationForm; } public void setMuseumOrganizationFormValue(MuseumOrganizationForm value) { // logger.debug("SetValue MuseumOrganizationForm value=[" + value + "]"); if (value == null) { if (this.museumOrganizationForm != null) { this.museumOrganizationForm = null; } return; } if (value.equals(this.museumOrganizationForm)) { return; } this.museumOrganizationForm = value; } @Override @Transient public Object getUntypedValue() { return museumOrganizationForm; } public void setValueAsString(String value) { // logger.debug("SetValue String value=[" + value + "]"); if (value == null) { if (this.museumOrganizationForm != null) { this.museumOrganizationForm = null; } return; } String trimmedStringValue = value.trim(); if (trimmedStringValue.isEmpty()) { if (this.museumOrganizationForm != null) { this.museumOrganizationForm = null; } return; } MuseumOrganizationForm museumOrganizationForm = MuseumOrganizationForm.getInstance(trimmedStringValue); setMuseumOrganizationFormValue(museumOrganizationForm); // this.museumOrganizationForm = MuseumOrganizationForm.getInstance(value.toString()); } @Override public boolean acceptValue(Object value) { return (value instanceof MuseumOrganizationForm); } @Transient public Locale getLocale() { return locale; } @Transient public String getValueAsString() { Locale currentLocale = this.locale; if (currentLocale == null) { currentLocale = LocaleTypeNameConst.BOKMAAL; } return this.museumOrganizationForm.toString(currentLocale); } public void setLocale(Locale locale) { this.locale = locale; } @Transient public static List<MuseumOrganizationFormFieldData> getMuseumOrganizationFormReferenceList( Locale currentLocale) { List<MuseumOrganizationForm> museumOrganizationFormList = MuseumOrganizationForm.getEnumAsList(); List<MuseumOrganizationFormFieldData> fieldDatas = new ArrayList<MuseumOrganizationFormFieldData>(); for (MuseumOrganizationForm museumOrganizationForm : museumOrganizationFormList) { MuseumOrganizationFormFieldData fieldData = new MuseumOrganizationFormFieldData(); fieldData.setMuseumOrganizationFormValue(museumOrganizationForm); fieldData.setLocale(currentLocale); fieldDatas.add(fieldData); } return fieldDatas; } }