Skip to main content
summaryrefslogtreecommitdiffstats
blob: a14604755044050a3f854e261503fa1ccd714384 (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
/*******************************************************************************
 * 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.client.ClientSessionManager;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.database.core.JoinUtility;
import org.eclipse.osee.framework.database.core.JoinUtility.CharJoinQuery;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;

/**
 * @author Ryan D. Brooks
 */
public class AttributeCriteria extends AbstractArtifactSearchCriteria {

   private final IAttributeType attributeType;
   private String value;
   private Collection<String> values;
   private String txsAlias;
   private String attrAlias;
   private String joinAlias;
   private final boolean historical;
   private final Operator operator;
   private CharJoinQuery joinQuery;

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

   /**
    * Constructor for search criteria that finds an attribute of the given type and any value (i.e. checks for
    * existence)
    * 
    * @param attributeTypeName
    * @param value
    * @throws OseeCoreException
    */
   public AttributeCriteria(IAttributeType attributeType) throws OseeCoreException {
      this(attributeType, null, null, false, 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
    * 
    * @param attributeTypeName
    * @param values
    * @throws OseeCoreException
    */
   public AttributeCriteria(IAttributeType attributeType, Collection<String> values) throws OseeCoreException {
      this(attributeType, null, validate(values), false, Operator.EQUAL);
   }

   public AttributeCriteria(String attributeTypeName, Collection<String> values) throws OseeCoreException {
      this(toAttributeType(attributeTypeName), null, validate(values), false, Operator.EQUAL);
   }

   /**
    * 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.
    * 
    * @param attributeTypeName
    * @param values
    * @throws OseeCoreException
    */
   public AttributeCriteria(String attributeTypeName, Collection<String> values, Operator operator) throws OseeCoreException {
      this(toAttributeType(attributeTypeName), null, validate(values), false, operator);
   }

   public AttributeCriteria(IAttributeType attributeType, Collection<String> values, Operator operator) throws OseeCoreException {
      this(attributeType, null, validate(values), false, operator);
   }

   /**
    * Constructor for search criteria that finds an attribute of the given type with its current value equal to the
    * given value.
    * 
    * @param attributeTypeName
    * @param value to search; supports % wildcard
    * @param historical if true will search on any branch and any attribute revision
    * @throws OseeCoreException
    */
   public AttributeCriteria(String attributeTypeName, String value, boolean historical) throws OseeCoreException {
      this(toAttributeType(attributeTypeName), value, null, historical, Operator.EQUAL);
   }

   private static AttributeType toAttributeType(String attributeTypeName) throws OseeCoreException {
      return attributeTypeName == null ? null : AttributeTypeManager.getType(attributeTypeName);
   }

   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, boolean historical, Operator operator) throws OseeDataStoreException {
      this.attributeType = attributeType;

      if (values == null) {
         this.value = value;
      } else {
         if (values.size() == 1) {
            this.value = values.iterator().next();
         } else {
            this.values = values;
            joinQuery = JoinUtility.createCharJoinQuery(ClientSessionManager.getSessionId());
            for (String str : values) {
               joinQuery.add(str);
            }
         }
      }
      this.operator = operator;
      this.historical = historical;
   }

   @Override
   public void addToTableSql(ArtifactQueryBuilder builder) {
      attrAlias = builder.appendAliasedTable("osee_attribute");
      txsAlias = builder.appendAliasedTable("osee_txs");
      if (joinQuery != null && operator == Operator.EQUAL) {
         joinAlias = builder.appendAliasedTable(joinQuery.getJoinTableName());
      }
   }

   @Override
   public void addToWhereSql(ArtifactQueryBuilder builder) throws OseeCoreException {
      if (attributeType != null) {
         builder.append(attrAlias);
         builder.append(".attr_type_id=? AND ");
         builder.addParameter(AttributeTypeManager.getTypeId(attributeType));
      }
      if (value != null) {
         builder.append(attrAlias);
         builder.append(".value");
         if (value.contains("%")) {
            if (operator == Operator.NOT_EQUAL) {
               builder.append(" NOT");
            }
            builder.append(" LIKE ");
         } else {
            builder.append(operator.toString());
         }
         builder.append("? AND ");
         builder.addParameter(value);
      }

      if (joinQuery != null) {
         if (operator == Operator.EQUAL) {
            builder.append(attrAlias);
            builder.append(".value = ");
            builder.append(joinAlias);
            builder.append(".id AND ");
            builder.append(joinAlias);
            builder.append(".query_id = ? AND ");
         } else {
            builder.append("NOT EXISTS (SELECT 1 from ");
            builder.append(joinQuery.getJoinTableName());
            builder.append(" WHERE id = ");
            builder.append(attrAlias);
            builder.append(".value AND query_id = ?) AND ");
         }

         builder.addParameter(joinQuery.getQueryId());
         joinQuery.store();
      }

      builder.append(attrAlias);
      builder.append(".gamma_id=");
      builder.append(txsAlias);
      builder.append(".gamma_id AND ");

      builder.addTxSql(txsAlias, historical);
   }

   @Override
   public void addJoinArtId(ArtifactQueryBuilder builder, boolean left) {
      builder.append(attrAlias);
      builder.append(".art_id");
   }

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

      if (joinQuery != null) {
         strB.append(attrAlias);
         strB.append("(" + Collections.toString(",", values) + ")");
      }
      return strB.toString();
   }

   @Override
   public void cleanUp() throws OseeDataStoreException {
      if (joinQuery != null) {
         joinQuery.delete();
      }
   }

}

Back to the top