Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5178b370885d1fbca51fcb7b9a552afe1a996ffc (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
/*******************************************************************************
 * 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.orcs.db.internal.types;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.core.data.IOseeBranch;
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.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.enums.CoreBranches;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.OseeEnumType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.util.HexUtil;

/**
 * @author Roberto E. Escobar
 */
public class OseeToXtextOperation extends AbstractOperation {

   private final OseeDsl oseeModel;
   private final OseeDslFactory factory;
   private final OseeTypeCache cache;

   public OseeToXtextOperation(OseeTypeCache cache, OseeDslFactory factory, OseeDsl oseeModel) {
      super("OSEE to Text Model", "");
      this.oseeModel = oseeModel;
      this.factory = factory;
      this.cache = cache;
   }

   private OseeDslFactory getFactory() {
      return factory;
   }

   private OseeDsl getModelByNamespace(String namespace) {
      return oseeModel;
   }

   private String getNamespace(String name) {
      String toReturn = "default";
      //      if (Strings.isValid(name)) {
      //         int index = name.lastIndexOf(".");
      //         if (index > 0) {
      //            toReturn = name.substring(0, index);
      //         }
      //      }
      return toReturn;
   }

   private String asPrimitiveType(String name) {
      return name.replace("org.eclipse.osee.framework.skynet.core.", "");
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      double workPercentage = 1.0 / 6.0;
      populateEnumTypes(monitor, workPercentage);
      populateAttributeTypes(monitor, workPercentage);
      populateArtifactTypes(monitor, workPercentage);
      populateArtifactTypeInheritance(monitor, workPercentage);
      populateArtifactTypeAttributeTypes(monitor, workPercentage);
      populateRelationTypes(monitor, workPercentage);
   }

   private void populateEnumTypes(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      Collection<OseeEnumType> enumTypes = cache.getEnumTypeCache().getAll();
      for (OseeEnumType enumType : enumTypes) {
         checkForCancelledStatus(monitor);
         XOseeEnumType modelType = getFactory().createXOseeEnumType();

         OseeDsl model = getModelByNamespace(getNamespace(enumType.getName()));
         model.getEnumTypes().add(modelType);

         modelType.setName(enumType.getName());
         modelType.setUuid(HexUtil.toString(enumType.getGuid()));

         for (OseeEnumEntry entry : enumType.values()) {
            checkForCancelledStatus(monitor);
            XOseeEnumEntry entryModelType = getFactory().createXOseeEnumEntry();
            modelType.getEnumEntries().add(entryModelType);

            entryModelType.setName(entry.getName());
            entryModelType.setOrdinal(String.valueOf(entry.ordinal()));
         }
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateAttributeTypes(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      monitor.setTaskName("Attribute Types");
      Collection<AttributeType> attributeTypes = cache.getAttributeTypeCache().getAll();
      for (AttributeType attributeType : attributeTypes) {
         checkForCancelledStatus(monitor);
         XAttributeType modelType = getFactory().createXAttributeType();

         OseeDsl model = getModelByNamespace(getNamespace(attributeType.getName()));
         model.getAttributeTypes().add(modelType);

         modelType.setName(attributeType.getName());

         modelType.setUuid(HexUtil.toString(attributeType.getGuid()));

         modelType.setBaseAttributeType(asPrimitiveType(attributeType.getBaseAttributeTypeId()));
         modelType.setDataProvider(asPrimitiveType(attributeType.getAttributeProviderId()));
         modelType.setMax(String.valueOf(attributeType.getMaxOccurrences()));
         modelType.setMin(String.valueOf(attributeType.getMinOccurrences()));
         modelType.setFileExtension(attributeType.getFileTypeExtension());
         modelType.setDescription(attributeType.getDescription());
         modelType.setDefaultValue(attributeType.getDefaultValue());
         modelType.setTaggerId(attributeType.getTaggerId());
         modelType.setMediaType(attributeType.getMediaType());

         OseeEnumType oseeEnumType = attributeType.getOseeEnumType();
         if (oseeEnumType != null) {
            XOseeEnumType enumType = toModelEnumType(model, oseeEnumType.getGuid());
            modelType.setEnumType(enumType);
         }
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateArtifactTypes(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      monitor.setTaskName("Artifact Types");
      Collection<ArtifactType> artifactTypes = cache.getArtifactTypeCache().getAll();
      for (ArtifactType artifactType : artifactTypes) {
         checkForCancelledStatus(monitor);
         XArtifactType modelType = getFactory().createXArtifactType();

         OseeDsl model = getModelByNamespace(getNamespace(artifactType.getName()));
         model.getArtifactTypes().add(modelType);

         modelType.setName(artifactType.getName());
         modelType.setUuid(HexUtil.toString(artifactType.getGuid()));
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateArtifactTypeInheritance(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      monitor.setTaskName("Artifact Type Inheritance");
      Collection<ArtifactType> artifactTypes = cache.getArtifactTypeCache().getAll();
      for (ArtifactType artifactType : artifactTypes) {
         checkForCancelledStatus(monitor);
         OseeDsl model = getModelByNamespace(getNamespace(artifactType.getName()));

         XArtifactType childType = getArtifactType(model, artifactType.getGuid());

         for (ArtifactType oseeSuperType : artifactType.getSuperArtifactTypes()) {
            XArtifactType superModelType = getArtifactType(model, oseeSuperType.getGuid());
            childType.getSuperArtifactTypes().add(superModelType);
         }
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateArtifactTypeAttributeTypes(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      monitor.setTaskName("Artifact Type to Attribute Types");
      Collection<ArtifactType> artifactTypes = cache.getArtifactTypeCache().getAll();
      for (ArtifactType artifactType : artifactTypes) {
         checkForCancelledStatus(monitor);

         OseeDsl model = getModelByNamespace(getNamespace(artifactType.getName()));
         XArtifactType modelArtifactType = getArtifactType(model, artifactType.getGuid());

         Map<IOseeBranch, Collection<AttributeType>> types = artifactType.getLocalAttributeTypes();
         if (types != null) {
            List<XAttributeTypeRef> references = new ArrayList<XAttributeTypeRef>();
            for (Entry<IOseeBranch, Collection<AttributeType>> entry : types.entrySet()) {
               IOseeBranch branch = entry.getKey();
               Collection<AttributeType> attributeTypes = entry.getValue();
               if (attributeTypes != null) {
                  for (AttributeType attributeType : attributeTypes) {

                     XAttributeTypeRef ref = getFactory().createXAttributeTypeRef();

                     XAttributeType modelType = getAttributeType(model, attributeType.getGuid());
                     if (modelType != null) {
                        ref.setValidAttributeType(modelType);
                        if (branch != null && !branch.equals(CoreBranches.SYSTEM_ROOT)) {
                           ref.setBranchGuid(branch.getGuid());
                        }
                        references.add(ref);
                     }
                  }
               }
            }
            modelArtifactType.getValidAttributeTypes().addAll(references);
         }
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private void populateRelationTypes(IProgressMonitor monitor, double workPercentage) throws OseeCoreException {
      monitor.setTaskName("Relation Types");
      Collection<RelationType> relationTypes = cache.getRelationTypeCache().getAll();
      for (RelationType relationType : relationTypes) {
         checkForCancelledStatus(monitor);
         XRelationType modelType = getFactory().createXRelationType();

         OseeDsl model = getModelByNamespace(getNamespace(relationType.getName()));
         model.getRelationTypes().add(modelType);

         modelType.setName(relationType.getName());
         modelType.setUuid(HexUtil.toString(relationType.getGuid()));

         modelType.setDefaultOrderType(OseeUtil.getRelationOrderType(relationType.getDefaultOrderTypeGuid()));
         modelType.setMultiplicity(RelationMultiplicityEnum.getByName(relationType.getMultiplicity().name()));

         modelType.setSideAName(relationType.getSideAName());
         modelType.setSideBName(relationType.getSideBName());

         modelType.setSideAArtifactType(getArtifactType(model, relationType.getArtifactTypeSideA().getGuid()));
         modelType.setSideBArtifactType(getArtifactType(model, relationType.getArtifactTypeSideB().getGuid()));
      }
      monitor.worked(calculateWork(workPercentage));
   }

   private XArtifactType getArtifactType(OseeDsl model, Long uuid) throws OseeCoreException {
      for (XArtifactType type : model.getArtifactTypes()) {
         Long normalizedGuid = HexUtil.toLong(type.getUuid());
         if (uuid.equals(normalizedGuid)) {
            return type;
         }
      }
      return null;
   }

   private XAttributeType getAttributeType(OseeDsl model, Long uuid) throws OseeCoreException {
      for (XAttributeType type : model.getAttributeTypes()) {
         Long normalizedGuid = HexUtil.toLong(type.getUuid());
         if (uuid.equals(normalizedGuid)) {
            return type;
         }
      }
      return null;
   }

   private XOseeEnumType toModelEnumType(OseeDsl model, Long uuid) throws OseeCoreException {
      for (XOseeEnumType type : model.getEnumTypes()) {
         Long normalizedGuid = HexUtil.toLong(type.getUuid());
         if (uuid.equals(normalizedGuid)) {
            return type;
         }
      }
      return null;
   }

}

Back to the top