001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.model.data;
028:
029: import org.databene.commons.OrderedMap;
030: import org.databene.commons.SystemInfo;
031: import org.databene.commons.collection.ListBasedSet;
032: import org.databene.commons.operation.MaxOperation;
033: import org.databene.commons.operation.MinOperation;
034:
035: import java.util.Map;
036: import java.util.Collection;
037: import java.util.Set;
038:
039: /**
040: * Describes an entity.
041: * Created: 30.06.2007 10:09:34
042: * @author Volker Bergmann
043: */
044: public class EntityDescriptor extends FeatureDescriptor {
045:
046: private boolean caseSensitive; // TODO v0.5 define more sophisticated mapping strategy
047: private Map<String, ComponentDescriptor> componentMap;
048:
049: public EntityDescriptor(String name, boolean caseSensitive) {
050: this (name, caseSensitive, null);
051: }
052:
053: public EntityDescriptor(String name, boolean caseSensitive,
054: EntityDescriptor parent) {
055: super (name, parent);
056: this .caseSensitive = caseSensitive;
057: this .componentMap = new OrderedMap<String, ComponentDescriptor>();
058: addDetailConfig("count", Long.class, false, null);
059: addDetailConfig("minCount", Long.class, false, 1L,
060: new MaxOperation<Long>());
061: addDetailConfig("maxCount", Long.class, false, null,
062: new MinOperation<Long>());
063: }
064:
065: public boolean isCaseSensitive() {
066: return caseSensitive;
067: }
068:
069: public void setComponentDescriptor(ComponentDescriptor descriptor) {
070: String name = descriptor.getName();
071: ComponentDescriptor parentsDescriptor = null;
072: if (parent != null)
073: parentsDescriptor = ((EntityDescriptor) parent)
074: .getComponentDescriptor(name);
075: if (parentsDescriptor != null) {
076: if (descriptor.getClass() == parentsDescriptor.getClass())
077: descriptor.setParent(parentsDescriptor);
078: descriptor.setName(parentsDescriptor.getName());
079: /*
080: if (parentsDescriptor instanceof AttributeDescriptor) {
081: AttributeDescriptor ad = (AttributeDescriptor) descriptor;
082: AttributeDescriptor pad = (AttributeDescriptor) parentsDescriptor;
083: ad.setType(pad.getType());
084: ad.setMaxLength(pad.getMaxLength());
085: }
086: */
087: }
088: componentMap.put(name, descriptor);
089: }
090:
091: public ComponentDescriptor getComponentDescriptor(String name) {
092: ComponentDescriptor descriptor = componentMap.get(name);
093: if (descriptor == null && !caseSensitive)
094: for (ComponentDescriptor candidate : componentMap.values())
095: if (candidate.getName().equalsIgnoreCase(name)) {
096: descriptor = candidate;
097: break;
098: }
099: if (descriptor == null && parent != null)
100: descriptor = ((EntityDescriptor) parent)
101: .getComponentDescriptor(name);
102: return descriptor;
103: }
104:
105: public Collection<ComponentDescriptor> getComponentDescriptors() {
106: Map<String, ComponentDescriptor> tmp = new OrderedMap<String, ComponentDescriptor>();
107: for (ComponentDescriptor d : componentMap.values())
108: tmp.put(d.getName(), d);
109: if (parent != null)
110: for (ComponentDescriptor d : ((EntityDescriptor) parent)
111: .getComponentDescriptors()) {
112: String name = d.getName();
113: if (!tmp.containsKey(name))
114: tmp.put(name, d);
115: }
116: return tmp.values();
117: }
118:
119: public Collection<ComponentDescriptor> getDeclaredComponentDescriptors() {
120: Set<ComponentDescriptor> declaredDescriptors = new ListBasedSet<ComponentDescriptor>(
121: componentMap.size());
122: for (ComponentDescriptor d : componentMap.values())
123: declaredDescriptors.add(d);
124: return declaredDescriptors;
125: }
126:
127: // properties ------------------------------------------------------------------------------------------------------
128:
129: public Long getCount() {
130: return (Long) getDetailValue("count");
131: }
132:
133: public void setCount(Long count) {
134: setDetail("count", count);
135: }
136:
137: // construction helper methods -------------------------------------------------------------------------------------
138:
139: public EntityDescriptor withComponent(
140: ComponentDescriptor componentDescriptor) {
141: setComponentDescriptor(componentDescriptor);
142: return this ;
143: }
144:
145: // java.lang.Object overrides --------------------------------------------------------------------------------------
146:
147: public String toString() {
148: if (componentMap.size() == 0)
149: return super .toString();
150: String sep = SystemInfo.lineSeparator();
151: StringBuilder builder = new StringBuilder(super .toString());
152: builder.append('{').append(sep);
153: for (ComponentDescriptor descriptor : componentMap.values())
154: builder.append(" ").append(descriptor).append(sep);
155: return builder.append('}').toString();
156: }
157:
158: public boolean equals(Object o) {
159: if (this == o)
160: return true;
161: if (o == null || getClass() != o.getClass())
162: return false;
163: if (!super .equals(o))
164: return false;
165:
166: final EntityDescriptor that = (EntityDescriptor) o;
167:
168: if (caseSensitive != that.caseSensitive)
169: return false;
170: if (!componentMap.equals(that.componentMap)) // TODO v0.5 consider case
171: return false;
172:
173: return true;
174: }
175:
176: public int hashCode() {
177: int result = super .hashCode();
178: result = 29 * result + (caseSensitive ? 1 : 0);
179: result = 29 * result + componentMap.hashCode();
180: return result;
181: }
182: }
|