Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3ad6cc50d78a38b0b706682617aec0333c78a77d (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
/*******************************************************************************
 * 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.Collection;
import java.util.LinkedList;
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.user.IAtsUser;
import org.eclipse.osee.ats.api.user.IAtsUserService;
import org.eclipse.osee.ats.impl.internal.util.AtsUtilServer;
import org.eclipse.osee.framework.core.data.ResultSet;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
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.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 implements IAtsUserService {

   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 IAtsUser getCurrentUser() throws OseeCoreException {
      return getUserById(SystemUser.OseeSystem.getUserId());
   }

   @Override
   public IAtsUser getUserById(String userId) throws OseeCoreException {
      IAtsUser atsUser = null;
      if (Strings.isValid(userId)) {
         ResultSet<ArtifactReadable> results =
            orcsApi.getQueryFactory(AtsUtilServer.getApplicationContext()).fromBranch(CoreBranches.COMMON).andIsOfType(
               CoreArtifactTypes.User).and(CoreAttributeTypes.UserId,
               org.eclipse.osee.framework.core.enums.Operator.EQUAL, userId).getResults();
         if (!results.isEmpty()) {
            ArtifactReadable userArt = results.getExactlyOne();
            atsUser = new AtsUser(userArt);
         }
      }
      return atsUser;
   }

   @Override
   public Collection<IAtsUser> getUsersByUserIds(Collection<String> userIds) throws OseeCoreException {
      List<IAtsUser> users = new LinkedList<IAtsUser>();
      for (String userId : userIds) {
         IAtsUser user = getUserById(userId);
         if (user != null) {
            users.add(user);
         }
      }
      return users;
   }

   @Override
   public IAtsUser getUserByName(String name) throws OseeCoreException {
      IAtsUser atsUser = null;
      if (Strings.isValid(name)) {
         ArtifactReadable userArt =
            orcsApi.getQueryFactory(AtsUtilServer.getApplicationContext()).fromBranch(CoreBranches.COMMON).andIsOfType(
               CoreArtifactTypes.User).and(CoreAttributeTypes.Name,
               org.eclipse.osee.framework.core.enums.Operator.EQUAL, name).getResults().getExactlyOne();
         if (userArt != null) {
            atsUser = new AtsUser(userArt);
         }
      }
      return atsUser;
   }

   @Override
   public boolean isUserIdValid(String userId) throws OseeCoreException {
      return getUserById(userId) != null;
   }

   @Override
   public boolean isUserNameValid(String name) throws OseeCoreException {
      return getUserByName(name) != null;
   }

   @Override
   public String getUserIdByName(String name) throws OseeCoreException {
      String userId = null;
      IAtsUser userByName = getUserByName(name);
      if (userByName != null) {
         userId = userByName.getUserId();
      }
      return userId;
   }

   @Override
   public boolean isAtsAdmin(IAtsUser user) {
      boolean admin =
         orcsApi.getQueryFactory(null).fromBranch(AtsUtilServer.getAtsBranch()).andGuid(
            AtsArtifactToken.AtsAdmin.getGuid()).andRelatedTo(CoreRelationTypes.Users_User, getUserArt(user)).getCount() == 1;
      return admin;
   }

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

   @Override
   public boolean isAssigneeMe(IAtsWorkItem workItem) throws OseeCoreException {
      return workItem.getStateMgr().getAssignees().contains(getCurrentUser());
   }

   public static ArtifactReadable getCurrentUserArt() throws OseeCoreException {
      // TODO Switch to real user
      return orcsApi.getQueryFactory(AtsUtilServer.getApplicationContext()).fromBranch(CoreBranches.COMMON).andIsOfType(
         CoreArtifactTypes.User).and(CoreAttributeTypes.UserId, org.eclipse.osee.framework.core.enums.Operator.EQUAL,
         SystemUser.OseeSystem.getUserId()).getResults().getExactlyOne();
   }
}

Back to the top