View Javadoc

1   package org.apache.turbine.services.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.commons.lang.StringUtils;
23  
24  import org.apache.torque.om.ComboKey;
25  
26  import org.apache.turbine.services.intake.IntakeException;
27  import org.apache.turbine.services.intake.xmlmodel.XmlField;
28  
29  /***
30   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
31   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33   * @version $Id: ComboKeyField.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  public class ComboKeyField
36          extends Field
37  {
38      /***
39       * Constructor.
40       *
41       * @param field xml field definition object
42       * @param group xml group definition object
43       * @throws IntakeException thrown by superclass
44       */
45      public ComboKeyField(XmlField field, Group group)
46              throws IntakeException
47      {
48          super(field, group);
49      }
50  
51      /***
52       * Sets the default value for a ComboKey field
53       *
54       * @param prop Parameter for the default values
55       */
56      public void setDefaultValue(String prop)
57      {
58          defaultValue = null;
59  
60          if (prop == null)
61          {
62              return;
63          }
64  
65          defaultValue = new ComboKey(prop);
66      }
67  
68      /***
69       * Set the empty Value. This value is used if Intake
70       * maps a field to a parameter returned by the user and
71       * the corresponding field is either empty (empty string)
72       * or non-existant.
73       *
74       * @param prop The value to use if the field is empty.
75       */
76      public void setEmptyValue(String prop)
77      {
78          emptyValue = null;
79  
80          if (prop == null)
81          {
82              return;
83          }
84  
85          emptyValue = new ComboKey(prop);
86      }
87  
88      /***
89       * Sets the value of the field from data in the parser.
90       */
91      protected void doSetValue()
92      {
93          if (isMultiValued)
94          {
95              String[] inputs = parser.getStrings(getKey());
96              ComboKey[] values = new ComboKey[inputs.length];
97              for (int i = 0; i < inputs.length; i++)
98              {
99                  values[i] = StringUtils.isNotEmpty(inputs[i])
100                         ? new ComboKey(inputs[i]) : (ComboKey) getEmptyValue();
101             }
102             setTestValue(values);
103         }
104         else
105         {
106             String val = parser.getString(getKey());
107             setTestValue(StringUtils.isNotEmpty(val) ? new ComboKey(val) : (ComboKey) getEmptyValue());
108         }
109     }
110 }