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.Converter;
030: import org.databene.commons.LocaleUtil;
031: import org.databene.commons.BeanUtil;
032: import org.databene.commons.ConfigurationError;
033: import org.databene.commons.converter.ParseFormatConverter;
034: import org.databene.region.Region;
035: import org.databene.benerator.Distribution;
036: import org.databene.benerator.Sequence;
037:
038: import java.util.Locale;
039: import java.text.Format;
040:
041: /**
042: * Descriptor for attributes<br/><br/>
043: * Created: 30.06.2007 07:29:43
044: * @author Volker Bergmann
045: */
046: public class AttributeDescriptor extends ComponentDescriptor {
047:
048: public AttributeDescriptor(String name) {
049: this (name, null);
050: }
051:
052: public AttributeDescriptor(ComponentDescriptor parent) {
053: this (parent.getName(), parent);
054: }
055:
056: protected AttributeDescriptor(String name,
057: ComponentDescriptor parent) {
058: super (name, parent);
059:
060: // general constraints
061: addDetailConfig("nullable", Boolean.class, true, true);
062:
063: // association constraints
064: //addDetailConfig("minCount", Integer.class, true, 1, new MaxOperation<Integer>()); // TODO v0.5 implement association handling
065: //addDetailConfig("maxCount", Integer.class, true, 1, new MinOperation<Integer>());
066:
067: addDetailConfig("script", String.class, false, "");
068: // string constraints
069: addDetailConfig("minLength", Integer.class, true, 1/*, new MaxOperation<Integer>()*/);
070: addDetailConfig("maxLength", Integer.class, true, 30/*, new MinOperation<Integer>()*/);
071:
072: // number constraints
073: addDetailConfig("min", String.class, true, "1");
074: addDetailConfig("max", String.class, true, "9");
075: addDetailConfig("precision", String.class, false, "1");
076:
077: // setups
078: addDetailConfig("converter", Converter.class, false, null);
079: addDetailConfig("nullQuota", Double.class, false, null);
080: addDetailConfig("trueQuota", Double.class, false, 0.5);
081: //addDetailConfig("lengthDistribution", Distribution.class);
082: addDetailConfig("variation1", String.class, false, "1");
083: addDetailConfig("variation2", String.class, false, "1");
084: addDetailConfig("distribution", Distribution.class, false, null);
085: //addDetailConfig("countDistribution", Distribution.class, null); // for v0.4
086:
087: // i18n setups
088: addDetailConfig("region", Region.class, false, null);
089: addDetailConfig("locale", Locale.class, false, null);
090: }
091:
092: // properties ------------------------------------------------------------------------------------------------------
093:
094: public Boolean isPrivacy() {
095: return (Boolean) getDetailValue("private");
096: }
097:
098: public void setPrivacy(Boolean privacy) {
099: setDetail("private", privacy);
100: }
101:
102: public Integer getMinLength() {
103: return (Integer) getDetailValue("minLength");
104: }
105:
106: public void setMinLength(Integer minLength) {
107: setDetail("minLength", minLength);
108: }
109:
110: public Integer getMaxLength() {
111: return (Integer) getDetailValue("maxLength");
112: }
113:
114: public void setMaxLength(Integer maxLength) {
115: setDetail("maxLength", maxLength);
116: }
117:
118: public Distribution getLengthDistribution() {
119: return mapDistribution((String) getDetailValue("lengthDistribution"));
120: }
121:
122: public String getMin() {
123: return (String) getDetailValue("min");
124: }
125:
126: public void setMin(String min) {
127: setDetail("min", min);
128: }
129:
130: public String getMax() {
131: return (String) getDetailValue("max");
132: }
133:
134: public void setMax(String max) {
135: setDetail("max", max);
136: }
137:
138: public String getPrecision() {
139: return (String) getDetailValue("precision");
140: }
141:
142: public void setPrecision(String precision) {
143: setDetail("precision", precision);
144: }
145:
146: public String getVariation1() {
147: return (String) getDetailValue("variation1");
148: }
149:
150: public void setVariation1(String variation1) {
151: setDetail("variation1", variation1);
152: }
153:
154: public String getVariation2() {
155: return (String) getDetailValue("variation2");
156: }
157:
158: public void setVariation2(String variation2) {
159: setDetail("variation2", variation2);
160: }
161:
162: public Distribution getDistribution() {
163: return (Distribution) getDetailValue("distribution");
164: }
165:
166: public void setDistribution(Distribution distribution) {
167: setDetail("distribution", distribution);
168: }
169:
170: public Region getRegion() {
171: return (Region) getDetailValue("region");
172: }
173:
174: public void setRegionId(String regionId) {
175: setDetail("region", (regionId != null ? Region
176: .getInstance(regionId) : null));
177: }
178:
179: public Locale getLocale() {
180: return (Locale) getDetailValue("locale");
181: }
182:
183: public void setLocaleId(String localeId) {
184: setDetail("locale", LocaleUtil.getLocale(localeId));
185: }
186:
187: public Boolean isNullable() {
188: return (Boolean) getDetailValue("nullable");
189: }
190:
191: public void setNullable(Boolean nullable) {
192: setDetail("nullable", nullable);
193: }
194:
195: public Integer getMinCardinality() {
196: return (Integer) getDetailValue("minCardinality");
197: }
198:
199: public void setMinCardinality(Integer minCardinality) {
200: setDetail("minCardinality", minCardinality);
201: }
202:
203: public Integer getMaxCardinality() {
204: return (Integer) getDetailValue("maxCardinality");
205: }
206:
207: public void setMaxCardinality(Integer maxCardinality) {
208: setDetail("maxCardinality", maxCardinality);
209: }
210:
211: public Distribution getCardinalityDistribution() {
212: return mapDistribution((String) getDetailValue("cardinalityDistribution"));
213: }
214:
215: public void setCardinalityDistribution(String distribution) {
216: setDetail("cardinalityDistribution", distribution);
217: }
218:
219: public Double getNullQuota() {
220: return (Double) getDetailValue("nullQuota");
221: }
222:
223: public void setNullQuota(Double nullQuota) {
224: setDetail("nullQuota", nullQuota);
225: }
226:
227: public Double getTrueQuota() {
228: return (Double) getDetailValue("trueQuota");
229: }
230:
231: public void setTrueQuota(Double trueQuota) {
232: setDetail("trueQuota", trueQuota);
233: }
234:
235: public String getSource() {
236: return (String) getDetailValue("source");
237: }
238:
239: public void setUri(String source) {
240: setDetail("source", source);
241: }
242:
243: public Converter<? extends Object, ? extends Object> getConverter() {
244: return (Converter<? extends Object, ? extends Object>) getDetailValue("converter");
245: }
246:
247: public void setConverter(
248: Converter<? extends Object, ? extends Object> converter) {
249: setDetail("converter", converter);
250: }
251:
252: public String getScript() {
253: return (String) getDetailValue("script");
254: }
255:
256: public void setScript(String script) {
257: setDetail("script", script);
258: }
259:
260: // literate build helpers ------------------------------------------------------------------------------------------
261:
262: public AttributeDescriptor withPrivacy(Boolean privacy) {
263: setPrivacy(privacy);
264: return this ;
265: }
266:
267: public AttributeDescriptor withMinLength(Integer minLength) {
268: setMinLength(minLength);
269: return this ;
270: }
271:
272: public AttributeDescriptor withMaxLength(Integer maxLength) {
273: setMaxLength(maxLength);
274: return this ;
275: }
276:
277: public AttributeDescriptor withMin(String min) {
278: setMin(min);
279: return this ;
280: }
281:
282: public AttributeDescriptor withMax(String max) {
283: setMax(max);
284: return this ;
285: }
286:
287: public AttributeDescriptor withPrecision(String precision) {
288: setPrecision(precision);
289: return this ;
290: }
291:
292: public AttributeDescriptor withVariation1(String variation1) {
293: setVariation1(variation1);
294: return this ;
295: }
296:
297: public AttributeDescriptor withVariation2(String variation2) {
298: setVariation2(variation2);
299: return this ;
300: }
301:
302: public AttributeDescriptor withDistribution(
303: Distribution distribution) {
304: setDistribution(distribution);
305: return this ;
306: }
307:
308: public AttributeDescriptor withRegionId(String regionId) {
309: setRegionId(regionId);
310: return this ;
311: }
312:
313: public AttributeDescriptor withLocaleId(String localeId) {
314: setLocaleId(localeId);
315: return this ;
316: }
317:
318: public AttributeDescriptor withNullable(Boolean nullable) {
319: setNullable(nullable);
320: return this ;
321: }
322:
323: public AttributeDescriptor withMinCardinality(Integer minCardinality) {
324: setMinCardinality(minCardinality);
325: return this ;
326: }
327:
328: public AttributeDescriptor withMaxCardinality(Integer maxCardinality) {
329: setMaxCardinality(maxCardinality);
330: return this ;
331: }
332:
333: public AttributeDescriptor withCardinalityDistribution(
334: String distribution) {
335: setCardinalityDistribution(distribution);
336: return this ;
337: }
338:
339: public AttributeDescriptor withNullQuota(Double nullQuota) {
340: setNullQuota(nullQuota);
341: return this ;
342: }
343:
344: public AttributeDescriptor withTrueQuota(Double trueQuota) {
345: setTrueQuota(trueQuota);
346: return this ;
347: }
348:
349: public AttributeDescriptor withUri(String source) {
350: setSource(source);
351: return this ;
352: }
353:
354: public AttributeDescriptor withConverter(Converter converter) {
355: setConverter(converter);
356: return this ;
357: }
358:
359: // generic property access -----------------------------------------------------------------------------------------
360:
361: public void setDetail(String detailName, Object detailValue) {
362: Class<? extends Object> targetType = getDetailType(detailName);
363: if (targetType == Distribution.class
364: && detailValue.getClass() == String.class)
365: detailValue = mapDistribution((String) detailValue);
366: else if (targetType == Converter.class
367: && detailValue.getClass() == String.class)
368: detailValue = mapConverter((String) detailValue);
369: super .setDetail(detailName, detailValue);
370: }
371:
372: // private helpers -------------------------------------------------------------------------------------------------
373:
374: private Converter<? extends Object, ? extends Object> mapConverter(
375: String converterString) {
376: Object result = BeanUtil.newInstance(converterString);
377: if (result instanceof Format)
378: result = new ParseFormatConverter(Object.class,
379: (Format) result);
380: else if (!(result instanceof Converter))
381: throw new ConfigurationError("Class is no Converter: "
382: + result.getClass());
383: return (Converter<? extends Object, ? extends Object>) result;
384: }
385:
386: private static Distribution mapDistribution(String distributionName) {
387: if (distributionName == null)
388: return null;
389: try {
390: return Sequence.getInstance(distributionName);
391: } catch (Exception e) {
392: return (Distribution) BeanUtil
393: .newInstance(distributionName);
394: }
395: }
396:
397: }
|