Skip to main content
summaryrefslogtreecommitdiffstats
blob: 45fab94ca76f0333a5a7d7ac9198529a76c78c60 (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
/*******************************************************************************
 * 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.impl.internal.user;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.ats.api.IAtsWorkItem;
import org.eclipse.osee.ats.api.data.AtsArtifactToken;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.core.users.AbstractAtsUserService;
import org.eclipse.osee.ats.core.util.AtsUtilCore;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.Active;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;

/**
 * Non-artifact base user service
 * 
 * @author Donald G Dunne
 */
public class AtsUserServiceImpl extends AbstractAtsUserService {

   private static OrcsApi orcsApi;

   public static void setOrcsApi(OrcsApi orcsApi) {
      AtsUserServiceImpl.orcsApi = orcsApi;
   }

   public void start() throws OseeCoreException {
      Conditions.checkNotNull(orcsApi, "OrcsApi");
      System.out.println("ATS - AtsUserService started");
   }

   @Override
   public String getCurrentUserId() throws OseeCoreException {
      if (currentUserId == null) {
         currentUserId = SystemUser.OseeSystem.getUserId();
      }
      return currentUserId;
   }

   // TODO Replace this once server has user account
   @Override
   public IAtsUser getCurrentUser() throws OseeCoreException {
      if (currentUser == null) {
         currentUser = getUserById(getCurrentUserId());
      }
      return currentUser;
   }

   @Override
   protected IAtsUser loadUserByUserIdFromDb(String userId) {
      IAtsUser atsUser = null;
      ResultSet<ArtifactReadable> results =
         orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(CoreArtifactTypes.User).and(
            CoreAttributeTypes.UserId, userId).getResults();
      if (!results.isEmpty()) {
         ArtifactReadable userArt = results.getExactlyOne();
         atsUser = new AtsUser(userArt);
      }
      return atsUser;
   }

   @Override
   protected IAtsUser loadUserByUserNameFromDb(String name) {
      IAtsUser atsUser = null;
      ArtifactReadable userArt =
         orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(CoreArtifactTypes.User).and(
            CoreAttributeTypes.Name, name).getResults().getExactlyOne();
      if (userArt != null) {
         atsUser = new AtsUser(userArt);
      }
      return atsUser;
   }

   @Override
   public boolean isAtsAdmin(IAtsUser user) {
      ensureLoaded();
      Boolean admin = userIdToAdmin.get(user.getUserId());
      if (admin == null) {
         admin =
            orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andGuid(
               AtsArtifactToken.AtsAdmin.getGuid()).andRelatedTo(CoreRelationTypes.Users_User, getUserArt(user)).getCount() == 1;
         userIdToAdmin.put(user.getUserId(), admin);
      }
      return admin;
   }

   private ArtifactReadable getUserArt(IAtsUser user) {
      ensureLoaded();
      if (user.getStoreObject() instanceof ArtifactReadable) {
         return (ArtifactReadable) user.getStoreObject();
      }
      return orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andGuid(user.getGuid()).getResults().getExactlyOne();
   }

   public static ArtifactReadable getCurrentUserArt() throws OseeCoreException {
      // TODO Switch to real user
      return orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(CoreArtifactTypes.User).and(
         CoreAttributeTypes.UserId, SystemUser.OseeSystem.getUserId()).getResults().getExactlyOne();
   }

   @Override
   public List<IAtsUser> getUsers(Active active) {
      ensureLoaded();
      List<IAtsUser> users = new ArrayList<IAtsUser>();
      for (ArtifactReadable userArt : orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(
         CoreArtifactTypes.User).getResults()) {
         Boolean activeFlag = userArt.getSoleAttributeValue(CoreAttributeTypes.Active, true);
         if (active == Active.Both || ((active == Active.Active) && activeFlag) || ((active == Active.InActive) && !activeFlag)) {
            users.add(new AtsUser(userArt));
         }
      }
      return users;
   }

   @Override
   protected synchronized void ensureLoaded() {
      if (!loaded) {
         for (ArtifactReadable art : orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(
            CoreArtifactTypes.User).getResults()) {
            AtsUser atsUser = new AtsUser(art);
            userIdToAtsUser.put(art.getSoleAttributeValue(CoreAttributeTypes.UserId, ""), atsUser);
            nameToAtsUser.put(art.getName(), atsUser);
         }
         loaded = true;
      }
   }

   @Override
   public boolean currentUserHasAccessToAtsBranch(Long branchUuid) {
      boolean hasPermission = false;
      IOseeBranch configAtsBranch = TokenFactory.createBranch(branchUuid, "ATS Branch");
      if (!orcsApi.getQueryFactory(null).branchQuery().andIds(configAtsBranch).getResults().isEmpty()) {
         String userId = getCurrentUserId();
         if (Strings.isValid(userId) && !userId.equals(SystemUser.Guest.getUserId())) {
            ResultSet<ArtifactReadable> results =
               orcsApi.getQueryFactory(null).fromBranch(AtsUtilCore.getAtsBranch()).andIsOfType(CoreArtifactTypes.User).and(
                  CoreAttributeTypes.UserId, userId).getResults();
            hasPermission = (results.size() == 1);
         }
      }
      return hasPermission;
   }

   @Override
   public List<IAtsUser> getSubscribed(IAtsWorkItem workItem) throws OseeCoreException {
      ArrayList<IAtsUser> arts = new ArrayList<IAtsUser>();
      for (ArtifactReadable art : ((ArtifactReadable) workItem.getStoreObject()).getRelated(AtsRelationTypes.SubscribedUser_User)) {
         arts.add(getUserById((String) art.getSoleAttributeValue(CoreAttributeTypes.UserId)));
      }
      return arts;
   }

}

Back to the top