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.apache.log4j.Logger;
025: import org.jboss.portal.migration.model20.model.Preference;
026: import org.jboss.portal.migration.model20.model.PreferenceSet;
027: import org.jboss.portal.migration.model20.other.FQN;
028: import org.jboss.portal.migration.model20.other.Value;
029:
030: import java.util.HashMap;
031: import java.util.HashSet;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.Set;
035:
036: /**
037: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
038: * @version $Revision: 8784 $
039: * @hibernate.class table="test_pref_set"
040: */
041: public class MappedPreferenceSet implements PreferenceSet {
042:
043: /** Our logger. */
044: private static Logger log = Logger
045: .getLogger(MappedPreferenceSet.class);
046:
047: public Integer id;
048: private MappedPreferenceSet parent;
049: private Set children;
050: private String name;
051: private FQN fqn;
052: protected Map map;
053:
054: /** Hibernate constructor. */
055: public MappedPreferenceSet() {
056: this .children = new HashSet();
057: this .id = null;
058: this .name = null;
059: this .parent = null;
060: this .fqn = null;
061: this .map = null;
062: }
063:
064: public MappedPreferenceSet(String name) {
065: this .children = new HashSet();
066: this .id = null;
067: this .name = name;
068: this .parent = null;
069: this .fqn = new FQN();
070: this .map = new HashMap();
071: }
072:
073: /** @hibernate.id column="jbp_id" generator-class="native" */
074: public Integer getID() {
075: return id;
076: }
077:
078: /** Called by hibernate. */
079: protected void setID(Integer id) {
080: this .id = id;
081: }
082:
083: /** @hibernate.property columns="jbp_name" not-null="true" update="false" */
084: public String getName() {
085: return name;
086: }
087:
088: /** Called by hibernate. */
089: protected void setName(String name) {
090: this .name = name;
091: }
092:
093: /** @hibernate.many-to-one column="jbp_parent_id" cascade="none" */
094: public MappedPreferenceSet getParent() {
095: return parent;
096: }
097:
098: public void setParent(MappedPreferenceSet parent) {
099: this .parent = parent;
100: }
101:
102: public void addChild(MappedPreferenceSet child) {
103: children.add(child);
104: child.setParent(this );
105: }
106:
107: /**
108: * @hibernate.set inverse="true" lazy="false" cascade="all-delete-orphan"
109: * @hibernate.collection-key column="jbp_parent_id"
110: * @hibernate.collection-one-to-many class="org.jboss.portal.core.impl.preferences.MappedPreferenceSet"
111: */
112: public Set getChildren() {
113: return children;
114: }
115:
116: public void setChildren(Set children) {
117: this .children = children;
118: }
119:
120: public MappedPreferenceSet getDescendant(FQN fqn) {
121: // We start by ourself
122: boolean debug = log.isDebugEnabled();
123: MappedPreferenceSet current = this ;
124:
125: // Iterate on the fqn
126: for (int i = 0; i < fqn.size(); i++) {
127: // Current name
128: String name = fqn.getName(i).toString();
129: if (debug) {
130: log.debug("Using for " + this .id + "/" + this .name
131: + " name (" + i + "," + name + ")");
132: }
133:
134: // Try to get next psc
135: MappedPreferenceSet next = null;
136: for (Iterator j = current.getChildren().iterator(); j
137: .hasNext();) {
138: MappedPreferenceSet child = (MappedPreferenceSet) j
139: .next();
140: String childName = child.getName();
141: if (name.equals(childName)) {
142: next = child;
143: if (debug) {
144: log.debug("Found for " + this .id + "/"
145: + this .name + " child " + childName);
146: }
147: break;
148: }
149: }
150:
151: // It does not exist, create it
152: if (next == null) {
153: if (debug) {
154: log.debug("Creating child for " + this .id + "/"
155: + this .name);
156: }
157: next = newChild(name);
158: current.addChild(next);
159: }
160:
161: current = next;
162: }
163:
164: return current;
165: }
166:
167: public FQN getFQN() {
168: if (fqn == null) {
169: if (parent == null) {
170: this .fqn = new FQN();
171: } else {
172: this .fqn = new FQN(parent.getFQN(), name);
173: }
174: }
175: return fqn;
176: }
177:
178: /**
179: * @hibernate.map cascade="all" inverse="false"
180: * @hibernate.collection-key column="jbp_set_id"
181: * @hibernate.collection-index column="jbp_name" type="string"
182: * @hibernate.collection-one-to-many class="org.jboss.portal.core.impl.preferences.MappedPreference"
183: */
184: public Map getContent() {
185: return map;
186: }
187:
188: public void setContent(Map content) {
189: this .map = content;
190: }
191:
192: //
193:
194: public Set keySet() {
195: return map.keySet();
196: }
197:
198: public Preference getPreference(String key) {
199: if (key == null) {
200: throw new IllegalArgumentException("key must not be null");
201: }
202: return (Preference) map.get(key);
203: }
204:
205: public Value getValue(String key) {
206: if (key == null) {
207: throw new IllegalArgumentException("key must not be null");
208: }
209: Preference preference = getPreference(key);
210: if (preference != null) {
211: return preference.getValue();
212: } else {
213: return null;
214: }
215: }
216:
217: public void setValue(String key, Value value) {
218: if (key == null) {
219: throw new IllegalArgumentException("key must not be null");
220: }
221: if (value == null) {
222: map.remove(key);
223: } else {
224: MappedPreference pref = (MappedPreference) map.get(key);
225: if (pref != null) {
226: pref.setValue(value);
227: } else {
228: map.put(key, newPreference(key, value, false));
229: }
230: }
231: }
232:
233: public boolean isReadOnly(String key) {
234: if (key == null) {
235: throw new IllegalArgumentException("key must not be null");
236: }
237: return false;
238: }
239:
240: public void setReadOnly(String key, boolean readOnly) {
241: throw new UnsupportedOperationException(
242: "The mapping does not support the read only flag");
243: }
244:
245: // Overrrided *******************************************************************************************************
246:
247: /** Override to provide the right object. */
248: protected MappedPreferenceSet newChild(String name) {
249: return new MappedPreferenceSet(name);
250: }
251:
252: /** Override to provide the right object. */
253: protected Preference newPreference(String name, Value value,
254: boolean readOnly) {
255: MappedPreference preference = new MappedPreference(name);
256: preference.setValue(value);
257: return preference;
258: }
259: }
|