Skip to main content
summaryrefslogtreecommitdiffstats
blob: 935f7ac5de3468b31d2479613390960fa672f785 (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
/*******************************************************************************
 * 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.database.init;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osee.framework.core.client.OseeClientProperties;
import org.eclipse.osee.framework.core.enums.BranchArchivedState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.database.init.internal.DatabaseInitActivator;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.ExtensionPoints;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.exportImport.HttpBranchExchange;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public class SkynetDbBranchDataImport implements IDbInitializationTask {
   private static final String ELEMENT_NAME = "OseeDbImportData";
   private static final String EXTENSION_POINT = DatabaseInitActivator.PLUGIN_ID + "." + ELEMENT_NAME;
   private static final String BRANCH_NAME = "branchName";
   private static final String BRANCH_DATA = "branchData";
   private static final String BRANCHES_TO_IMPORT = "BranchesToImport";

   @Override
   public void run() throws OseeCoreException {
      if (OseeClientProperties.isOseeImportAllowed()) {
         // Clean up and delete all branches except Common
         for (Branch branch : BranchManager.getBranches(BranchArchivedState.UNARCHIVED, BranchType.WORKING,
               BranchType.BASELINE)) {
            if (!branch.equals(CoreBranches.COMMON)) {
               BranchManager.purgeBranch(branch);
            }
         }

         Collection<ImportData> importDatas = loadDataFromExtensions();
         for (ImportData importData : importDatas) {
            OseeLog.log(DatabaseInitActivator.class, Level.INFO, String.format("Import Branch Data: [%s]", importData));
            try {
               File importFile = importData.getExchangeFile();
               //TODO not yet supported               importData.getSelectedBranches();
               HttpBranchExchange.importBranches(importFile.toURI().toASCIIString(), true, true);
            } catch (OseeDataStoreException ex) {
               OseeLog.log(DatabaseInitActivator.class, Level.SEVERE, String.format(
                     "Exception while importing branch: [%s]", importData), ex);
               throw ex;
            }
         }
      }
   }

   private Collection<ImportData> loadDataFromExtensions() throws OseeDataStoreException {
      List<ImportData> toReturn = new ArrayList<ImportData>();
      Map<String, String> selectedBranches = new HashMap<String, String>();
      List<IConfigurationElement> elements = ExtensionPoints.getExtensionElements(EXTENSION_POINT, ELEMENT_NAME);
      for (IConfigurationElement element : elements) {
         String bundleName = element.getContributor().getName();
         String branchData = element.getAttribute(BRANCH_DATA);

         if (Strings.isValid(bundleName) && Strings.isValid(branchData)) {
            File exchangeFile;
            try {
               exchangeFile = getExchangeFile(bundleName, branchData);
            } catch (Exception ex) {
               throw new OseeDataStoreException(ex);
            }
            ImportData importData = new ImportData(exchangeFile);
            for (IConfigurationElement innerElement : element.getChildren(BRANCHES_TO_IMPORT)) {
               String branchName = innerElement.getAttribute(BRANCH_NAME);
               if (Strings.isValid(branchName)) {
                  importData.addSelectedBranch(branchName);
                  if (!selectedBranches.containsKey(branchName.toLowerCase())) {
                     selectedBranches.put(branchName.toLowerCase(),
                           element.getDeclaringExtension().getUniqueIdentifier());
                  } else {
                     throw new OseeDataStoreException(
                           String.format(
                                 "Branch import error - cannot import twice into a branch - [%s] was already specified by [%s] ",
                                 branchName, selectedBranches.get(branchName.toLowerCase())));
                  }
               }
            }
            toReturn.add(importData);
         } else {
            throw new OseeDataStoreException(String.format("Branch import error: [%s] attributes were empty.",
                  element.getDeclaringExtension().getExtensionPointUniqueIdentifier()));
         }
      }
      return toReturn;
   }

   private File getExchangeFile(String bundleName, String exchangeFile) throws IOException, URISyntaxException {
      if (exchangeFile.endsWith("zip") != true) {
         throw new IOException(String.format("Branch data file is invalid [%s] ", exchangeFile));
      }
      Bundle bundle = Platform.getBundle(bundleName);
      URL url = bundle.getResource(exchangeFile);
      url = FileLocator.toFileURL(url);
      String urlValue = url.toString();
      URI uri = new URI(urlValue.replaceAll(" ", "%20"));
      File toReturn = new File(uri);
      if (toReturn.exists() != true) {
         throw new FileNotFoundException(String.format("Branch data file cannot be found [%s]", exchangeFile));
      }
      return toReturn;
   }

   private static final class ImportData {
      private final File exchangeFile;
      private final Set<String> selectedBranches;

      public ImportData(File exchangeFile) {
         super();
         this.exchangeFile = exchangeFile;
         this.selectedBranches = new HashSet<String>();
      }

      public void addSelectedBranch(String branchName) {
         this.selectedBranches.add(branchName);
      }

      @Override
      public String toString() {
         return String.format("%s - %s", exchangeFile, selectedBranches);
      }

      public File getExchangeFile() {
         return exchangeFile;
      }
   }
}

Back to the top