Skip to main content
summaryrefslogtreecommitdiffstats
blob: 53e2ac3f5b809214cc097069550062207014939c (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************
 * Copyright (c) 2009 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.datastore.cache;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.datastore.internal.Activator;
import org.eclipse.osee.framework.core.enums.StorageState;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.exception.OseeInvalidInheritanceException;
import org.eclipse.osee.framework.core.model.AbstractOseeType;
import org.eclipse.osee.framework.core.model.Branch;
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.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.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.type.CompositeKeyHashMap;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public class DatabaseArtifactTypeAccessor extends AbstractDatabaseAccessor<ArtifactType> {
   protected static final int ABSTRACT_TYPE_INDICATOR = 1;
   protected static final int CONCRETE_TYPE_INDICATOR = 0;

   private static final String SELECT_ARTIFACT_TYPES = "select * from osee_artifact_type";
   private static final String INSERT_ARTIFACT_TYPE =
      "insert into osee_artifact_type (art_type_id, art_type_guid, name, is_abstract) VALUES (?,?,?,?)";

   private static final String UPDATE_ARTIFACT_TYPE =
      "update osee_artifact_type SET name = ?, is_abstract = ? where art_type_id = ?";

   private static final String SELECT_ARTIFACT_TYPE_INHERITANCE =
      "select * from osee_artifact_type_inheritance order by super_art_type_id, art_type_id";
   private static final String INSERT_ARTIFACT_TYPE_INHERITANCE =
      "insert into osee_artifact_type_inheritance (art_type_id, super_art_type_id) VALUES (?,?)";
   private static final String DELETE_ARTIFACT_TYPE_INHERITANCE =
      "delete from osee_artifact_type_inheritance where art_type_id = ?";

   private static final String SELECT_ARTIFACT_TYPE_ATTRIBUTES =
      "select * from osee_artifact_type_attributes order by art_type_id, branch_id, attr_type_id";
   private static final String INSERT_ARTIFACT_TYPE_ATTRIBUTES =
      "insert into osee_artifact_type_attributes (art_type_id, attr_type_id, branch_id) VALUES (?, ?, ?)";
   private static final String DELETE_ARTIFACT_TYPE_ATTRIBUTES =
      "delete from osee_artifact_type_attributes where art_type_id = ?";

   private final AttributeTypeCache attributeCache;
   private final BranchCache branchCache;
   private final ArtifactTypeFactory artifactTypeFactory;

   public DatabaseArtifactTypeAccessor(IOseeDatabaseService databaseService, BranchCache branchCache, AttributeTypeCache attributeCache, ArtifactTypeFactory artifactTypeFactory) {
      super(databaseService);
      this.attributeCache = attributeCache;
      this.branchCache = branchCache;
      this.artifactTypeFactory = artifactTypeFactory;
   }

   private ArtifactTypeFactory getArtifactTypeFactory() {
      return artifactTypeFactory;
   }

   @Override
   public void load(IOseeCache<ArtifactType> cache) throws OseeCoreException {
      attributeCache.ensurePopulated();
      Set<ArtifactType> loadedTypes = new HashSet<ArtifactType>();
      ArtifactTypeCache artCache = (ArtifactTypeCache) cache;
      loadArtifactTypes(artCache, loadedTypes);
      loadTypeInheritance(artCache);
      loadAllTypeValidity(artCache);
      for (ArtifactType type : loadedTypes) {
         type.clearDirty();
      }
   }

   private void loadArtifactTypes(ArtifactTypeCache cache, Set<ArtifactType> loadedTypes) throws OseeCoreException {
      ArtifactTypeFactory factory = getArtifactTypeFactory();

      IOseeStatement chStmt = getDatabaseService().getStatement();
      try {
         chStmt.runPreparedQuery(SELECT_ARTIFACT_TYPES);

         while (chStmt.next()) {
            try {
               int artTypeId = chStmt.getInt("art_type_id");
               boolean isAbstract = chStmt.getInt("is_abstract") == ABSTRACT_TYPE_INDICATOR;
               String artifactTypeName = chStmt.getString("name");
               String guid = chStmt.getString("art_type_guid");

               ArtifactType artifactType =
                  factory.createOrUpdate(cache, artTypeId, StorageState.LOADED, guid, isAbstract, artifactTypeName);
               loadedTypes.add(artifactType);
            } catch (OseeDataStoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
      } finally {
         chStmt.close();
      }
   }

   private void loadTypeInheritance(ArtifactTypeCache cache) throws OseeCoreException {
      IOseeStatement chStmt2 = getDatabaseService().getStatement();
      try {
         chStmt2.runPreparedQuery(SELECT_ARTIFACT_TYPE_INHERITANCE);
         HashCollection<ArtifactType, ArtifactType> baseToSuperTypes =
            new HashCollection<ArtifactType, ArtifactType>(false, HashSet.class);

         while (chStmt2.next()) {
            int artTypeId = chStmt2.getInt("art_type_id");
            int superArtTypeId = chStmt2.getInt("super_art_type_id");
            ArtifactType baseArtType = cache.getById(artTypeId);
            ArtifactType superArtType = cache.getById(superArtTypeId);

            if (baseArtType == null) {
               throw new OseeInvalidInheritanceException("ArtifactType [%s] which inherits from [%s] is null",
                  artTypeId, superArtType);
            }
            if (superArtType == null) {
               throw new OseeInvalidInheritanceException("ArtifactType [%s] which inherits from null artifact [%s]",
                  artTypeId, superArtType);
            }
            baseToSuperTypes.put(baseArtType, superArtType);
         }
         for (ArtifactType artifactType : baseToSuperTypes.keySet()) {
            Set<ArtifactType> superTypes = (Set<ArtifactType>) baseToSuperTypes.getValues(artifactType);
            if (superTypes != null) {
               artifactType.setSuperTypes(superTypes);
            }
         }
      } finally {
         chStmt2.close();
      }
   }

   private void loadAllTypeValidity(ArtifactTypeCache cache) throws OseeCoreException {
      CompositeKeyHashMap<ArtifactType, Branch, Collection<AttributeType>> typeValidity =
         new CompositeKeyHashMap<ArtifactType, Branch, Collection<AttributeType>>();
      IOseeStatement chStmt = getDatabaseService().getStatement();
      try {
         chStmt.runPreparedQuery(2000, SELECT_ARTIFACT_TYPE_ATTRIBUTES);
         while (chStmt.next()) {
            try {
               ArtifactType artifactType = cache.getById(chStmt.getInt("art_type_id"));
               Branch branch = branchCache.getById(chStmt.getInt("branch_id"));
               AttributeType attributeType = attributeCache.getById(chStmt.getInt("attr_type_id"));
               Collection<AttributeType> attributes = typeValidity.get(artifactType, branch);
               if (attributes == null) {
                  attributes = new HashSet<AttributeType>();
                  typeValidity.put(artifactType, branch, attributes);
               }
               attributes.add(attributeType);
            } catch (OseeCoreException ex) {
               OseeLog.log(Activator.class, Level.SEVERE, ex);
            }
         }
      } finally {
         chStmt.close();
      }
      for (Entry<Pair<ArtifactType, Branch>, Collection<AttributeType>> entry : typeValidity.entrySet()) {
         try {
            Pair<ArtifactType, Branch> key = entry.getKey();
            key.getFirst().setAttributeTypes(entry.getValue(), key.getSecond());
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   @Override
   public void store(Collection<ArtifactType> types) throws OseeCoreException {
      Set<ArtifactType> typeInheritanceChanges = new HashSet<ArtifactType>();
      Set<ArtifactType> typeValidityChanges = new HashSet<ArtifactType>();
      List<Object[]> insertData = new ArrayList<Object[]>();
      List<Object[]> updateData = new ArrayList<Object[]>();

      for (ArtifactType type : types) {
         if (isDataDirty(type)) {
            int abstractValue = type.isAbstract() ? ABSTRACT_TYPE_INDICATOR : CONCRETE_TYPE_INDICATOR;
            switch (type.getStorageState()) {
               case CREATED:
                  type.setId(getSequence().getNextArtifactTypeId());
                  insertData.add(new Object[] {type.getId(), type.getGuid(), type.getName(), abstractValue});
                  break;
               case MODIFIED:
                  updateData.add(new Object[] {type.getName(), abstractValue, type.getId()});
                  break;
               default:
                  break;
            }
         }
         if (type.isFieldDirty(ArtifactType.ARTIFACT_INHERITANCE_FIELD_KEY)) {
            typeInheritanceChanges.add(type);

         }
         if (type.isFieldDirty(ArtifactType.ARTIFACT_TYPE_ATTRIBUTES_FIELD_KEY)) {
            typeValidityChanges.add(type);
         }
      }
      getDatabaseService().runBatchUpdate(INSERT_ARTIFACT_TYPE, insertData);
      getDatabaseService().runBatchUpdate(UPDATE_ARTIFACT_TYPE, updateData);

      storeArtifactTypeInheritance(typeInheritanceChanges);
      storeAttributeTypeValidity(types);

      for (ArtifactType type : types) {
         type.clearDirty();
      }
   }

   private boolean isDataDirty(ArtifactType type) throws OseeCoreException {
      return type.areFieldsDirty(AbstractOseeType.NAME_FIELD_KEY, AbstractOseeType.UNIQUE_ID_FIELD_KEY,
         ArtifactType.ARTIFACT_IS_ABSTRACT_FIELD_KEY);
   }

   private void storeArtifactTypeInheritance(Collection<ArtifactType> types) throws OseeCoreException {
      List<Object[]> insertInheritanceData = new ArrayList<Object[]>();
      List<Object[]> deleteInheritanceData = new ArrayList<Object[]>();
      for (ArtifactType type : types) {
         deleteInheritanceData.add(new Object[] {type.getId()});
         for (ArtifactType superType : type.getSuperArtifactTypes()) {
            insertInheritanceData.add(new Object[] {type.getId(), superType.getId()});
         }
      }
      getDatabaseService().runBatchUpdate(DELETE_ARTIFACT_TYPE_INHERITANCE, deleteInheritanceData);
      getDatabaseService().runBatchUpdate(INSERT_ARTIFACT_TYPE_INHERITANCE, insertInheritanceData);
   }

   private void storeAttributeTypeValidity(Collection<ArtifactType> types) throws OseeCoreException {
      List<Object[]> insertData = new ArrayList<Object[]>();
      List<Object[]> deleteData = new ArrayList<Object[]>();
      for (ArtifactType artifactType : types) {
         deleteData.add(new Object[] {artifactType.getId()});
         Map<Branch, Collection<AttributeType>> entries = artifactType.getLocalAttributeTypes();
         if (entries != null) {
            for (Entry<Branch, Collection<AttributeType>> entry : entries.entrySet()) {
               Branch branch = entry.getKey();
               for (AttributeType attributeType : entry.getValue()) {
                  insertData.add(new Object[] {artifactType.getId(), attributeType.getId(), branch.getId()});
               }
            }
         }
      }
      getDatabaseService().runBatchUpdate(DELETE_ARTIFACT_TYPE_ATTRIBUTES, deleteData);
      getDatabaseService().runBatchUpdate(INSERT_ARTIFACT_TYPE_ATTRIBUTES, insertData);
   }

}

Back to the top