Skip to main content
summaryrefslogtreecommitdiffstats
blob: 077f601f1082732d5bc7b9521886099af9dee923 (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
/*******************************************************************************
 * 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.orcs.db.internal.types;

import java.io.OutputStream;
import org.eclipse.osee.event.EventService;
import org.eclipse.osee.executor.admin.ExecutorAdmin;
import org.eclipse.osee.framework.core.dsl.OseeDslResource;
import org.eclipse.osee.framework.core.dsl.OseeDslResourceUtil;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDsl;
import org.eclipse.osee.framework.core.dsl.oseeDsl.OseeDslFactory;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.OseeCachingService;
import org.eclipse.osee.framework.core.model.OseeImportModelRequest;
import org.eclipse.osee.framework.core.model.OseeImportModelResponse;
import org.eclipse.osee.framework.core.model.cache.ArtifactTypeCache;
import org.eclipse.osee.framework.core.model.cache.AttributeTypeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.OseeEnumTypeCache;
import org.eclipse.osee.framework.core.model.cache.RelationTypeCache;
import org.eclipse.osee.framework.core.model.cache.TransactionCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.OseeEnumType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.core.services.IOseeModelFactoryService;
import org.eclipse.osee.framework.core.services.IdentityService;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.db.internal.accessor.ArtifactTypeDataAccessor;
import org.eclipse.osee.orcs.db.internal.accessor.DatabaseBranchAccessor;
import org.eclipse.osee.orcs.db.internal.accessor.DatabaseTransactionRecordAccessor;
import org.eclipse.osee.orcs.db.internal.accessor.TypeLoaderImpl;

/**
 * @author Roberto E. Escobar
 */
public class OseeModelingServiceImpl implements IOseeModelingService {

   private final Log logger;
   private final IOseeDatabaseService dbService;
   private final IdentityService identityService;
   private final ExecutorAdmin executorAdmin;
   private final IResourceManager resourceManager;
   private final IOseeModelFactoryService modelFactoryService;
   private final IOseeCachingService caches;
   private final EventService eventService;

   public OseeModelingServiceImpl(Log logger, IOseeDatabaseService dbService, IdentityService identityService, ExecutorAdmin executorAdmin, IResourceManager resourceManager, IOseeModelFactoryService modelFactoryService, EventService eventService, IOseeCachingService caches) {
      super();
      this.logger = logger;
      this.dbService = dbService;
      this.identityService = identityService;
      this.executorAdmin = executorAdmin;
      this.resourceManager = resourceManager;
      this.modelFactoryService = modelFactoryService;
      this.caches = caches;
      this.eventService = eventService;
   }

   @Override
   public void exportOseeTypes(OutputStream outputStream) throws OseeCoreException {
      OseeTypeCache cache =
         new OseeTypeCache(caches.getArtifactTypeCache(), caches.getAttributeTypeCache(),
            caches.getRelationTypeCache(), caches.getEnumTypeCache());

      OseeDslFactory modelFactory = OseeDslFactory.eINSTANCE;
      OseeDsl model = modelFactory.createOseeDsl();

      IOperation operation = new OseeToXtextOperation(cache, modelFactory, model);
      Operations.executeWorkAndCheckStatus(operation, null);
      try {
         OseeDslResourceUtil.saveModel(model, "osee:/oseeTypes_" + Lib.getDateTimeString() + ".osee", outputStream,
            false);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   @Override
   public void importOseeTypes(boolean isInitializing, OseeImportModelRequest request, OseeImportModelResponse response) throws OseeCoreException {
      String modelName = request.getModelName();
      if (!modelName.endsWith(".osee")) {
         modelName += ".osee";
      }

      OseeDsl inputModel = null;
      try {
         OseeDslResource dslResource = OseeDslResourceUtil.loadModel("osee:/" + modelName, request.getModel());
         inputModel = dslResource.getModel();
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }

      IOseeCachingService tempCacheService = createCachingService(false);
      OseeTypeCache tempCache =
         new OseeTypeCache(tempCacheService.getArtifactTypeCache(), tempCacheService.getAttributeTypeCache(),
            tempCacheService.getRelationTypeCache(), tempCacheService.getEnumTypeCache());

      IOperation operation =
         new XTextToOseeTypeOperation(modelFactoryService, tempCache, tempCacheService.getBranchCache(), inputModel);
      Operations.executeWorkAndCheckStatus(operation);

      if (request.isPersistAllowed()) {
         tempCache.storeAllModified();
         response.setPersisted(true);
         if (isInitializing) {
            caches.clearAll();
         }
         caches.getEnumTypeCache().cacheFrom(tempCache.getEnumTypeCache());
         caches.getAttributeTypeCache().cacheFrom(tempCache.getAttributeTypeCache());
         caches.getArtifactTypeCache().cacheFrom(tempCache.getArtifactTypeCache());
         caches.getRelationTypeCache().cacheFrom(tempCache.getRelationTypeCache());

         caches.reloadAll();
      } else {
         response.setPersisted(false);
      }
   }

   public IOseeCachingService createCachingService(boolean needsPriming) {
      TransactionCache txCache = new TransactionCache();
      BranchCache branchCache =
         new BranchCache(new DatabaseBranchAccessor(logger, executorAdmin, eventService, dbService, txCache,
            modelFactoryService.getBranchFactory()));
      txCache.setAccessor(new DatabaseTransactionRecordAccessor(dbService, branchCache,
         modelFactoryService.getTransactionFactory()));

      TypeLoaderImpl loader =
         new TypeLoaderImpl(this, identityService, dbService, resourceManager, branchCache, needsPriming);

      OseeEnumTypeCache oseeEnumTypeCache =
         new OseeEnumTypeCache(new ArtifactTypeDataAccessor<OseeEnumType>(identityService, loader));
      AttributeTypeCache attributeCache =
         new AttributeTypeCache(new ArtifactTypeDataAccessor<AttributeType>(identityService, loader));
      ArtifactTypeCache artifactCache =
         new ArtifactTypeCache(new ArtifactTypeDataAccessor<ArtifactType>(identityService, loader));
      RelationTypeCache relationCache =
         new RelationTypeCache(new ArtifactTypeDataAccessor<RelationType>(identityService, loader));

      return new OseeCachingService(branchCache, txCache, artifactCache, attributeCache, relationCache,
         oseeEnumTypeCache, identityService);
   }
}

Back to the top