Skip to main content
summaryrefslogtreecommitdiffstats
blob: b6cea0f18e6aa72f3e8e2de807eefcb5188f4962 (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
/*******************************************************************************
 * 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.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.branch.management.exchange.handler.ExportItem;
import org.eclipse.osee.framework.branch.management.exchange.transform.ExchangeDataProcessor;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.xml.Xml;
import org.eclipse.osee.logger.Log;

/**
 * @author Roberto E. Escobar
 */
public class ExchangeIntegrity {
   private final OseeServices services;
   private final IOseeExchangeDataProvider exportDataProvider;
   private final ExchangeDataProcessor processor;
   private final List<ReferentialIntegrityConstraint> constraints = new ArrayList<ReferentialIntegrityConstraint>();
   private String verifyFile;

   public ExchangeIntegrity(OseeServices services, IOseeExchangeDataProvider exportDataProvider, ExchangeDataProcessor processor) {
      this.services = services;
      this.exportDataProvider = exportDataProvider;
      this.processor = processor;
   }

   public String getExchangeCheckFileName() {
      return verifyFile;
   }

   private void initializeConstraints() {
      ReferentialIntegrityConstraint constraint;

      constraint = new ReferentialIntegrityConstraint(ExportItem.OSEE_TX_DETAILS_DATA, "transaction_id");
      constraint.addForeignKey(ExportItem.OSEE_BRANCH_DATA, "parent_transaction_id", "baseline_transaction_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_DATA, "transaction_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_ARCHIVED_DATA, "transaction_id");
      constraint.addForeignKey(ExportItem.OSEE_MERGE_DATA, "commit_transaction_id");
      constraints.add(constraint);

      constraint = new ReferentialIntegrityConstraint(ExportItem.OSEE_ARTIFACT_DATA, "art_id");
      constraint.addForeignKey(ExportItem.OSEE_TX_DETAILS_DATA, "author", "commit_art_id");
      constraint.addForeignKey(ExportItem.OSEE_ATTRIBUTE_DATA, "art_id");
      constraint.addForeignKey(ExportItem.OSEE_RELATION_LINK_DATA, "a_art_id", "b_art_id");
      constraint.addForeignKey(ExportItem.OSEE_ARTIFACT_ACL_DATA, "art_id", "privilege_entity_id");
      constraint.addForeignKey(ExportItem.OSEE_BRANCH_ACL_DATA, "privilege_entity_id");
      constraints.add(constraint);

      constraint = new ReferentialIntegrityConstraint(ExportItem.OSEE_ARTIFACT_DATA, "gamma_id");
      constraint.addPrimaryKey(ExportItem.OSEE_ATTRIBUTE_DATA, "gamma_id");
      constraint.addPrimaryKey(ExportItem.OSEE_RELATION_LINK_DATA, "gamma_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_DATA, "gamma_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_ARCHIVED_DATA, "gamma_id");
      constraint.addForeignKey(ExportItem.OSEE_CONFLICT_DATA, "source_gamma_id");
      constraint.addForeignKey(ExportItem.OSEE_CONFLICT_DATA, "dest_gamma_id");
      constraints.add(constraint);

      constraint = new ReferentialIntegrityConstraint(ExportItem.OSEE_BRANCH_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_BRANCH_DATA, "parent_branch_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_TXS_ARCHIVED_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_TX_DETAILS_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_ARTIFACT_ACL_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_BRANCH_ACL_DATA, "branch_id");
      constraint.addForeignKey(ExportItem.OSEE_MERGE_DATA, "source_branch_id");
      constraint.addForeignKey(ExportItem.OSEE_MERGE_DATA, "dest_branch_id");
      constraint.addForeignKey(ExportItem.OSEE_MERGE_DATA, "merge_branch_id");
      constraint.addForeignKey(ExportItem.OSEE_CONFLICT_DATA, "merge_branch_id");
      constraints.add(constraint);
   }

   public void execute() throws OseeCoreException {
      long startTime = System.currentTimeMillis();

      initializeConstraints();

      Writer writer = null;
      try {
         writer = openResults();

         Log logger = services.getLogger();
         for (ReferentialIntegrityConstraint constraint : constraints) {
            logger.info("Verifing constraint [%s]", constraint.getPrimaryKeyListing());

            constraint.checkConstraint(logger, services.getDatabaseService(), processor);
            writeConstraintResults(writer, constraint);
         }
         ExportImportXml.closeXmlNode(writer, ExportImportXml.DATA);
      } catch (IOException ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         Lib.close(writer);
         processor.cleanUp();
         services.getLogger().info("Verified [%s] in [%s]", exportDataProvider.getExportedDataRoot(),
            Lib.getElapseString(startTime));
      }
   }

   private void writeConstraintResults(Writer writer, ReferentialIntegrityConstraint constraint) throws IOException {
      HashCollection<String, Long> missingPrimaryKeys = constraint.getMissingPrimaryKeys();

      Set<Long> unreferencedPrimaryKeys = constraint.getUnreferencedPrimaryKeys();
      boolean passedCheck = missingPrimaryKeys.isEmpty() && unreferencedPrimaryKeys.isEmpty();

      writer.append("\t");
      ExportImportXml.openPartialXmlNode(writer, ExportImportXml.PRIMARY_KEY);
      ExportImportXml.addXmlAttribute(writer, ExportImportXml.ID, constraint.getPrimaryKeyListing());
      ExportImportXml.addXmlAttribute(writer, "status", passedCheck ? "OK" : "FAILED");

      if (passedCheck) {
         ExportImportXml.closePartialXmlNode(writer);
      } else {
         ExportImportXml.endOpenedPartialXmlNode(writer);
         writer.append("\t\t");
         ExportImportXml.openXmlNode(writer, ExportImportXml.UNREFERENCED_PRIMARY_KEY);
         Xml.writeAsCdata(writer, "\t\t\t" + unreferencedPrimaryKeys.toString());
         writer.append("\n\t\t");
         ExportImportXml.closeXmlNode(writer, ExportImportXml.UNREFERENCED_PRIMARY_KEY);

         for (String foreignKey : missingPrimaryKeys.keySet()) {
            writer.append("\t\t");
            ExportImportXml.openPartialXmlNode(writer, "ForeignKey");
            ExportImportXml.addXmlAttribute(writer, ExportImportXml.ID, foreignKey);
            ExportImportXml.endOpenedPartialXmlNode(writer);
            Xml.writeAsCdata(writer, "\t\t\t" + missingPrimaryKeys.getValues(foreignKey).toString());
            writer.append("\n\t\t");
            ExportImportXml.closeXmlNode(writer, "ForeignKey");
         }
         writer.append("\t");
         ExportImportXml.closeXmlNode(writer, ExportImportXml.PRIMARY_KEY);
      }
   }

   private Writer openResults() throws IOException {
      verifyFile = exportDataProvider.getExportedDataRoot().getName() + ".verify.xml";
      File writeLocation = exportDataProvider.getExportedDataRoot().getParentFile();

      Writer writer = ExchangeUtil.createXmlWriter(writeLocation, verifyFile, (int) Math.pow(2, 20));
      ExportImportXml.openXmlNode(writer, ExportImportXml.DATA);
      return writer;
   }
}

Back to the top