Skip to main content
summaryrefslogtreecommitdiffstats
blob: f86e53b730b8414fa95d3b2af285b16b5c4111e1 (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
/*******************************************************************************
 * Copyright (c) 2013 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.disposition.rest.internal;

import static org.eclipse.osee.disposition.model.DispoStrings.Dispo_Config_Art;
import static org.eclipse.osee.disposition.rest.DispoConstants.DispoTypesArtifact;
import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.OseeTypeDefinition;
import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.UriGeneralStringData;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.disposition.model.DispoItem;
import org.eclipse.osee.disposition.model.DispoProgram;
import org.eclipse.osee.disposition.model.DispoSet;
import org.eclipse.osee.disposition.rest.DispoConstants;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.jdk.core.type.Identifiable;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.type.ResultSets;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.ApplicationContext;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactId;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;
import org.eclipse.osee.orcs.transaction.TransactionFactory;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @author Angel Avila
 */
public class OrcsStorageImpl implements Storage {

   private final Log logger;
   private final OrcsApi orcsApi;

   public OrcsStorageImpl(Log logger, OrcsApi orcsApi) {
      this.logger = logger;
      this.orcsApi = orcsApi;
   }

   private IOseeBranch getAdminBranch() {
      return CoreBranches.COMMON;
   }

   @SuppressWarnings("unchecked")
   private ArtifactReadable getDispoUser() throws OseeCoreException {
      return getQuery().fromBranch(getAdminBranch()).andIds(SystemUser.OseeSystem).getResults().getExactlyOne();
   }

   private ApplicationContext getContext() {
      return null;
   }

   private QueryFactory getQuery() {
      return orcsApi.getQueryFactory(getContext());
   }

   private TransactionFactory getTxFactory() {
      return orcsApi.getTransactionFactory(getContext());
   }

   private void reloadTypes() {
      orcsApi.getOrcsTypes(getContext()).invalidateAll();
   }

   @SuppressWarnings("unchecked")
   private ResultSet<ArtifactReadable> getDispoTypesArtifact() throws OseeCoreException {
      return getQuery().fromBranch(getAdminBranch()).andIds(DispoConstants.DispoTypesArtifact).getResults();
   }

   @Override
   public boolean typesExist() {
      boolean result = false;
      try {
         result = !getDispoTypesArtifact().isEmpty();
      } catch (OseeCoreException ex) {
         logger.warn(ex, "Error checking for Dispo Types");
      }
      return result;
   }

   @Override
   public void storeTypes(IResource resource) {
      TransactionBuilder tx =
         getTxFactory().createTransaction(getAdminBranch(), getDispoUser(), "Initialize Dispo Types");
      ArtifactId artifactId = DispoTypesArtifact;
      if (!typesExist()) {
         tx.createArtifact(OseeTypeDefinition, artifactId.getName(), artifactId.getGuid());
      }
      InputStream stream = null;
      try {
         stream = resource.getContent();
         tx.setSoleAttributeFromStream(artifactId, UriGeneralStringData, stream);
      } finally {
         Lib.close(stream);
      }
      tx.commit();
      reloadTypes();
   }

   @SuppressWarnings("unchecked")
   @Override
   public ArtifactReadable findUser() {
      return getQuery().fromBranch(getAdminBranch()).andIds(SystemUser.OseeSystem).getResults().getExactlyOne();
   }

   @Override
   public ArtifactReadable findUser(String userId) {
      return getQuery().fromBranch(getAdminBranch()).andGuid(userId).getResults().getExactlyOne();
   }

   @SuppressWarnings("unchecked")
   @Override
   public ArtifactReadable findUnassignedUser() {
      //      return getQuery().fromBranch(getAdminBranch()).andNameEquals("Unassigned").getResults().getExactlyOne(); COMMENTED OUT TEMP
      return getQuery().fromBranch(getAdminBranch()).andIds(SystemUser.OseeSystem).getResults().getExactlyOne();

   }

   @Override
   public boolean isUniqueSetName(DispoProgram program, String name) {
      IOseeBranch branch = getProgramBranch(program);
      ResultSet<ArtifactReadable> results = getQuery()//
      .fromBranch(branch)//
      .andTypeEquals(DispoConstants.DispoSet)//
      .andNameEquals(name)//
      .getResults();

      return results.isEmpty();
   }

   private IOseeBranch getProgramBranch(DispoProgram program) {
      return TokenFactory.createBranch(program.getUuid(), program.getName());
   }

   @Override
   public boolean isUniqueItemName(DispoProgram program, String setId, String name) {
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable setArt = findDispoArtifact(branch, setId, DispoConstants.DispoSet);
      ResultSet<ArtifactReadable> results = getQuery()//
      .fromBranch(branch)//
      .andRelatedTo(CoreRelationTypes.Default_Hierarchical__Parent, setArt)//
      .andTypeEquals(DispoConstants.DispoItem)//
      .andNameEquals(name)//
      .getResults();

      return results.isEmpty();
   }

   @Override
   public ResultSet<DispoSet> findDispoSets(DispoProgram program) {
      IOseeBranch branch = getProgramBranch(program);
      ResultSet<ArtifactReadable> results = getQuery()//
      .fromBranch(branch)//
      .andTypeEquals(DispoConstants.DispoSet)//
      .getResults();

      List<DispoSet> toReturn = new ArrayList<DispoSet>();
      for (ArtifactReadable art : results) {
         toReturn.add(new DispoSetArtifact(art));
      }
      return ResultSets.newResultSet(toReturn);
   }

   @Override
   public DispoSet findDispoSetsById(DispoProgram program, String setId) {
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable result = findDispoArtifact(branch, setId, DispoConstants.DispoSet);
      return new DispoSetArtifact(result);
   }

   private ArtifactReadable findDispoArtifact(IOseeBranch branch, String setId, IArtifactType type) {
      return getQuery()//
      .fromBranch(branch)//
      .andTypeEquals(type)//
      .andGuid(setId)//
      .getResults().getOneOrNull();
   }

   @Override
   public ResultSet<DispoItem> findDipoItems(DispoProgram program, String setId) {
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable setArt = findDispoArtifact(branch, setId, DispoConstants.DispoSet);
      ResultSet<ArtifactReadable> results = setArt.getRelated(CoreRelationTypes.Default_Hierarchical__Child);

      List<DispoItem> toReturn = new ArrayList<DispoItem>();
      for (ArtifactReadable art : results) {
         toReturn.add(new DispoItemArtifact(art));
      }
      return ResultSets.newResultSet(toReturn);
   }

   @Override
   public DispoItem findDispoItemById(DispoProgram program, String itemId) {
      DispoItem toReturn = null;
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable dispoArtifact = findDispoArtifact(branch, itemId, DispoConstants.DispoItem);
      if (dispoArtifact != null) {
         toReturn = new DispoItemArtifact(dispoArtifact);
      }
      return toReturn;
   }

   @Override
   public Identifiable<String> createDispoSet(ArtifactReadable author, DispoProgram program, DispoSet descriptor) {
      IOseeBranch branch = getProgramBranch(program);
      TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Create Dispo Set");
      ArtifactId creatdArtId = tx.createArtifact(DispoConstants.DispoSet, descriptor.getName());
      tx.setSoleAttributeFromString(creatdArtId, DispoConstants.ImportPath, descriptor.getImportPath());
      tx.setSoleAttributeFromString(creatdArtId, DispoConstants.ImportState, descriptor.getImportState());
      tx.setSoleAttributeFromString(creatdArtId, DispoConstants.StatusCount, descriptor.getStatusCount());
      tx.setSoleAttributeFromString(creatdArtId, DispoConstants.DispoNotesJson, descriptor.getNotesList().toString());
      tx.commit();
      return creatdArtId;
   }

   @Override
   public boolean deleteDispoItem(ArtifactReadable author, DispoProgram program, String itemId) {
      return deleteDispoEntityArtifact(author, program, itemId, DispoConstants.DispoItem);
   }

   @Override
   public boolean deleteDispoSet(ArtifactReadable author, DispoProgram program, String setId) {
      return deleteDispoEntityArtifact(author, program, setId, DispoConstants.DispoSet);
   }

   private boolean deleteDispoEntityArtifact(ArtifactReadable author, DispoProgram program, String entityId, IArtifactType type) {
      boolean toReturn = false;
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable dispoArtifact = findDispoArtifact(branch, entityId, type);
      if (dispoArtifact != null) {
         TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Delete Dispo Artifact");
         tx.deleteArtifact(dispoArtifact);
         tx.commit();
         toReturn = true;
      }

      return toReturn;
   }

   @Override
   public void updateDispoSet(ArtifactReadable author, DispoProgram program, String setId, DispoSet newData) {
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable dispoSet = findDispoArtifact(branch, setId, DispoConstants.DispoSet);

      String name = newData.getName();
      String importPath = newData.getImportPath();
      String importState = newData.getImportState();
      //      DispoOperationsEnum operationRequest = dispositionSet.getOperation(); //  Operation classes still not created
      JSONArray notesList = newData.getNotesList();

      TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Delete Dispo Set");
      if (name != null) {
         tx.setName(dispoSet, name);
      }
      if (importPath != null) {
         tx.setSoleAttributeFromString(dispoSet, DispoConstants.ImportPath, importPath);
      }
      if (importState != null) {
         tx.setSoleAttributeFromString(dispoSet, DispoConstants.ImportState, importState);
      }
      if (notesList != null) {
         tx.setSoleAttributeFromString(dispoSet, DispoConstants.DispoNotesJson, notesList.toString());
      }
      tx.commit();
   }

   @Override
   public Identifiable<String> createDispoItem(ArtifactReadable author, DispoProgram program, DispoSet parentSet, DispoItem data, ArtifactReadable assignee) {
      IOseeBranch branch = getProgramBranch(program);
      ArtifactReadable parentSetArt = findDispoArtifact(branch, parentSet.getGuid(), DispoConstants.DispoSet);
      TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Create Dispoable Item");
      ArtifactId createdItem = tx.createArtifact(DispoConstants.DispoItem, data.getName());

      tx.setSoleAttributeValue(createdItem, DispoConstants.DispoDateCreated, data.getCreationDate());
      tx.setSoleAttributeValue(createdItem, DispoConstants.DispoLastUpdated, data.getLastUpdate());

      tx.setSoleAttributeValue(createdItem, DispoConstants.DispoItemStatus, data.getStatus());
      tx.setSoleAttributeFromString(createdItem, DispoConstants.DispoDiscrepanciesJson,
         data.getDiscrepanciesList().toString());
      tx.setSoleAttributeFromString(createdItem, DispoConstants.DispoAnnotationsJson,
         data.getAnnotationsList().toString());

      tx.relate(parentSetArt, CoreRelationTypes.Default_Hierarchical__Child, createdItem);
      //      tx.relate(createdItem, DispoConstants.DispoAssigned_Assignee, assignee); // UNCOMMENT ONCE on production DB
      tx.commit();
      return createdItem;
   }

   @Override
   public void createAnnotation(ArtifactReadable author, DispoProgram program, ArtifactId disposition, String annotationsJson) {
      IOseeBranch branch = getProgramBranch(program);
      TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Create Dispo Annotation");

      tx.setSoleAttributeFromString(disposition, DispoConstants.DispoAnnotationsJson, annotationsJson);
      tx.commit();
   }

   @Override
   public void updateDispoItem(ArtifactReadable author, DispoProgram program, String dispoItemId, DispoItem data) {
      boolean wasEdited = false;
      IOseeBranch branch = getProgramBranch(program);
      ArtifactId dispoItemArt = findDispoArtifact(branch, dispoItemId, DispoConstants.DispoItem);
      String assigneeId = data.getAssignee();

      TransactionBuilder tx = getTxFactory().createTransaction(branch, author, "Edit Dispoable Item");
      String name = data.getName();
      JSONArray discrepanciesList = data.getDiscrepanciesList();
      JSONObject annotationsList = data.getAnnotationsList();
      String status = data.getStatus();
      if (name != null) {
         tx.setName(dispoItemArt, name);
         wasEdited = true;
      }
      if (discrepanciesList != null) {
         tx.setSoleAttributeFromString(dispoItemArt, DispoConstants.DispoDiscrepanciesJson,
            discrepanciesList.toString());
         wasEdited = true;
      }
      if (annotationsList != null) {
         tx.setSoleAttributeFromString(dispoItemArt, DispoConstants.DispoAnnotationsJson, annotationsList.toString());
         wasEdited = true;
      }
      if (assigneeId != null) {
         ArtifactReadable userAsArt = findUser(assigneeId);
         tx.relate(dispoItemArt, DispoConstants.DispoAssigned_Assignee, userAsArt);
         wasEdited = true;
      }
      if (status != null) {
         tx.setSoleAttributeFromString(dispoItemArt, DispoConstants.DispoItemStatus, status);
         wasEdited = true;
      }
      if (wasEdited) {
         tx.setSoleAttributeValue(dispoItemArt, DispoConstants.DispoLastUpdated, new Date());
      }
      tx.commit();
   }

   @Override
   public IOseeBranch findProgramId(DispoProgram program) {
      IOseeBranch toReturn = null;
      BranchReadable branchRead = getQuery().branchQuery().andIds(CoreBranches.COMMON).getResults().getExactlyOne();

      ArtifactReadable configArt =
         getQuery().fromBranch(branchRead).andNameEquals(Dispo_Config_Art).getResults().getExactlyOne();

      String configContents = configArt.getSoleAttributeAsString(CoreAttributeTypes.GeneralStringData);

      Pattern regex = Pattern.compile(program.getUuid() + "\\s*:\\s*.*");
      Matcher matcher = regex.matcher(configContents);
      String guid = null;
      if (matcher.find()) {
         String match = matcher.group();
         String[] split = match.split(":");
         guid = split[1];
      }

      regex = Pattern.compile(program.getUuid() + "\\s*:\\s*.*");
      matcher = regex.matcher(configContents);
      Long uuid = null;
      if (matcher.find()) {
         String match = matcher.group();
         String[] split = match.split(":");
         uuid = Long.valueOf(split[1]);
      }
      toReturn = TokenFactory.createBranch(uuid, "");
      return toReturn;
   }

   @Override
   public ResultSet<? extends IOseeBranch> findBaselineBranches() {
      return getQuery().branchQuery().andIsOfType(BranchType.BASELINE).getResults();
   }
}

Back to the top