Skip to main content
summaryrefslogtreecommitdiffstats
blob: e121df438d96b85fd3e8275d28e86165ca74b17e (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
/*******************************************************************************
 * Copyright (c) 2016 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.ats.core.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.osee.ats.api.IAtsConfigObject;
import org.eclipse.osee.ats.api.IAtsServices;
import org.eclipse.osee.ats.api.config.WorkType;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.program.IAtsProgram;
import org.eclipse.osee.ats.api.query.IAtsConfigQuery;
import org.eclipse.osee.ats.api.query.IAtsQueryFilter;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.AttributeTypeId;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.RelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.QueryOption;
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.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.type.ResultSets;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Donald G. Dunne
 */
public abstract class AbstractAtsConfigQueryImpl implements IAtsConfigQuery {

   protected final List<AtsAttributeQuery> andAttr;
   protected List<IArtifactType> artifactTypes;
   protected Collection<Long> uuids;
   protected final IAtsServices services;
   protected Collection<Long> aiUuids;
   protected List<ArtifactId> onlyIds = null;
   protected final List<IAtsQueryFilter> queryFilters;

   public AbstractAtsConfigQueryImpl(IAtsServices services) {
      this.services = services;
      andAttr = new ArrayList<>();
      aiUuids = new ArrayList<>();
      uuids = new ArrayList<>();
      queryFilters = new ArrayList<>();
   }

   @Override
   public Collection<ArtifactId> getItemIds() throws OseeCoreException {
      onlyIds = new LinkedList<>();
      getItems();
      return onlyIds;
   }

   public abstract void createQueryBuilder();

   @Override
   public <T extends IAtsConfigObject> Collection<T> getItems() {
      createQueryBuilder();

      if (artifactTypes != null) {
         queryAndIsOfType(artifactTypes);
      }

      if (uuids != null && uuids.size() > 0) {
         addUuidCriteria(uuids);
      }

      addAttributeCriteria();

      Set<T> allResults = new HashSet<>();
      collectResults(allResults, artifactTypes);

      return allResults;
   }

   public abstract Collection<ArtifactId> runQuery();

   @SuppressWarnings("unchecked")
   private <T> Collection<T> collectResults(Set<T> allResults, List<IArtifactType> artifactTypes) {
      Set<T> results = new HashSet<>();
      if (isOnlyIds()) {
         onlyIds.addAll(queryGetIds());
      }
      // filter on original artifact types
      else {
         Collection<ArtifactId> artifacts = runQuery();
         for (ArtifactId artifact : artifacts) {
            if (artifactTypes != null || isArtifactTypeMatch(artifact, artifactTypes)) {
               results.add((T) createFromFactory(artifact));
            }
         }
      }
      addtoResultsWithNullCheck(allResults, results);

      return results;
   }

   @SuppressWarnings("unchecked")
   private <T> T createFromFactory(ArtifactId artifact) {
      return (T) services.getConfigItemFactory().getConfigObject(artifact);
   }

   private <T> void addtoResultsWithNullCheck(Set<T> allResults, Collection<? extends T> configObjects) {
      if (configObjects.contains(null)) {
         OseeLog.log(AbstractAtsConfigQueryImpl.class, Level.SEVERE, "Null found in results");
      } else {
         allResults.addAll(configObjects);
      }
   }

   private boolean isArtifactTypeMatch(ArtifactId artifact, List<IArtifactType> artTypes) {
      if (artTypes == null || artTypes.isEmpty()) {
         return true;
      }
      for (IArtifactType artType : artTypes) {
         if (services.getArtifactResolver().isOfType(artifact, artType)) {
            return true;
         }
      }
      return false;
   }

   public abstract void queryAndNotExists(RelationTypeSide relationTypeSide);

   public abstract void queryAndExists(RelationTypeSide relationTypeSide);

   public abstract void queryAndIsOfType(IArtifactType artifactType);

   public boolean isOnlyIds() {
      return onlyIds != null;
   }

   public abstract List<? extends ArtifactId> queryGetIds();

   @Override
   public IAtsConfigQuery isOfType(IArtifactType... artifactType) {
      if (this.artifactTypes != null) {
         throw new OseeArgumentException("Can only specify one artifact type");
      }
      this.artifactTypes = new LinkedList<>();
      for (IArtifactType type : artifactType) {
         this.artifactTypes.add(type);
      }
      return this;
   }

   @Override
   public IAtsConfigQuery andAttr(AttributeTypeId attributeType, Collection<String> values, QueryOption... queryOptions) throws OseeCoreException {
      andAttr.add(new AtsAttributeQuery(attributeType, values, queryOptions));
      return this;
   }

   @Override
   public IAtsConfigQuery andUuids(Long... uuids) {
      this.uuids = Arrays.asList(uuids);
      return this;
   }

   @Override
   public IAtsConfigQuery andAttr(AttributeTypeId attributeType, String value, QueryOption... queryOption) {
      return andAttr(attributeType, Collections.singleton(value), queryOption);
   }

   @Override
   public <T extends IAtsConfigObject> ResultSet<T> getResults() {
      return ResultSets.newResultSet(getItems());
   }

   @SuppressWarnings("unchecked")
   @Override
   public <T extends ArtifactId> ResultSet<T> getResultArtifacts() {
      List<T> items = new ArrayList<>();
      ResultSet<IAtsConfigObject> results = getResults();
      for (IAtsConfigObject configObject : results) {
         if (configObject == null) {
            OseeLog.log(AbstractAtsConfigQueryImpl.class, Level.SEVERE, "Null found in results");
         } else {
            items.add((T) configObject.getStoreObject());
         }
      }
      // filter on original artifact types
      List<T> artifacts = new LinkedList<>();
      for (ArtifactId artifact : items) {
         boolean artifactTypeMatch = isArtifactTypeMatch(artifact, artifactTypes);
         if (artifactTypeMatch) {
            artifacts.add((T) artifact);
         }
      }
      return ResultSets.newResultSet(artifacts);
   }

   public abstract void queryAndIsOfType(List<IArtifactType> artTypes);

   public abstract void queryAnd(AttributeTypeId attrType, String value);

   private void addAttributeCriteria() {
      if (!andAttr.isEmpty()) {
         for (AtsAttributeQuery attrQuery : andAttr) {
            queryAnd(attrQuery.getAttrType(), attrQuery.getValues(), attrQuery.getQueryOption());
         }
      }
   }

   public abstract void queryAnd(AttributeTypeId attrType, Collection<String> values, QueryOption[] queryOption);

   public abstract void queryAnd(AttributeTypeId attrType, String value, QueryOption[] queryOption);

   private void addUuidCriteria(Collection<Long> uuids) {
      if (uuids != null) {
         List<Integer> artIds = new LinkedList<>();
         for (Long uuid : uuids) {
            artIds.add(uuid.intValue());
         }
         queryAndLocalIds(artIds);
      }
   }

   public abstract void queryAndLocalIds(List<Integer> artIds);

   public abstract void queryAnd(AttributeTypeId attrType, Collection<String> values);

   public Collection<IArtifactType> getArtifactTypes() {
      return artifactTypes;
   }

   public void setArtifactType(List<IArtifactType> artifactTypes) {
      this.artifactTypes = artifactTypes;
   }

   @Override
   public IAtsConfigQuery andProgram(IAtsProgram program) {
      return andProgram(program.getId());
   }

   @Override
   public IAtsConfigQuery andProgram(Long uuid) {
      return andAttr(AtsAttributeTypes.ProgramUuid, Collections.singleton(String.valueOf(uuid)));
   }

   @Override
   public IAtsConfigQuery andWorkType(WorkType workType, WorkType... workTypes) {
      List<String> workTypeStrs = new LinkedList<>();
      workTypeStrs.add(workType.name());
      for (WorkType workType2 : workTypes) {
         workTypeStrs.add(workType2.name());
      }
      return andAttr(AtsAttributeTypes.WorkType, workTypeStrs);
   }

   @Override
   public IAtsConfigQuery andCsci(Collection<String> cscis) {
      return andAttr(AtsAttributeTypes.CSCI, cscis);
   }

   @Override
   public IAtsConfigQuery andName(String name) {
      return andAttr(CoreAttributeTypes.Name, name);
   }

   @Override
   public IAtsConfigQuery andWorkType(Collection<WorkType> workTypes) {
      List<String> workTypeStrs = new LinkedList<>();
      for (WorkType workType2 : workTypes) {
         workTypeStrs.add(workType2.name());
      }
      return andAttr(AtsAttributeTypes.WorkType, workTypeStrs);
   }

   @Override
   public IAtsConfigQuery andTag(String... tags) {
      List<String> values = Arrays.asList(tags);
      return andAttr(CoreAttributeTypes.StaticId, values, QueryOption.EXACT_MATCH_OPTIONS);
   }

   @Override
   public IAtsConfigQuery andActive(boolean active) {
      return andAttr(AtsAttributeTypes.Active, active ? "true" : "false");
   }

   @Override
   public <T extends IAtsConfigObject> Collection<T> getItems(Class<T> clazz) {
      return org.eclipse.osee.framework.jdk.core.util.Collections.castAll(getItems());
   }

   @Override
   public <T extends IAtsConfigObject> T getOneOrNull(Class<T> clazz) {
      Collection<T> items = getItems(clazz);
      if (!items.isEmpty()) {
         return items.iterator().next();
      }
      return null;
   }

}

Back to the top