View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  package org.caleigo.core;
19  
20  import org.caleigo.toolkit.log.*;
21  
22  /*** Abstract class for calculated fields. Use it if you want to have a virtual
23   * field in an entity that has data that isn't persistent but dependent and
24   * calculated from one or more often several other fields.
25   * E.g. Totalcost = UnitPrice * Quantity
26   * 
27   * @author Niklas Norberg
28   * @version 1.00
29   * 
30   *//* WHEN        WHO               WHY & WHAT
31   * -----------------------------------------------------------------------------
32   * 2003-06-19  Niklas Norberg   Creation
33   */
34  public abstract class AbstractCalculatedFieldDescriptor extends FieldDescriptor implements ICalculatedFieldDescriptor
35  {
36      
37      // Constants ---------------------------------------------------------------
38      private final static int MASK = IFieldDescriptor.REQUIRED | 
39                                      IFieldDescriptor.NAME_FIELD |
40                                      IFieldDescriptor.OVERVIEW_FIELD |
41                                      IFieldDescriptor.HINT_FIELD |
42                                      IFieldDescriptor.READ_ONLY_FIELD;
43      
44      // Data members ------------------------------------------------------------
45      IFieldDescriptor[] mRequiredFields;
46      
47      // Constructors ------------------------------------------------------------
48      
49      /*** Default constructor for CalculatedFieldDescriptor. Not all flags are
50       * relevant and are therefor removed if given.
51       */
52      protected AbstractCalculatedFieldDescriptor(String codeName, String displayName, String entityDescriptorName, DataType dataType, int length, int flags, IFieldDescriptor[] requiredFields)
53      {
54          super(codeName,  null,  displayName,  entityDescriptorName,  dataType, length,  flags & MASK ,  null);
55          if ( (flags & ~MASK) != 0 )
56              Log.printWarning(this, "Removed flag(s) for field " + codeName +  " in entity: " + entityDescriptorName);
57          this.mRequiredFields = requiredFields;
58      }
59      
60      /*** Alternative constructor for CalculatedFieldDescriptor. Normally only
61       * used if one want to implement a calculated fields that only uses the
62       * getFieldDataFrom-method. The parameter readOnly sets the corresponding
63       * flag.
64       */
65      protected AbstractCalculatedFieldDescriptor(String codeName, String displayName, String entityDescriptorName, DataType dataType, int length,  boolean readOnly, IFieldDescriptor[] requiredFields)
66      {
67          this(codeName, displayName,  entityDescriptorName,  dataType,  length, readOnly ? IFieldDescriptor.READ_ONLY_FIELD : 0 , requiredFields);
68      }
69      
70      // ICalculatedFieldDescriptor implementation -------------------------------
71      
72      /*** This one must always be overridden since there would be no sence in
73       * creating a subclass without a coherent getFieldDataFrom-method!
74       * Normally this method would have some logic and math that calculate the
75       * data in this field from others.
76       */ 
77      public abstract Object getFieldDataFrom(IEntity entity);
78      
79      /*** This one should never be called unless it's overridden! Could be used to
80       * force changes in other fields. Normally the inverse of the logic and math
81       * in the getFieldDataFrom-method.
82       */ 
83      public void setFieldDataTo(IEntity entity, Object data)
84      {
85          String msg = "SetFieldDataTo has no default logic for editable fields!";
86          Log.printWarning(this, msg);
87          throw new UnsupportedOperationException(msg);
88      }
89  
90      /*** Should Returns true if the descriptor is "valid for" = can be a part 
91       * of and calculate its value based on the data defined by the submited 
92       * entity descriptor.
93       */
94      public boolean isValidFor(IEntityDescriptor descriptor)
95      {
96          for(int j=0; j<mRequiredFields.length; j++)
97          {
98              if ( descriptor.getFieldIndex(mRequiredFields[j]) < 0 )
99                  return false;
100         }
101         return true;
102     }
103     
104     /*** Should return an array of the field descriptors required for the 
105      * "calculation" of the descriptors value.
106      */
107     public IFieldDescriptor[] getRequiredFields()
108     {
109         return mRequiredFields;
110     }
111 
112 }