Skip to main content
summaryrefslogtreecommitdiffstats
blob: b24c0ca01d5683c4219d0785ddbac83e274e7a05 (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
/*******************************************************************************
 * 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.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import org.eclipse.osee.framework.branch.management.ExportOptions;
import org.eclipse.osee.framework.branch.management.IExchangeTaskListener;
import org.eclipse.osee.framework.branch.management.exchange.export.AbstractExportItem;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.server.ServerThreads;
import org.eclipse.osee.framework.database.core.ExportImportJoinQuery;
import org.eclipse.osee.framework.database.core.JoinUtility;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
final class ExportController implements IExchangeTaskListener {
   private static final String ZIP_EXTENSION = ".zip";

   private String exportName;
   private final PropertyStore options;
   private final List<Integer> branchIds;
   private final ExportImportJoinQuery exportJoinId;
   private ExecutorService executorService;
   private final List<String> errorList = new CopyOnWriteArrayList<String>();
   private final OseeServices oseeServices;

   ExportController(OseeServices oseeServices, String exportName, PropertyStore options, List<Integer> branchIds) throws OseeCoreException {
      if (branchIds.isEmpty()) {
         throw new OseeArgumentException("No branch selected for export.");
      }
      this.oseeServices = oseeServices;
      this.exportName = exportName;
      this.options = options;
      this.branchIds = branchIds;
      this.exportJoinId = JoinUtility.createExportImportJoinQuery();
   }

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

   public void setExchangeFileName(String value) {
      this.exportName = value;
   }

   private void cleanUp(List<AbstractExportItem> taskList) {
      for (AbstractExportItem exportItem : taskList) {
         exportItem.cleanUp();
      }
      try {
         exportJoinId.delete();
      } catch (OseeCoreException ex) {
         onException("Export Clean-Up", ex);
      }
      this.executorService.shutdown();
      this.executorService = null;
   }

   private File createTempFolder() {
      File rootDirectory = ExchangeUtil.createTempFolder();
      if (!Strings.isValid(getExchangeFileName())) {
         setExchangeFileName(rootDirectory.getName());
      }
      return rootDirectory;
   }

   private void setUp() throws OseeCoreException {
      for (int branchId : branchIds) {
         exportJoinId.add((long) branchId, -1L);
      }
      exportJoinId.store();

      long maxTx = oseeServices.getDatabaseService().runPreparedQueryFetchObject(-1L, ExchangeDb.GET_MAX_TX);
      long userMaxTx = ExchangeDb.getMaxTransaction(options);
      if (userMaxTx == Long.MIN_VALUE || userMaxTx > maxTx) {
         options.put(ExportOptions.MAX_TXS.name(), Long.toString(maxTx));
      }

      executorService =
         Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(),
            ServerThreads.createNewThreadFactory("branch.export.worker"));
   }

   protected void handleTxWork() throws OseeCoreException {
      long startTime = System.currentTimeMillis();
      setUp();
      ExchangeDb exchangeDb = new ExchangeDb(oseeServices, options, exportJoinId.getQueryId());

      List<AbstractExportItem> taskList = exchangeDb.createTaskList();
      try {
         File tempFolder = createTempFolder();

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

         sendTasksToExecutor(taskList, tempFolder);

         finishExport(tempFolder);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         cleanUp(taskList);
      }
      OseeLog.logf(this.getClass(), Level.INFO, "Exported [%s] branch%s in [%s]", branchIds.size(),
         branchIds.size() != 1 ? "es" : "", Lib.getElapseString(startTime));
   }

   private void finishExport(File tempFolder) throws IllegalArgumentException, IOException {
      String zipTargetName = getExchangeFileName() + ZIP_EXTENSION;
      if (options.getBoolean(ExportOptions.COMPRESS.name())) {
         OseeLog.logf(this.getClass(), Level.INFO, "Compressing Branch Export Data - [%s]", zipTargetName);
         File zipTarget = new File(tempFolder.getParent(), zipTargetName);
         Lib.compressDirectory(tempFolder, zipTarget.getAbsolutePath(), true);
         OseeLog.logf(this.getClass(), Level.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)) {
               OseeLog.logf(this.getClass(), Level.INFO, "Unable to move [%s] to [%s]", tempFolder.getAbsolutePath(),
                  target.getAbsolutePath());
            }
         }
      }
   }

   private void sendTasksToExecutor(List<AbstractExportItem> taskList, final File exportFolder) throws InterruptedException, ExecutionException, OseeCoreException {
      List<Future<?>> futures = new ArrayList<Future<?>>();
      for (AbstractExportItem exportItem : taskList) {
         futures.add(this.executorService.submit(exportItem));
      }

      for (Future<?> future : futures) {
         future.get();
         if (!this.errorList.isEmpty()) {
            throw new OseeCoreException(errorList.toString());
         }
      }
   }

   @Override
   public void onException(String name, Throwable ex) {
      errorList.add(Lib.exceptionToString(ex));
   }

   @Override
   synchronized public void onExportItemCompleted(String name, long timeToProcess) {
      System.out.println(String.format("Exported: [%s] in [%s] ms", name, timeToProcess));
   }
}

Back to the top