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  
19  package org.caleigo.core.meta;
20  
21  import org.caleigo.core.*;
22  
23  /***
24   *
25   * @author  Mattias Hagstrand
26   * @version 1.0
27   */
28  /* 
29  *
30  * WHEN        WHO               WHY & WHAT
31  * ------------------------------------------------------------------------------
32  * 2002-05-21  Mattias Hagstrand    Creation
33  */
34  class CustomFieldRelation implements IFieldRelation
35  {
36      // Data members ------------------------------------------------------------
37      private IEntityRelation mEntityRelation;
38      
39      private CustomDataSourceDescriptor mDataSourceDescriptor;
40      private String mReferenceEntitySourceName;
41      private String mReferenceFieldSourceName;
42      private String mTargetEntitySourceName;
43      private String mTargetFieldSourceName;
44      
45      private transient IFieldDescriptor mReferenceField;
46      private transient IFieldDescriptor mTargetField;
47      
48      // Constructors ------------------------------------------------------------
49      CustomFieldRelation(CustomDataSourceDescriptor dataSourceDescriptor, String referenceEntitySourceName, String referenceFieldSourceName,
50                          String targetEntitySourceName, String targetFieldSourceName)
51      {
52          mDataSourceDescriptor = dataSourceDescriptor;
53          mReferenceEntitySourceName = referenceEntitySourceName;
54          mReferenceFieldSourceName = referenceFieldSourceName;
55          mTargetEntitySourceName = targetEntitySourceName;
56          mTargetFieldSourceName = targetFieldSourceName;
57      }
58      
59      // IFieldRelation implementation -------------------------------------------
60      
61      /*** Access method that return the IEntityRelation that the called
62       * IFieldRelation is a part of.
63       */ 
64      public IEntityRelation getEntityRelation()
65      {
66          return mEntityRelation;
67      }
68      
69      /*** Access method that return the IFieldDescriptor that is the 
70       * source/reference field that addresses another field. In RDB terms the
71       * reference field is or should be defined as a foreign key.  
72       */ 
73      public IFieldDescriptor getReferenceField()
74      {
75          if(mReferenceField == null)
76          {
77              IEntityDescriptor entityDescriptor = mDataSourceDescriptor.getEntityDescriptorBySourceName(mReferenceEntitySourceName);
78              for (int i = 0; i < entityDescriptor.getFieldCount() && mReferenceField == null; i++)
79                  if (entityDescriptor.getFieldDescriptor(i).getSourceName().compareTo(mReferenceFieldSourceName) == 0)
80                      mReferenceField = entityDescriptor.getFieldDescriptor(i);
81          }
82          return mReferenceField;
83      }
84      
85      /*** Access method that return the IFieldDescriptor that is the 
86       * target field that are addressed by the reference field. In RDB terms the
87       * target field is usually a primary-key field.  
88       */ 
89      public IFieldDescriptor getTargetField()
90      {
91          if(mTargetField == null)
92          {
93              IEntityDescriptor entityDescriptor = mDataSourceDescriptor.getEntityDescriptorBySourceName(mTargetEntitySourceName);
94              for (int i = 0; i < entityDescriptor.getFieldCount() && mTargetField == null; i++)
95                  if (entityDescriptor.getFieldDescriptor(i).getSourceName().compareTo(mTargetFieldSourceName) == 0)
96                      mTargetField = entityDescriptor.getFieldDescriptor(i);
97          }
98          return mTargetField;
99      }
100     
101     // Help methods ------------------------------------------------------------
102     
103     /*** Mutation method with restricted access to avoid missuse.
104      */
105     void setEntityRelation(IEntityRelation relation)
106     {
107         mEntityRelation = relation;
108     }
109 }