Skip to main content
summaryrefslogtreecommitdiffstats
blob: 29580250622ae4e6faf909b0a3b9ac199849a0ab (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
/*******************************************************************************
 * 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.List;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.database.core.AbstractJoinQuery;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.orcs.core.ds.criteria.CriteriaArtifactType;
import org.eclipse.osee.orcs.db.internal.sql.AbstractSqlWriter;
import org.eclipse.osee.orcs.db.internal.sql.SqlHandler;
import org.eclipse.osee.orcs.db.internal.sql.TableEnum;

/**
 * @author Roberto E. Escobar
 */
public class ArtifactTypeSqlHandler extends SqlHandler<CriteriaArtifactType> {

   private CriteriaArtifactType criteria;

   private String jIdAlias;
   private String artAlias;
   private String txsAlias;
   private AbstractJoinQuery joinQuery;
   private Collection<Long> typeIds;

   private List<String> artAliases;
   private List<String> txsAliases;

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

   @Override
   public void addTables(AbstractSqlWriter writer) throws OseeCoreException {
      typeIds = getLocalTypeIds();
      if (typeIds.size() > 1) {
         jIdAlias = writer.addTable(TableEnum.ID_JOIN_TABLE);
      }

      artAliases = writer.getAliases(TableEnum.ARTIFACT_TABLE);
      txsAliases = writer.getAliases(TableEnum.TXS_TABLE);

      if (artAliases.isEmpty()) {
         artAlias = writer.addTable(TableEnum.ARTIFACT_TABLE);
      }
      if (txsAliases.isEmpty()) {
         txsAlias = writer.addTable(TableEnum.TXS_TABLE);
      }
      artAliases = writer.getAliases(TableEnum.ARTIFACT_TABLE);
      txsAliases = writer.getAliases(TableEnum.TXS_TABLE);
   }

   private Collection<Long> getLocalTypeIds() throws OseeCoreException {
      Collection<? extends IArtifactType> types = criteria.getTypes();
      Collection<Long> toReturn = new HashSet<Long>();
      for (IArtifactType type : types) {
         long localId = getIdentityService().getLocalId(type);
         toReturn.add(localId);
      }
      return toReturn;
   }

   @Override
   public boolean addPredicates(AbstractSqlWriter writer) throws OseeCoreException {
      boolean modified = false;

      if (typeIds.size() > 1) {
         modified = true;
         joinQuery = writer.writeIdJoin(typeIds);
         writer.write(jIdAlias);
         writer.write(".query_id = ?");
         writer.addParameter(joinQuery.getQueryId());

         if (!artAliases.isEmpty()) {
            writer.write(" AND ");
            int aSize = artAliases.size();
            for (int index = 0; index < aSize; index++) {
               String artAlias = artAliases.get(index);
               writer.write(artAlias);
               writer.write(".art_type_id = ");
               writer.write(jIdAlias);
               writer.write(".id");
               if (index + 1 < aSize) {
                  writer.write(" AND ");
               }
            }
         }
      } else {
         modified = true;
         long localId = typeIds.iterator().next();

         int aSize = artAliases.size();
         for (int index = 0; index < aSize; index++) {
            String artAlias = artAliases.get(index);
            writer.write(artAlias);
            writer.write(".art_type_id = ?");
            writer.addParameter(localId);
            if (index + 1 < aSize) {
               writer.write(" AND ");
            }
         }
      }

      if (artAlias != null && txsAlias != null) {
         writer.write(" AND ");
         writer.write(artAlias);
         writer.write(".gamma_id = ");
         writer.write(txsAlias);
         writer.write(".gamma_id AND ");
         writer.write(writer.getTxBranchFilter(txsAlias));
         modified = true;
      }
      return modified;
   }

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

Back to the top