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.meta;
19  
20  import org.caleigo.core.*;
21  import org.caleigo.toolkit.log.*;
22  
23  
24  /*** MetaEntityRelationEntiy is the entity class for MetaEntityRelation objects.
25  *
26  * @author dennis
27  * @version 1.0.0
28  *//*
29  * WHEN    WHO                WHY & WHAT 
30  * ------------------------------------------------------------------------------
31  * 030627  dennis    Creation
32  */
33  public class MetaEntityRelationEntity extends AbstractMetaEntity 
34  {
35      // Constructors ------------------------------------------------------------
36      
37      /*** Default constructor for MetaEntityRelationEntity.
38       */
39      public MetaEntityRelationEntity() 
40      {
41      }
42      
43      /*** Copy constructor for MetaEntityRelationEntity.
44       */
45      public MetaEntityRelationEntity(MetaEntityRelationEntity entity) 
46      {
47          this.copyData(entity);
48          this.clearAllDirtyFlags();
49      }
50      
51      /*** This constructor creates a new MetaEntityRelationEntity and loads
52       * it with data from the data source using the entity's identity data.
53       */
54      public MetaEntityRelationEntity(String aSourceName) 
55      {
56          super(Qualifier.create(MetaEntityRelation.SourceName, new String(aSourceName)));
57      }
58      
59      
60      /*** This constructor creates a new MetaEntityRelationEntity and loads
61       * it with data from the data source using the provided identity qualifier.
62       */
63      public MetaEntityRelationEntity(Qualifier identityQualifier) 
64      {
65          super(identityQualifier);
66      }
67      
68      // Superclass overrides ----------------------------------------------------
69      public IEntityDescriptor getEntityDescriptor()
70      {
71          return MetaEntityRelation.instance;
72      }
73      
74      protected void doOnDataChange(IFieldDescriptor fieldDescriptor, Object oldValue, Object newValue)
75      {
76          // Sugest display name if none is set.
77          if(fieldDescriptor==MetaEntityRelation.ReferenceEntity && oldValue==null && this.isDataNull(MetaEntityRelation.ReverseName))
78          {
79              MetaEntityDescriptorEntity entity = MetaEntityDescriptor.load(newValue.toString());
80              if(entity!=null)
81                  this.setData(MetaEntityRelation.ReverseName, entity.getDisplayName());
82          }            
83          if(fieldDescriptor==MetaEntityRelation.TargetEntity && oldValue==null && this.isDataNull(MetaEntityRelation.ForwardName))
84          {
85              MetaEntityDescriptorEntity entity = MetaEntityDescriptor.load(newValue.toString());
86              if(entity!=null)
87                  this.setData(MetaEntityRelation.ForwardName, entity.getDisplayName());
88          }
89             
90          // Sugest source and code name if none is set. 
91          if(fieldDescriptor==MetaEntityRelation.ReferenceEntity && oldValue==null && !this.isDataNull(MetaEntityRelation.TargetEntity)
92                  || fieldDescriptor==MetaEntityRelation.TargetEntity && oldValue==null && !this.isDataNull(MetaEntityRelation.ReferenceEntity))
93          {
94              try
95              {
96                  IEntity refEntity = MetaEntityDescriptor.load((String)this.getData(MetaEntityRelation.ReferenceEntity));
97                  IEntity tgtEntity = MetaEntityDescriptor.load((String)this.getData(MetaEntityRelation.TargetEntity));
98                  String compositeName = (String)refEntity.getData(MetaEntityDescriptor.CodeName)+(String)tgtEntity.getData(MetaEntityDescriptor.CodeName);
99                  
100                 if(this.isDataNull(MetaEntityRelation.CodeName))
101                     this.setData(MetaEntityRelation.CodeName, compositeName);
102                 if(this.isDataNull(MetaEntityRelation.SourceName))
103                     this.setData(MetaEntityRelation.SourceName, compositeName);
104             }
105             catch(Exception e)
106             {
107                 Log.printWarning(this, "Failed to calculate entity relation code and source name: "+e.getClass().getName());
108             }
109         }
110     }
111         
112     // Access methods ----------------------------------------------------------
113     public String getSourceName() {return (String)this.getData(MetaEntityRelation.SourceName);}
114     public String getCodeName() {return (String)this.getData(MetaEntityRelation.CodeName);}
115     public String getForwardName() {return (String)this.getData(MetaEntityRelation.ForwardName);}
116     public String getReverseName() {return (String)this.getData(MetaEntityRelation.ReverseName);}
117     public String getReferenceEntity() {return (String)this.getData(MetaEntityRelation.ReferenceEntity);}
118     public String getTargetEntity() {return (String)this.getData(MetaEntityRelation.TargetEntity);}
119 
120     public void setSourceName(String value) {this.setData(MetaEntityRelation.SourceName, value);}
121     public void setCodeName(String value) {this.setData(MetaEntityRelation.CodeName, value);}
122     public void setForwardName(String value) {this.setData(MetaEntityRelation.ForwardName, value);}
123     public void setReverseName(String value) {this.setData(MetaEntityRelation.ReverseName, value);}
124     public void setReferenceEntity(String value) {this.setData(MetaEntityRelation.ReferenceEntity, value);}
125     public void setTargetEntity(String value) {this.setData(MetaEntityRelation.TargetEntity, value);}
126 
127     public Object getSourceNameData() {return this.getData(MetaEntityRelation.SourceName);}
128     public Object getCodeNameData() {return this.getData(MetaEntityRelation.CodeName);}
129     public Object getForwardNameData() {return this.getData(MetaEntityRelation.ForwardName);}
130     public Object getReverseNameData() {return this.getData(MetaEntityRelation.ReverseName);}
131     public Object getReferenceEntityData() {return this.getData(MetaEntityRelation.ReferenceEntity);}
132     public Object getTargetEntityData() {return this.getData(MetaEntityRelation.TargetEntity);}
133 
134     public void setSourceNameData(Object value) {this.setData(MetaEntityRelation.SourceName, value);}
135     public void setCodeNameData(Object value) {this.setData(MetaEntityRelation.CodeName, value);}
136     public void setForwardNameData(Object value) {this.setData(MetaEntityRelation.ForwardName, value);}
137     public void setReverseNameData(Object value) {this.setData(MetaEntityRelation.ReverseName, value);}
138     public void setReferenceEntityData(Object value) {this.setData(MetaEntityRelation.ReferenceEntity, value);}
139     public void setTargetEntityData(Object value) {this.setData(MetaEntityRelation.TargetEntity, value);}    
140 }