Skip to main content
summaryrefslogtreecommitdiffstats
blob: e97106d196d85cc8bcd177620589ac87e39120d7 (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
/*******************************************************************************
 * Copyright (c) 2010 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.transform;

import java.io.File;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.eclipse.osee.framework.branch.management.TxCurrentsAndModTypesCommand;
import org.eclipse.osee.framework.branch.management.exchange.ExchangeUtil;
import org.eclipse.osee.framework.branch.management.exchange.handler.ExportItem;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.operation.OperationReporter;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.database.operation.Address;
import org.eclipse.osee.framework.jdk.core.text.rules.ReplaceAll;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.osgi.framework.Version;

/**
 * @author Roberto E. Escobar
 */
public class V0_9_2Transformer implements IOseeExchangeVersionTransformer {
   private static final Version MAX_VERSION = new Version("0.9.2");

   private final Map<ModificationType, ModificationType[]> allowedStates =
      new HashMap<ModificationType, ModificationType[]>();

   public V0_9_2Transformer() {
      ModificationType[] FROM_NEW_OR_INTRODUCED =
         new ModificationType[] {ModificationType.DELETED, ModificationType.MERGED};
      ModificationType[] END_STATE = new ModificationType[0];
      allowedStates.put(ModificationType.NEW, FROM_NEW_OR_INTRODUCED);
      allowedStates.put(ModificationType.INTRODUCED, FROM_NEW_OR_INTRODUCED);
      allowedStates.put(ModificationType.MERGED, new ModificationType[] {ModificationType.DELETED});
      allowedStates.put(ModificationType.DELETED, END_STATE);
      allowedStates.put(ModificationType.MODIFIED, END_STATE);
   }

   @Override
   public Version applyTransform(ExchangeDataProcessor processor, OperationReporter reporter) throws OseeCoreException {
      List<Integer> branchIds = convertBranchTable(processor);

      Map<Long, Long> artifactGammaToNetGammaId = convertArtifactAndConflicts(processor);
      consolidateTxsAddressing(processor, ExportItem.OSEE_TXS_DATA, branchIds, artifactGammaToNetGammaId);

      HashCollection<String, String> tableToColumns = new HashCollection<String, String>();
      tableToColumns.put("osee_artifact", "<column id=\"gamma_id\" type=\"NUMERIC\" />\n");
      tableToColumns.put("osee_branch", "<column id=\"baseline_transaction_id\" type=\"INTEGER\" />\n");
      processor.transform(ExportItem.EXPORT_DB_SCHEMA, new DbSchemaRuleAddColumn(tableToColumns));

      processor.transform(ExportItem.EXPORT_MANIFEST, new ReplaceAll("<entry id=\"osee.artifact.version.data.xml[^<]+",
         ""));
      processor.deleteExportItem("osee.artifact.version.data.xml");
      return getMaxVersion();
   }

   @Override
   public Version getMaxVersion() {
      return MAX_VERSION;
   }

   @Override
   public void finalizeTransform(ExchangeDataProcessor processor, OperationReporter reporter) throws OseeCoreException {
      Operations.executeWorkAndCheckStatus(new TxCurrentsAndModTypesCommand(reporter, false));
      Operations.executeWorkAndCheckStatus(new TxCurrentsAndModTypesCommand(reporter, true));
   }

   private List<Integer> convertBranchTable(ExchangeDataProcessor processor) throws OseeCoreException {
      Map<Integer, Integer> branchToBaseTx = new HashMap<Integer, Integer>(10000);
      processor.parse(ExportItem.OSEE_TX_DETAILS_DATA, new V0_9_2TxDetailsHandler(branchToBaseTx));
      processor.transform(ExportItem.OSEE_BRANCH_DATA, new V0_9_2BranchTransformer(branchToBaseTx));
      return new ArrayList<Integer>(branchToBaseTx.keySet());
   }

   private Map<Long, Long> convertArtifactAndConflicts(ExchangeDataProcessor processor) throws OseeCoreException {
      V0_9_2ArtifactVersionHandler handler = new V0_9_2ArtifactVersionHandler();
      processor.parse("osee.artifact.version.data.xml", handler);
      Map<Long, Long> artifactGammaToNetGammaId = handler.getArtifactGammaToNetGammaId();
      Map<Integer, Long> artIdToNetGammaId = handler.getArtIdToNetGammaId();

      processor.transform(ExportItem.OSEE_ARTIFACT_DATA, new V0_9_2ArtifactDataTransformer(artIdToNetGammaId));
      processor.transform(ExportItem.OSEE_CONFLICT_DATA, new V0_9_2ConflictTransformer(artifactGammaToNetGammaId));

      return artifactGammaToNetGammaId;
   }

   private void consolidateTxsAddressing(ExchangeDataProcessor processor, ExportItem exportItem, List<Integer> branchIds, Map<Long, Long> artifactGammaToNetGammaId) throws OseeCoreException {
      File targetFile = processor.getDataProvider().getFile(exportItem);
      File tempFile = new File(Lib.changeExtension(targetFile.getPath(), "temp"));
      Writer fileWriter = null;
      HashCollection<Long, Address> addressMap = new HashCollection<Long, Address>(false, TreeSet.class);
      V0_9_2TxsConsolidateParser transformer = new V0_9_2TxsConsolidateParser(artifactGammaToNetGammaId, addressMap);
      try {
         fileWriter = processor.startTransform(targetFile, tempFile, transformer);
         ExchangeUtil.readExchange(tempFile, transformer);

         for (int branchId : branchIds) {
            transformer.setBranchId(branchId);
            ExchangeUtil.readExchange(tempFile, transformer);

            for (Long gammaId : addressMap.keySet()) {
               Collection<Address> addresses = addressMap.getValues(gammaId);
               fixAddressing(addresses);
               writeAddresses(transformer.getWriter(), addresses);
            }
            addressMap.clear();
         }
         tempFile.delete();
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         try {
            transformer.finish();
         } catch (Exception ex) {
            OseeExceptions.wrapAndThrow(ex);
         } finally {
            Lib.close(fileWriter);
         }
      }
   }

   private void writeAddresses(XMLStreamWriter writer, Collection<Address> addresses) throws XMLStreamException {
      for (Address address : addresses) {
         writer.writeStartElement("entry");
         writer.writeAttribute("branch_id", String.valueOf(address.getBranchId()));
         writer.writeAttribute("gamma_id", String.valueOf(address.getGammaId()));
         writer.writeAttribute("tx_current", String.valueOf(address.getCorrectedTxCurrent().getValue()));
         writer.writeAttribute("mod_type", String.valueOf(address.getModType().getValue()));
         writer.writeAttribute("transaction_id", String.valueOf(address.getTransactionId()));
         writer.writeEndElement();
      }
   }

   private void fixAddressing(Collection<Address> addresses) throws OseeStateException {
      Iterator<Address> iterator = addresses.iterator();

      Address previousAddress = iterator.next();
      if (previousAddress.getModType() == ModificationType.MODIFIED) {
         previousAddress.setModType(ModificationType.NEW);
      }

      while (iterator.hasNext()) {
         previousAddress.setCorrectedTxCurrent(TxChange.NOT_CURRENT);
         Address address = iterator.next();
         ModificationType[] nextValidStates = getNextPossibleStates(previousAddress.getModType());
         if (!address.getModType().matches(nextValidStates)) {
            iterator.remove();
         }
         previousAddress = address;
      }
      previousAddress.setCorrectedTxCurrent(TxChange.getCurrent(previousAddress.getModType()));
   }

   private ModificationType[] getNextPossibleStates(ModificationType state) throws OseeStateException {
      ModificationType[] nextAllowed = allowedStates.get(state);
      if (nextAllowed == null) {
         throw new OseeStateException(String.format("Unexcepted modification type [%s]", state.toString()));
      }
      return nextAllowed;
   }
}

Back to the top