Skip to main content
summaryrefslogtreecommitdiffstats
blob: 67cd68b1d81212b3957d2c931125a92113b88db2 (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
/*******************************************************************************
 * 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.define.relation.Import;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osee.framework.jdk.core.util.io.xml.ExcelSaxHandler;
import org.eclipse.osee.framework.jdk.core.util.io.xml.RowProcessor;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.SkynetActivator;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactPersistenceManager;
import org.eclipse.osee.framework.skynet.core.artifact.Branch;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.relation.CoreRelationEnumeration;
import org.eclipse.osee.framework.skynet.core.utility.Requirements;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author Ryan D. Brooks
 */
public class RelationImporter implements RowProcessor {
   private static final ArtifactPersistenceManager artifactManager = ArtifactPersistenceManager.getInstance();
   private static final int leadingColumnCount = 2;
   private final ExcelSaxHandler excelHandler;
   private final XMLReader xmlReader;
   private Artifact[] columnArtifacts;
   private IProgressMonitor monitor;
   private boolean done;
   private final Branch branch;

   /**
    * @throws SAXException
    */
   public RelationImporter(Branch branch) throws SAXException {
      this.branch = branch;
      excelHandler = new ExcelSaxHandler(this, true, true);

      xmlReader = XMLReaderFactory.createXMLReader();
      xmlReader.setContentHandler(excelHandler);
   }

   public void extractRelationsFromSheet(InputStream importStream, IProgressMonitor monitor) throws IOException, SAXException {
      this.monitor = monitor;
      xmlReader.parse(new InputSource(importStream));
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#processRow(java.lang.String[])
    */
   public void processRow(String[] row) {
      if (done) return;
      try {
         monitor.worked(1);
         Collection<Artifact> artifacts =
               ArtifactQuery.getArtifactsFromTypeAndAttribute(Requirements.SUBSYSTEM_REQUIREMENT,
                     "Imported Paragraph Number", row[1], branch);

         Artifact rowArtifact;
         try {
            rowArtifact = getSoleArtifact(artifacts);
         } catch (IllegalArgumentException ex) {
            OseeLog.log(SkynetActivator.class, Level.SEVERE, ex);
            return;
         }

         if (rowArtifact == null) {
            System.out.println("Skipping " + row[0] + " becuase no matching artifact was found");
         } else {
            if (!row[0].equals(rowArtifact.getDescriptiveName())) {
               System.out.printf("Warning %s != %s%n", row[0], rowArtifact.getDescriptiveName());
            }
            monitor.subTask(rowArtifact.getDescriptiveName());
            for (int i = 0; i < columnArtifacts.length; i++) {
               String rationale = row[i + leadingColumnCount];
               if (rationale != null) {
                  if (rationale.equalsIgnoreCase("x")) {
                     rationale = "";
                  }
                  columnArtifacts[i].addRelation(CoreRelationEnumeration.ALLOCATION__REQUIREMENT, rowArtifact,
                        rationale);
                  columnArtifacts[i].persistRelations();
               }
            }
         }
      } catch (Exception ex) {
         OseeLog.log(SkynetActivator.class, Level.SEVERE, ex);
      }
   }

   private Artifact getSoleArtifact(Collection<Artifact> artifacts) {
      Artifact artifactResult = null;
      boolean soleArtifact = true;
      for (Artifact artifact : artifacts) {
         if (soleArtifact) {
            soleArtifact = false;
         } else {
            throw new IllegalArgumentException("Found more than one match for: " + artifact);
         }
         artifactResult = artifact;
      }
      return artifactResult;
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#processHeaderRow(java.lang.String[])
    */
   public void processHeaderRow(String[] row) {
      monitor.setTaskName("Aquire Column Artifacts");
      columnArtifacts = new Artifact[row.length - leadingColumnCount];
      for (int i = 0; i < columnArtifacts.length; i++) {
         monitor.worked(1);
         try {
            Collection<Artifact> artifacts =
                  ArtifactQuery.getArtifactsFromTypeAndName(Requirements.COMPONENT, row[i + leadingColumnCount], branch);

            columnArtifacts[i] = getSoleArtifact(artifacts);
            monitor.subTask(columnArtifacts[i].getDescriptiveName());
         } catch (Exception ex) {
            System.out.println(ex);
         }
      }
      System.out.println(Arrays.deepToString(columnArtifacts));
      monitor.setTaskName("Relate Row Artifacts");
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#processEmptyRow()
    */
   public void processEmptyRow() {
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#processCommentRow(java.lang.String[])
    */
   public void processCommentRow(String[] row) {
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#reachedEndOfWorksheet()
    */
   public void reachedEndOfWorksheet() {
      monitor.done();
      done = true;
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#detectedTotalRowCount(int)
    */
   public void detectedRowAndColumnCounts(int rowCount, int columnCount) {
      monitor.beginTask("Importing Relations", rowCount + columnCount - leadingColumnCount);
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.define.artifact.Import.RowProcessor#foundStartOfWorksheet(java.lang.String)
    */
   public void foundStartOfWorksheet(String sheetName) {
   }
}

Back to the top