diff options
author | Ryan G. Rader | 2014-10-14 22:24:27 +0000 |
---|---|---|
committer | jmisinco | 2014-11-04 16:43:23 +0000 |
commit | bfa253f40a9465ce9729e1196fefc9493256d0d9 (patch) | |
tree | 9447f47102a833d57f2c1aa5b8dc0830cb64ee4c | |
parent | 713f8fe98592217d544be941aea19f59681546e6 (diff) | |
download | org.eclipse.osee-bfa253f40a9465ce9729e1196fefc9493256d0d9.tar.gz org.eclipse.osee-bfa253f40a9465ce9729e1196fefc9493256d0d9.tar.xz org.eclipse.osee-bfa253f40a9465ce9729e1196fefc9493256d0d9.zip |
feature[ats_ATS98067]: Import Code Data Rights
Change-Id: I4c7a796a04277a31a9bd88bbc9aeaf48ab264c71
Signed-off-by: Ryan G. Rader <ryan.rader@boeing.com>
2 files changed, 119 insertions, 51 deletions
diff --git a/plugins/org.eclipse.osee.define/src/org/eclipse/osee/define/blam/operation/DataRightsImporter.java b/plugins/org.eclipse.osee.define/src/org/eclipse/osee/define/blam/operation/DataRightsImporter.java index 0b7343768cf..72b7d1ad20a 100644 --- a/plugins/org.eclipse.osee.define/src/org/eclipse/osee/define/blam/operation/DataRightsImporter.java +++ b/plugins/org.eclipse.osee.define/src/org/eclipse/osee/define/blam/operation/DataRightsImporter.java @@ -14,18 +14,22 @@ import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.CodeUnit; import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.DataRightsBasis; import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.DataRightsClassification; import static org.eclipse.osee.framework.core.enums.CoreAttributeTypes.SubjectMatterExpert; -import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.osee.framework.core.enums.QueryOption; -import org.eclipse.osee.framework.core.model.Branch; +import org.eclipse.osee.framework.core.data.IOseeBranch; +import org.eclipse.osee.framework.jdk.core.type.ResultSet; +import org.eclipse.osee.framework.jdk.core.util.Lib; import org.eclipse.osee.framework.jdk.core.util.Strings; 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.skynet.core.artifact.Artifact; import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery; +import org.eclipse.osee.framework.skynet.core.artifact.search.QueryBuilderArtifact; import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction; import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager; import org.eclipse.osee.framework.ui.skynet.blam.AbstractBlam; @@ -38,9 +42,7 @@ import org.xml.sax.helpers.XMLReaderFactory; * @author Ryan Rader */ public class DataRightsImporter extends AbstractBlam { - - public Branch branch; - public SkynetTransaction transaction; + private Map<String, Artifact> nameToArtifact; @Override public Collection<String> getCategories() { @@ -49,32 +51,48 @@ public class DataRightsImporter extends AbstractBlam { @Override public String getXWidgetsXml() { - return "<XWidgets><XWidget xwidgetType=\"XBranchSelectWidget\" horizontalLabel=\"true\" displayName=\"Branch\" /><XWidget xwidgetType=\"XText\" displayName=\"Path to DataRights XML\" /></XWidgets>"; + StringBuffer buffer = new StringBuffer("<xWidgets>"); + buffer.append("<XWidget xwidgetType=\"XBranchSelectWidget\" horizontalLabel=\"true\" displayName=\"Branch\" />"); + buffer.append("<XWidget xwidgetType=\"XFileSelectionDialog\" displayName=\"Path to DataRights XML\" />"); + buffer.append("</xWidgets>"); + return buffer.toString(); } @Override public void runOperation(VariableMap variableMap, IProgressMonitor monitor) throws Exception { - // Get branch by name from blam - branch = variableMap.getBranch("Branch"); + IOseeBranch branch = variableMap.getBranch("Branch"); String xmlPath = variableMap.getString("Path to DataRights XML"); + if (branch == null) { log(String.format("A branch needs to be defined.")); - return; - } - - if (!Strings.isValid(xmlPath)) { + } else if (!Strings.isValid(xmlPath)) { log(String.format("A path needs to be defined.")); - return; - } - - transaction = TransactionManager.createTransaction(branch, "Data Rights Importer"); - - File path = new File(xmlPath); - XMLReader xmlReader = XMLReaderFactory.createXMLReader(); - xmlReader.setContentHandler(new ExcelSaxHandler(new DataRightsProcessor(), true)); - xmlReader.parse(new InputSource(new FileInputStream(path))); + } else { + log("path [" + xmlPath + "]"); + + if (nameToArtifact == null) { + QueryBuilderArtifact builder = ArtifactQuery.createQueryBuilder(branch); + builder.andIsOfType(CodeUnit); + ResultSet<Artifact> results = builder.getResults(); + nameToArtifact = new HashMap<String, Artifact>(); + for (Artifact artifact : results) { + nameToArtifact.put(artifact.getName(), artifact); + } + } - transaction.execute(); + XMLReader xmlReader = XMLReaderFactory.createXMLReader(); + DataRightsProcessor processor = null; + InputStream inputStream = null; + try { + inputStream = new FileInputStream(xmlPath); + processor = new DataRightsProcessor(branch); + xmlReader.setContentHandler(new ExcelSaxHandler(processor, true)); + xmlReader.parse(new InputSource(inputStream)); + } finally { + Lib.close(inputStream); + } + processor.persist(); + } } @Override @@ -82,13 +100,35 @@ public class DataRightsImporter extends AbstractBlam { return "Data Rights Importer"; } - public class DataRightsProcessor implements RowProcessor { + @Override + public String getDescriptionUsage() { + return "This BLAM imports data rights attributes (SME, Classification and Basis) from a XML spreadsheet into existing Artifacts in ATS"; + } + + private final class DataRightsProcessor implements RowProcessor { + + private final IOseeBranch branch; - private boolean ignore = false; + private boolean ignore; private int smeIndex; private int dataRightsIndex; private int fileNameIndex; private int dataRightsBasisIndex; + private boolean changesAvailable; + private SkynetTransaction transaction; + + public DataRightsProcessor(IOseeBranch branch) { + super(); + this.branch = branch; + } + + private SkynetTransaction getTransaction() { + if (transaction == null) { + transaction = TransactionManager.createTransaction(branch, "Data Rights Importer"); + changesAvailable = true; + } + return transaction; + } @Override public void processRow(String[] row) throws Exception { @@ -97,36 +137,54 @@ public class DataRightsImporter extends AbstractBlam { String dataRightsClassification = row[dataRightsIndex]; String fileName = row[fileNameIndex]; String dataRightsBasis = row[dataRightsBasisIndex]; - - Artifact artifact = - ArtifactQuery.getArtifactFromTypeAndName(CodeUnit, fileName, branch, QueryOption.CONTAINS_MATCH_OPTIONS); - - if (artifact == null) { + if (Strings.isValid(SME)) { + SME = "Unspecified"; + } + if (Strings.isValid(dataRightsClassification)) { + dataRightsClassification = "Unspecified"; + } + if (Strings.isValid(dataRightsBasis)) { + dataRightsBasis = "Unspecified"; + } + try { + Artifact artifact = getArtifactByName(fileName); + if (artifact == null) { + log("artifact [" + fileName + "] does not exist"); + } else { + artifact.setSoleAttributeValue(DataRightsClassification, dataRightsClassification); + artifact.setSoleAttributeValue(DataRightsBasis, dataRightsBasis); + artifact.setSoleAttributeValue(SubjectMatterExpert, SME); + + SkynetTransaction transaction = getTransaction(); + artifact.persist(transaction); + } + } catch (Exception ex) { + log("dataRightsClassification [" + dataRightsClassification + "] does not exist"); + log("dataRightsBasis [" + dataRightsBasis + "] does not exist"); + log("SME [" + SME + "] does not exist"); log("artifact [" + fileName + "] does not exist"); - - } else { - artifact.setSoleAttributeValue(DataRightsClassification, dataRightsClassification); - artifact.setSoleAttributeValue(DataRightsBasis, dataRightsBasis); - artifact.setSoleAttributeValue(SubjectMatterExpert, SME); - - artifact.persist(transaction); + log(ex); } + } } + public Artifact getArtifactByName(String name) { + return nameToArtifact.get(name); + } + @Override public void processHeaderRow(String[] row) { - for (int i = 0; i < row.length; i++) { - if (row[i] != null) { - if (row[i].equals("SME")) { - smeIndex = i; - } else if (row[i].equals("Classification")) { - dataRightsIndex = i; - } else if (row[i].equals("Code Unit")) { - fileNameIndex = i; - } else if (row[i].equals("Basis")) { - dataRightsBasisIndex = i; - } + for (int index = 0; index < row.length; index++) { + String header = row[index]; + if ("SME".equals(header)) { + smeIndex = index; + } else if ("Classification".equals(header)) { + dataRightsIndex = index; + } else if ("Virtual Path (formula)".equals(header)) { + fileNameIndex = index; + } else if ("Basis".equals(header)) { + dataRightsBasisIndex = index; } } } @@ -134,11 +192,12 @@ public class DataRightsImporter extends AbstractBlam { @Override public void reachedEndOfWorksheet() { ignore = true; + } @Override public void processEmptyRow() { - // do nothing + ignore = true; } @Override @@ -156,5 +215,11 @@ public class DataRightsImporter extends AbstractBlam { // do nothing } + public void persist() { + if (changesAvailable) { + getTransaction().execute(); + } + } + } } diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelSaxHandler.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelSaxHandler.java index c2b90f1525b..eca886b2bd6 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelSaxHandler.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelSaxHandler.java @@ -60,7 +60,7 @@ public class ExcelSaxHandler extends AbstractSaxHandler { } } else if (localName.equalsIgnoreCase("Cell")) { String indexStr = attributes.getValue("ss:Index"); - cellData = new StringBuilder(); + cellData = null; inCellData = true; if (indexStr != null) { cellIndex = Integer.parseInt(indexStr) - 1; // translate from Excel's 1-based index to @@ -90,9 +90,12 @@ public class ExcelSaxHandler extends AbstractSaxHandler { @Override public void endElementFound(String uri, String localName, String qName) throws Exception { if (inCellData) { + if (cellData == null) { + cellData = new StringBuilder(); + } cellData.append(getContents()); } - if (localName.equalsIgnoreCase("Data")) { + if (localName.equalsIgnoreCase("Data") && cellData != null) { inCellData = false; String contentStr = cellData.toString(); cellData = null; |