Skip to main content
summaryrefslogtreecommitdiffstats
blob: 260a86a59668675f878c17f61f4fbe5bec1fc9cf (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
/*******************************************************************************
 * 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.model.type;

import org.eclipse.osee.framework.core.enums.StorageState;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
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.cache.IOseeTypeFactory;
import org.eclipse.osee.framework.core.model.cache.OseeEnumTypeCache;
import org.eclipse.osee.framework.core.util.Conditions;

/**
 * @author Roberto E. Escobar
 */
public class OseeEnumTypeFactory implements IOseeTypeFactory {

   public OseeEnumTypeFactory() {
   }

   public OseeEnumType createEnumType(String guid, String name) throws OseeCoreException {
      Conditions.checkNotNullOrEmpty(name, "osee enum type name");
      String checkedGuid = Conditions.checkGuidCreateIfNeeded(guid);
      return new OseeEnumType(checkedGuid, name);
   }

   public OseeEnumEntry createEnumEntry(String guid, String name, int ordinal) throws OseeCoreException {
      Conditions.checkNotNullOrEmpty(name, "osee enum entry name");
      Conditions.checkExpressionFailOnTrue(ordinal < 0, "ordinal must be greater than or equal to zero");
      String checkedGuid = Conditions.checkGuidCreateIfNeeded(guid);
      return new OseeEnumEntry(checkedGuid, name, ordinal);
   }

   public OseeEnumType createOrUpdate(IOseeCache<OseeEnumType> cache, int enumTypeId, StorageState storageState, String guid, String enumTypeName) throws OseeCoreException {
      OseeEnumType oseeEnumType = cache.getById(enumTypeId);
      if (oseeEnumType == null) {
         oseeEnumType = createEnumType(guid, enumTypeName);
         oseeEnumType.setId(enumTypeId);
         oseeEnumType.setStorageState(storageState);
      } else {
         cache.decache(oseeEnumType);
         oseeEnumType.setName(enumTypeName);
      }
      cache.cache(oseeEnumType);
      return oseeEnumType;
   }

   public OseeEnumType createOrUpdate(OseeEnumTypeCache cache, String guid, String enumTypeName) throws OseeCoreException {
      OseeEnumType oseeEnumType = cache.getByGuid(guid);
      if (oseeEnumType == null) {
         oseeEnumType = createEnumType(guid, enumTypeName);
      } else {
         cache.decache(oseeEnumType);
         oseeEnumType.setName(enumTypeName);
      }
      cache.cache(oseeEnumType);
      return oseeEnumType;
   }

   public OseeEnumEntry createOrUpdate(IOseeCache<OseeEnumType> cache, String enumTypeGuid, String enumEntryGuid, String enumEntryName, int ordinal) throws OseeCoreException {
      OseeEnumType oseeEnumType = ((AbstractOseeCache<OseeEnumType>) cache).getByGuid(enumTypeGuid);
      OseeEnumEntry enumEntry = oseeEnumType.getEntryByGuid(enumEntryGuid);
      if (enumEntry == null) {
         enumEntry = createEnumEntry(enumEntryGuid, enumEntryName, ordinal);
         oseeEnumType.addEntry(enumEntry);
      } else {
         enumEntry.setName(enumEntryName);
         enumEntry.setOrdinal(ordinal);
      }
      return enumEntry;
   }
}

Back to the top