Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0b0d0cab0c2e553ef9aed8185198871617ff64d0 (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
/*******************************************************************************
 * 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.orcs.core.internal.search;

import static org.eclipse.osee.framework.core.enums.BranchArchivedState.UNARCHIVED;
import static org.eclipse.osee.framework.core.enums.BranchState.DELETED;
import static org.eclipse.osee.framework.core.enums.BranchState.DELETE_IN_PROGRESS;
import static org.eclipse.osee.framework.core.enums.BranchState.PURGED;
import static org.eclipse.osee.framework.core.enums.BranchState.PURGE_IN_PROGRESS;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.ds.Criteria;
import org.eclipse.osee.orcs.core.ds.CriteriaSet;
import org.eclipse.osee.orcs.core.ds.Options;
import org.eclipse.osee.orcs.core.ds.OptionsUtil;
import org.eclipse.osee.orcs.core.ds.QueryData;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.search.BranchQuery;

/**
 * @author Roberto E. Escobar
 */
public class BranchQueryImpl implements BranchQuery {

   private final BranchCallableQueryFactory queryFactory;
   private final BranchCriteriaFactory criteriaFactory;
   private final OrcsSession session;
   private final QueryData queryData;
   private boolean includeArchived;

   public BranchQueryImpl(BranchCallableQueryFactory queryFactory, BranchCriteriaFactory criteriaFactory, OrcsSession session, QueryData queryData) {
      this.queryFactory = queryFactory;
      this.criteriaFactory = criteriaFactory;
      this.session = session;
      this.queryData = queryData;
      includeDeleted();
      includeArchived();
   }

   private QueryData getQueryData() {
      return queryData;
   }

   private Options getOptions() {
      return queryData.getOptions();
   }

   @Override
   public BranchQuery includeDeleted() {
      includeDeleted(true);
      return this;
   }

   @Override
   public BranchQuery excludeDeleted() {
      includeDeleted(false);
      return this;
   }

   @Override
   public BranchQuery includeDeleted(boolean enabled) {
      OptionsUtil.setIncludeDeletedArtifacts(getOptions(), enabled);
      return this;
   }

   @Override
   public boolean areDeletedIncluded() {
      return OptionsUtil.areDeletedArtifactsIncluded(getOptions());
   }

   @Override
   public BranchQuery includeArchived() {
      includeArchived(true);
      return this;
   }

   @Override
   public BranchQuery includeArchived(boolean enabled) {
      includeArchived = enabled;
      return this;
   }

   @Override
   public BranchQuery excludeArchived() {
      includeArchived(false);
      return this;
   }

   @Override
   public boolean areArchivedIncluded() {
      return includeArchived;
   }

   @Override
   public BranchQuery andUuids(long... uuids) throws OseeCoreException {
      Set<Long> allUuids = new HashSet<Long>();
      for (Long uuid : uuids) {
         allUuids.add(uuid);
      }
      return andUuids(allUuids);
   }

   @Override
   public BranchQuery andUuids(Collection<Long> uuids) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchUuidsCriteria(uuids);
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andIds(IOseeBranch... ids) throws OseeCoreException {
      return andIds(Arrays.asList(ids));
   }

   @Override
   public BranchQuery andIds(Collection<? extends IOseeBranch> ids) throws OseeCoreException {
      Set<Long> allIds = new HashSet<Long>();
      for (IOseeBranch token : ids) {
         allIds.add(token.getUuid());
      }
      return andUuids(allIds);
   }

   @Override
   public BranchQuery andIsOfType(BranchType... branchType) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchTypeCriteria(Arrays.asList(branchType));
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andStateIs(BranchState... branchState) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchStateCriteria(Arrays.asList(branchState));
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andNameEquals(String value) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchNameCriteria(value, false);
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andNamePattern(String pattern) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchNameCriteria(pattern, true);
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andIsChildOf(IOseeBranch parent) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchChildOfCriteria(parent);
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public BranchQuery andIsAncestorOf(IOseeBranch child) throws OseeCoreException {
      Criteria criteria = criteriaFactory.createBranchAncestorOfCriteria(child);
      return addAndCheck(getQueryData(), criteria);
   }

   @Override
   public ResultSet<BranchReadable> getResults() throws OseeCoreException {
      ResultSet<BranchReadable> result = null;
      try {
         result = createSearch().call();
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return result;
   }

   @Override
   public ResultSet<IOseeBranch> getResultsAsId() throws OseeCoreException {
      ResultSet<IOseeBranch> result = null;
      try {
         result = createSearchResultsAsIds().call();
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return result;
   }

   @Override
   public int getCount() throws OseeCoreException {
      Integer result = -1;
      try {
         result = createCount().call();
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return result;
   }

   @Override
   public CancellableCallable<Integer> createCount() throws OseeCoreException {
      return queryFactory.createBranchCount(session, checkAndCloneQueryData());
   }

   @Override
   public CancellableCallable<ResultSet<BranchReadable>> createSearch() throws OseeCoreException {
      return queryFactory.createBranchSearch(session, checkAndCloneQueryData());
   }

   @Override
   public CancellableCallable<ResultSet<IOseeBranch>> createSearchResultsAsIds() throws OseeCoreException {
      return queryFactory.createBranchAsIdSearch(session, checkAndCloneQueryData());
   }

   private QueryData checkAndCloneQueryData() throws OseeCoreException {
      QueryData queryData = getQueryData().clone();
      CriteriaSet criteriaSet = queryData.getCriteriaSet();
      if (criteriaSet.getCriterias().isEmpty()) {
         addAndCheck(queryData, criteriaFactory.createAllBranchesCriteria());
      }
      if (!areArchivedIncluded()) {
         Collection<BranchArchivedState> states = Arrays.asList(UNARCHIVED);
         addAndCheck(queryData, criteriaFactory.createBranchArchivedCriteria(states));
      }
      if (!areDeletedIncluded()) {
         Collection<BranchState> states = new ArrayList<BranchState>();
         for (BranchState state : BranchState.values()) {
            if (state != DELETE_IN_PROGRESS && state != DELETED && state != PURGE_IN_PROGRESS && state != PURGED) {
               states.add(state);
            }
         }
         addAndCheck(queryData, criteriaFactory.createBranchStateCriteria(states));
      }
      return queryData;
   }

   private BranchQuery addAndCheck(QueryData queryData, Criteria criteria) throws OseeCoreException {
      criteria.checkValid(getOptions());
      queryData.addCriteria(criteria);
      return this;
   }

}

Back to the top