Skip to main content
summaryrefslogtreecommitdiffstats
blob: 83727e3127ee6ea062940f2c679ae90a8de2dd22 (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
/*******************************************************************************
 * Copyright (c) 2013 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.ats.core.client.internal.store;

import java.util.Map;
import org.eclipse.osee.ats.api.IAtsConfigObject;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.core.client.internal.IAtsArtifactReader;
import org.eclipse.osee.ats.core.client.internal.IAtsArtifactStore;
import org.eclipse.osee.ats.core.client.internal.IAtsArtifactWriter;
import org.eclipse.osee.ats.core.client.internal.config.AtsArtifactConfigCache;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;

/**
 * @author Donald G. Dunne
 */
public class AtsArtifactStore implements IAtsArtifactStore {

   private final Map<IArtifactType, IAtsArtifactReader<? extends IAtsConfigObject>> readers;
   private final Map<Class<? extends IAtsConfigObject>, IAtsArtifactWriter<? extends IAtsConfigObject>> writers;

   public AtsArtifactStore(Map<IArtifactType, IAtsArtifactReader<? extends IAtsConfigObject>> readers, Map<Class<? extends IAtsConfigObject>, IAtsArtifactWriter<? extends IAtsConfigObject>> writers) {
      this.readers = readers;
      this.writers = writers;
   }

   @Override
   public <T extends IAtsConfigObject> Artifact store(AtsArtifactConfigCache cache, T configObject, IAtsChangeSet changes) throws OseeCoreException {
      Conditions.checkNotNull(cache, "cache");
      Conditions.checkNotNull(configObject, "configObject");
      Conditions.checkNotNull(changes, "transaction");

      IAtsArtifactWriter<T> writer = getWriter(configObject.getClass());
      Conditions.checkNotNull(writer, "writer");

      return writer.store(configObject, cache, changes);
   }

   @Override
   public <T extends IAtsConfigObject> T load(AtsArtifactConfigCache cache, Artifact artifact) throws OseeCoreException {
      Conditions.checkNotNull(cache, "cache");
      Conditions.checkNotNull(artifact, "artifact");

      IAtsArtifactReader<T> reader = getReader(artifact.getArtifactTypeToken());
      Conditions.checkNotNull(reader, "reader");

      return reader.load(cache, artifact);
   }

   @SuppressWarnings("unchecked")
   private <T extends IAtsConfigObject> IAtsArtifactWriter<T> getWriter(Class<? extends IAtsConfigObject> clazz) {
      for (Class<? extends IAtsConfigObject> writerClazz : writers.keySet()) {
         if (writerClazz.isAssignableFrom(clazz)) {
            return (IAtsArtifactWriter<T>) writers.get(writerClazz);
         }
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   private <T extends IAtsConfigObject> IAtsArtifactReader<T> getReader(IArtifactType artifactType) {
      return (IAtsArtifactReader<T>) readers.get(artifactType);
   }

}

Back to the top