1 package com.bonevich.erj.app;
2
3 import com.bonevich.erj.model.*;
4
5 import ru.novosoft.mdf.ext.MDFOutermostPackage;
6
7 import java.util.Collection;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.LinkedList;
11
12 public final class MofModelConverter extends ModelConverter
13 {
14 //////////////////////////////////////////////////////////
15 // Attributes
16
17 protected MofModelConverter()
18 {
19 createRepository();
20 }
21
22 public Object convert(Object source)
23 {
24 createModel();
25 buildInfoModel();
26 return _model;
27 }
28
29 private void buildInfoModel()
30 {
31 Collection roots = ((MDFOutermostPackage) _repository).getRoots();
32 //System.err.println("roots = " + roots);
33 //System.err.println("count = " + roots.size());
34
35 com.bonevich.erj.mof.interfaces.model.Schema schema = null;
36 Iterator itr = roots.iterator();
37 while (itr.hasNext())
38 {
39 Object root = itr.next();
40 if (root instanceof com.bonevich.erj.mof.interfaces.model.Schema)
41 {
42 schema = (com.bonevich.erj.mof.interfaces.model.Schema) root;
43 break;
44 }
45 }
46 buildSchema(schema);
47 }
48
49 private void buildModelElement(com.bonevich.erj.mof.interfaces.model.ModelElement elt, ModelElement infoElt)
50 {
51 infoElt.setName(elt.getName());
52 infoElt.setIdentifier(elt.getIdentifier());
53 infoElt.setDescription(elt.getDescription());
54 }
55
56 private void buildSchema(com.bonevich.erj.mof.interfaces.model.Schema schema)
57 {
58 buildModelElement(schema, _model);
59 Iterator domains = schema.getDomains().iterator();
60 while (domains.hasNext())
61 {
62 buildDomain((com.bonevich.erj.mof.interfaces.model.Domain) domains.next());
63 }
64
65 Iterator relations = schema.getRelations().iterator();
66 while (relations.hasNext())
67 {
68 buildRelation((com.bonevich.erj.mof.interfaces.model.Relation) relations.next());
69 }
70 }
71
72 private void buildDomain(com.bonevich.erj.mof.interfaces.model.Domain domain)
73 {
74 Domain newDomain = _model.createDomain();
75 buildModelElement(domain, newDomain);
76
77 com.bonevich.erj.mof.interfaces.model.DataType type = domain.getDataType();
78 newDomain.setDataType((DataType) findType(type));
79
80 com.bonevich.erj.mof.interfaces.model.DefaultValue value = domain.getDefaultValue();
81 newDomain.setDefaultValue(findDefaultValue(value));
82
83 com.bonevich.erj.mof.interfaces.model.AttributeConstraint constraint = domain.getDefaultConstraint();
84 newDomain.setDefaultConstraint(findAttributeConstraint(constraint));
85 }
86
87 private void buildRelation(com.bonevich.erj.mof.interfaces.model.Relation relation)
88 {
89 Relation newRelation = _model.createRelation();
90 buildModelElement(relation, newRelation);
91
92 Iterator attributes = relation.getAttributes().iterator();
93 while (attributes.hasNext())
94 {
95 buildAttribute((com.bonevich.erj.mof.interfaces.model.Attribute) attributes.next(), newRelation);
96 }
97
98 Iterator keys = relation.getKeys().iterator();
99 while (keys.hasNext())
100 {
101 buildKeyConstraint((com.bonevich.erj.mof.interfaces.model.KeyConstraint) keys.next(), newRelation);
102 }
103 }
104
105 private void buildAttribute(com.bonevich.erj.mof.interfaces.model.Attribute attribute, Relation parent)
106 {
107 Attribute newAttr = parent.createAttribute();
108 buildModelElement(attribute, newAttr);
109
110 com.bonevich.erj.mof.interfaces.model.AttributeType type = attribute.getType();
111 newAttr.setType(findType(type));
112
113 com.bonevich.erj.mof.interfaces.model.DefaultValue value = attribute.getDefaultValue();
114 newAttr.setDefaultValue(findDefaultValue(value));
115
116 com.bonevich.erj.mof.interfaces.model.AttributeConstraint constraint = attribute.getConstraint();
117 newAttr.setConstraint(findAttributeConstraint(constraint));
118 }
119
120 private void buildKeyConstraint(com.bonevich.erj.mof.interfaces.model.KeyConstraint key, Relation parent)
121 {
122 List attrList = new LinkedList();
123 Iterator keyAttrs = key.getAttributes().iterator();
124 while (keyAttrs.hasNext())
125 {
126 com.bonevich.erj.mof.interfaces.model.Attribute ka = (com.bonevich.erj.mof.interfaces.model.Attribute) keyAttrs.next();
127 Iterator attrItr = parent.getAttributeIterator();
128 while (attrItr.hasNext())
129 {
130 Attribute a = (Attribute) attrItr.next();
131 if (a.getIdentifier().equals(ka.getIdentifier()))
132 {
133 attrList.add(a);
134 break;
135 }
136 }
137 }
138 Attribute[] attributes = new Attribute[attrList.size()];
139 attributes = (Attribute[]) attrList.toArray(attributes);
140
141 KeyConstraint newKey = null;
142 if (key instanceof com.bonevich.erj.mof.interfaces.model.UniqueKey)
143 {
144 newKey = parent.createUniqueKey(attributes);
145 }
146 else if (key instanceof com.bonevich.erj.mof.interfaces.model.PrimaryKey)
147 {
148 newKey = parent.createPrimaryKey(attributes);
149 }
150 else if (key instanceof com.bonevich.erj.mof.interfaces.model.ForeignKey)
151 {
152 com.bonevich.erj.mof.interfaces.model.ForeignKey fk
153 = (com.bonevich.erj.mof.interfaces.model.ForeignKey) key;
154 com.bonevich.erj.mof.interfaces.model.Relation ref
155 = (com.bonevich.erj.mof.interfaces.model.Relation) fk.getReferent().get(0);
156 Relation referent = null;
157 Iterator itr = _model.getRelationIterator();
158 while (itr.hasNext())
159 {
160 Relation r = (Relation) itr.next();
161 if (r.getIdentifier().equals(ref.getIdentifier()))
162 {
163 referent = r;
164 break;
165 }
166 }
167 newKey = parent.createForeignKey(referent, attributes);
168 ((ForeignKey) newKey).setCardinality( findCardinality(fk.getCardinality()) );
169 }
170 else
171 {
172 return;
173 }
174 buildModelElement(key, newKey);
175 }
176
177 private AttributeType findType(com.bonevich.erj.mof.interfaces.model.AttributeType type)
178 {
179 if (type instanceof com.bonevich.erj.mof.interfaces.model.DataType)
180 {
181 com.bonevich.erj.mof.interfaces.model.DataType dt = (com.bonevich.erj.mof.interfaces.model.DataType) type;
182 if (dt.getDescriptor() != null)
183 {
184 return new DataType(dt.getName(), dt.getDescriptor());
185 }
186 return new DataType(dt.getName());
187 }
188 else if (type instanceof com.bonevich.erj.mof.interfaces.model.Domain)
189 {
190 com.bonevich.erj.mof.interfaces.model.Domain domain = (com.bonevich.erj.mof.interfaces.model.Domain) type;
191 Iterator itr = _model.getDomainIterator();
192 while (itr.hasNext())
193 {
194 Domain d = (Domain) itr.next();
195 if (d.getIdentifier().equals(domain.getIdentifier()))
196 {
197 return d;
198 }
199 }
200 }
201 return null;
202 }
203
204 private DefaultValue findDefaultValue(com.bonevich.erj.mof.interfaces.model.DefaultValue dv)
205 {
206 if (dv != null)
207 {
208 String value = dv.getValue();
209 if (DefaultValue.NULL_STR.equals(value))
210 {
211 return DefaultValue.NULL;
212 }
213 else if (DefaultValue.EMPTY_STR.equals(value))
214 {
215 return DefaultValue.EMPTY;
216 }
217 else if (value != null)
218 {
219 return new DefaultValue(value);
220 }
221 }
222 return null;
223 }
224
225 private AttributeConstraint findAttributeConstraint(com.bonevich.erj.mof.interfaces.model.AttributeConstraint ac)
226 {
227 if (ac == com.bonevich.erj.mof.interfaces.model.AttributeConstraintEnum.NOTNULL)
228 {
229 return AttributeConstraint.NOTNULL;
230 }
231 return AttributeConstraint.NULL;
232 }
233
234 private ForeignKey.Cardinality findCardinality(com.bonevich.erj.mof.interfaces.model.Cardinality card)
235 {
236 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ZERO_TO_ZERO)
237 {
238 return ForeignKey.ZERO_TO_ZERO;
239 }
240 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ONE_TO_ZERO)
241 {
242 return ForeignKey.ONE_TO_ZERO;
243 }
244 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ONE_TO_ONE)
245 {
246 return ForeignKey.ONE_TO_ONE;
247 }
248 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ZERO_TO_MANY)
249 {
250 return ForeignKey.ZERO_TO_MANY;
251 }
252 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ONE_TO_MANY)
253 {
254 return ForeignKey.ONE_TO_MANY;
255 }
256 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ZERO_TO_MANY_MANDATORY)
257 {
258 return ForeignKey.ZERO_TO_MANY_MANDATORY;
259 }
260 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.ONE_TO_MANY_MANDATORY)
261 {
262 return ForeignKey.ONE_TO_MANY_MANDATORY;
263 }
264 if (card == com.bonevich.erj.mof.interfaces.model.CardinalityEnum.MANY_TO_MANY)
265 {
266 return ForeignKey.MANY_TO_MANY;
267 }
268 return ForeignKey.ZERO_TO_MANY;
269 }
270
271 } /*** end class MofModelConverter */
This page was automatically generated by Maven