Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad0147cc2d18036b565b0db9cee3fa16bd57892f (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
/*******************************************************************************
 * 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.branch.management.exchange;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.OseeConnection;

/**
 * @author Roberto E. Escobar
 */
public class IdTranslator {
   private static final String INSERT_INTO_IMPORT_INDEX_MAP =
      "INSERT INTO osee_import_index_map (sequence_id, original_id, mapped_id) VALUES (?, ?, ?)";

   private static final String SELECT_IDS_BY_DB_SOURCE_AND_SEQ_NAME =
      "SELECT original_id, mapped_id FROM osee_import_source ois, osee_import_map oim, osee_import_index_map oiim WHERE ois.import_id = oim.import_id AND oim.sequence_id = oiim.sequence_id AND oiim.sequence_id = oiim.sequence_id AND ois.db_source_guid = ?  AND oim.sequence_name = ?";

   private final String sequenceName;
   private final Map<Long, Long> originalToMapped;
   private final List<Long> newIds;
   private final Set<String> aliases;
   private final IOseeDatabaseService service;

   IdTranslator(IOseeDatabaseService service, String sequenceName, String... aliases) {
      this.service = service;
      this.sequenceName = sequenceName;
      this.originalToMapped = new HashMap<Long, Long>();
      this.newIds = new ArrayList<Long>();
      this.aliases = new HashSet<String>();
      if (aliases != null && aliases.length > 0) {
         for (String alias : aliases) {
            this.aliases.add(alias.toLowerCase());
         }
      }
   }

   public boolean hasAliases() {
      return this.aliases.size() > 0;
   }

   public Set<String> getAliases() {
      return this.aliases;
   }

   public Object getId(Object original) throws OseeCoreException {
      Long originalLong = null;
      if (original instanceof Double) {
         originalLong = ((Double) original).longValue();
      } else if (original instanceof Integer) {
         originalLong = ((Integer) original).longValue();
      } else if (original instanceof Long) {
         originalLong = ((Long) original).longValue();
      } else if (original instanceof BigInteger) {
         originalLong = ((BigInteger) original).longValue();
      } else if (original instanceof BigDecimal) {
         originalLong = ((BigDecimal) original).longValue();
      } else {
         throw new OseeCoreException("Undefined Type [%s]",
            original != null ? original.getClass().getSimpleName() : original);
      }
      Long newVersion = transalateId(originalLong);
      Object toReturn = newVersion;
      if (original instanceof Double) {
         toReturn = Double.valueOf(newVersion);
      } else if (original instanceof Integer) {
         toReturn = newVersion.intValue();
      } else if (original instanceof Long) {
         toReturn = newVersion;
      } else if (original instanceof BigInteger) {
         toReturn = BigInteger.valueOf(newVersion);
      } else if (original instanceof BigDecimal) {
         toReturn = BigDecimal.valueOf(newVersion);
      } else {
         throw new OseeCoreException("Undefined Type [%s]", original.getClass().getSimpleName());
      }
      return toReturn;
   }

   private Long transalateId(Long original) throws OseeCoreException {
      Long newVersion = null;
      if (original <= 0L) {
         newVersion = original;
      } else {
         newVersion = this.originalToMapped.get(original);
         if (newVersion == null) {
            newVersion = service.getSequence().getNextSequence(getSequence());
            addToCache(original, newVersion);
         }
      }
      return newVersion;
   }

   public String getSequence() {
      return this.sequenceName;
   }

   public void addToCache(Long original, Long newValue) {
      originalToMapped.put(original, newValue);
      newIds.add(original);
   }

   public Long getFromCache(Long original) {
      Long newVersion = null;
      if (original <= 0L) {
         newVersion = original;
      } else {
         newVersion = this.originalToMapped.get(original);
      }
      return newVersion;
   }

   public void load(String sourceDatabaseId) throws OseeCoreException {
      IOseeStatement chStmt = service.getStatement();
      try {
         originalToMapped.clear();
         chStmt.runPreparedQuery(SELECT_IDS_BY_DB_SOURCE_AND_SEQ_NAME, sourceDatabaseId, getSequence());
         while (chStmt.next()) {
            originalToMapped.put(chStmt.getLong("original_id"), chStmt.getLong("mapped_id"));
         }
      } finally {
         chStmt.close();
      }
   }

   public boolean hasItemsToStore() {
      return !newIds.isEmpty();
   }

   public void store(OseeConnection connection, int sequenceId) throws OseeCoreException {
      if (hasItemsToStore()) {
         List<Object[]> data = new ArrayList<Object[]>();
         for (Long original : newIds) {
            Long mapped = originalToMapped.get(original);
            data.add(new Object[] {sequenceId, original, mapped});
         }
         service.runBatchUpdate(connection, INSERT_INTO_IMPORT_INDEX_MAP, data);
      }
   }
}

Back to the top