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  
22  import java.util.*;
23  
24  import org.caleigo.core.*;
25  
26  /***
27   *
28   * @author  Mattias Hagstrand
29   * @version 1.0
30   */
31  /* 
32  *
33  * WHEN        WHO               WHY & WHAT
34  * ------------------------------------------------------------------------------
35  * 2002-03-22  Mattias Hagstrand    Creation
36  */
37  public class DescriptorBuilder implements IMetaConsumer
38  {
39  	// Data members ------------------------------------------------------------
40  	private CustomDataSourceDescriptor mDataSourceDescriptor;
41  	private Vector mEntityDescriptors = new Vector();
42  	private Vector mCurrentFieldDescriptors = new Vector();
43  	private String mCurrentCodeName, mCurrentSourceName, mCurrentDisplayName;
44      private int mCurrentEntityType, mCurrentEntityFlags, mCurrentCacheTime;
45      private String mCurrentEntityRelationRef, mCurrentEntityRelationTarget, mCurrentEntityRelationCodeName, mCurrentEntityRelationForwardName, mCurrentEntityRelationReverseName;
46      private Vector mCurrentFieldRelations = new Vector();
47      private Vector mCurrentEntityRelations = new Vector();
48  	
49  	// Constructors ------------------------------------------------------------
50      public DescriptorBuilder()
51  	{
52          Meta.instance.createMetaEntities();
53      }
54  	
55  	// IMetaConsumer implementation --------------------------------------------
56  	public void beginDataSource(String codeName, String sourceName, String displayName, String version, boolean readOnly)
57  	{
58  		mDataSourceDescriptor = new CustomDataSourceDescriptor(codeName, sourceName, displayName, version, readOnly);
59  	}
60  	
61  	public void beginEntity(String codeName, String sourceName, String displayName, int entityType, int flags, int cacheTime)
62  	{
63  		mCurrentCodeName = codeName;
64  		mCurrentSourceName = sourceName;
65  		mCurrentDisplayName = displayName;
66          mCurrentEntityType = entityType;
67          mCurrentEntityFlags = flags;
68          mCurrentCacheTime = cacheTime;
69  	}
70  	
71  	public void addField(String codeName, String sourceName, String displayName, DataType dataType, int length, int flags, Object defValue)
72  	{
73  		mCurrentFieldDescriptors.add(new CustomFieldDescriptor(codeName, sourceName, displayName, dataType, length, flags, getDefaultValue(defValue, dataType)));
74  	}
75  	
76      public void beginEntityRelation(String refEntitySourceName, String targetEntitySourceName, String sourceName, String codeName, String forwardName, String reverseName)
77      {
78          mCurrentEntityRelationRef = refEntitySourceName;
79          mCurrentEntityRelationTarget = targetEntitySourceName;
80          mCurrentEntityRelationCodeName = codeName;
81          mCurrentEntityRelationForwardName = forwardName;
82          mCurrentEntityRelationReverseName = reverseName;
83      }
84      
85      public void addFieldRelation(String refFieldSourceName, String targetFieldSourceName)
86      {
87          mCurrentFieldRelations.add(new CustomFieldRelation(mDataSourceDescriptor, mCurrentEntityRelationRef, refFieldSourceName, mCurrentEntityRelationTarget, targetFieldSourceName));
88      }
89      
90      public void endEntityRelation()
91      {
92          IEntityRelation entityRelation = CustomEntityDescriptor.createEntityRelation((IFieldRelation[]) mCurrentFieldRelations.toArray(new IFieldRelation[0]), mCurrentEntityRelationCodeName, mCurrentEntityRelationForwardName, mCurrentEntityRelationReverseName);
93          for (int i = 0; i < mCurrentFieldRelations.size(); i++)
94              ((CustomFieldRelation) mCurrentFieldRelations.get(i)).setEntityRelation(entityRelation);
95          mCurrentEntityRelations.add(entityRelation);
96          mCurrentFieldRelations.removeAllElements();
97      }
98      
99  	public void endEntity()
100 	{
101 		IEntityDescriptor entityDescriptor = new CustomEntityDescriptor(mCurrentCodeName, mCurrentSourceName, mCurrentDisplayName, (IFieldDescriptor[]) mCurrentFieldDescriptors.toArray(new IFieldDescriptor[0]), mCurrentEntityType, mCurrentEntityFlags, mCurrentCacheTime, (IEntityRelation[]) mCurrentEntityRelations.toArray(new IEntityRelation[0]));
102 		((CustomEntityDescriptor) entityDescriptor).setDataSourceDescriptor(mDataSourceDescriptor);
103 		((CustomDataSourceDescriptor) mDataSourceDescriptor).addEntityDescriptor(entityDescriptor);
104 		for (int i = 0; i < mCurrentFieldDescriptors.size(); i++)
105 			((CustomFieldDescriptor) mCurrentFieldDescriptors.elementAt(i)).setEntityDescriptor(entityDescriptor);
106 
107 		mCurrentFieldDescriptors.removeAllElements();
108         mCurrentEntityRelations.removeAllElements();
109 	}
110 	
111 	public void endDataSource()
112 	{
113 	}
114 	
115 	// Access methods ----------------------------------------------------------
116 	public IDataSourceDescriptor getDataSourceDescriptor()
117 	{
118 		return mDataSourceDescriptor;
119 	}
120     
121     private static Object getDefaultValue(Object defValue, DataType dataType)
122     {
123     	if(dataType.getDataClass()==java.util.Date.class)
124         	return DateProxyData.convertToDateObject(defValue);
125         else if(defValue!=null && defValue.getClass()!=dataType.getDataClass())
126         	return dataType.convertFrom(defValue);
127         else
128         	return defValue;
129     }    
130     
131 }