KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > map > Embeddable


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.map;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.SortedMap JavaDoc;
26 import java.util.TreeMap JavaDoc;
27
28 import org.apache.cayenne.util.XMLEncoder;
29 import org.apache.cayenne.util.XMLSerializable;
30
31 /**
32  * A mapping descriptor of an embeddable class. Embeddable is a persistent class that
33  * doesn't have its own identity and is embedded in other persistent classes. It can be
34  * viewed as a custom type mapped to one or more database columns. Embeddable mapping can
35  * include optional default column names that can be overriden by the owning entity.
36  *
37  * @since 3.0
38  * @author Andrus Adamchik
39  */

40 public class Embeddable implements XMLSerializable, Serializable JavaDoc {
41
42     protected String JavaDoc className;
43     protected SortedMap JavaDoc attributes;
44
45     public Embeddable() {
46         this(null);
47     }
48
49     public Embeddable(String JavaDoc className) {
50         this.attributes = new TreeMap JavaDoc();
51         this.className = className;
52     }
53
54     /**
55      * Returns EmbeddableAttribute of this Embeddable that maps to
56      * <code>dbAttribute</code> parameter. Returns null if no such attribute is found.
57      */

58     public EmbeddableAttribute getAttributeForDbPath(String JavaDoc dbPath) {
59         Iterator JavaDoc it = attributes.values().iterator();
60         while (it.hasNext()) {
61             EmbeddableAttribute attribute = (EmbeddableAttribute) it.next();
62             if (dbPath.equals(attribute.getDbAttributeName())) {
63                 return attribute;
64             }
65         }
66         
67         return null;
68     }
69
70     /**
71      * Returns an unmodifiable sorted map of embeddable attributes.
72      */

73     public SortedMap JavaDoc getAttributeMap() {
74         // create a new instance ... Caching unmodifiable map causes
75
// serialization issues (esp. with Hessian).
76
return Collections.unmodifiableSortedMap(attributes);
77     }
78
79     /**
80      * Returns an unmodifiable collection of embeddable attributes.
81      */

82     public Collection JavaDoc getAttributes() {
83         // create a new instance. Caching unmodifiable collection causes
84
// serialization issues (esp. with Hessian).
85
return Collections.unmodifiableCollection(attributes.values());
86     }
87
88     /**
89      * Adds new embeddable attribute to the entity, setting its parent embeddable to be
90      * this object. If attribute has no name, IllegalArgumentException is thrown.
91      */

92     public void addAttribute(EmbeddableAttribute attribute) {
93         if (attribute.getName() == null) {
94             throw new IllegalArgumentException JavaDoc("Attempt to insert unnamed attribute.");
95         }
96
97         Object JavaDoc existingAttribute = attributes.get(attribute.getName());
98         if (existingAttribute != null) {
99             if (existingAttribute == attribute) {
100                 return;
101             }
102             else {
103                 throw new IllegalArgumentException JavaDoc(
104                         "An attempt to override embeddable attribute '"
105                                 + attribute.getName()
106                                 + "'");
107             }
108         }
109
110         attributes.put(attribute.getName(), attribute);
111         attribute.setEmbeddable(this);
112     }
113
114     public EmbeddableAttribute getAttribute(String JavaDoc name) {
115         return (EmbeddableAttribute) attributes.get(name);
116     }
117
118     public void removeAttribute(String JavaDoc name) {
119         attributes.remove(name);
120     }
121
122     public String JavaDoc getClassName() {
123         return className;
124     }
125
126     public void setClassName(String JavaDoc className) {
127         this.className = className;
128     }
129
130     /**
131      * {@link XMLSerializable} implementation that generates XML for embeddable.
132      */

133     public void encodeAsXML(XMLEncoder encoder) {
134         encoder.print("<embeddable");
135         if (getClassName() != null) {
136             encoder.print(" className=\"");
137             encoder.print(getClassName());
138             encoder.print("\"");
139         }
140         encoder.println(">");
141
142         encoder.indent(1);
143         encoder.print(attributes);
144         encoder.indent(-1);
145         encoder.println("</embeddable>");
146     }
147 }
148
Popular Tags