Skip to main content
summaryrefslogtreecommitdiffstats
blob: 26693a6baeaa477d0523e4d232d8a3374c55cd6b (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
/*******************************************************************************
 * 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.message.internal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.TransactionRecordFactory;
import org.eclipse.osee.framework.core.model.type.AttributeTypeFactory;
import org.eclipse.osee.framework.core.services.IOseeModelFactoryService;
import org.eclipse.osee.framework.core.services.TempCachingService;
import org.eclipse.osee.framework.core.translation.IDataTranslationService;
import org.eclipse.osee.framework.core.translation.ITranslator;
import org.eclipse.osee.framework.core.translation.ITranslatorId;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Conditions;

/**
 * @author Roberto E. Escobar
 */
public class DataTranslationService implements IDataTranslationService {

   private final DataTranslationServiceFactory factoryConfigurator = new DataTranslationServiceFactory();
   private final Map<ITranslatorId, ITranslator<?>> translators =
      new ConcurrentHashMap<ITranslatorId, ITranslator<?>>();

   private IOseeModelFactoryService modelFactory;
   private TempCachingService cachingService;

   public void setModelFactory(IOseeModelFactoryService modelFactory) {
      this.modelFactory = modelFactory;
   }

   public void setTempCachingService(TempCachingService cachingService) {
      this.cachingService = cachingService;
   }

   public void start() throws OseeCoreException {
      TransactionRecordFactory txFactory = modelFactory.getTransactionFactory();
      AttributeTypeFactory attributeTypeFactory = modelFactory.getAttributeTypeFactory();

      translators.clear();
      factoryConfigurator.configureService(this, txFactory, attributeTypeFactory, cachingService);
   }

   public void stop() {
      translators.clear();
   }

   @SuppressWarnings("unchecked")
   @Override
   public <T> T convert(PropertyStore propertyStore, ITranslatorId txId) throws OseeCoreException {
      Conditions.checkNotNull(txId, "translator Id");

      T object = null;
      if (propertyStore != null && !propertyStore.isEmpty()) {
         ITranslator<?> translator = getTranslator(txId);
         object = (T) translator.convert(propertyStore);
      }
      return object;
   }

   @SuppressWarnings("unchecked")
   @Override
   public <T> PropertyStore convert(T object, ITranslatorId txId) throws OseeCoreException {
      PropertyStore propertyStore = null;
      if (object == null) {
         propertyStore = new PropertyStore();
      } else {
         ITranslator<T> translator = (ITranslator<T>) getTranslator(txId);
         propertyStore = translator.convert(object);
      }
      return propertyStore;
   }

   @Override
   public ITranslator<?> getTranslator(ITranslatorId txId) throws OseeCoreException {
      Conditions.checkNotNull(txId, "translator Id");
      ITranslator<?> toReturn = translators.get(txId);
      if (toReturn == null) {
         throw new OseeStateException("Unable to find a match for translator id [%s]", txId);
      }
      return toReturn;

   }

   @Override
   public boolean addTranslator(ITranslator<?> translator, ITranslatorId txId) throws OseeCoreException {
      Conditions.checkNotNull(txId, "translator Id");
      Conditions.checkNotNull(translator, "translator");
      boolean wasAdded = false;
      if (!translators.containsKey(txId)) {
         translators.put(txId, translator);
         wasAdded = true;
      }
      return wasAdded;
   }

   @Override
   public boolean removeTranslator(ITranslatorId txId) throws OseeCoreException {
      Conditions.checkNotNull(txId, "translator Id");
      return translators.remove(txId) != null;
   }

   @Override
   public Collection<ITranslatorId> getSupportedClasses() {
      return new HashSet<ITranslatorId>(translators.keySet());
   }

   @Override
   public <T> T convert(InputStream inputStream, ITranslatorId txId) throws OseeCoreException {
      Conditions.checkNotNull(inputStream, "inputStream");
      Conditions.checkNotNull(txId, "translator Id");

      T toReturn = null;
      PropertyStore propertyStore = new PropertyStore();
      try {
         propertyStore.load(inputStream);
         toReturn = convert(propertyStore, txId);
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return toReturn;
   }

   @Override
   public <T> InputStream convertToStream(T object, ITranslatorId txId) throws OseeCoreException {
      PropertyStore propertyStore = convert(object, txId);
      InputStream inputStream = null;
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      try {
         propertyStore.save(buffer);
         inputStream = new ByteArrayInputStream(buffer.toByteArray());
      } catch (Exception ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
      return inputStream;
   }
}

Back to the top