Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4623f53ad9f9a1477bc541699fe7d4d634c500b4 (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
/*******************************************************************************
 * 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.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.executor.admin.ExecutorAdmin;
import org.eclipse.osee.executor.admin.WorkUtility;
import org.eclipse.osee.executor.admin.WorkUtility.PartitionFactory;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.type.MatchLocation;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.ds.Options;
import org.eclipse.osee.orcs.core.ds.OptionsUtil;
import org.eclipse.osee.orcs.core.ds.QueryPostProcessor;
import org.eclipse.osee.orcs.core.ds.criteria.CriteriaAttributeKeywords;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.AttributeReadable;
import org.eclipse.osee.orcs.data.AttributeTypes;
import org.eclipse.osee.orcs.db.internal.search.tagger.Tagger;
import org.eclipse.osee.orcs.search.Match;

/**
 * @author Roberto E. Escobar
 */
public abstract class AbstractQueryPostProcessor extends QueryPostProcessor implements PartitionFactory<ArtifactReadable, Match<ArtifactReadable, AttributeReadable<?>>> {

   private final Options options;
   private final CriteriaAttributeKeywords criteria;
   private final ExecutorAdmin executorAdmin;
   private List<Future<Collection<Match<ArtifactReadable, AttributeReadable<?>>>>> futures;

   protected AbstractQueryPostProcessor(Log logger, ExecutorAdmin executorAdmin, CriteriaAttributeKeywords criteria, Options options) {
      super(logger);
      this.executorAdmin = executorAdmin;
      this.criteria = criteria;
      this.options = options;
   }

   protected Collection<? extends IAttributeType> getTypes() {
      return criteria.getTypes();
   }

   protected String getQuery() {
      return criteria.getValues().iterator().next();
   }

   protected Options getOptions() {
      return options;
   }

   @Override
   public List<Match<ArtifactReadable, AttributeReadable<?>>> innerCall() throws Exception {
      Conditions.checkNotNull(getItemsToProcess(), "Query first pass results");

      futures = WorkUtility.partitionAndScheduleWork(executorAdmin, this, getItemsToProcess());

      checkForCancelled();

      List<Match<ArtifactReadable, AttributeReadable<?>>> results =
         new ArrayList<Match<ArtifactReadable, AttributeReadable<?>>>();
      for (Future<Collection<Match<ArtifactReadable, AttributeReadable<?>>>> future : futures) {
         results.addAll(future.get());
         checkForCancelled();
      }
      return results;
   }

   @Override
   public void setCancel(boolean isCancelled) {
      super.setCancel(isCancelled);
      if (futures != null) {
         for (Future<?> future : futures) {
            future.cancel(true);
         }
      }
   }

   protected abstract Tagger getTagger(String taggerId) throws OseeCoreException;

   @Override
   public Callable<Collection<Match<ArtifactReadable, AttributeReadable<?>>>> createWorker(Collection<ArtifactReadable> toProcess) {
      return new PostProcessorWorker(toProcess);
   }

   private class PostProcessorWorker extends CancellableCallable<Collection<Match<ArtifactReadable, AttributeReadable<?>>>> {

      private final Collection<ArtifactReadable> artifacts;

      public PostProcessorWorker(Collection<ArtifactReadable> artifacts) {
         this.artifacts = artifacts;
      }

      @Override
      public Collection<Match<ArtifactReadable, AttributeReadable<?>>> call() throws Exception {
         List<Match<ArtifactReadable, AttributeReadable<?>>> results =
            new ArrayList<Match<ArtifactReadable, AttributeReadable<?>>>();

         AttributeTypes attributeTypes = getAttributeTypes();

         DeletionFlag includeDeleted = OptionsUtil.getIncludeDeleted(options);
         Map<AttributeReadable<?>, List<MatchLocation>> matchedAttributes = null;
         for (ArtifactReadable artifact : artifacts) {
            checkForCancelled();
            for (AttributeReadable<Object> attribute : getAttributes(artifact, includeDeleted)) {
               checkForCancelled();
               try {
                  if (getTypes().contains(attribute.getAttributeType())) {
                     checkForCancelled();

                     String taggerId = attributeTypes.getTaggerId(attribute.getAttributeType());
                     Tagger tagger = getTagger(taggerId);
                     if (tagger != null) {
                        checkForCancelled();
                        List<MatchLocation> locations = tagger.find(attribute, getQuery(), true, criteria.getOptions());
                        if (!locations.isEmpty()) {
                           if (matchedAttributes == null) {
                              matchedAttributes = new HashMap<AttributeReadable<?>, List<MatchLocation>>();
                           }
                           matchedAttributes.put(attribute, locations);
                        }
                     }
                  }
               } catch (Exception ex) {
                  getLogger().error(ex, "Error processing: [%s]", attribute);
               }
            }
            if (matchedAttributes != null && !matchedAttributes.isEmpty()) {
               results.add(new ArtifactMatch(artifact, matchedAttributes));
               matchedAttributes = null;
            }
         }
         return results;
      }

      private List<AttributeReadable<Object>> getAttributes(ArtifactReadable artifact, DeletionFlag includeDeleted) throws OseeCoreException {
         List<AttributeReadable<Object>> toReturn;

         Collection<? extends IAttributeType> toCheck = getTypes();
         if (toCheck != null && !toCheck.isEmpty()) {
            toReturn = new ArrayList<AttributeReadable<Object>>();
            for (IAttributeType attributeType : toCheck) {
               for (AttributeReadable<Object> attr : artifact.getAttributes(attributeType, includeDeleted)) {
                  toReturn.add(attr);
                  checkForCancelled();
               }
            }
         } else {
            toReturn = artifact.getAttributes(includeDeleted);
         }
         return toReturn;
      }
   }
}

Back to the top