Skip to main content
summaryrefslogtreecommitdiffstats
blob: d6330fa6b59cc69bf7170cd2f141c04dd12c6735 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*******************************************************************************
 * Copyright (c) 2015 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.rest.internal.writer;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.exception.OseeWrappedException;
import org.eclipse.osee.framework.core.util.XResultData;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.DateUtil;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifact;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifactToken;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwArtifactType;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwAttribute;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwAttributeType;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwCollector;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwRelation;
import org.eclipse.osee.orcs.rest.model.writer.reader.OwRelationType;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;

/**
 * @author Donald G. Dunne
 */
public class OrcsCollectorWriter {

   private final OwCollector collector;
   private final OrcsApi orcsApi;
   private Map<Long, ArtifactId> uuidToArtifact;
   private TransactionBuilder transaction;
   private BranchId branch;
   private ArtifactReadable user;
   private final XResultData results;

   public OrcsCollectorWriter(OrcsApi orcsApi, OwCollector collector, XResultData results) {
      this.orcsApi = orcsApi;
      this.collector = collector;
      this.results = results;
      uuidToArtifact = new HashMap<>();
   }

   public XResultData run() {
      processCreate(results);
      processUpdate(results);
      processDelete(results);
      getTransaction().commit();
      results.log("Complete");
      return results;
   }

   private void processDelete(XResultData results) {
      for (OwArtifactToken owArtifact : collector.getDelete()) {
         ArtifactReadable artifact = orcsApi.getQueryFactory().fromBranch(getBranch()).andUuid(
            owArtifact.getUuid()).getResults().getAtMostOneOrNull();
         if (artifact == null) {
            results.warningf("Delete Artifact Token %s does not exist in database.  Skipping", owArtifact);
         } else {
            getTransaction().deleteArtifact(artifact);
            results.logf("Deleted artifact %s", owArtifact);
         }
      }
   }

   private void processUpdate(XResultData results) {
      for (OwArtifact owArtifact : collector.getUpdate()) {
         ArtifactReadable artifact = orcsApi.getQueryFactory().fromBranch(getBranch()).andUuid(
            owArtifact.getUuid()).getResults().getAtMostOneOrNull();

         if (artifact == null) {
            throw new OseeArgumentException("Artifact not found for OwArtifact %s", owArtifact);
         }

         if (!owArtifact.getName().equals(artifact.getName())) {
            getTransaction().setName(artifact, owArtifact.getName());
            logChange(artifact, CoreAttributeTypes.Name, artifact.getName(), owArtifact.getName());
         }

         try {
            createMissingRelations(owArtifact.getRelations(), artifact, results);
         } catch (Exception ex) {
            throw new OseeWrappedException(ex, "Exception processing relations for [%s]", owArtifact);
         }

         try {
            for (OwAttribute owAttribute : owArtifact.getAttributes()) {
               IAttributeType attrType = getAttributeType(orcsApi, owAttribute.getType());

               if (artifact.getAttributeCount(attrType) <= 1 && owAttribute.getValues().size() <= 1) {
                  String currValue = artifact.getSoleAttributeAsString(attrType, null);

                  String newValue = null;
                  if (owAttribute.getValues().size() == 1) {
                     Object object = owAttribute.getValues().iterator().next();
                     if (object != null) {
                        newValue = owAttribute.getValues().iterator().next().toString();
                     }
                  }

                  // handle delete attribute case first
                  if (Strings.isValid(currValue) && newValue == null) {
                     logChange(artifact, attrType, currValue, newValue);
                     getTransaction().deleteAttributes(artifact, attrType);
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isBooleanType(attrType)) {
                     Boolean currVal = getBoolean(currValue);
                     Boolean newVal = getBoolean(newValue);
                     if (currVal == null || !currVal.equals(newVal)) {
                        logChange(artifact, attrType, currValue, newValue);
                        getTransaction().setSoleAttributeValue(artifact, attrType, newVal);
                     }
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isFloatingType(attrType)) {
                     try {
                        Double currVal = getDouble(currValue);
                        Double newVal = getDouble(newValue);
                        if (currVal == null || !currVal.equals(newVal)) {
                           logChange(artifact, attrType, currValue, newValue);
                           getTransaction().setSoleAttributeValue(artifact, attrType, newVal);
                        }
                     } catch (Exception ex) {
                        throw new OseeArgumentException("Exception processing Double for OwAttribute %s Exception %s",
                           owAttribute, ex);
                     }
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isIntegerType(attrType)) {
                     try {
                        Integer currVal = getInteger(currValue);
                        Integer newVal = getInteger(newValue);
                        if (currVal == null || !currVal.equals(newVal)) {
                           logChange(artifact, attrType, currValue, newValue);
                           getTransaction().setSoleAttributeValue(artifact, attrType, newVal);
                        }
                     } catch (Exception ex) {
                        throw new OseeArgumentException("Exception processing Integer for OwAttribute %s Exception %s",
                           owAttribute, ex);
                     }
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isDateType(attrType)) {
                     try {
                        Date currVal = artifact.getSoleAttributeValue(attrType, null);
                        Date newVal = getDate(newValue);
                        if (currVal == null || currVal.compareTo(newVal) != 0) {
                           logChange(artifact, attrType, DateUtil.getMMDDYYHHMM(currVal),
                              DateUtil.getMMDDYYHHMM(newVal));
                           TransactionBuilder tx = getTransaction();
                           tx.setSoleAttributeValue(artifact, attrType, newVal);
                        }
                     } catch (Exception ex) {
                        throw new OseeArgumentException("Exception processing Integer for OwAttribute %s Exception %s",
                           owAttribute, ex);
                     }
                  } else if (currValue == null && newValue != null || currValue != null && !currValue.equals(
                     newValue)) {
                     logChange(artifact, attrType, currValue, newValue);
                     getTransaction().setSoleAttributeValue(artifact, attrType, newValue);
                  }
               } else if (owAttribute.getValues().size() > 1 && orcsApi.getOrcsTypes().getAttributeTypes().getMaxOccurrences(
                  attrType) > 1) {
                  if (orcsApi.getOrcsTypes().getAttributeTypes().isDateType(attrType)) {
                     throw new OseeArgumentException(
                        "Date attributes not supported for multi-value set for OwAttribute %s Exception %s",
                        owAttribute);
                  }
                  List<String> values = new LinkedList<>();
                  for (Object obj : owAttribute.getValues()) {
                     values.add(obj.toString());
                  }
                  getTransaction().setAttributesFromStrings(artifact, attrType, values);
                  logChange(artifact, attrType, artifact.getAttributeValues(attrType).toString(), values.toString());
               }
            }
         } catch (Exception ex) {
            throw new OseeWrappedException(ex, "Exception processing attributes for [%s]", owArtifact);
         }
      }
   }

   private void logChange(ArtifactReadable artifact, IAttributeType attrType, String currValue, String newValue) {
      results.log(String.format("Attribute Updated: Current [%s], New [%s] for attr type [%s] and artifact %s",
         currValue, newValue, attrType, artifact.toStringWithId()));
   }

   private Integer getInteger(String value) {
      Integer result = null;
      if (Strings.isValid(value)) {
         result = Integer.valueOf(value);
      }
      return result;
   }

   private Double getDouble(String value) {
      Double result = null;
      if (Strings.isValid(value)) {
         result = Double.valueOf(value);
      }
      return result;
   }

   private Boolean getBoolean(String value) {
      if (Strings.isValid(value)) {
         if (value.toLowerCase().equals("true")) {
            return true;
         } else if (value.toLowerCase().equals("false")) {
            return false;
         } else if (value.equals("1")) {
            return true;
         } else if (value.equals("0")) {
            return false;
         }
      }
      return null;
   }

   protected static IAttributeType getAttributeType(OrcsApi orcsApi, OwAttributeType attributeType) {
      if (attributeType.getUuid() <= 0L) {
         for (IAttributeType type : orcsApi.getOrcsTypes().getAttributeTypes().getAll()) {
            if (type.getName().equals(attributeType.getName())) {
               return type;
            }
         }
         throw new OseeArgumentException("Invalid attribute type name [%s]", attributeType);
      }
      return orcsApi.getOrcsTypes().getAttributeTypes().getByUuid(attributeType.getUuid());
   }

   private void processCreate(XResultData results) {
      for (OwArtifact owArtifact : collector.getCreate()) {
         OwArtifactType owArtType = owArtifact.getType();
         IArtifactType artType = orcsApi.getOrcsTypes().getArtifactTypes().getByUuid(owArtType.getUuid());

         long artifactUuid = owArtifact.getUuid();
         if (artifactUuid > 0L) {
            if (uuidToArtifact == null) {
               uuidToArtifact = new HashMap<>();
            }
         } else {
            artifactUuid = Lib.generateArtifactIdAsInt();
         }
         String name = owArtifact.getName();
         ArtifactId artifact = getTransaction().createArtifact(artType, name, GUID.create(), artifactUuid);

         uuidToArtifact.put(artifactUuid, artifact);

         try {
            createAttributes(owArtifact, artifact, results);
         } catch (Exception ex) {
            throw new OseeWrappedException(ex, "Exception creating attributes for [%s]", owArtifact);
         }

         try {
            createMissingRelations(owArtifact, artifact, results);
         } catch (Exception ex) {
            throw new OseeWrappedException(ex, "Exception creating relations for [%s]", owArtifact);
         }
      }
   }

   private void createMissingRelations(OwArtifact owArtifact, ArtifactId artifact, XResultData results) {
      createMissingRelations(owArtifact.getRelations(), artifact, results);
   }

   private void createMissingRelations(List<OwRelation> relations, ArtifactId artifact, XResultData results) {
      for (OwRelation relation : relations) {
         OwRelationType owRelType = relation.getType();
         IRelationType relType = orcsApi.getOrcsTypes().getRelationTypes().getByUuid(owRelType.getUuid());

         OwArtifactToken artToken = relation.getArtToken();
         long branchUuid = collector.getBranch().getUuid();
         ArtifactReadable otherArtifact = null;

         if (uuidToArtifact.containsKey(artToken.getUuid())) {
            otherArtifact = (ArtifactReadable) uuidToArtifact.get(artToken.getUuid());
         } else {
            otherArtifact = orcsApi.getQueryFactory().fromBranch(branchUuid).andUuid(
               artToken.getUuid()).getResults().getExactlyOne();
            uuidToArtifact.put(artToken.getUuid(), otherArtifact);
         }
         if (relation.getType().isSideA()) {
            IRelationTypeSide relTypeSide = TokenFactory.createRelationTypeSide(RelationSide.SIDE_A,
               relation.getType().getUuid(), relation.getType().getName());
            if (!otherArtifact.areRelated(relTypeSide, (ArtifactReadable) artifact)) {
               getTransaction().relate(otherArtifact, relType, artifact);
            }
         } else {
            IRelationTypeSide relTypeSide = TokenFactory.createRelationTypeSide(RelationSide.SIDE_B,
               relation.getType().getUuid(), relation.getType().getName());
            if (!otherArtifact.areRelated(relTypeSide, (ArtifactReadable) artifact)) {
               getTransaction().relate(artifact, relType, otherArtifact);
            }
         }
      }
   }

   private void createAttributes(OwArtifact owArtifact, ArtifactId artifact, XResultData results) {
      for (OwAttribute owAttribute : owArtifact.getAttributes()) {
         if (!CoreAttributeTypes.Name.getGuid().equals(owAttribute.getType().getUuid())) {
            OwAttributeType owAttrType = owAttribute.getType();
            IAttributeType attrType = getAttributeType(orcsApi, owAttrType);

            List<Object> values = owAttribute.getValues();
            for (Object value : values) {
               String valueOf = String.valueOf(value);
               if (Strings.isValid(valueOf) && !valueOf.equals("null")) {
                  if (orcsApi.getOrcsTypes().getAttributeTypes().isFloatingType(attrType)) {
                     getTransaction().setSoleAttributeValue(artifact, attrType, Double.valueOf((String) value));
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isIntegerType(attrType)) {
                     getTransaction().setSoleAttributeValue(artifact, attrType, Integer.valueOf((String) value));
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isBooleanType(attrType)) {
                     Boolean set = getBoolean((String) value);
                     if (set != null) {
                        getTransaction().setSoleAttributeValue(artifact, attrType, set);
                     }
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().isDateType(attrType)) {
                     Date date = getDate(value);
                     if (date != null) {
                        getTransaction().setSoleAttributeValue(artifact, attrType, date);
                     } else {
                        throw new OseeArgumentException("Unexpected date format [%s]", value);
                     }
                  } else if (orcsApi.getOrcsTypes().getAttributeTypes().getMaxOccurrences(attrType) == 1) {
                     getTransaction().setSoleAttributeValue(artifact, attrType, value);
                  } else {
                     getTransaction().createAttribute(artifact, attrType, value);
                  }
               }
            }
         }
      }
   }

   private Date getDate(Object value) {
      Date date = null;
      boolean resolved = false;
      if (Strings.isNumeric((String) value)) {
         date = new Date(Long.valueOf((String) value));
      } else {
         try {
            date = DateUtil.getDate(DateUtil.MMDDYY, (String) value);
            resolved = true;
         } catch (Exception ex) {
            // do nothing
         }
         if (date == null) {
            try {
               date = DateUtil.getDate("MM/dd/yy", (String) value);
               resolved = true;
            } catch (Exception ex) {
               // do nothing
            }
         }
         if (date == null) {
            try {
               date = DateUtil.getDate(DateUtil.MMDDYYHHMM, (String) value);
               resolved = true;
            } catch (Exception ex) {
               // do nothing
            }
         }
         if (date == null) {
            try {
               Calendar calendar = javax.xml.bind.DatatypeConverter.parseDateTime((String) value);
               date = calendar.getTime();
               resolved = true;
            } catch (Exception ex) {
               // do nothing
            }
         }
      }
      if (Strings.isValid((String) value) && !resolved) {
         throw new OseeArgumentException("Date format [%s] not supported.", value);
      }
      return date;
   }

   private BranchId getBranch() {
      if (branch == null) {
         branch = orcsApi.getQueryFactory().branchQuery().andUuids(
            collector.getBranch().getUuid()).getResults().getAtMostOneOrNull();
      }
      return branch;
   }

   public TransactionBuilder getTransaction() throws OseeCoreException {
      if (transaction == null) {
         transaction =
            orcsApi.getTransactionFactory().createTransaction(getBranch(), getUser(), collector.getPersistComment());
      }
      return transaction;
   }

   private ArtifactReadable getUser() {
      if (user == null) {
         user = orcsApi.getQueryFactory().fromBranch(CoreBranches.COMMON).and(CoreAttributeTypes.UserId,
            collector.getAsUserId()).getResults().getExactlyOne();
      }
      return user;
   }

}

Back to the top