KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > schema > StaticListItemImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.schema;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.GregorianCalendar JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.outerj.daisy.repository.ValueType;
24 import org.outerj.daisy.repository.VariantKey;
25 import org.outerj.daisy.repository.schema.StaticListItem;
26 import org.outerx.daisy.x10.ListItemDocument;
27
28 /**
29  * Implementation of StaticListItem
30  */

31 public class StaticListItemImpl implements StaticListItem {
32     private Object JavaDoc value;
33     private LocaleMap labelMap = new LocaleMap();
34     private IntimateAccess intimateAccess = new IntimateAccess();
35     private SchemaStrategy creatingStrategy = null;
36     private FieldTypeImpl owner;
37     private ValueType valueType;
38     private long localizedLabelId = -1;
39
40     /**
41      * @param value
42      * the value for this ListItem
43      */

44     public StaticListItemImpl(Object JavaDoc value, ValueType valueType, SchemaStrategy creatingStrategy,
45             FieldTypeImpl owner) {
46         this.value = value;
47         this.creatingStrategy = creatingStrategy;
48         this.valueType = valueType;
49         this.owner = owner;
50     }
51
52     public Object JavaDoc getValue() {
53         return value;
54     }
55
56     public void setLabel(Locale JavaDoc locale, String JavaDoc label) {
57         if (owner.isReadOnly())
58             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
59
60         labelMap.put(locale, label);
61     }
62
63     public String JavaDoc getLabel(Locale JavaDoc locale) {
64         return (String JavaDoc) labelMap.get(locale);
65     }
66
67     public String JavaDoc getLabelExact(Locale JavaDoc locale) {
68         return (String JavaDoc) labelMap.getExact(locale);
69     }
70
71     public void setValue(Object JavaDoc value) {
72         if (owner.isReadOnly())
73             throw new RuntimeException JavaDoc(FieldTypeImpl.READ_ONLY_MESSAGE);
74
75         this.value = value;
76     }
77
78     public IntimateAccess getIntimateAccess(SchemaStrategy strategy) {
79         if (strategy == creatingStrategy) {
80             return intimateAccess;
81         } else {
82             return null;
83         }
84     }
85
86     public class IntimateAccess {
87         private IntimateAccess() {
88         }
89
90         public LocaleMap getLabels() {
91             return labelMap;
92         }
93
94         public void setLocalizedLabelId(long localizedLabelId) {
95             StaticListItemImpl.this.localizedLabelId = localizedLabelId;
96         }
97
98         public long getLocalizedLabelId() {
99             return localizedLabelId;
100         }
101     }
102
103     public ListItemDocument getXml() {
104         ListItemDocument listItemDocument =
105             ListItemDocument.Factory.newInstance();
106         ListItemDocument.ListItem listItem = listItemDocument.addNewListItem();
107
108         listItem.setLabels(labelMap.getAsLabelsXml());
109
110         if (valueType == ValueType.STRING) {
111             listItem.setString((String JavaDoc) value);
112         } else if (valueType == ValueType.DATE) {
113             GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
114             calendar.setTime((Date JavaDoc) value);
115             listItem.setDate(calendar);
116         } else if (valueType == ValueType.DATETIME) {
117             GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
118             calendar.setTime((Date JavaDoc) value);
119             listItem.setDateTime(calendar);
120         } else if (valueType == ValueType.DECIMAL) {
121             listItem.setDecimal((BigDecimal JavaDoc) value);
122         } else if (valueType == ValueType.DOUBLE) {
123             listItem.setDouble(((Double JavaDoc) value).doubleValue());
124         } else if (valueType == ValueType.LONG) {
125             listItem.setLong(((Long JavaDoc) value).longValue());
126         } else if (valueType == ValueType.BOOLEAN) {
127             listItem.setBoolean(((Boolean JavaDoc)value).booleanValue());
128         } else if (valueType == ValueType.LINK) {
129             ListItemDocument.ListItem.Link link = listItem.addNewLink();
130             VariantKey variantKey = (VariantKey)value;
131             link.setDocumentId(variantKey.getDocumentId());
132             link.setBranchId(variantKey.getBranchId());
133             link.setLanguageId(variantKey.getLanguageId());
134         } else {
135             throw new RuntimeException JavaDoc("Unsupported ValueType: " + valueType);
136         }
137         return listItemDocument;
138     }
139 }
140
Popular Tags