001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model20.impl.preferences;
023:
024: import org.jboss.portal.migration.model20.model.Preference;
025: import org.jboss.portal.migration.model20.other.StringValues;
026: import org.jboss.portal.migration.model20.other.Value;
027:
028: /**
029: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
030: * @version $Revision: 8784 $
031: * @hibernate.class table="test_pref"
032: */
033: public class MappedPreference implements Preference {
034:
035: private static final String[] EMPTY_STRING_ARRAY = new String[0];
036: private static final Value EMPTY_VALUE = new StringValues(
037: EMPTY_STRING_ARRAY);
038:
039: /*
040: // The id
041: private Integer id;
042:
043: // The name
044: private String name;
045:
046: // The db value
047: private int type;
048: private String[] strings;
049: */
050:
051: // The id
052: public Integer id;
053:
054: // The name
055: public String name;
056:
057: // The db value
058: public int type;
059: public String[] strings;
060:
061: // The value which is actually equals to type+strings
062: private Value value;
063:
064: // This flag is used because the value is made up of strings and type
065: // and it is not possible to update the value when strings or type
066: // is updated, so when strings of type is modified with set dirty
067: private boolean dirty;
068:
069: public MappedPreference() {
070: this .id = null;
071: this .name = null;
072: this .type = 0;
073: this .strings = EMPTY_STRING_ARRAY;
074: this .value = EMPTY_VALUE;
075: this .dirty = false;
076: }
077:
078: public MappedPreference(String name) {
079: this .id = null;
080: this .name = name;
081: this .type = 0;
082: this .strings = EMPTY_STRING_ARRAY;
083: this .value = EMPTY_VALUE;
084: }
085:
086: /** @hibernate.id column="jbp_id" generator-class="native" */
087: protected Integer getID() {
088: return id;
089: }
090:
091: /** Called by hibernate. */
092: protected void setID(Integer id) {
093: this .id = id;
094: }
095:
096: /** @hibernate.property column="jbp_name" not-null="true" update="false" */
097: public String getName() {
098: return name;
099: }
100:
101: /** Called by hibernate. */
102: protected void setName(String name) {
103: this .name = name;
104: }
105:
106: /** @hibernate.property column="jbp_type" */
107: public int getType() {
108: return type;
109: }
110:
111: /** Called by hibernate. */
112: protected void setType(int type) {
113: this .type = type;
114: this .dirty = true;
115: }
116:
117: /**
118: * @hibernate.array table="test_prop_value" cascade="all"
119: * @hibernate.collection-key column="jbp_prop_id"
120: * @hibernate.collection-index column="jbp_idx"
121: * @hibernate.collection-element column="jbp_value" type="string"
122: */
123: public String[] getStrings() {
124: return strings;
125: }
126:
127: public void setValue(Value value) {
128: TypedStringArray tsa = ValueManager
129: .createTypedStringArray(value);
130: this .value = value;
131: this .type = tsa.getType();
132: this .strings = tsa.getStrings();
133: }
134:
135: /** Called by hibernate. */
136: private void setStrings(String[] strings) {
137: this .strings = strings;
138: this .dirty = true;
139: }
140:
141: public Value getValue() {
142: if (dirty) {
143: value = ValueManager.createValue(new TypedStringArray(type,
144: strings));
145: dirty = false;
146: }
147: return value;
148: }
149:
150: /** By default it's never read only. Subclasses may provide custom behaviour here. */
151: public boolean isReadOnly() {
152: return false;
153: }
154:
155: /** Provide a default impl. */
156: public String toString() {
157: StringBuffer buffer = new StringBuffer("[");
158: buffer.append(id).append(",").append(name).append(",");
159: if (strings == null) {
160: buffer.append("null,");
161: } else {
162: buffer.append("(");
163: for (int i = 0; i < strings.length; i++) {
164: String s = strings[i];
165: buffer.append(i > 0 ? "," : "").append(s);
166: }
167: buffer.append("),");
168: }
169: buffer.append(value).append("]");
170: return buffer.toString();
171: }
172: }
|