1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
36
37 class CustomEntityDescriptor extends AbstractEntityDescriptor
38 {
39
40 private IDataSourceDescriptor mDataSourceDescriptor;
41
42
43
44
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
51
52 }
53
54
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
65 entity = (IEntity) new CustomEntity(this);
66
67
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
91 entity = (IEntity) new CustomEntity(this);
92
93
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
110 public void setDataSourceDescriptor(IDataSourceDescriptor dataSourceDescriptor)
111 {
112 mDataSourceDescriptor = dataSourceDescriptor;
113 }
114
115
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
122 protected Object writeReplace() throws java.io.ObjectStreamException
123 {
124 return new Dezerializer(this.getDataSourceDescriptor().getCodeName(), this.getCodeName());
125 }
126
127
128
129
130
131
132
133 protected static class Dezerializer implements java.io.Serializable
134 {
135
136 protected String mDataSourceDescriptorCodeName;
137 protected String mCodeName;
138
139
140 public Dezerializer(String dataSourceDescriptorCodeName, String codeName)
141 {
142 mDataSourceDescriptorCodeName = dataSourceDescriptorCodeName;
143 mCodeName = codeName;
144 }
145
146
147 protected Object readResolve() throws java.io.ObjectStreamException
148 {
149
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 }