Skip to main content
summaryrefslogtreecommitdiffstats
blob: c51606f94469165476a64465246c0f11e038c209 (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
/*******************************************************************************
 * 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.core.dsl.ui.integration.operations;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.emf.compare.diff.metamodel.ComparisonSnapshot;
import org.eclipse.emf.compare.ui.editor.ModelCompareEditorInput;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.osee.framework.core.data.OseeServerContext;
import org.eclipse.osee.framework.core.dsl.ui.integration.internal.DslUiIntegrationConstants;
import org.eclipse.osee.framework.core.enums.CoreTranslatorId;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.OseeImportModelRequest;
import org.eclipse.osee.framework.core.model.OseeImportModelResponse;
import org.eclipse.osee.framework.core.model.TableData;
import org.eclipse.osee.framework.core.operation.AbstractOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.HttpClientMessage;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.UIJob;

/**
 * @author Roberto E. Escobar
 */
public class OseeTypesImportOperation extends AbstractOperation {
   private final IOseeCachingService cacheService;
   private final URI model;
   private final boolean isPersistAllowed;
   private final boolean createTypeChangeReport;
   private final boolean createCompareReport;

   public OseeTypesImportOperation(IOseeCachingService cacheService, URI model, boolean createTypeChangeReport, boolean createCompareReport, boolean isPersistAllowed) {
      super("Import Osee Types Model", DslUiIntegrationConstants.PLUGIN_ID);
      this.cacheService = cacheService;
      this.model = model;
      this.isPersistAllowed = isPersistAllowed;
      this.createCompareReport = createCompareReport;
      this.createTypeChangeReport = createTypeChangeReport;
   }

   private String getModel(URL url) throws IOException {
      InputStream inputStream = null;
      try {
         inputStream = new BufferedInputStream(url.openStream());
         return Lib.inputStreamToString(inputStream);
      } finally {
         Lib.close(inputStream);
      }
   }

   private String getName(URI uri) {
      String name = uri.toASCIIString();
      int index = name.lastIndexOf("/");
      if (index > 0) {
         name = name.substring(index + 1, name.length());
      }
      return name;
   }

   @Override
   protected void doWork(IProgressMonitor monitor) throws Exception {
      Map<String, String> parameters = new HashMap<String, String>();

      OseeImportModelRequest modelRequest =
         new OseeImportModelRequest(getName(model), getModel(model.toURL()), createTypeChangeReport,
            createCompareReport, isPersistAllowed);

      OseeImportModelResponse response =
         HttpClientMessage.send(OseeServerContext.OSEE_MODEL_CONTEXT, parameters,
            CoreTranslatorId.OSEE_IMPORT_MODEL_REQUEST, modelRequest, CoreTranslatorId.OSEE_IMPORT_MODEL_RESPONSE);

      if (response.wasPersisted()) {
         cacheService.getEnumTypeCache().reloadCache();
         cacheService.getAttributeTypeCache().reloadCache();
         cacheService.getArtifactTypeCache().reloadCache();
         cacheService.getRelationTypeCache().reloadCache();
      }

      if (createTypeChangeReport) {
         openTabReport(response.getReportData());
      }

      if (createCompareReport) {
         String compareName = response.getComparisonSnapshotModelName();
         String compareData = response.getComparisonSnapshotModel();
         if (Strings.isValid(compareData) && Strings.isValid(compareName)) {
            ComparisonSnapshot snapshot = loadComparisonSnapshot(compareName, compareData);
            openCompareEditor(snapshot);
         }
      }
   }

   private static ComparisonSnapshot loadComparisonSnapshot(String compareName, String compareData) throws OseeCoreException {
      ComparisonSnapshot snapshot = null;
      try {
         ResourceSet resourceSet = new ResourceSetImpl();
         Resource resource = resourceSet.createResource(org.eclipse.emf.common.util.URI.createURI(compareName));
         resource.load(new ByteArrayInputStream(compareData.getBytes("UTF-8")), resourceSet.getLoadOptions());
         snapshot = (ComparisonSnapshot) resource.getContents().get(0);
      } catch (IOException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return snapshot;
   }

   private void openCompareEditor(final ComparisonSnapshot snapshot) {
      Job job = new UIJob("Open Compare") {

         @Override
         public IStatus runInUIThread(IProgressMonitor monitor) {
            IStatus status;
            try {
               CompareEditorInput input = new ModelCompareEditorInput(snapshot);
               IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
               page.openEditor(input, "org.eclipse.compare.CompareEditor", true);
               status = Status.OK_STATUS;
            } catch (Exception ex) {
               status =
                  new Status(IStatus.ERROR, DslUiIntegrationConstants.PLUGIN_ID, "Error opening compare editor", ex);
            }
            return status;
         }
      };
      Jobs.startJob(job);
   }

   private void openTabReport(List<TableData> tableData) {
      Operations.executeAsJob(new CreateEditorReportOperation("Un-Persisted Osee Types", tableData), true);
   }
}

Back to the top