Skip to main content
summaryrefslogtreecommitdiffstats
blob: 58d9dd31bf6362bbc4288a31bec992b633444629 (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.database.core.AbstractJoinQuery;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.core.ds.DataPostProcessor;
import org.eclipse.osee.orcs.core.ds.DataPostProcessorFactory;
import org.eclipse.osee.orcs.core.ds.QueryOptions;
import org.eclipse.osee.orcs.core.ds.criteria.CriteriaAttributeKeywords;
import org.eclipse.osee.orcs.db.internal.search.tagger.HasTagProcessor;
import org.eclipse.osee.orcs.db.internal.search.tagger.TagCollector;
import org.eclipse.osee.orcs.db.internal.search.tagger.TagProcessor;
import org.eclipse.osee.orcs.db.internal.sql.AbstractSqlWriter;
import org.eclipse.osee.orcs.db.internal.sql.HasDataPostProcessorFactory;
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.WithClause;
import org.eclipse.osee.orcs.db.internal.sql.WithClause.WithAlias;

/**
 * @author Roberto E. Escobar
 */
public class AttributeTokenSqlHandler extends SqlHandler<CriteriaAttributeKeywords, QueryOptions> implements HasTagProcessor, HasDataPostProcessorFactory<CriteriaAttributeKeywords> {

   private CriteriaAttributeKeywords criteria;
   private static final int MAX_TOKEN_SIZE = 20;

   private String artAlias;
   private String attrAlias;
   private String txsAlias;

   private DataPostProcessorFactory<CriteriaAttributeKeywords> factory;
   private TagProcessor tagProcessor;

   @Override
   public void setDataPostProcessorFactory(DataPostProcessorFactory<CriteriaAttributeKeywords> factory) {
      this.factory = factory;
   }

   @Override
   public DataPostProcessorFactory<CriteriaAttributeKeywords> getDataPostProcessorFactory() {
      return factory;
   }

   @Override
   public void setTagProcessor(TagProcessor tagProcessor) {
      this.tagProcessor = tagProcessor;
   }

   @Override
   public TagProcessor getTagProcessor() {
      return tagProcessor;
   }

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

   @Override
   public void addWithTables(AbstractSqlWriter<QueryOptions> writer) throws OseeCoreException {
      List<String> values = new ArrayList<String>(criteria.getValues());
      Collection<? extends IAttributeType> types = criteria.getTypes();

      int valueCount = values.size();
      int tagsCount = 0;

      StringBuilder gammaSb = new StringBuilder();

      for (int valueIdx = 0; valueIdx < valueCount; valueIdx++) {
         List<Long> tags = new ArrayList<Long>();
         String value = values.get(valueIdx);
         tokenize(value, tags);
         int tagsSize = tags.size();
         gammaSb.append("(");
         for (int tagIdx = 0; tagIdx < tagsSize; tagIdx++, tagsCount++) {
            Long tag = tags.get(tagIdx);
            gammaSb.append("SELECT gamma_id FROM osee_search_tags WHERE coded_tag_id = ?");
            writer.addParameter(tag);
            if (tagIdx + 1 < tagsSize) {
               gammaSb.append("\n INTERSECT \n");
            }
         }
         gammaSb.append(") ");
         if (valueIdx + 1 < valueCount) {
            gammaSb.append("\n UNION ALL \n");
         }
      }

      Conditions.checkExpressionFailOnTrue(tagsCount > MAX_TOKEN_SIZE, "Parsed tokens for [%s] is greater than [%d]",
         Collections.toString(", ", criteria.getValues()), MAX_TOKEN_SIZE);

      WithClause gammaWith = new WithClause(gammaSb.toString(), WithAlias.GAMMA);
      String gammaAlias = writer.addWithClause(gammaWith);
      String jIdAlias = null;

      StringBuilder attrSb = new StringBuilder();
      attrSb.append("SELECT art_id FROM osee_attribute att, osee_txs txs, ");
      if (!criteria.isIncludeAllTypes() && types.size() > 1) {
         jIdAlias = writer.getNextAlias(TableEnum.ID_JOIN_TABLE);
         attrSb.append("osee_join_id ");
         attrSb.append(jIdAlias);
         attrSb.append(", ");
      }
      attrSb.append(gammaAlias);
      attrSb.append(" WHERE att.gamma_id = ");
      attrSb.append(gammaAlias);
      attrSb.append(".gamma_id AND att.gamma_id = txs.gamma_id AND ");
      attrSb.append(writer.getTxBranchFilter("txs"));
      if (!criteria.isIncludeAllTypes()) {
         attrSb.append(" AND att.attr_type_id = ");
         if (types.size() == 1) {
            attrSb.append("?");
            int localId = toLocalId(criteria.getTypes().iterator().next());
            writer.addParameter(localId);
         } else {
            Set<Integer> typeIds = new HashSet<Integer>();
            for (IAttributeType type : types) {
               typeIds.add(toLocalId(type));
            }
            AbstractJoinQuery joinQuery = writer.writeIdJoin(typeIds);

            attrSb.append(jIdAlias);
            attrSb.append(".id AND ");
            attrSb.append(jIdAlias);
            attrSb.append(".query_id = ?");
            writer.addParameter(joinQuery.getQueryId());
         }
      }

      WithClause attrWith = new WithClause(attrSb.toString(), WithAlias.ATTRIBUTE);
      attrAlias = writer.addWithClause(attrWith);
      writer.addTable(attrAlias);
   }

   @Override
   public void addTables(AbstractSqlWriter<QueryOptions> writer) {
      List<String> aliases = writer.getAliases(TableEnum.ARTIFACT_TABLE);
      List<String> txs = writer.getAliases(TableEnum.TXS_TABLE);

      if (aliases.isEmpty()) {
         artAlias = writer.addTable(TableEnum.ARTIFACT_TABLE);
      }
      if (txs.isEmpty()) {
         txsAlias = writer.addTable(TableEnum.TXS_TABLE);
      }
   }

   @Override
   public boolean addPredicates(AbstractSqlWriter<QueryOptions> writer) throws OseeCoreException {
      DataPostProcessor<?> processor = getDataPostProcessorFactory().createPostProcessor(criteria, writer.getOptions());
      writer.addPostProcessor(processor);

      if (!Strings.isValid(artAlias)) {
         artAlias = writer.getAliases(TableEnum.ARTIFACT_TABLE).iterator().next();
      }
      if (!Strings.isValid(txsAlias)) {
         txsAlias = writer.getAliases(TableEnum.TXS_TABLE).iterator().next();
      }

      writer.write("%s.art_id = %s.art_id", artAlias, attrAlias);
      writer.writeAndLn();
      writer.write(writer.getTxBranchFilter(txsAlias));
      writer.writeAndLn();
      writer.write("%s.gamma_id = %s.gamma_id", txsAlias, artAlias);
      return true;
   }

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

   private void tokenize(String value, final Collection<Long> codedTags) {
      TagCollector collector = new TagCollector() {
         @Override
         public void addTag(String word, Long codedTag) {
            codedTags.add(codedTag);
         }
      };
      getTagProcessor().collectFromString(value, collector);
   }

}

Back to the top