Skip to main content
summaryrefslogtreecommitdiffstats
blob: ecb1c3aebf6047736d24ea01d3a208d178e283e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.dsl.ui.integration.operations;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDsl;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDslFactory;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.RelationMultiplicityEnum;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XArtifactType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XAttributeType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XAttributeTypeRef;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XOseeEnumEntry;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XOseeEnumType;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XRelationType;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeInvalidInheritanceException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.jdk.core.util.xml.Jaxp;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeExtensionManager;
import org.eclipse.osee.framework.skynet.core.attribute.EnumeratedAttribute;
import org.eclipse.osee.framework.skynet.core.importing.IOseeDataTypeProcessor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Roberto E. Escobar
 */
public class ExcelToEMFModel implements IOseeDataTypeProcessor {
   private final OseeDslFactory factory;
   private final Map<String, OseeDsl> oseeModels;
   private OseeDsl currentModel;

   public ExcelToEMFModel(Map<String, OseeDsl> oseeModels) {
      this.factory = OseeDslFactory.eINSTANCE;
      this.oseeModels = oseeModels;
   }

   public void createModel(String name) {
      currentModel = factory.createOseeDsl();
      oseeModels.put(name, currentModel);
   }

   private OseeDsl getCurrentModel() {
      return currentModel;
   }

   private String toQualifiedName(String name) {
      return "\"" + name + "\"";
   }

   private OseeType getObject(String name, Class<? extends OseeType> classToLookFor) throws OseeArgumentException {
      EList<? extends OseeType> types;

      if (classToLookFor.equals(XArtifactType.class)) {
         types = getCurrentModel().getArtifactTypes();
      } else if (classToLookFor.equals(XAttributeType.class)) {
         types = getCurrentModel().getAttributeTypes();
      } else if (classToLookFor.equals(XRelationType.class)) {
         types = getCurrentModel().getRelationTypes();
      } else if (classToLookFor.equals(XOseeEnumType.class)) {
         types = getCurrentModel().getEnumTypes();
      } else {
         throw new OseeArgumentException("[%s] not a supported type", classToLookFor.getName());
      }
      for (OseeType oseeTypes : types) {
         if (name.equals(oseeTypes.getName())) {
            return classToLookFor.cast(oseeTypes);
         }
      }
      return null;
   }

   @Override
   public void onArtifactTypeInheritance(String ancestor, Collection<String> descendants) throws OseeCoreException {
      XArtifactType ancestorType = (XArtifactType) getObject(ancestor, XArtifactType.class);
      if (ancestorType == null) {
         throw new OseeInvalidInheritanceException("Ancestor [%s]", ancestor);
      }

      //      if (superArtifactTypeName != null) {
      //         ArtifactType superArtifactType = (ArtifactType) getObject(superArtifactTypeName, ArtifactType.class);
      //         if (superArtifactType == null) {
      //            boolean isAbstractSuper = false;
      //            onArtifactType(isAbstractSuper, superArtifactTypeName, null);
      //            superArtifactType = (ArtifactType) getObject(superArtifactTypeName, ArtifactType.class);
      //         }
      //         artifactType.setSuperArtifactType(superArtifactType);
      //      }
   }

   @Override
   public void onArtifactType(boolean isAbstract, String name) throws OseeCoreException {
      String id = toQualifiedName(name);
      OseeType types = getObject(id, XArtifactType.class);
      if (types == null) {
         XArtifactType artifactType = factory.createXArtifactType();
         artifactType.setName(id);
         getCurrentModel().getArtifactTypes().add(artifactType);
      }
   }

   @Override
   public void onAttributeType(String attributeBaseType, String attributeProviderTypeName, String fileTypeExtension, String name, String defaultValue, String validityXml, int minOccurrence, int maxOccurrence, String toolTipText, String taggerId) throws OseeCoreException {
      String id = toQualifiedName(name);
      OseeType types = getObject(id, XAttributeType.class);
      if (types == null) {
         XAttributeType attributeType = factory.createXAttributeType();
         attributeType.setName(id);
         attributeType.setBaseAttributeType(Lib.getExtension(attributeBaseType));
         attributeType.setDataProvider(Lib.getExtension(attributeProviderTypeName));
         attributeType.setMin(String.valueOf(minOccurrence));

         String maxValue;
         if (maxOccurrence == Integer.MAX_VALUE) {
            maxValue = "unlimited";
         } else {
            maxValue = String.valueOf(maxOccurrence);
         }
         attributeType.setMax(maxValue);

         if (Strings.isValid(fileTypeExtension)) {
            attributeType.setFileExtension(fileTypeExtension);
         }
         if (Strings.isValid(defaultValue)) {
            attributeType.setDefaultValue(defaultValue);
         }
         if (Strings.isValid(toolTipText)) {
            attributeType.setDescription(toolTipText);
         }
         if (Strings.isValid(taggerId)) {
            attributeType.setTaggerId(taggerId);
         }

         XOseeEnumType enumType = getEnumType(attributeBaseType, attributeProviderTypeName, name, validityXml);
         if (enumType != null) {
            attributeType.setEnumType(enumType);
         }
         getCurrentModel().getAttributeTypes().add(attributeType);
      }
   }

   @Override
   public boolean doesArtifactSuperTypeExist(String artifactSuperTypeName) throws OseeCoreException {
      return getObject(artifactSuperTypeName, XArtifactType.class) != null;
   }

   @Override
   public void onAttributeValidity(String attributeName, String artifactSuperTypeName, Collection<String> concreteTypes) throws OseeCoreException {
      XArtifactType superArtifactType =
         (XArtifactType) getObject(toQualifiedName(artifactSuperTypeName), XArtifactType.class);
      XAttributeType attributeType = (XAttributeType) getObject(toQualifiedName(attributeName), XAttributeType.class);

      if (superArtifactType == null && "Artifact".equals(artifactSuperTypeName)) {
         onArtifactType(false, "Artifact");
         superArtifactType = (XArtifactType) getObject(toQualifiedName(artifactSuperTypeName), XArtifactType.class);
      }

      if (superArtifactType == null || attributeType == null) {
         throw new OseeStateException("Type Missing: %s - %s", artifactSuperTypeName, attributeName);
      }
      XAttributeTypeRef reference = factory.createXAttributeTypeRef();
      reference.setValidAttributeType(attributeType);
      superArtifactType.getValidAttributeTypes().add(reference);
   }

   @Override
   public void onRelationType(String name, String sideAName, String sideBName, String artifactTypeSideA, String artifactTypeSideB, String multiplicity, String ordered, String defaultOrderTypeGuid) throws OseeCoreException {
      String id = toQualifiedName(name);
      OseeType types = getObject(id, XRelationType.class);
      if (types == null) {
         XRelationType relationType = factory.createXRelationType();
         relationType.setName(id);
         relationType.setSideAName(sideAName);
         relationType.setSideBName(sideBName);

         String arranger;
         if ("Yes".equals(ordered)) {
            arranger = "Lexicographical_Ascending";
         } else {
            arranger = "Unordered";
         }
         relationType.setDefaultOrderType(arranger);
         getCurrentModel().getRelationTypes().add(relationType);
      }
   }

   @Override
   public void onRelationValidity(String artifactTypeName, String relationTypeName, int sideAMax, int sideBMax) throws OseeCoreException {
      XRelationType relationType = (XRelationType) getObject(toQualifiedName(relationTypeName), XRelationType.class);
      XArtifactType artifactType = (XArtifactType) getObject(toQualifiedName(artifactTypeName), XArtifactType.class);

      if (sideAMax > 0) {
         relationType.setSideAArtifactType(artifactType);
      }
      if (sideBMax > 0) {
         relationType.setSideBArtifactType(artifactType);
      }

      RelationMultiplicityEnum multiplicity = relationType.getMultiplicity();
      if (sideAMax == Integer.MAX_VALUE && sideBMax == 1) {
         multiplicity = RelationMultiplicityEnum.ONE_TO_MANY;

      } else if (sideAMax == 1 && sideBMax == Integer.MAX_VALUE) {
         multiplicity = RelationMultiplicityEnum.MANY_TO_ONE;

      } else if (sideAMax == Integer.MAX_VALUE && sideBMax == Integer.MAX_VALUE) {
         multiplicity = RelationMultiplicityEnum.MANY_TO_MANY;
      } else if (sideAMax == 1 && sideBMax == 1) {
         multiplicity = RelationMultiplicityEnum.ONE_TO_ONE;
      } else {
         System.out.println("None detected - " + relationTypeName);
      }

      if (multiplicity != null && !multiplicity.equals(relationType.getMultiplicity())) {
         relationType.setMultiplicity(multiplicity);
      } else {
         System.out.println("Null multiplicity - " + relationTypeName);
      }
   }

   private static void checkEnumTypeName(String enumTypeName) throws OseeCoreException {
      if (!Strings.isValid(enumTypeName)) {
         throw new OseeArgumentException("Osee Enum Type Name cannot be null.");
      }
   }

   private XOseeEnumType getEnumType(String attributeBaseType, String attributeProviderTypeName, String name, String validityXml) throws OseeCoreException {
      Class<? extends Attribute<?>> baseAttributeClass =
         AttributeExtensionManager.getAttributeClassFor(attributeBaseType);

      XOseeEnumType oseeEnumType = null;
      if (EnumeratedAttribute.class.isAssignableFrom(baseAttributeClass)) {
         createEnumTypeFromXml(toQualifiedEnumName(name), validityXml);
      }
      return oseeEnumType;
   }

   private String toQualifiedEnumName(String name) {
      return "\"" + name + ".enum\"";
   }

   private XOseeEnumType createEnumTypeFromXml(String attributeTypeName, String xmlDefinition) throws OseeCoreException {
      List<Pair<String, Integer>> entries = new ArrayList<Pair<String, Integer>>();
      String enumTypeName = "";

      if (!Strings.isValid(xmlDefinition)) {
         throw new OseeArgumentException("The enum xml definition must not be null or empty");
      }

      Document document = null;
      try {
         document = Jaxp.readXmlDocument(xmlDefinition);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      enumTypeName = attributeTypeName;
      Element choicesElement = document.getDocumentElement();
      NodeList enumerations = choicesElement.getChildNodes();
      Set<String> choices = new LinkedHashSet<String>();

      for (int i = 0; i < enumerations.getLength(); i++) {
         Node node = enumerations.item(i);
         if (node.getNodeName().equalsIgnoreCase("Enum")) {
            choices.add(node.getTextContent());
         } else {
            throw new OseeArgumentException("Validity Xml not of excepted enum format");
         }
      }

      int ordinal = 0;
      for (String choice : choices) {
         entries.add(new Pair<String, Integer>(choice, ordinal++));
      }

      return createEnumType(enumTypeName, entries);
   }

   private XOseeEnumType createEnumType(String enumTypeName, List<Pair<String, Integer>> entries) throws OseeCoreException {
      checkEnumTypeName(enumTypeName);
      checkEntryIntegrity(enumTypeName, entries);

      XOseeEnumType oseeEnumType = null;

      OseeType types = getObject(enumTypeName, XOseeEnumType.class);
      if (types == null) {
         oseeEnumType = factory.createXOseeEnumType();
         oseeEnumType.setName(enumTypeName);

         for (Pair<String, Integer> entry : entries) {
            XOseeEnumEntry oseeEnum = factory.createXOseeEnumEntry();
            oseeEnum.setName(entry.getFirst());
            oseeEnum.setOrdinal(String.valueOf(entry.getSecond()));
            oseeEnumType.getEnumEntries().add(oseeEnum);
         }
         getCurrentModel().getEnumTypes().add(oseeEnumType);
      } else {
         oseeEnumType = (XOseeEnumType) types;
      }
      return oseeEnumType;
   }

   private static void checkEntryIntegrity(String enumTypeName, List<Pair<String, Integer>> entries) throws OseeCoreException {
      if (entries == null) {
         throw new OseeArgumentException("Osee Enum Type [%s] had null entries", enumTypeName);
      }

      //      if (entries.size() <= 0) throw new OseeArgumentException("Osee Enum Type [%s] had 0 entries",
      //            enumTypeName);
      Map<String, Integer> values = new HashMap<String, Integer>();
      for (Pair<String, Integer> entry : entries) {
         String name = entry.getFirst();
         int ordinal = entry.getSecond();
         if (!Strings.isValid(name)) {
            throw new OseeArgumentException("Enum entry name cannot be null");
         }
         if (ordinal < 0) {
            throw new OseeArgumentException("Enum entry ordinal cannot be of negative value");
         }
         if (values.containsKey(name)) {
            throw new OseeArgumentException("Unique enum entry name violation - [%s] already exists.", name);
         }
         if (values.containsValue(ordinal)) {
            throw new OseeArgumentException("Unique enum entry ordinal violation - [%s] already exists.", ordinal);
         }
         values.put(name, ordinal);
      }
   }

   @Override
   public void onFinish() {

   }
}

Back to the top