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 java.util.*;
22  
23  import org.caleigo.core.*;
24  import org.caleigo.toolkit.log.*;
25  
26  /***
27   *
28   * @author  Mattias Hagstrand
29   * @version 1.0
30   */
31  /* 
32  *
33  * WHEN        WHO               WHY & WHAT
34  * ------------------------------------------------------------------------------
35  * 2002-03-25  Mattias Hagstrand    Creation
36  */
37  class CustomEntityDescriptor extends AbstractEntityDescriptor
38  {
39  	// Data members ------------------------------------------------------------
40  	private IDataSourceDescriptor mDataSourceDescriptor;
41      
42  //    protected static HashMap sRegisteredEntityDescriptors = new HashMap();
43  	
44  	// Custructors -------------------------------------------------------------
45      public CustomEntityDescriptor(String codeName, String sourceName, String displayName, IFieldDescriptor[] fields, int entityType, int flags, int cacheTime, IEntityRelation[] relations)
46  	{
47  		super(codeName, sourceName, displayName, "org.caleigo.core.CustomEntity", null, entityType, fields, relations);
48          this.setFlags(flags);
49          this.setCacheTime(cacheTime);
50  //        sRegisteredEntityDescriptors.remove(codeName);
51  //        sRegisteredEntityDescriptors.put(codeName, this);
52      }
53  	
54  	// Superclass overrides ----------------------------------------------------
55  	
56  	/*** Creates an entity with of the type described by this descriptor and
57       * loads it with default data.
58       */
59      public IEntity createEntity()
60      {
61          IEntity entity = null;
62          try
63          {
64              // Create entity.
65              entity = (IEntity) new CustomEntity(this);
66              
67              // Set default data.
68              Iterator fielddescriptors = this.getFieldDescriptors();
69              while (fielddescriptors.hasNext())
70              {
71                  IFieldDescriptor currentFieldDescriptor = (IFieldDescriptor) fielddescriptors.next();
72                  entity.setData(currentFieldDescriptor, currentFieldDescriptor.getDefaultValue());
73              }
74          }
75          catch(Exception e)
76          {
77              e.printStackTrace();
78          }
79          return entity;
80      }
81      
82      /*** Creates an entity with of the type described by this descriptor and
83       * loads it with data from the provided property source.
84       */
85      public IEntity createEntity(IDataProvider propertySource)
86      {
87          IEntity entity = null;
88          try
89          {
90              // Create entity.
91              entity = (IEntity) new CustomEntity(this);
92              
93              // Load entity with data from the propertySource.
94              entity.setData(propertySource);
95          }
96          catch(Exception e)
97          {
98          }
99          return entity;
100     }
101 	    
102 	/*** Returns the DataSource descriptor that this entity is a part of.
103      */
104     public IDataSourceDescriptor getDataSourceDescriptor()
105 	{
106         return mDataSourceDescriptor;
107     }
108     
109 	// Access methods ----------------------------------------------------------
110 	public void setDataSourceDescriptor(IDataSourceDescriptor dataSourceDescriptor)
111 	{
112 		mDataSourceDescriptor = dataSourceDescriptor;
113 	}
114     
115     // Help methods ------------------------------------------------------------
116     protected static IEntityRelation createEntityRelation(IFieldRelation[] fieldRelation, String codeName, String forwardName, String reverseName)
117     {
118         return AbstractEntityDescriptor.createEntityRelation(fieldRelation, codeName, forwardName, reverseName);
119     }
120     
121     // Superclass overrides ----------------------------------------------------
122     protected Object writeReplace() throws java.io.ObjectStreamException
123     {
124       return new Dezerializer(this.getDataSourceDescriptor().getCodeName(), this.getCodeName());
125     }
126     
127 //    protected void finalize()
128 //    {
129 //        sRegisteredEntityDescriptors.remove(this.getCodeName());
130 //    }
131     
132     // Nested classes ----------------------------------------------------------
133     protected static class Dezerializer implements java.io.Serializable
134     {
135         // Data members --------------------------------------------------------
136         protected String mDataSourceDescriptorCodeName;
137         protected String mCodeName;
138                 
139         // Constructors --------------------------------------------------------
140         public Dezerializer(String dataSourceDescriptorCodeName, String codeName)
141         {
142             mDataSourceDescriptorCodeName = dataSourceDescriptorCodeName;
143             mCodeName = codeName;
144         }
145         
146         // Action methods ------------------------------------------------------
147         protected Object readResolve() throws java.io.ObjectStreamException
148         {
149 //            return CustomEntityDescriptor.sRegisteredEntityDescriptors.get(mCodeName);
150 
151             CustomDataSourceDescriptor dataSourceDescriptor = CustomDataSourceDescriptor.getRegisteredCustomDataSourceDescriptor(mDataSourceDescriptorCodeName);
152             
153             if (dataSourceDescriptor != null)
154             {
155                 IEntityDescriptor entityDescriptor = dataSourceDescriptor.getEntityDescriptor(mCodeName);
156                 if (entityDescriptor != null)
157                     return entityDescriptor;
158                 else
159                 {
160                     Log.printError(this, "CustomEntityDescriptor with code name " + mCodeName + " not found");
161                     throw new java.io.InvalidObjectException("CustomEntityDescriptor with code name " + mCodeName + " not found");
162                 }
163             }
164             else
165             {
166                 Log.printError(this, "CustomDataSourceDescriptor with code name " + mDataSourceDescriptorCodeName + " not found");
167                 throw new java.io.InvalidObjectException("CustomDataSourceDescriptor with code name " + mDataSourceDescriptorCodeName + " not found");
168             }
169         }
170     }
171     
172     
173     
174 }