Skip to main content
summaryrefslogtreecommitdiffstats
blob: 498346f26e76ba1ce8c3ae7bd7a93d9cd5dcfcdc (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
/*******************************************************************************
 * 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.lang.ref.Reference;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.ds.AttributeData;
import org.eclipse.osee.orcs.core.ds.DataProxy;
import org.eclipse.osee.orcs.core.ds.HasOrcsData;
import org.eclipse.osee.orcs.core.internal.artifact.AttributeManager;
import org.eclipse.osee.orcs.data.AttributeTypes;
import org.eclipse.osee.orcs.data.AttributeWriteable;

/**
 * @author Ryan D. Brooks
 */
public abstract class Attribute<T> implements HasOrcsData<AttributeData>, Comparable<Attribute<T>>, AttributeWriteable<T> {
   private AttributeTypes attributeTypeCache;
   private Reference<AttributeManager> containerReference;
   private boolean dirty;
   private String defaultValue;
   private Log logger;
   private AttributeData attributeData;

   public void internalInitialize(AttributeTypes attributeTypeCache, Reference<AttributeManager> containerReference, AttributeData attributeData, boolean isDirty, boolean setDefaultValue) throws OseeCoreException {
      this.attributeTypeCache = attributeTypeCache;
      this.containerReference = containerReference;
      this.attributeData = attributeData;

      if (setDefaultValue) {
         setToDefaultValue();
      }
      dirty = isDirty;
      uponInitialize();
   }

   protected Log getLogger() {
      return logger;
   }

   @Override
   public AttributeData getOrcsData() {
      return attributeData;
   }

   @Override
   public void setOrcsData(AttributeData data) {
      this.attributeData = data;
   }

   /**
    * Base implementation does nothing. Subclasses may override to do setup that depends on the attribute state data.
    */
   @SuppressWarnings("unused")
   protected void uponInitialize() throws OseeCoreException {
      // provided for subclass implementation
   }

   private void markAsNewOrChanged() {
      if (isInDb()) {
         markAsChanged(ModificationType.MODIFIED);
      } else {
         markAsChanged(ModificationType.NEW);
      }
   }

   @Override
   public void setValue(T value) throws OseeCoreException {
      // TODO Artifact Checks      
      //      if (attributeType.getName().equals("Name") && !value.equals(getValue())) {
      //         // Confirm artifact is fit to rename
      //         for (IArtifactCheck check : ArtifactChecks.getArtifactChecks()) {
      //            IStatus result = check.isRenamable(Arrays.asList(getArtifact()));
      //            if (!result.isOK()) {
      //               throw new OseeCoreException(result.getMessage());
      //            }
      //         }
      //      }

      if (subClassSetValue(value)) {
         markAsNewOrChanged();
      }
   }

   @Override
   public boolean setFromString(String value) throws OseeCoreException {
      // TODO Artifact Checks 
      //      if (attributeType.equals(CoreAttributeTypes.Name) && !value.equals(getValue())) {
      //         // Confirm artifact is fit to rename
      //         for (IArtifactCheck check : ArtifactChecks.getArtifactChecks()) {
      //            IStatus result = check.isRenamable(Arrays.asList(getArtifact()));
      //            if (!result.isOK()) {
      //               throw new OseeCoreException(result.getMessage());
      //            }
      //         }
      //      }

      boolean response = subClassSetValue(convertStringToValue(value));
      if (response) {
         markAsNewOrChanged();
      }
      return response;
   }

   protected abstract T convertStringToValue(String value) throws OseeCoreException;

   @Override
   public final void resetToDefaultValue() throws OseeCoreException {
      getOrcsData().setModType(ModificationType.MODIFIED);
      setToDefaultValue();
   }

   protected void setToDefaultValue() throws OseeCoreException {
      if (defaultValue != null) {
         subClassSetValue(convertStringToValue(defaultValue));
      }
   }

   @Override
   public boolean setValueFromInputStream(InputStream value) throws OseeCoreException {
      try {
         boolean response = setFromString(Lib.inputStreamToString(value));
         if (response) {
            markAsNewOrChanged();
         }
         return response;
      } catch (IOException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return false; // unreachable since wrapAndThrow() always throws an exception
      }
   }

   /**
    * Subclasses must provide an implementation of this method and in general should not override the other set value
    * methods
    */
   protected abstract boolean subClassSetValue(T value) throws OseeCoreException;

   @Override
   public abstract T getValue() throws OseeCoreException;

   @Override
   public String getDisplayableString() throws OseeCoreException {
      return getDataProxy().getDisplayableString();
   }

   @Override
   public String toString() {
      try {
         return getDisplayableString();
      } catch (OseeCoreException ex) {
         return Lib.exceptionToString(ex);
      }
   }

   public DataProxy getDataProxy() {
      return getOrcsData().getDataProxy();
   }

   /**
    * @return <b>true</b> if this attribute is dirty
    */
   @Override
   public boolean isDirty() {
      return dirty;
   }

   protected void markAsChanged(ModificationType modificationType) {
      setDirtyFlag(true);
      getOrcsData().setModType(modificationType);
   }

   public void clearDirty() {
      setDirtyFlag(false);
   }

   private void setDirtyFlag(boolean dirty) {
      this.dirty = dirty;
      //      try {
      //         Artifact artifact = getArtifact();
      //         ArtifactCache.updateCachedArtifact(artifact.getArtId(), artifact.getBranch().getId());
      //      } catch (OseeCoreException ex) {
      //         OseeLog.log(Attribute.class, Level.SEVERE, ex);
      //      }
   }

   public AttributeManager getContainer() throws OseeStateException {
      if (containerReference.get() == null) {
         throw new OseeStateException("Attribute parent has been garbage collected");
      }
      return containerReference.get();
   }

   protected String getDefaultValueFromMetaData() throws OseeCoreException {
      return attributeTypeCache.getDefaultValue(getAttributeType());
   }

   /**
    * @return attributeType Attribute Type Information
    * @throws OseeCoreException
    */
   @Override
   public IAttributeType getAttributeType() throws OseeCoreException {
      return attributeTypeCache.getByUuid(getOrcsData().getTypeUuid());
   }

   /**
    * Currently this method provides support for quasi attribute type inheritance
    * 
    * @return whether this attribute's type or any of its super-types are the specified type
    * @throws OseeCoreException
    */
   @Override
   public boolean isOfType(IAttributeType otherAttributeType) throws OseeCoreException {
      return getAttributeType().equals(otherAttributeType);
   }

   public void resetModType() {
      getOrcsData().setModType(ModificationType.MODIFIED);
   }

   /**
    * Deletes the attribute
    */
   public final void setArtifactDeleted() {
      markAsChanged(ModificationType.ARTIFACT_DELETED);
   }

   /**
    * Deletes the attribute
    * 
    * @throws OseeStateException
    */
   @Override
   public final void delete() throws OseeCoreException {
      if (isInDb()) {
         markAsChanged(ModificationType.DELETED);
      } else {
         getContainer().remove(getAttributeType(), this);
      }
   }

   @Override
   public ModificationType getModificationType() {
      return getOrcsData().getModType();
   }

   @Override
   public boolean canDelete() {
      try {
         return getContainer().getAttributeCount(getAttributeType()) > attributeTypeCache.getMinOccurrences(getAttributeType());
      } catch (OseeCoreException ex) {
         return false;
      }
   }

   /**
    * Purges the attribute from the database.
    */
   public void purge() throws OseeCoreException {
      getDataProxy().purge();
   }

   public void markAsPurged() {
      getOrcsData().setModType(ModificationType.DELETED);
      setDirtyFlag(false);
   }

   /**
    * @return true if in data store
    */
   public boolean isInDb() {
      return getGammaId() > 0;
   }

   /**
    * @return Returns the attrId.
    */
   @Override
   public int getId() {
      return getOrcsData().getLocalId();
   }

   @Override
   public long getGammaId() {
      return getOrcsData().getVersion().getGammaId();
   }

   public void internalSetGammaId(int gammaId) {
      getOrcsData().getVersion().setGammaId(gammaId);
   }

   public void internalSetAttributeId(int attrId) {
      getOrcsData().setLocalId(attrId);
   }

   @Override
   public boolean isDeleted() {
      return getModificationType().isDeleted();
   }

   /**
    * artifact.persist(); artifact.reloadAttributesAndRelations(); Will need to be called afterwards to see replaced
    * data in memory
    * 
    * @throws OseeCoreException
    */
   public void replaceWithVersion(int gammaId) throws OseeCoreException {
      internalSetModificationType(ModificationType.REPLACED_WITH_VERSION);
      getOrcsData().getVersion().setGammaId(gammaId);
      setDirtyFlag(true);
   }

   /**
    * @param modificationType the modificationType to set
    * @throws OseeCoreException
    */
   public void internalSetModificationType(ModificationType modificationType) throws OseeCoreException {
      Conditions.checkNotNull(modificationType, "modification type");
      getOrcsData().setModType(modificationType);
   }

   public void internalSetDeletedFromRemoteEvent() throws OseeCoreException {
      internalSetModificationType(ModificationType.DELETED);
   }

   @Override
   public int compareTo(Attribute<T> other) {
      return toString().compareTo(other.toString());
   }
}

Back to the top