Skip to main content
summaryrefslogtreecommitdiffstats
blob: 551564a2eea5d3a1100c87c2a9db2287c59b1001 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2011 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.data;

import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.jdk.core.type.BaseIdentity;
import org.eclipse.osee.framework.jdk.core.type.FullyNamedIdentity;
import org.eclipse.osee.framework.jdk.core.type.NamedId;
import org.eclipse.osee.framework.jdk.core.type.NamedIdentity;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;

public final class TokenFactory {

   private TokenFactory() {
      // Utility Class
   }

   public static IArtifactType createArtifactType(long guid, String name) {
      return new ArtifactTypeToken(guid, name);
   }

   public static IAttributeType createAttributeType(long guid, String name) {
      return new AttributeTypeToken(guid, name);
   }

   public static IAttributeType createAttributeType(long guid, String name, String description) {
      return new AttributeTypeToken(guid, name, description);
   }

   public static IRelationType createRelationType(long guid, String name) {
      return new RelationTypeToken(guid, name);
   }

   public static IRelationTypeSide createRelationTypeSide(RelationSide relationSide, long guid, String name) {
      return new RelationTypeSideToken(guid, name, relationSide);
   }

   public static IAccessContextId createAccessContextId(String guid, String name) {
      return new AccessContextIdToken(guid, name);
   }

   public static ArtifactId createArtifactId(Long uuid) {
      return new ArtifactToken(uuid, null, null, null);
   }

   public static IArtifactToken createArtifactToken(long uuid, String name, IArtifactType artifactType) {
      Conditions.checkExpressionFailOnTrue(uuid <= 0, "Artifact Token Uuid must be > 0 for token [%s] type [%s]", name,
         artifactType);
      return new ArtifactToken(uuid, null, name, artifactType);
   }

   public static IArtifactToken createArtifactToken(long uuid, String guid, String name, IArtifactType artifactType) {
      Conditions.checkExpressionFailOnTrue(uuid <= 0, "Artifact Token Uuid must be > 0 for token [%s] type [%s]", name,
         artifactType);
      return new ArtifactToken(uuid, guid, name, artifactType);
   }

   public static IUserToken createUserToken(long uuid, String guid, String name, String email, String userId, boolean active, boolean admin, boolean creationRequired) {
      Conditions.checkExpressionFailOnTrue(uuid <= 0, "User Token Uuid must be > 0 for userId [%s]", userId);
      return new UserToken(uuid, guid, name, userId, active, admin, email, creationRequired);
   }

   public static IOseeBranch createBranch(String name) {
      return createBranch(Lib.generateUuid(), name);
   }

   public static IOseeBranch createBranch(Long branchId, String name) {
      return new BranchToken(branchId, name);
   }

   public static IOseeBranch createBranch(long branchId, String name) {
      return new BranchToken(branchId, name);
   }

   public static IOseeBranch createBranch(Long branchId) {
      return new BranchToken(branchId, null);
   }

   public static BranchId createBranch() {
      return new BranchToken(Lib.generateUuid(), null);
   }

   public static IRelationSorterId createSorterId(String guid, String name) {
      return new SorterIdToken(guid, name);
   }

   private final static class SorterIdToken extends NamedIdentity<String> implements IRelationSorterId {

      public SorterIdToken(String guid, String name) {
         super(guid, name);
      }

      @Override
      public String toString() {
         return String.format("[%s:%s]", getName(), getGuid());
      }
   }

   private final static class ArtifactTypeToken extends NamedId implements IArtifactType {
      public ArtifactTypeToken(Long id, String name) {
         super(id, name);
      }

      @Override
      public Long getGuid() {
         return getId();
      }
   }

   public static ITransaction createTransaction(int txId) {
      return new TransactionToken(txId);
   }

   private static final class TransactionToken extends BaseIdentity<Integer> implements ITransaction {
      public TransactionToken(Integer txId) {
         super(txId);
      }
   }

   private static final class BranchToken extends NamedId implements IOseeBranch {
      public BranchToken(Long branchId, String name) {
         super(branchId, name);
      }
   }

   private final static class AttributeTypeToken extends FullyNamedIdentity<Long> implements IAttributeType {
      public AttributeTypeToken(Long guid, String name) {
         super(guid, name);
      }

      public AttributeTypeToken(Long guid, String name, String description) {
         super(guid, name, description);
      }
   }

   private final static class RelationTypeToken extends FullyNamedIdentity<Long> implements IRelationType {
      public RelationTypeToken(Long guid, String name) {
         super(guid, name);
      }
   }

   private static class ArtifactToken extends NamedIdentity<String> implements IArtifactToken {
      private final IArtifactType artifactType;
      private final long uuid;

      public ArtifactToken(long uuid, String guid, String name, IArtifactType artifactType) {
         super(guid, name);
         this.uuid = uuid;
         this.artifactType = artifactType;
      }

      @Override
      public IArtifactType getArtifactType() {
         return artifactType;
      }

      @Override
      public Long getUuid() {
         return uuid;
      }
   }

   private static class UserToken extends NamedIdentity<String> implements IUserToken {

      private final String userId;
      private final boolean active;
      private final boolean admin;
      private final String email;
      private final boolean creationRequired;
      private final long uuid;

      public UserToken(long uuid, String guid, String name, String userId, boolean active, boolean admin, String email, boolean creationRequired) {
         super(guid, name);
         this.uuid = uuid;
         this.userId = userId;
         this.active = active;
         this.admin = admin;
         this.email = email;
         this.creationRequired = creationRequired;
      }

      @Override
      public IArtifactType getArtifactType() {
         return CoreArtifactTypes.User;
      }

      @Override
      public String getUserId() {
         return userId;
      }

      @Override
      public boolean isActive() {
         return active;
      }

      @Override
      public boolean isAdmin() {
         return admin;
      }

      @Override
      public String getEmail() {
         return email;
      }

      @Override
      public boolean isCreationRequired() {
         return creationRequired;
      }

      @Override
      public String toString() {
         return String.format("UserToken [userId=[%s], active=[%s], admin=[%s], email=[%s], creationRequired=[%s]",
            userId, active, admin, email, creationRequired);
      }

      @Override
      public Long getUuid() {
         return uuid;
      }
   }

   private final static class AccessContextIdToken extends NamedIdentity<String> implements IAccessContextId {
      public AccessContextIdToken(String guid, String name) {
         super(guid, name);
      }

      @Override
      public String toString() {
         return String.format("%s - %s", getName(), getGuid());
      }
   }

   private final static class RelationTypeSideToken extends FullyNamedIdentity<Long> implements IRelationTypeSide {

      private final RelationSide relationSide;
      private RelationTypeSideToken opposite;

      private RelationTypeSideToken(Long guid, String name, RelationSide relationSide) {
         super(guid, name);
         this.relationSide = relationSide;
      }

      @Override
      public RelationSide getSide() {
         return relationSide;
      }

      @Override
      public boolean isOfType(IRelationType type) {
         return type.getGuid() == getGuid();
      }

      @Override
      public int hashCode() {
         // Do not add relation side to hash code because it will violate the hash code contract
         return super.hashCode();
      }

      @Override
      public boolean equals(Object obj) {
         if (obj instanceof IRelationTypeSide) {
            IRelationTypeSide otherSide = (IRelationTypeSide) obj;
            if (relationSide != otherSide.getSide()) {
               return false;
            }
         }
         if (obj instanceof IRelationType) {
            return super.equals(obj);
         }
         return false;
      }

      @Override
      public synchronized IRelationTypeSide getOpposite() {
         if (opposite == null) {
            opposite = new RelationTypeSideToken(getGuid(), getName(), getSide().oppositeSide());
         }
         return opposite;
      }

      @Override
      public String toString() {
         return String.format("RelationTypeSide - uuid=[%s] type=[%s] side=[%s]", getGuid(), getName(), getSide());
      }
   }

}

Back to the top