Skip to main content
summaryrefslogtreecommitdiffstats
blob: 82e9d0b1fbac87cb0309782da1946e39326cfba2 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*******************************************************************************
 * 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.core.internal.attribute;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.ResultSet;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.exception.AttributeDoesNotExist;
import org.eclipse.osee.framework.core.exception.MultipleAttributesExist;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.type.BaseIdentity;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.orcs.core.ds.ArtifactData;
import org.eclipse.osee.orcs.core.ds.HasOrcsData;
import org.eclipse.osee.orcs.core.internal.util.MultiplicityState;
import org.eclipse.osee.orcs.core.internal.util.OrcsPredicates;
import org.eclipse.osee.orcs.data.AttributeId;
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;

/**
 * @author Roberto E. Escobar
 */
public abstract class AttributeManagerImpl extends BaseIdentity<String> implements HasOrcsData<ArtifactData>, AttributeManager, AttributeExceptionFactory {

   private final AttributeCollection attributes;
   private boolean isLoaded;

   private final AttributeFactory attributeFactory;

   protected AttributeManagerImpl(String guid, AttributeFactory attributeFactory) {
      super(guid);
      this.attributeFactory = attributeFactory;
      this.attributes = new AttributeCollection(this);
   }

   protected Collection<Attribute<?>> getAllAttributes() {
      return attributes.getAll();
   }

   @Override
   public synchronized void add(IAttributeType type, Attribute<? extends Object> attribute) {
      attributes.add(type, attribute);
      attribute.getOrcsData().setArtifactId(getLocalId());
   }

   @Override
   public synchronized void remove(IAttributeType type, Attribute<? extends Object> attribute) {
      attributes.remove(type, attribute);
      attribute.getOrcsData().setArtifactId(-1);
   }

   @Override
   public boolean isLoaded() {
      return isLoaded;
   }

   @Override
   public void setLoaded(boolean value) throws OseeCoreException {
      this.isLoaded = value;
      if (value == true) {
         onLoaded();
      }
   }

   @Override
   public void setAttributesNotDirty() {
      for (Attribute<?> attribute : getAllAttributes()) {
         attribute.clearDirty();
      }
   }

   @Override
   public boolean areAttributesDirty() {
      return attributes.hasDirty();
   }

   @Override
   public String getName() {
      String name;
      try {
         name = getSoleAttributeAsString(CoreAttributeTypes.Name);
      } catch (Exception ex) {
         name = Lib.exceptionToString(ex);
      }
      return name;
   }

   @Override
   public int getMaximumAttributeTypeAllowed(IAttributeType attributeType) throws OseeCoreException {
      int result = -1;
      if (isAttributeTypeValid(attributeType)) {
         result = attributeFactory.getMaxOccurrenceLimit(attributeType);
      }
      return result;
   }

   @Override
   public int getMinimumAttributeTypeAllowed(IAttributeType attributeType) throws OseeCoreException {
      int result = -1;
      if (isAttributeTypeValid(attributeType)) {
         result = attributeFactory.getMinOccurrenceLimit(attributeType);
      }
      return result;
   }

   @Override
   public Collection<? extends IAttributeType> getExistingAttributeTypes() throws OseeCoreException {
      ensureAttributesLoaded();
      return attributes.getExistingTypes(DeletionFlag.EXCLUDE_DELETED);
   }

   @Override
   public int getAttributeCount(IAttributeType attributeType) throws OseeCoreException {
      return getAttributesExcludeDeleted(attributeType).size();
   }

   @Override
   public Attribute<Object> getAttributeById(AttributeId attributeId) throws OseeCoreException {
      Attribute<Object> attribute = null;
      Optional<Attribute<Object>> tryFind = Iterables.tryFind(getAttributes(), OrcsPredicates.attributeId(attributeId));
      if (tryFind.isPresent()) {
         attribute = tryFind.get();
      } else {
         throw new AttributeDoesNotExist("Attribute[%s] does not exist for %s", attributeId, getExceptionString());
      }
      return attribute;
   }

   @Override
   public List<Attribute<Object>> getAttributes() throws OseeCoreException {
      return getAttributesExcludeDeleted();
   }

   @Override
   public <T> List<Attribute<T>> getAttributes(IAttributeType attributeType) throws OseeCoreException {
      return getAttributesExcludeDeleted(attributeType);
   }

   @Override
   public <T> List<T> getAttributeValues(IAttributeType attributeType) throws OseeCoreException {
      List<Attribute<T>> attributes = getAttributesExcludeDeleted(attributeType);

      List<T> values = new LinkedList<T>();
      for (Attribute<T> attribute : attributes) {
         T value = attribute.getValue();
         if (value != null) {
            values.add(value);
         }
      }
      return values;
   }

   @Override
   public int getAttributeCount(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException {
      return getAttributesHelper(attributeType, includeDeleted).size();
   }

   @Override
   public List<Attribute<Object>> getAttributes(DeletionFlag includeDeleted) throws OseeCoreException {
      return getAttributesHelper(includeDeleted);
   }

   @Override
   public <T> List<Attribute<T>> getAttributes(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException {
      return getAttributesHelper(attributeType, includeDeleted);
   }

   @Override
   public String getSoleAttributeAsString(IAttributeType attributeType, String defaultValue) throws OseeCoreException {
      String toReturn = defaultValue;
      List<Attribute<Object>> items = getAttributesExcludeDeleted(attributeType);
      if (!items.isEmpty()) {
         Attribute<Object> firstItem = items.iterator().next();
         toReturn = String.valueOf(firstItem.getValue());
      }
      return toReturn;
   }

   @Override
   public String getSoleAttributeAsString(IAttributeType attributeType) throws OseeCoreException {
      String toReturn = null;
      Object value = getSoleAttributeValue(attributeType);
      if (value instanceof InputStream) {
         InputStream inputStream = (InputStream) value;
         try {
            toReturn = Lib.inputStreamToString(inputStream);
         } catch (IOException ex) {
            OseeExceptions.wrapAndThrow(ex);
         } finally {
            try {
               inputStream.close();
            } catch (IOException ex) {
               OseeExceptions.wrapAndThrow(ex);
            }
         }
      } else {
         if (value != null) {
            toReturn = value.toString();
         }
      }
      return toReturn;
   }

   @Override
   public <T> T getSoleAttributeValue(IAttributeType attributeType) throws OseeCoreException {
      Attribute<T> attribute = getSoleAttribute(attributeType);
      return attribute.getValue();
   }

   @Override
   public <T> T getSoleAttributeValue(IAttributeType attributeType, T defaultValue) throws OseeCoreException {
      T value = defaultValue;
      Attribute<T> attribute = null;
      try {
         attribute = getSoleAttribute(attributeType);
      } catch (AttributeDoesNotExist ex) {
         // do nothing
      }
      if (attribute != null) {
         value = attribute.getValue();
      }
      return value;
   }

   @Override
   public <T> void setSoleAttributeValue(IAttributeType attributeType, T value) throws OseeCoreException {
      Attribute<T> attribute = getOrCreateSoleAttribute(attributeType);
      attribute.setValue(value);
   }

   @Override
   public void setSoleAttributeFromString(IAttributeType attributeType, String value) throws OseeCoreException {
      getOrCreateSoleAttribute(attributeType).setFromString(value);
   }

   @Override
   public void setSoleAttributeFromStream(IAttributeType attributeType, InputStream inputStream) throws OseeCoreException {
      getOrCreateSoleAttribute(attributeType).setValueFromInputStream(inputStream);
   }

   @Override
   public void setAttributesFromStrings(IAttributeType attributeType, String... values) throws OseeCoreException {
      setAttributesFromStrings(attributeType, Arrays.asList(values));
   }

   @Override
   public void setAttributesFromStrings(IAttributeType attributeType, Collection<String> values) throws OseeCoreException {
      AttributeSetHelper<Object, String> attributeStringSetter = new FromStringAttributeSetHelper(attributes, this);
      setAttributesFromValuesHelper(attributeStringSetter, attributeType, values);
   }

   @Override
   public <T> void setAttributesFromValues(IAttributeType attributeType, T... values) throws OseeCoreException {
      setAttributesFromValues(attributeType, Arrays.asList(values));
   }

   @Override
   public <T> void setAttributesFromValues(IAttributeType attributeType, Collection<T> values) throws OseeCoreException {
      AttributeSetHelper<T, T> setter = new TypedValueAttributeSetHelper<T>(attributes, this);
      setAttributesFromValuesHelper(setter, attributeType, values);
   }

   @Override
   public void deleteAttributesByArtifact() throws OseeCoreException {
      for (Attribute<?> attribute : getAttributesIncludeDeleted()) {
         attribute.setArtifactDeleted();
      }
   }

   @Override
   public void unDeleteAttributesByArtifact() throws OseeCoreException {
      for (Attribute<?> attribute : getAttributesIncludeDeleted()) {
         if (ModificationType.ARTIFACT_DELETED == attribute.getModificationType()) {
            attribute.unDelete();
         }
      }
   }

   @Override
   public void deleteSoleAttribute(IAttributeType attributeType) throws OseeCoreException {
      Attribute<?> attribute = getSoleAttribute(attributeType);
      if (attribute != null) {
         deleteAttribute(attribute);
      }
   }

   @Override
   public void deleteAttributes(IAttributeType attributeType) throws OseeCoreException {
      for (Attribute<?> attribute : getAttributesIncludeDeleted(attributeType)) {
         attribute.delete();
      }
   }

   @Override
   public void deleteAttributesWithValue(IAttributeType attributeType, Object value) throws OseeCoreException {
      for (Attribute<Object> attribute : getAttributesIncludeDeleted(attributeType)) {
         if (attribute.getValue().equals(value)) {
            deleteAttribute(attribute);
            break;
         }
      }
   }

   private void deleteAttribute(Attribute<?> attribute) throws OseeCoreException {
      IAttributeType attributeType = attribute.getAttributeType();
      checkMultiplicityCanDelete(attributeType);
      attribute.delete();
   }

   @Override
   public <T> Attribute<T> createAttribute(IAttributeType attributeType) throws OseeCoreException {
      return internalCreateAttributeHelper(attributeType);
   }

   @Override
   public <T> Attribute<T> createAttribute(IAttributeType attributeType, T value) throws OseeCoreException {
      Attribute<T> attribute = internalCreateAttributeHelper(attributeType);
      attribute.setValue(value);
      return attribute;
   }

   @Override
   public <T> Attribute<T> createAttributeFromString(IAttributeType attributeType, String value) throws OseeCoreException {
      Attribute<T> attribute = internalCreateAttributeHelper(attributeType);
      attribute.setFromString(value);
      return attribute;
   }

   //////////////////////////////////////////////////////////////
   private <T> Attribute<T> internalCreateAttributeHelper(IAttributeType attributeType) throws OseeCoreException {
      checkTypeValid(attributeType);
      checkMultiplicityCanAdd(attributeType);
      Attribute<T> attr = attributeFactory.createAttributeWithDefaults(this, getOrcsData(), attributeType);
      add(attributeType, attr);
      return attr;
   }

   private <T> Attribute<T> getOrCreateSoleAttribute(IAttributeType attributeType) throws OseeCoreException {
      ResultSet<Attribute<T>> result = attributes.getResultSet(attributeType, DeletionFlag.EXCLUDE_DELETED);
      Attribute<T> attribute = result.getAtMostOneOrNull();
      if (attribute == null) {
         attribute = internalCreateAttributeHelper(attributeType);
      }
      return attribute;
   }

   private <T> Attribute<T> getSoleAttribute(IAttributeType attributeType) throws OseeCoreException {
      ensureAttributesLoaded();
      ResultSet<Attribute<T>> result = attributes.getResultSet(attributeType, DeletionFlag.EXCLUDE_DELETED);
      return result.getExactlyOne();
   }

   //////////////////////////////////////////////////////////////

   private List<Attribute<Object>> getAttributesExcludeDeleted() throws OseeCoreException {
      return getAttributesHelper(DeletionFlag.EXCLUDE_DELETED);
   }

   private List<Attribute<Object>> getAttributesIncludeDeleted() throws OseeCoreException {
      return getAttributesHelper(DeletionFlag.INCLUDE_DELETED);
   }

   private <T> List<Attribute<T>> getAttributesExcludeDeleted(IAttributeType attributeType) throws OseeCoreException {
      return getAttributesHelper(attributeType, DeletionFlag.EXCLUDE_DELETED);
   }

   private <T> List<Attribute<T>> getAttributesIncludeDeleted(IAttributeType attributeType) throws OseeCoreException {
      return getAttributesHelper(attributeType, DeletionFlag.INCLUDE_DELETED);
   }

   private List<Attribute<Object>> getAttributesHelper(DeletionFlag includeDeleted) throws OseeCoreException {
      ensureAttributesLoaded();
      return Collections.castAll(attributes.getList(includeDeleted));
   }

   private <T> List<Attribute<T>> getAttributesHelper(IAttributeType attributeType, DeletionFlag includeDeleted) throws OseeCoreException {
      ensureAttributesLoaded();
      return attributes.getList(attributeType, includeDeleted);
   }

   //////////////////////////////////////////////////////////////

   private <A, T> void setAttributesFromValuesHelper(AttributeSetHelper<A, T> helper, IAttributeType attributeType, Collection<T> values) throws OseeCoreException {
      ensureAttributesLoaded();

      Set<T> uniqueItems = new LinkedHashSet<T>(values);
      List<Attribute<A>> remainingAttributes = getAttributesExcludeDeleted(attributeType);
      List<T> remainingNewValues = new ArrayList<T>(uniqueItems.size());

      // all existing attributes matching a new value will be left untouched
      for (T newValue : uniqueItems) {
         boolean found = false;
         for (Attribute<A> attribute : remainingAttributes) {
            if (helper.matches(attribute, newValue)) {
               remainingAttributes.remove(attribute);
               found = true;
               break;
            }
         }

         if (!found) {
            remainingNewValues.add(newValue);
         }
      }

      for (T newValue : remainingNewValues) {
         if (remainingAttributes.isEmpty()) {
            helper.createAttribute(attributeType, newValue);
         } else {
            int index = remainingAttributes.size() - 1;
            Attribute<A> attribute = remainingAttributes.get(index);
            helper.setAttributeValue(attribute, newValue);
            remainingAttributes.remove(index);
         }
      }

      for (Attribute<A> attribute : remainingAttributes) {
         attribute.delete();
      }
   }

   //////////////////////////////////////////////////////////////

   private void checkTypeValid(IAttributeType attributeType) throws OseeCoreException {
      if (!CoreAttributeTypes.Name.equals(attributeType)) {
         Conditions.checkExpressionFailOnTrue(!isAttributeTypeValid(attributeType),
            "The attribute type [%s] is not valid for artifacts [%s]", attributeType, getExceptionString());
      }
   }

   private void checkMultiplicityCanAdd(IAttributeType attributeType) throws OseeCoreException {
      checkMultiplicity(attributeType, getAttributeCount(attributeType) + 1);
   }

   private void checkMultiplicityCanDelete(IAttributeType attributeType) throws OseeCoreException {
      checkMultiplicity(attributeType, getAttributeCount(attributeType) - 1);
   }

   private void checkMultiplicity(IAttributeType attributeType, int count) throws OseeCoreException {
      MultiplicityState state = getAttributeMuliplicityState(attributeType, count);
      switch (state) {
         case MAX_VIOLATION:
            throw new OseeStateException("Attribute type [%s] exceeds max occurrence rule on [%s]", attributeType,
               getExceptionString());
         case MIN_VIOLATION:
            throw new OseeStateException("Attribute type [%s] is less than min occurrence rule on [%s]", attributeType,
               getExceptionString());
         default:
            break;
      }
   }

   private MultiplicityState getAttributeMuliplicityState(IAttributeType attributeType, int count) throws OseeCoreException {
      MultiplicityState state = MultiplicityState.IS_VALID;
      if (count > attributeFactory.getMaxOccurrenceLimit(attributeType)) {
         state = MultiplicityState.MAX_VIOLATION;
      } else if (count < attributeFactory.getMinOccurrenceLimit(attributeType)) {
         state = MultiplicityState.MIN_VIOLATION;
      }
      return state;
   }

   //////////////////////////////////////////////////////////////

   private void onLoaded() throws OseeCoreException {
      //      computeLastDateModified();
      meetMinimumAttributes();
   }

   private void ensureAttributesLoaded() {
      //      if (!isLoaded() && isInDb()) {
      //         ArtifactLoader.loadArtifactData(this, LoadLevel.ATTRIBUTE);
      //      }
   }

   private void meetMinimumAttributes() throws OseeCoreException {
      for (IAttributeType attributeType : getValidAttributeTypes()) {
         int missingCount = getRemainingAttributeCount(attributeType);
         for (int i = 0; i < missingCount; i++) {
            Attribute<Object> attr = attributeFactory.createAttributeWithDefaults(this, getOrcsData(), attributeType);
            add(attributeType, attr);
            attr.clearDirty();
         }
      }
   }

   private final int getRemainingAttributeCount(IAttributeType attributeType) throws OseeCoreException {
      int minLimit = attributeFactory.getMinOccurrenceLimit(attributeType);
      return minLimit - getAttributeCount(attributeType);
   }

   //////////////////////////////////////////////////////////////

   @Override
   public MultipleAttributesExist createManyExistException(IAttributeType type, int count) {
      MultipleAttributesExist toReturn;
      if (type != null) {
         toReturn =
            new MultipleAttributesExist(
               "The attribute type [%s] has [%s] instances on [%s], but only [1] instance is allowed", type.getName(),
               count, getExceptionString());
      } else {
         toReturn =
            new MultipleAttributesExist(
               "Multiple items found - total instances [%s] on [%s], but only [1] instance is allowed", count,
               getExceptionString());
      }
      return toReturn;
   }

   @Override
   public AttributeDoesNotExist createDoesNotExistException(IAttributeType type) {
      AttributeDoesNotExist toReturn;
      if (type != null) {
         toReturn =
            new AttributeDoesNotExist("Attribute of type [%s] could not be found on [%s]", type.getName(),
               getExceptionString());
      } else {
         toReturn = new AttributeDoesNotExist("Attribute not be found on [%s]", getExceptionString());
      }
      return toReturn;
   }
}

Back to the top