Skip to main content
summaryrefslogtreecommitdiffstats
blob: 66609009c0b767475d32e6773b6ef41ff2f2afbb (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) 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.db.internal.loader;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.osee.executor.admin.HasCancellation;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.AbstractJoinQuery;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.ds.DataLoader;
import org.eclipse.osee.orcs.core.ds.DataLoaderFactory;
import org.eclipse.osee.orcs.core.ds.Options;
import org.eclipse.osee.orcs.core.ds.OptionsUtil;
import org.eclipse.osee.orcs.core.ds.QueryContext;
import org.eclipse.osee.orcs.db.internal.loader.executors.AbstractLoadExecutor;
import org.eclipse.osee.orcs.db.internal.loader.executors.LoadExecutor;
import org.eclipse.osee.orcs.db.internal.loader.executors.QueryContextLoadExecutor;
import org.eclipse.osee.orcs.db.internal.search.QuerySqlContext;

/**
 * @author Roberto E. Escobar
 */
public class DataLoaderFactoryImpl implements DataLoaderFactory {

   private final Log logger;
   private final IOseeDatabaseService dbService;
   private final SqlObjectLoader loader;
   private final BranchCache branchCache;

   public DataLoaderFactoryImpl(Log logger, IOseeDatabaseService dbService, SqlObjectLoader loader, BranchCache branchCache) {
      super();
      this.logger = logger;
      this.dbService = dbService;
      this.loader = loader;
      this.branchCache = branchCache;
   }

   @Override
   public int getCount(HasCancellation cancellation, QueryContext queryContext) throws OseeCoreException {
      QuerySqlContext context = toSqlContext(queryContext);

      int count = -1;
      long startTime = 0;
      if (logger.isTraceEnabled()) {
         startTime = System.currentTimeMillis();
         logger.trace("%s Count - queryContext[%s]", getClass().getSimpleName(), queryContext);
      }

      try {
         for (AbstractJoinQuery join : context.getJoins()) {
            join.store();
         }
         if (cancellation != null) {
            cancellation.checkForCancelled();
         }
         count = dbService.runPreparedQueryFetchObject(-1, context.getSql(), context.getParameters().toArray());
      } finally {
         for (AbstractJoinQuery join : context.getJoins()) {
            try {
               join.delete();
            } catch (Exception ex) {
               // Do nothing
            }
         }
      }

      if (logger.isTraceEnabled()) {
         logger.trace("%s Count [%s] - count[%s] queryContext[%s]", getClass().getSimpleName(),
            Lib.getElapseString(startTime), count, queryContext);
      }
      return count;
   }

   @Override
   public DataLoader fromQueryContext(QueryContext queryContext) throws OseeCoreException {
      QuerySqlContext sqlQueryContext = toSqlContext(queryContext);
      AbstractLoadExecutor executor = new QueryContextLoadExecutor(loader, dbService, sqlQueryContext);
      Options options = OptionsUtil.createOptions();
      return new DataLoaderImpl(logger, executor, options);
   }

   @Override
   public DataLoader fromBranchAndArtifactIds(OrcsSession session, IOseeBranch branch, Collection<Integer> artifactIds) throws OseeCoreException {
      Conditions.checkNotNullOrEmpty(artifactIds, "artifactIds");

      int branchId = branchCache.getLocalId(branch);
      AbstractLoadExecutor executor = new LoadExecutor(loader, dbService, session, branchId, artifactIds);
      Options options = OptionsUtil.createOptions();
      return new DataLoaderImpl(logger, executor, options);
   }

   @Override
   public DataLoader fromBranchAndArtifactIds(OrcsSession session, IOseeBranch branch, int... artifactIds) throws OseeCoreException {
      return fromBranchAndArtifactIds(session, branch, toCollection(artifactIds));
   }

   private QuerySqlContext toSqlContext(QueryContext queryContext) throws OseeCoreException {
      QuerySqlContext sqlContext = null;
      if (queryContext instanceof QuerySqlContext) {
         sqlContext = (QuerySqlContext) queryContext;
      } else {
         throw new OseeCoreException("Invalid query context type [%s] - expected SqlContext",
            queryContext.getClass().getName());
      }
      return sqlContext;
   }

   private Collection<Integer> toCollection(int... ids) {
      Set<Integer> toReturn = new HashSet<Integer>();
      for (Integer id : ids) {
         toReturn.add(id);
      }
      return toReturn;
   }

}

Back to the top