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  import org.caleigo.core.service.*;
26  
27  /***
28   *
29   * @author  Mattias Hagstrand
30   * @version 1.0
31   */
32  /* 
33  *
34  * WHEN        WHO               WHY & WHAT
35  * ------------------------------------------------------------------------------
36  * 2002-03-20  Mattias Hagstrand    Creation
37  */
38  public class EntityBuilder implements IMetaConsumer
39  {
40  	// Data members ------------------------------------------------------------
41  	//private IDataSourceDescriptor mDataSourceDescriptor;
42  	private IDataService mService;
43      
44      private MetaDataSourceDescriptorEntity mCurrentDataSourceEntity;
45  	private IEntityDescriptor mEntityEntityDescriptor;
46  	private IEntityDescriptor mFieldEntityDescriptor;
47  	private MetaEntityDescriptorEntity mCurrentEntity;
48  	private MetaFieldDescriptorEntity mCurrentField;
49      private MetaEntityRelationEntity mCurrentRelationEntity;
50      
51      private HashMap mMetaEntityCodeNames;
52      private List mEntityRelationEntities;
53      private List mFieldRelationEntities;
54      private List mFieldEntities;
55  	
56  	// Constructors ------------------------------------------------------------
57      public EntityBuilder()
58  	{
59          mMetaEntityCodeNames = new HashMap();
60          mEntityRelationEntities = new ArrayList();
61          mFieldRelationEntities = new ArrayList();
62          mFieldEntities = new ArrayList();
63  	}
64  	
65  	// IMetaConsumer implementation --------------------------------------------
66  	public void beginDataSource(String codeName, String sourceName, String displayName, String version, boolean readOnly)
67  	{
68  		mService = new MemoryDataService(Meta.instance);
69  		Meta.instance.setDefaultDataSource(new SingleServiceDataSource(mService));
70          
71          Meta.instance.createMetaEntities();
72          
73          ((AbstractDataService)mService).setValidating(false);
74          mCurrentDataSourceEntity = MetaDataSourceDescriptor.create();
75          mCurrentDataSourceEntity.setCodeName(codeName);
76          mCurrentDataSourceEntity.setSourceName(sourceName);
77          mCurrentDataSourceEntity.setDisplayName(displayName);
78          mCurrentDataSourceEntity.setVersion(version);
79          mCurrentDataSourceEntity.setReadOnly(readOnly);
80          mCurrentDataSourceEntity.store();
81          ((AbstractDataService)mService).setValidating(true);
82  	}
83  	
84  	public void beginEntity(String codeName, String sourceName, String displayName, int entityType, int flags, int cacheTime)
85  	{
86  		mCurrentEntity = MetaEntityDescriptor.create();
87  		mCurrentEntity.setCodeName(codeName);
88  		mCurrentEntity.setSourceName(sourceName);
89  		mCurrentEntity.setDisplayName(displayName);
90          mCurrentEntity.setEntityType(entityType);
91          mCurrentEntity.setSizeType(flags & AbstractEntityDescriptor.SELECTABLE);
92          mCurrentEntity.setCacheTime(cacheTime);
93          mCurrentEntity.setCreatable((flags & AbstractEntityDescriptor.CREATABLE)!=0);
94          mCurrentEntity.setEditable((flags & AbstractEntityDescriptor.EDITABLE)!=0);
95          mCurrentEntity.setDeletable((flags & AbstractEntityDescriptor.DELETABLE)!=0);
96          mCurrentEntity.setDataSourceData(mCurrentDataSourceEntity.getSourceName());
97  		mCurrentEntity.store();
98          
99          mMetaEntityCodeNames.put(mCurrentEntity.getSourceName(), mCurrentEntity.getCodeName());
100 	}
101 	
102 	public void addField(String codeName, String sourceName, String displayName, DataType dataType, int length, int flags, Object defValue)
103 	{
104         // Access data type.
105         ISelection typeSelection = mService.loadSelection(MetaDataType.instance, Qualifier.create(MetaDataType.ObjectTypeClass, dataType.getDataClass().getName()));
106         if(typeSelection.size()!=1)
107             throw new IllegalArgumentException("DataType \""+dataType.getTypeName()+"\" Not supperted in meta-meta structure!");
108         MetaDataTypeEntity typeEntity = (MetaDataTypeEntity)typeSelection.getEntity(0);
109 
110         // Creat e field descriptor meta entity and set data values.
111 		mCurrentField = MetaFieldDescriptor.create();
112 		mCurrentField.setCodeName(codeName);
113 		mCurrentField.setSourceName(sourceName);
114 		mCurrentField.setDisplayName(displayName);        
115 		mCurrentField.setDataType(typeEntity.getID());
116 		mCurrentField.setLength(length);
117         mCurrentField.setDefaultValue(dataType.convertToString(defValue));
118 		mCurrentField.setIdentityField((flags & IFieldDescriptor.IDENTITY_FIELD) != 0);
119 		mCurrentField.setNaturalOrder((flags & IFieldDescriptor.NATURAL_ORDER) != 0);
120 		mCurrentField.setRequired((flags & IFieldDescriptor.REQUIRED) != 0);
121 		mCurrentField.setIndexed((flags & IFieldDescriptor.INDEXED) != 0);
122 		mCurrentField.setAutoGenerated((flags & IFieldDescriptor.AUTOGEN) != 0);
123 		mCurrentField.setNameField((flags & IFieldDescriptor.NAME_FIELD) != 0);
124 		mCurrentField.setOverviewField((flags & IFieldDescriptor.OVERVIEW_FIELD) != 0);
125 		mCurrentField.setHintField((flags & IFieldDescriptor.HINT_FIELD) != 0);
126 		mCurrentField.setHidden((flags & IFieldDescriptor.HIDDEN_FIELD) != 0);
127 		mCurrentField.setReadOnly((flags & IFieldDescriptor.READ_ONLY_FIELD) != 0);
128 		mCurrentField.setEntity(mCurrentEntity.getSourceName());
129 		mCurrentField.store();
130         
131         mFieldEntities.add(mCurrentField);
132 	}
133 	
134     public void beginEntityRelation(String refEntitySourceName, String targetEntitySourceName, String sourceName, String codeName, String forwardName, String reverseName)
135     {
136         mCurrentRelationEntity = MetaEntityRelation.create();
137         mCurrentRelationEntity.setSourceName(sourceName);
138         mCurrentRelationEntity.setCodeName(codeName);
139         mCurrentRelationEntity.setForwardName(forwardName);
140         mCurrentRelationEntity.setReverseName(reverseName);
141         mCurrentRelationEntity.setReferenceEntity(refEntitySourceName);
142         mCurrentRelationEntity.setTargetEntity(targetEntitySourceName);
143         mCurrentRelationEntity.store();
144         
145         mEntityRelationEntities.add(mCurrentRelationEntity);
146     }
147     
148     public void addFieldRelation(String referenceFieldSourceName, String targetFieldSourceName)
149     {
150         MetaFieldRelationEntity fieldRelationEntity = MetaFieldRelation.create();
151         fieldRelationEntity.setEntityRelation(mCurrentRelationEntity.getSourceName());        
152         fieldRelationEntity.setReferenceEntity(mCurrentRelationEntity.getReferenceEntity());
153         fieldRelationEntity.setReferenceField(referenceFieldSourceName);        
154         fieldRelationEntity.setTargetEntity(mCurrentRelationEntity.getTargetEntity());
155         fieldRelationEntity.setTargetField(targetFieldSourceName);
156         fieldRelationEntity.store();
157         
158         mFieldRelationEntities.add(fieldRelationEntity);
159     }
160     
161     public void endEntityRelation()
162     {
163         mCurrentRelationEntity = null;
164     }
165     
166 	public void endEntity()
167 	{
168         mCurrentEntity = null;
169 	}
170 	
171 	public void endDataSource()
172 	{
173         for (int i = 0; i < mEntityRelationEntities.size(); i++)
174         {
175             MetaEntityRelationEntity currentRelation = (MetaEntityRelationEntity) mEntityRelationEntities.get(i);
176             if (!mMetaEntityCodeNames.containsKey(currentRelation.getReferenceEntity()))
177                 currentRelation.setReferenceEntity(this.findEntitySourceName(currentRelation.getReferenceEntity()));
178                 
179             if (!mMetaEntityCodeNames.containsKey(currentRelation.getTargetEntity()))
180                 currentRelation.setTargetEntity(this.findEntitySourceName(currentRelation.getTargetEntity()));
181         }
182         
183         for (int i = 0; i < mFieldRelationEntities.size(); i++)
184         {
185             MetaFieldRelationEntity currentRelation = (MetaFieldRelationEntity) mFieldRelationEntities.get(i);
186             if (!mMetaEntityCodeNames.containsKey(currentRelation.getReferenceEntity()))
187                 currentRelation.setReferenceEntity(this.findEntitySourceName(currentRelation.getReferenceEntity()));
188                 
189             if (!mMetaEntityCodeNames.containsKey(currentRelation.getTargetEntity()))
190                 currentRelation.setTargetEntity(this.findEntitySourceName(currentRelation.getTargetEntity()));
191         }
192         
193         for (int i = 0; i < mFieldEntities.size(); i++)
194         {
195             MetaFieldDescriptorEntity currentField = (MetaFieldDescriptorEntity) mFieldEntities.get(i);
196             if (!mMetaEntityCodeNames.containsKey(currentField.getEntity()))
197                 currentField.setEntity(this.findEntitySourceName(currentField.getEntity()));
198                 
199             if (!mMetaEntityCodeNames.containsKey(currentField.getEntity()))
200                 currentField.setEntity(this.findEntitySourceName(currentField.getEntity()));
201         }
202         
203         mService = null;
204 	}
205     
206     // Help methods ------------------------------------------------------------
207     private String findEntitySourceName(String origSourceName)
208     {
209         Iterator sourceNames = mMetaEntityCodeNames.keySet().iterator();
210         while (sourceNames.hasNext())
211         {
212             String currentSourceName = (String) sourceNames.next();
213             if (currentSourceName.equalsIgnoreCase(origSourceName))
214                 return currentSourceName;
215         }
216         
217         return origSourceName;
218     }
219 }