Skip to main content
summaryrefslogtreecommitdiffstats
blob: e1d7b22158239bcd7e89fabc7168366cc21fcd63 (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
/*******************************************************************************
 * 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.framework.database.core;

import java.sql.Timestamp;
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.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.database.core.DatabaseJoinAccessor.JoinItem;
import org.eclipse.osee.framework.jdk.core.util.time.GlobalTime;

/**
 * @author Roberto E. Escobar
 */
public abstract class AbstractJoinQuery {

   protected static interface IJoinRow {
      Object[] toArray();

      @Override
      String toString();
   }

   private final IJoinAccessor joinAccessor;
   private final JoinItem joinItem;
   private final int queryId;

   private final Timestamp insertTime;
   protected final Set<IJoinRow> entries = new HashSet<IJoinRow>();

   private boolean wasStored;
   private int storedSize;

   protected AbstractJoinQuery(IJoinAccessor joinAccessor, JoinItem joinItem, int queryId) {
      this.joinAccessor = joinAccessor;
      this.joinItem = joinItem;
      this.queryId = queryId;
      this.insertTime = GlobalTime.GreenwichMeanTimestamp();
      this.storedSize = -1;
      this.wasStored = false;
   }

   public boolean isEmpty() {
      return this.wasStored != true ? entries.isEmpty() : this.storedSize > 0;
   }

   public int size() {
      return this.wasStored != true ? entries.size() : this.storedSize;
   }

   public int getQueryId() {
      return queryId;
   }

   public Timestamp getInsertTime() {
      return insertTime;
   }

   public String getJoinTableName() {
      return joinItem.getJoinTableName();
   }

   public boolean wasStored() {
      return wasStored;
   }

   public void store(OseeConnection connection) throws OseeCoreException {
      if (this.wasStored != true) {
         List<Object[]> data = new ArrayList<Object[]>();
         for (IJoinRow joinArray : entries) {
            data.add(joinArray.toArray());
         }
         joinAccessor.store(connection, joinItem, getQueryId(), data);
         this.storedSize = this.entries.size();
         this.wasStored = true;
         this.entries.clear();
      } else {
         throw new OseeDataStoreException("Cannot store query id twice");
      }
   }

   public int delete(OseeConnection connection) throws OseeCoreException {
      return joinAccessor.delete(connection, joinItem, getQueryId());
   }

   public void store() throws OseeCoreException {
      store(null);
   }

   public int delete() throws OseeCoreException {
      return delete(null);
   }

   public Collection<Integer> getAllQueryIds(OseeConnection connection) throws OseeCoreException {
      return joinAccessor.getAllQueryIds(connection, joinItem);
   }

   public Collection<Integer> getAllQueryIds() throws OseeCoreException {
      return joinAccessor.getAllQueryIds(null, joinItem);
   }

   @Override
   public String toString() {
      return String.format("id: [%s] entrySize: [%d]", getQueryId(), size());
   }
}

Back to the top