Skip to main content
summaryrefslogtreecommitdiffstats
blob: a4062aa72ad86fbf3a46763be42f1505d1e21cec (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
/*******************************************************************************
 * 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.ui.skynet.widgets.workflow;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * Definition of WorkItem. Once created, nothing in this class, or any subclasses, should be modified as these
 * definitions are shared by all instantiations of pages, rules, workflows and widgets.
 * 
 * @author Donald G. Dunne
 */
public abstract class WorkItemDefinition {

   protected String id;
   protected String name;
   protected String parentId;
   protected String description;
   protected Map<String, String> workDataKeyValueMap = new HashMap<String, String>();
   private final Pattern keyValuePattern = Pattern.compile("^(.*?)=(.*)$", Pattern.MULTILINE | Pattern.DOTALL);
   protected String type;
   public static enum WriteType {
      Update,
      New
   };

   public Map<String, String> getWorkDataKeyValueMap() {
      return workDataKeyValueMap;
   }

   public void setWorkDataKeyValueMap(Map<String, String> workDataKeyValueMap) {
      this.workDataKeyValueMap = workDataKeyValueMap;
   }

   public void setDescription(String description) {
      this.description = description;
   }

   public String getDescription() {
      return description;
   }

   public WorkItemDefinition(String name, String id, String parentId) {
      this(name, id, parentId, null);
   }

   public WorkItemDefinition(String name, String id, String parentId, String type) {
      this(name, id, parentId, type, null);
   }

   private WorkItemDefinition(String name, String id, String parentId, String type, String description) {
      this.name = name;
      this.id = id;
      this.type = type;
      this.parentId = parentId;
      this.description = description;
      if (parentId != null && parentId.equals("")) {
         throw new IllegalArgumentException(
            "parentId must either be null or a valid parent Id.  Invalid for WorkItemDefinition " + id);
      }
      if (type != null && type.equals("")) {
         throw new IllegalArgumentException(
            "type must either be null or a value, not empty string.  Invalid for WorkItemDefinition " + id);
      }
      if (!Strings.isValid(id)) {
         throw new IllegalArgumentException("id must be unique and non-null");
      }

   }

   /**
    * Determine if this workItemDefinition is or has a parent definition of pageId. This will walk up the tree of
    * definition inheritance to answer the question
    */
   public boolean isInstanceOfPage(String pageId, String... visitedPageIds) throws OseeCoreException {
      // Collect all ids already visited
      Set<String> visitedIds = new HashSet<String>();
      for (String visitedId : visitedPageIds) {
         visitedIds.add(visitedId);
      }

      // Check for circular dependency
      if (visitedIds.contains(getId())) {
         throw new IllegalStateException("Circular dependency detected.  Id already visited: " + getId());
      }

      // Check for instanceof 
      if (getId().equals(pageId)) {
         return true;
      }

      // If parentId exists, check if it isInstanceOfPage
      if (getParentId() != null) {
         visitedIds.add(getId());
         WorkItemDefinition workItemDefinition = WorkItemDefinitionFactory.getWorkItemDefinition(getParentId());
         return workItemDefinition.isInstanceOfPage(pageId, visitedIds.toArray(new String[visitedIds.size()]));
      }
      return false;
   }

   public boolean hasParent() {
      return getParentId() != null;
   }

   public WorkItemDefinition getParent() throws OseeCoreException {
      if (!hasParent()) {
         return null;
      }
      return WorkItemDefinitionFactory.getWorkItemDefinition(getParentId());
   }

   @Override
   public String toString() {
      return getArtifactType() + ":    Name: \"" + name +
      //
      "\"    Id: \"" + id + "\"   " +
      //
      (parentId != null ? "   Parent: " + parentId : "");
   }

   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public String getType() {
      return type;
   }

   public String getParentId() {
      return parentId;
   }

   public Artifact toArtifact(WriteType writeType) throws OseeCoreException {
      Artifact artifact = WorkItemDefinitionFactory.getWorkItemDefinitionArtifact(getId());
      if (writeType == WriteType.New) {
         // Double-check that doesn't already exist in db.  If so, exception cause duplicates
         if (ArtifactQuery.getArtifactListFromAttribute(CoreAttributeTypes.WorkId, getId(),
            BranchManager.getCommonBranch()).size() > 0) {
            throw new IllegalStateException(
               "WorkItemDefinition artifact creation failed.  \"" + getId() + "\" already exists.");
         }
      }
      if (artifact == null) {
         // Create new
         artifact = ArtifactTypeManager.addArtifact(getArtifactType(), BranchManager.getCommonBranch());
      }
      //      if (!getId().equals("atsStatePercentCompleteWeight.DefaultWorkflow")) {
      //         System.err.println("Skipping all but atsStatePercentCompleteWeight.DefaultWorkflow - Remove This");
      //         return artifact;
      //      }
      artifact.setName(getName());
      if (Strings.isValid(getParentId())) {
         artifact.setSoleAttributeValue(CoreAttributeTypes.WorkParentId, getParentId());
      }
      if (getDescription() != null) {
         artifact.setSoleAttributeValue(CoreAttributeTypes.WorkDescription, getDescription());
      }
      artifact.setSoleAttributeValue(CoreAttributeTypes.WorkId, getId());
      if (getType() != null) {
         artifact.setSoleAttributeValue(CoreAttributeTypes.WorkType, getType());
      }
      if (workDataKeyValueMap.size() > 0) {
         Set<String> keyValues = new HashSet<String>();
         for (Entry<String, String> entry : workDataKeyValueMap.entrySet()) {
            keyValues.add(entry.getKey() + "=" + entry.getValue());
         }
         artifact.setAttributeValues(CoreAttributeTypes.WorkData, keyValues);
      }
      WorkItemDefinitionFactory.cacheWorkItemDefinitionArtifact(writeType, this, artifact);
      return artifact;
   }

   public abstract IArtifactType getArtifactType();

   public void setType(String type) {
      this.type = type;
   }

   public void loadWorkDataKeyValueMap(Artifact artifact) throws OseeCoreException {
      for (String value : artifact.getAttributesToStringList(CoreAttributeTypes.WorkData)) {
         Matcher m = keyValuePattern.matcher(value);
         if (m.find()) {
            addWorkDataKeyValue(m.group(1), m.group(2));
         } else {
            throw new OseeStateException("Illegal value for WorkData; must be key=value");
         }
      }
   }

   public String getWorkDataValue(String key) {
      return workDataKeyValueMap.get(key);
   }

   public void addWorkDataKeyValue(String key, String value) {
      workDataKeyValueMap.put(key, value);
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setParentId(String parentId) {
      this.parentId = parentId;
   }

}

Back to the top