1 package org.apache.fulcrum.intake.model;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.fulcrum.intake.IntakeException;
23 import org.apache.fulcrum.intake.validator.BooleanValidator;
24
25 /**
26 * Processor for boolean fields.
27 *
28 * @version $Id$
29 */
30 public class BooleanField
31 extends Field<Boolean>
32 {
33 /** Serial version */
34 private static final long serialVersionUID = 6689670469518374083L;
35
36 /**
37 * Constructor.
38 *
39 * @param field xml field definition object
40 * @param group xml group definition object
41 * @throws IntakeException thrown by superclass
42 */
43 public BooleanField(XmlField field, Group group)
44 throws IntakeException
45 {
46 super(field, group);
47 }
48
49 /**
50 * Sets the default value for a Boolean field
51 *
52 * @param prop Parameter for the default values
53 */
54 @Override
55 public void setDefaultValue(String prop)
56 {
57 defaultValue = null;
58
59 if (prop == null)
60 {
61 return;
62 }
63
64 defaultValue = Boolean.valueOf(prop);
65 }
66
67 /**
68 * Set the empty Value. This value is used if Intake
69 * maps a field to a parameter returned by the user and
70 * the corresponding field is either empty (empty string)
71 * or non-existant.
72 *
73 * @param prop The value to use if the field is empty.
74 */
75 @Override
76 public void setEmptyValue(String prop)
77 {
78 emptyValue = null;
79
80 if (prop == null)
81 {
82 return;
83 }
84
85 emptyValue = Boolean.valueOf(prop);
86 }
87
88 /**
89 * Provides access to emptyValue such that the value returned will be
90 * acceptable as an argument parameter to Method.invoke. Subclasses
91 * that deal with primitive types should ensure that they return an
92 * appropriate value wrapped in the object wrapper class for the
93 * primitive type.
94 *
95 * @return the value to use when the field is empty or an Object that
96 * wraps the empty value for primitive types.
97 */
98 @Override
99 protected Object getSafeEmptyValue()
100 {
101 if (isMultiValued())
102 {
103 return new boolean[0];
104 }
105 else
106 {
107 return (null == getEmptyValue()) ? Boolean.FALSE : getEmptyValue();
108 }
109 }
110
111 /**
112 * A suitable validator.
113 *
114 * @return class name of the validator
115 */
116 @Override
117 protected String getDefaultValidator()
118 {
119 return BooleanValidator.class.getName();
120 }
121
122 /**
123 * Sets the value of the field from data in the parser.
124 */
125 @Override
126 protected void doSetValue()
127 {
128 if (isMultiValued())
129 {
130 Boolean[] inputs = parser.getBooleanObjects(getKey());
131 boolean[] values = new boolean[inputs.length];
132
133 for (int i = 0; i < inputs.length; i++)
134 {
135 values[i] = inputs[i] == null
136 ? getEmptyValue().booleanValue()
137 : inputs[i].booleanValue();
138 }
139
140 setTestValue(values);
141 }
142 else
143 {
144 setTestValue(parser.getBooleanObject(getKey(), getEmptyValue()));
145 }
146 }
147
148 /**
149 * Gets the boolean value of the field. A value of false will be returned
150 * if the value of the field is null.
151 *
152 * @return value of the field.
153 */
154 public boolean booleanValue()
155 {
156 boolean result = false;
157 try
158 {
159 result = getValue().booleanValue();
160 }
161 catch (Exception e)
162 {
163 log.error("Error getting boolean value " + getValue(), e);
164 }
165 return result;
166 }
167
168 }