Skip to main content
summaryrefslogtreecommitdiffstats
blob: d7bb717c9ed9b2dad65e86ee7f1e36c0abb7beb8 (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
/*******************************************************************************
 * 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.skynet.core.internal.accessors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.CoreTranslatorId;
import org.eclipse.osee.framework.core.message.ArtifactTypeCacheUpdateResponse;
import org.eclipse.osee.framework.core.message.ArtifactTypeCacheUpdateResponse.ArtifactTypeRow;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.cache.AbstractOseeCache;
import org.eclipse.osee.framework.core.model.cache.IOseeCache;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.ArtifactTypeFactory;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.jdk.core.type.CompositeKeyHashMap;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.type.Triplet;

/**
 * @author Roberto E. Escobar
 */
public class ClientArtifactTypeAccessor extends AbstractClientDataAccessor<Long, ArtifactType> {

   private final AbstractOseeCache<Long, AttributeType> attrCache;
   private final AbstractOseeCache<Long, Branch> branchCache;

   private final ArtifactTypeFactory artifactTypeFactory;

   public ClientArtifactTypeAccessor(ArtifactTypeFactory artifactTypeFactory, AbstractOseeCache<Long, AttributeType> attrCache, AbstractOseeCache<Long, Branch> branchCache) {
      this.artifactTypeFactory = artifactTypeFactory;
      this.attrCache = attrCache;
      this.branchCache = branchCache;
   }

   private ArtifactTypeFactory getFactory() {
      return artifactTypeFactory;
   }

   @Override
   public void load(IOseeCache<Long, ArtifactType> cache) throws OseeCoreException {
      attrCache.ensurePopulated();
      branchCache.ensurePopulated();
      super.load(cache);
   }

   @Override
   protected Collection<ArtifactType> updateCache(IOseeCache<Long, ArtifactType> cache) throws OseeCoreException {
      List<ArtifactType> updatedItems = new ArrayList<ArtifactType>();

      ArtifactTypeCacheUpdateResponse response =
         requestUpdateMessage(cache, CoreTranslatorId.ARTIFACT_TYPE_CACHE_UPDATE_RESPONSE);

      ArtifactTypeFactory factory = getFactory();
      for (ArtifactTypeRow row : response.getArtTypeRows()) {
         ArtifactType cached =
            factory.createOrUpdate(cache, row.getId(), row.getStorageState(), row.getGuid(), row.isAbstract(),
               row.getName());
         updatedItems.add(cached);
      }

      for (Entry<Long, Long[]> entry : response.getBaseToSuperTypes().entrySet()) {
         ArtifactType baseType = cache.getById(entry.getKey());
         Set<ArtifactType> superTypes = new HashSet<ArtifactType>();
         for (Long superId : entry.getValue()) {
            ArtifactType superType = cache.getById(superId);
            if (superType != null) {
               superTypes.add(superType);
            }
         }
         baseType.setSuperTypes(superTypes);
      }

      CompositeKeyHashMap<ArtifactType, IOseeBranch, Collection<AttributeType>> attrs =
         new CompositeKeyHashMap<ArtifactType, IOseeBranch, Collection<AttributeType>>();

      for (Triplet<Long, Long, Long> entry : response.getAttributeTypes()) {
         ArtifactType key1 = cache.getByGuid(entry.getFirst());
         Long branchUuid = entry.getSecond();
         IOseeBranch branchToken = branchCache.getByGuid(branchUuid);
         if (branchToken == null) {
            branchToken = TokenFactory.createBranch(branchUuid, String.valueOf(branchUuid));
         }
         Collection<AttributeType> types = attrs.get(key1, branchToken);
         if (types == null) {
            types = new HashSet<AttributeType>();
            attrs.put(key1, branchToken, types);
         }
         types.add(attrCache.getByGuid(entry.getThird()));
      }

      for (Entry<Pair<ArtifactType, IOseeBranch>, Collection<AttributeType>> entry : attrs.entrySet()) {
         ArtifactType type = entry.getKey().getFirst();
         IOseeBranch branch = entry.getKey().getSecond();
         Collection<AttributeType> attrTypes = entry.getValue();
         type.setAttributeTypes(attrTypes, branch);
      }
      return updatedItems;
   }
}

Back to the top