Skip to main content
summaryrefslogtreecommitdiffstats
blob: def9a2657eb55d22980bb50cae6b8814ffa9ebe9 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * Copyright (c) 2012 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.orcs.db.internal.callable;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import org.eclipse.osee.executor.admin.ExecutionCallbackAdapter;
import org.eclipse.osee.executor.admin.ExecutorAdmin;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.ExportOptions;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.SystemPreferences;
import org.eclipse.osee.orcs.db.internal.exchange.ExchangeUtil;
import org.eclipse.osee.orcs.db.internal.exchange.ExportItemFactory;
import org.eclipse.osee.orcs.db.internal.exchange.export.AbstractExportItem;
import org.eclipse.osee.orcs.db.internal.resource.ResourceConstants;
import org.eclipse.osee.orcs.db.internal.sql.join.AbstractJoinQuery;
import org.eclipse.osee.orcs.db.internal.sql.join.ExportImportJoinQuery;
import org.eclipse.osee.orcs.db.internal.sql.join.SqlJoinFactory;

/**
 * @author Roberto E. Escobar
 */
public class ExportBranchDatabaseCallable extends AbstractDatastoreCallable<URI> {

   private static final String BRANCH_EXPORT_EXECUTOR_ID = "branch.export.worker";

   private final ExportItemFactory factory;

   private final SqlJoinFactory joinFactory;
   private final SystemPreferences preferences;
   private final ExecutorAdmin executorAdmin;
   private final List<IOseeBranch> branches;
   private final PropertyStore options;

   private String exportName;

   public ExportBranchDatabaseCallable(OrcsSession session, ExportItemFactory factory, SqlJoinFactory joinFactory, SystemPreferences preferences, ExecutorAdmin executorAdmin, List<IOseeBranch> branches, PropertyStore options, String exportName) {
      super(factory.getLogger(), session, factory.getDbService());
      this.joinFactory = joinFactory;
      this.factory = factory;
      this.preferences = preferences;
      this.executorAdmin = executorAdmin;
      this.branches = branches;
      this.options = options;
      this.exportName = exportName;
   }

   private SystemPreferences getSystemPreferences() {
      return preferences;
   }

   private ExecutorAdmin getExecutorAdmin() {
      return executorAdmin;
   }

   private String getExchangeFileName() {
      return this.exportName;
   }

   private void setExchangeFileName(String name) {
      this.exportName = name;
   }

   @Override
   public URI call() throws Exception {
      long startTime = System.currentTimeMillis();
      try {
         Conditions.checkNotNull(factory, "exportItemFactory");
         Conditions.checkNotNull(executorAdmin, "executorAdmin");

         Conditions.checkNotNullOrEmpty(branches, "branches");
         Conditions.checkNotNull(options, "options");
         doWork();
         return factory.getResourceManager().generateResourceLocator(ResourceConstants.EXCHANGE_RESOURCE_PROTOCOL, "",
            getExchangeFileName()).getLocation();
      } finally {
         getLogger().info("Exported [%s] branch%s in [%s]", branches.size(), branches.size() != 1 ? "es" : "",
            Lib.getElapseString(startTime));
      }
   }

   private File createTempFolder() throws OseeCoreException {
      String exchangeBasePath = ResourceConstants.getExchangeDataPath(getSystemPreferences());
      File rootDirectory = ExchangeUtil.createTempFolder(exchangeBasePath);
      if (!Strings.isValid(getExchangeFileName())) {
         setExchangeFileName(rootDirectory.getName());
      }
      return rootDirectory;
   }

   private void doWork() throws Exception {
      ExportImportJoinQuery joinQuery = joinFactory.createExportImportJoinQuery();

      for (IOseeBranch branch : branches) {
         long branchUuid = branch.getUuid();
         joinQuery.add(branchUuid, -1L);
      }
      joinQuery.store();

      List<AbstractExportItem> taskList = factory.createTaskList(joinQuery.getQueryId(), options);
      try {
         File tempFolder = createTempFolder();

         for (AbstractExportItem exportItem : taskList) {
            exportItem.setWriteLocation(tempFolder);
         }

         executeTasks(taskList);

         finishExport(tempFolder);
      } finally {
         cleanUp(joinQuery, taskList);
      }
   }

   private void cleanUp(AbstractJoinQuery joinQuery, List<AbstractExportItem> taskList) {
      for (AbstractExportItem exportItem : taskList) {
         exportItem.cleanUp();
      }
      try {
         joinQuery.delete();
      } catch (OseeCoreException ex) {
         getLogger().warn(ex, "Error during export clean-up");
      }
   }

   private void finishExport(File tempFolder) throws IllegalArgumentException, IOException {
      String zipTargetName = getExchangeFileName() + "." + ResourceConstants.ZIP_EXTENSION;

      if (options.getBoolean(ExportOptions.COMPRESS.name())) {
         getLogger().info("Compressing Branch Export Data - [%s]", zipTargetName);
         File zipTarget = new File(tempFolder.getParent(), zipTargetName);
         Lib.compressDirectory(tempFolder, zipTarget.getAbsolutePath(), true);
         getLogger().info("Deleting Branch Export Temp Folder - [%s]", tempFolder);
         Lib.deleteDir(tempFolder);
      } else {
         File target = new File(tempFolder.getParent(), getExchangeFileName());
         if (!target.equals(tempFolder)) {
            if (!tempFolder.renameTo(target)) {
               getLogger().info("Unable to move [%s] to [%s]", tempFolder.getAbsolutePath(), target.getAbsolutePath());
            }
         }
      }
   }

   private void executeTasks(List<AbstractExportItem> taskList) throws Exception {
      final List<Throwable> throwables = new LinkedList<>();
      final List<Future<?>> futures = new CopyOnWriteArrayList<>();

      ExecutorAdmin executor = getExecutorAdmin();
      for (AbstractExportItem exportItem : taskList) {
         Future<?> future =
            executor.schedule(BRANCH_EXPORT_EXECUTOR_ID, exportItem, new ExecutionCallbackAdapter<Boolean>() {

               @Override
               public void onFailure(Throwable throwable) {
                  super.onFailure(throwable);
                  throwables.add(throwable);
                  for (Future<?> future : futures) {
                     if (!future.isDone() && !future.isCancelled()) {
                        future.cancel(true);
                     }
                  }
               }

            });
         futures.add(future);
      }

      for (Future<?> future : futures) {
         future.get();
      }

      if (!throwables.isEmpty()) {
         List<StackTraceElement> trace = new LinkedList<>();
         for (Throwable th : throwables) {
            for (StackTraceElement element : th.getStackTrace()) {
               trace.add(element);
            }
         }
         OseeCoreException exception = new OseeCoreException("Error detected during branch export");
         exception.setStackTrace(trace.toArray(new StackTraceElement[trace.size()]));
         throw exception;
      }
   }

}

Back to the top