Skip to main content
summaryrefslogtreecommitdiffstats
blob: 26316d0c03df86f8a24d343e52093b9e1d61aae7 (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
/*******************************************************************************
 * 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.skynet.core.artifact.search;

import java.util.Collection;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.enums.Operator;
import org.eclipse.osee.framework.core.enums.QueryOption;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.rest.client.QueryBuilder;

/**
 * @author Ryan D. Brooks
 */
public class AttributeCriteria implements ArtifactSearchCriteria {

   private final IAttributeType attributeType;
   private String value;
   private Collection<String> values;
   private final Operator operator;
   private final QueryOption[] options;

   public IAttributeType getAttributeType() {
      return attributeType;
   }

   public String getValue() {
      return value;
   }

   public Collection<String> getValues() {
      return values;
   }

   public Operator getOperator() {
      return operator;
   }

   public QueryOption[] getOptions() {
      return options;
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type with its current value equal to the
    * given value.
    * 
    * @param value to search;
    */
   public AttributeCriteria(IAttributeType attributeType, String value, QueryOption... options) {
      this(attributeType, value, null, Operator.EQUAL, options);
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type and any value (i.e. checks for
    * existence)
    */
   public AttributeCriteria(IAttributeType attributeType) {
      this(attributeType, null, null, Operator.EQUAL);
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type with its current value exactly equal to
    * any one of the given literal values. If the list only contains one value, then the search is conducted exactly as
    * if the single value constructor was called. This search does not support the wildcard for multiple values. Throws
    * OseeArgumentException values is empty or null
    */
   public AttributeCriteria(IAttributeType attributeType, Collection<String> values) throws OseeCoreException {
      this(attributeType, null, validate(values), Operator.EQUAL);
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type with its current value relative to the
    * given value based on the operator provided.
    */
   public AttributeCriteria(IAttributeType attributeType, String value, Operator operator) {
      this(attributeType, value, null, operator);
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type with its current value exactly equal (or
    * not equal) to any one of the given literal values. If the list only contains one value, then the search is
    * conducted exactly as if the single value constructor was called. This search does not support the wildcard for
    * multiple values.
    */
   public AttributeCriteria(IAttributeType attributeType, Collection<String> values, Operator operator) throws OseeCoreException {
      this(attributeType, null, validate(values), operator);
   }

   private static Collection<String> validate(Collection<String> values) throws OseeArgumentException {
      if (values == null || values.isEmpty()) {
         throw new OseeArgumentException("values provided to AttributeCriteria must not be null or empty");
      }
      return values;
   }

   public AttributeCriteria(IAttributeType attributeType, String value, Collection<String> values, Operator operator, QueryOption... options) {
      this.attributeType = attributeType;

      if (values == null) {
         this.value = value;
      } else {
         if (values.size() == 1) {
            this.value = values.iterator().next();
         } else {
            this.values = values;
         }
      }
      this.operator = operator;

      if (this.value != null && operator == Operator.EQUAL && options.length == 0) {
         this.options = QueryOptions.EXACT_MATCH_OPTIONS;
      } else {
         this.options = options;
      }
   }

   @Override
   public String toString() {
      StringBuilder strB = new StringBuilder();
      if (attributeType != null) {
         strB.append(attributeType);
      } else {
         strB.append("*");
      }
      strB.append(" ");
      strB.append(operator);
      strB.append(" ");
      if (value != null) {
         strB.append(value);
      }

      return strB.toString();
   }

   @Override
   public void addToQueryBuilder(QueryBuilder builder) throws OseeCoreException {
      if (Strings.isValid(getValue())) {
         if (getOperator() == Operator.EQUAL) {
            builder.and(getAttributeType(), getValue(), getOptions());
         } else {
            builder.and(getAttributeType(), getOperator(), getValue());
         }
      } else if (getValues() != null) {
         builder.and(getAttributeType(), getOperator(), getValues());
      } else {
         builder.andExists(getAttributeType());
      }
   }
}

Back to the top