KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > FieldImpl


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;
17
18 import org.outerj.daisy.repository.Field;
19 import org.outerj.daisy.repository.ValueType;
20 import org.outerj.daisy.repository.RepositoryException;
21 import org.outerj.daisy.repository.VariantKey;
22 import org.outerj.daisy.repository.schema.FieldType;
23 import org.outerx.daisy.x10.FieldDocument;
24
25 import java.util.GregorianCalendar JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.math.BigDecimal JavaDoc;
28
29 public class FieldImpl implements Field {
30     private long typeId;
31     private Object JavaDoc value;
32     private ValueType cachedValueType;
33     private boolean cachedMultiValue;
34     private DocumentVariantImpl.IntimateAccess ownerVariantInt;
35
36     public FieldImpl(DocumentVariantImpl.IntimateAccess ownerVariantInt, long typeId, Object JavaDoc value) {
37         if (value == null)
38             throw new NullPointerException JavaDoc("Field value cannot be null!");
39         this.ownerVariantInt = ownerVariantInt;
40         this.typeId = typeId;
41         this.value = value;
42     }
43
44     public long getTypeId() {
45         return typeId;
46     }
47
48     public Object JavaDoc getValue() {
49         return value;
50     }
51
52     public ValueType getValueType() {
53         assureTypeInfoLoaded();
54         return cachedValueType;
55     }
56
57     public boolean isMultiValue() {
58         assureTypeInfoLoaded();
59         return cachedMultiValue;
60     }
61
62     private void assureTypeInfoLoaded() {
63         if (cachedValueType == null) {
64             FieldType fieldType;
65             try {
66                 fieldType = ownerVariantInt.getRepositorySchema().getFieldTypeById(typeId, false, ownerVariantInt.getCurrentUser());
67             } catch (RepositoryException e) {
68                 throw new RuntimeException JavaDoc(DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e);
69             }
70             cachedValueType = fieldType.getValueType();
71             cachedMultiValue = fieldType.isMultiValue();
72         }
73     }
74
75     public String JavaDoc getTypeName() {
76         FieldType fieldType;
77         try {
78             fieldType = ownerVariantInt.getRepositorySchema().getFieldTypeById(typeId, false, ownerVariantInt.getCurrentUser());
79         } catch (RepositoryException e) {
80             throw new RuntimeException JavaDoc(DocumentImpl.ERROR_ACCESSING_REPOSITORY_SCHEMA, e);
81         }
82         return fieldType.getName();
83     }
84
85     public FieldDocument getXml() {
86         FieldDocument fieldDocument = FieldDocument.Factory.newInstance();
87         FieldDocument.Field fieldXml = fieldDocument.addNewField();
88         fieldXml.setTypeId(typeId);
89         final ValueType valueType = getValueType();
90         if (isMultiValue()) {
91             Object JavaDoc[] values = (Object JavaDoc[])value;
92             for (int i = 0; i < values.length; i++) {
93                 addValueToXml(fieldXml, valueType, values[i]);
94             }
95         } else {
96             addValueToXml(fieldXml, valueType, value);
97         }
98
99         return fieldDocument;
100     }
101
102     private void addValueToXml(FieldDocument.Field fieldXml, ValueType valueType, Object JavaDoc value) {
103         if (valueType == ValueType.STRING) {
104             fieldXml.addString((String JavaDoc)value);
105         } else if (valueType == ValueType.DATE) {
106             GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
107             calendar.setTime((Date JavaDoc)value);
108             fieldXml.addDate(calendar);
109         } else if (valueType == ValueType.DATETIME) {
110             GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
111             calendar.setTime((Date JavaDoc)value);
112             fieldXml.addDateTime(calendar);
113         } else if (valueType == ValueType.DECIMAL) {
114             fieldXml.addDecimal((BigDecimal JavaDoc)value);
115         } else if (valueType == ValueType.DOUBLE) {
116             fieldXml.addDouble(((Double JavaDoc)value).doubleValue());
117         } else if (valueType == ValueType.LONG) {
118             fieldXml.addLong(((Long JavaDoc)value).longValue());
119         } else if (valueType == ValueType.BOOLEAN) {
120             fieldXml.addBoolean(((Boolean JavaDoc)value).booleanValue());
121         } else if (valueType == ValueType.LINK) {
122             FieldDocument.Field.Link link = fieldXml.addNewLink();
123             VariantKey variantKey = (VariantKey)value;
124             link.setDocumentId(variantKey.getDocumentId());
125             link.setBranchId(variantKey.getBranchId());
126             link.setLanguageId(variantKey.getLanguageId());
127         } else {
128             throw new RuntimeException JavaDoc("Unsupported ValueType: " + valueType);
129         }
130     }
131 }
Popular Tags