Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66821297b0a04a3de2e18f63d31c52fc36ff27d7 (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
/*******************************************************************************
 * 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.orcs.db.internal.search.handlers;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.orcs.core.ds.criteria.CriteriaAttributeTypeNotExists;
import org.eclipse.osee.orcs.db.internal.sql.AbstractSqlWriter;
import org.eclipse.osee.orcs.db.internal.sql.ObjectType;
import org.eclipse.osee.orcs.db.internal.sql.SqlHandler;
import org.eclipse.osee.orcs.db.internal.sql.TableEnum;
import org.eclipse.osee.orcs.db.internal.sql.join.AbstractJoinQuery;

/**
 * @author John Misinco
 */
public class AttributeTypeNotExistsSqlHandler extends SqlHandler<CriteriaAttributeTypeNotExists> {

   private CriteriaAttributeTypeNotExists criteria;

   private String jIdAlias;
   private String artAlias;
   private String txsAlias;
   private AbstractJoinQuery joinQuery;

   @Override
   public void setData(CriteriaAttributeTypeNotExists criteria) {
      this.criteria = criteria;
   }

   @Override
   public void addTables(AbstractSqlWriter writer) {
      if (criteria.getTypes().size() > 1) {
         jIdAlias = writer.addTable(TableEnum.ID_JOIN_TABLE);
      }
      artAlias = writer.getOrCreateTableAlias(TableEnum.ARTIFACT_TABLE);
      txsAlias = writer.getOrCreateTableAlias(TableEnum.TXS_TABLE, ObjectType.ARTIFACT);
   }

   @Override
   public boolean addPredicates(AbstractSqlWriter writer) throws OseeCoreException {
      Collection<? extends IAttributeType> types = criteria.getTypes();

      writer.writeEquals(artAlias, txsAlias, "gamma_id");
      writer.writeAndLn();
      writer.write(writer.getTxBranchFilter(txsAlias));
      writer.writeAndLn();
      writer.write("NOT EXISTS (SELECT 1 FROM ");
      writer.write(TableEnum.ATTRIBUTE_TABLE.getName());
      writer.write(" attr, ");
      writer.write(TableEnum.TXS_TABLE.getName());
      writer.write(" txs WHERE ");

      if (types.size() > 1) {
         Set<Long> typeIds = new HashSet<>();
         for (IAttributeType type : types) {
            typeIds.add(type.getGuid());
         }
         joinQuery = writer.writeIdJoin(typeIds);

         writer.write("attr.attr_type_id = ");
         writer.write(jIdAlias);
         writer.write(".id AND ");
         writer.write(jIdAlias);
         writer.write(".query_id = ?");
         writer.addParameter(joinQuery.getQueryId());
      } else {
         IAttributeType type = types.iterator().next();
         writer.write("attr.attr_type_id = ?");
         writer.addParameter(type.getGuid());
      }

      writer.writeAndLn();

      writer.writeEquals("attr", "txs", "gamma_id");
      writer.writeAndLn();
      writer.write(writer.getTxBranchFilter("txs"));
      writer.writeAndLn();
      writer.writeEquals("attr", artAlias, "art_id");
      writer.write(")");

      return true;
   }

   @Override
   public int getPriority() {
      return SqlHandlerPriority.ATTRIBUTE_TYPE_NOT_EXISTS.ordinal();
   }

}

Back to the top