Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05a452f40478db72ed5bac09abbb6627c89f4d33 (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
/*******************************************************************************
 * Copyright (c) 2012 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.rest.internal.search.artifact.predicate;

import java.util.Collection;
import java.util.List;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.RelationSide;
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.util.Conditions;
import org.eclipse.osee.orcs.rest.internal.search.artifact.PredicateHandler;
import org.eclipse.osee.orcs.rest.model.search.artifact.Predicate;
import org.eclipse.osee.orcs.rest.model.search.artifact.SearchMethod;
import org.eclipse.osee.orcs.search.QueryBuilder;

/**
 * @author John R. Misinco
 * @author Roberto E. Escobar
 */
public class ExistenceTypePredicateHandler implements PredicateHandler {

   @Override
   public QueryBuilder handle(QueryBuilder builder, Predicate predicate) throws OseeCoreException {
      if (!predicate.getType().isOfType(SearchMethod.EXISTS_TYPE, SearchMethod.NOT_EXISTS_TYPE)) {
         throw new OseeArgumentException("This predicate handler only supports [%s] and [%s]", SearchMethod.EXISTS_TYPE,
            SearchMethod.NOT_EXISTS_TYPE);
      }
      List<String> typeParameters = predicate.getTypeParameters();
      Collection<String> values = predicate.getValues();

      Conditions.checkNotNullOrEmpty(typeParameters, "typeParameters");
      Conditions.checkNotNull(values, "values");

      if (typeParameters.size() >= 1) {
         String existsType = typeParameters.get(0);
         if ("attrType".equals(existsType)) {
            Collection<IAttributeType> attributeTypes = PredicateHandlerUtil.getIAttributeTypes(values);
            if (!attributeTypes.isEmpty()) {
               if (checkExists(predicate.getType())) {
                  builder.andExists(attributeTypes);
               } else {
                  builder.andNotExists(attributeTypes);
               }
            }
         } else if ("relType".equals(existsType)) {
            Collection<IRelationType> iRelationTypes = PredicateHandlerUtil.getIRelationTypes(values);
            for (IRelationType rt : iRelationTypes) {
               if (checkExists(predicate.getType())) {
                  builder.andExists(rt);
               } else {
                  builder.andNotExists(rt);
               }
            }
         } else if ("relTypeSide".equals(existsType)) {
            RelationSide side = typeParameters.get(1).equals("A") ? RelationSide.SIDE_A : RelationSide.SIDE_B;
            for (IRelationType rt : PredicateHandlerUtil.getIRelationTypes(values)) {
               IRelationTypeSide rts = TokenFactory.createRelationTypeSide(side, rt.getId(), "SearchRelTypeSide");
               if (checkExists(predicate.getType())) {
                  builder.andExists(rts);
               } else {
                  builder.andNotExists(rts);
               }
            }
         }
      }

      return builder;
   }

   private boolean checkExists(SearchMethod method) {
      return method == SearchMethod.EXISTS_TYPE;
   }

}

Back to the top