Skip to main content
summaryrefslogtreecommitdiffstats
blob: 629b5b7252a45d18a11c8c38f295a9a413e8a4ae (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
/*******************************************************************************
 * 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.ats.core.client.review.role;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.notify.AtsNotifyType;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.core.client.internal.Activator;
import org.eclipse.osee.ats.core.client.internal.AtsClientService;
import org.eclipse.osee.ats.core.client.notify.AtsNotificationManager;
import org.eclipse.osee.ats.core.client.review.PeerToPeerReviewArtifact;
import org.eclipse.osee.ats.core.client.review.defect.ReviewDefectManager;
import org.eclipse.osee.ats.core.client.validator.ArtifactValueProvider;
import org.eclipse.osee.ats.core.client.workflow.AbstractWorkflowArtifact;
import org.eclipse.osee.ats.core.validator.IValueProvider;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.AHTML;
import org.eclipse.osee.framework.jdk.core.util.AXml;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLevel;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;

/**
 * @author Donald G. Dunne
 */
public class UserRoleManager {

   private final static String ROLE_ITEM_TAG = "Role";
   private static final IAttributeType ATS_ROLE_STORAGE_TYPE = AtsAttributeTypes.Role;

   private final Matcher roleMatcher = java.util.regex.Pattern.compile(
      "<" + ROLE_ITEM_TAG + ">(.*?)</" + ROLE_ITEM_TAG + ">", Pattern.DOTALL | Pattern.MULTILINE).matcher("");
   private final IValueProvider valueProvider;
   private List<UserRole> roles;

   public UserRoleManager(Artifact artifact) {
      this(new ArtifactValueProvider(artifact, ATS_ROLE_STORAGE_TYPE));
   }

   public UserRoleManager(IValueProvider valueProvider) {
      this.valueProvider = valueProvider;
   }

   public static String getHtml(PeerToPeerReviewArtifact peerArt) throws OseeCoreException {
      if (getUserRoles(peerArt).isEmpty()) {
         return "";
      }
      StringBuffer sb = new StringBuffer();
      sb.append(AHTML.addSpace(1) + AHTML.getLabelStr(AHTML.LABEL_FONT, "Roles"));
      sb.append(getTable(peerArt));
      return sb.toString();
   }

   public static List<UserRole> getUserRoles(Artifact artifact) throws OseeCoreException {
      return new UserRoleManager(artifact).getUserRoles();
   }

   public void ensureLoaded() throws OseeCoreException {
      if (roles == null) {
         roles = new ArrayList<UserRole>();
         for (String xml : valueProvider.getValues()) {
            roleMatcher.reset(xml);
            while (roleMatcher.find()) {
               UserRole item = new UserRole(roleMatcher.group());
               roles.add(item);
            }
         }
      }
   }

   public List<UserRole> getUserRoles() throws OseeCoreException {
      ensureLoaded();
      return roles;
   }

   public List<UserRole> getUserRoles(Role role) throws OseeCoreException {
      List<UserRole> roles = new ArrayList<UserRole>();
      for (UserRole uRole : getUserRoles()) {
         if (uRole.getRole() == role) {
            roles.add(uRole);
         }
      }
      return roles;
   }

   public List<IAtsUser> getRoleUsers(Role role) throws OseeCoreException {
      List<IAtsUser> users = new ArrayList<IAtsUser>();
      for (UserRole uRole : getUserRoles()) {
         if (uRole.getRole() == role && !users.contains(uRole.getUser())) {
            users.add(uRole.getUser());
         }
      }
      return users;
   }

   @SuppressWarnings("deprecation")
   private List<UserRole> getStoredUserRoles(Artifact artifact) throws OseeCoreException {
      // Add new ones: items in userRoles that are not in dbuserRoles
      List<UserRole> storedUserRoles = new ArrayList<UserRole>();
      for (Attribute<?> attr : artifact.getAttributes(ATS_ROLE_STORAGE_TYPE)) {
         UserRole storedRole = new UserRole((String) attr.getValue());
         storedUserRoles.add(storedRole);
      }
      return storedUserRoles;
   }

   public void saveToArtifact(IAtsChangeSet changes) throws OseeCoreException {
      if (valueProvider instanceof ArtifactValueProvider) {
         saveToArtifact(((ArtifactValueProvider) valueProvider).getArtifact(), changes);
      } else {
         throw new OseeArgumentException(
            "Can't saveToArtifact unless provider is ArtifactValueProvider, use saveToArtifact(Artifact artifact, SkynetTransaction transaction)");
      }
   }

   @SuppressWarnings("deprecation")
   public void saveToArtifact(Artifact artifact, IAtsChangeSet changes) {
      try {
         List<UserRole> storedUserRoles = getStoredUserRoles(artifact);

         // Change existing ones
         for (Attribute<?> attr : artifact.getAttributes(ATS_ROLE_STORAGE_TYPE)) {
            UserRole storedRole = new UserRole((String) attr.getValue());
            for (UserRole pItem : getUserRoles()) {
               if (pItem.equals(storedRole)) {
                  attr.setFromString(AXml.addTagData(ROLE_ITEM_TAG, pItem.toXml()));
               }
            }
         }
         List<UserRole> userRoles = getUserRoles();
         List<UserRole> updatedStoredUserRoles = getStoredUserRoles(artifact);
         // Remove deleted ones; items in dbuserRoles that are not in userRoles
         for (UserRole delUserRole : org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(
            updatedStoredUserRoles, userRoles)) {
            for (Attribute<?> attr : artifact.getAttributes(ATS_ROLE_STORAGE_TYPE)) {
               UserRole storedRole = new UserRole((String) attr.getValue());
               if (storedRole.equals(delUserRole)) {
                  attr.delete();
               }
            }
         }
         for (UserRole newRole : org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(userRoles,
            updatedStoredUserRoles)) {
            artifact.addAttributeFromString(ATS_ROLE_STORAGE_TYPE, AXml.addTagData(ROLE_ITEM_TAG, newRole.toXml()));
         }
         rollupHoursSpentToReviewState(artifact);
         validateUserRolesCompleted(artifact, storedUserRoles, userRoles);
         changes.add(artifact);
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, "Can't create ats review role document", ex);
      }
   }

   private void validateUserRolesCompleted(Artifact artifact, List<UserRole> currentUserRoles, List<UserRole> newUserRoles) {
      //all reviewers are complete; send notification to author/moderator
      int numCurrentCompleted = 0, numNewCompleted = 0;
      for (UserRole role : newUserRoles) {
         if (role.getRole() == Role.Reviewer) {
            if (!role.isCompleted()) {
               return;
            } else {
               numNewCompleted++;
            }
         }
      }
      for (UserRole role : currentUserRoles) {
         if (role.getRole() == Role.Reviewer) {
            if (role.isCompleted()) {
               numCurrentCompleted++;
            }
         }
      }
      if (numNewCompleted != numCurrentCompleted) {
         try {
            AtsNotificationManager.notify((AbstractWorkflowArtifact) artifact, AtsNotifyType.Peer_Reviewers_Completed);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      }
   }

   public void addOrUpdateUserRole(UserRole userRole, PeerToPeerReviewArtifact peerArt) throws OseeCoreException {
      List<UserRole> roleItems = getUserRoles();
      boolean found = false;
      for (UserRole uRole : roleItems) {
         if (userRole.equals(uRole)) {
            uRole.update(userRole);
            found = true;
         }
      }
      if (!found) {
         roleItems.add(userRole);
         if (!peerArt.getAssignees().contains(userRole.getUser())) {
            peerArt.getStateMgr().addAssignee(userRole.getUser());
         }
      }
   }

   public void removeUserRole(UserRole userRole) throws OseeCoreException {
      List<UserRole> roleItems = getUserRoles();
      roleItems.remove(userRole);
   }

   public static String getTable(PeerToPeerReviewArtifact peerArt) throws OseeCoreException {
      StringBuilder builder = new StringBuilder();
      builder.append("<TABLE BORDER=\"1\" cellspacing=\"1\" cellpadding=\"3%\" width=\"100%\"><THEAD><TR><TH>Role</TH>" + "<TH>User</TH><TH>Hours</TH><TH>Major</TH><TH>Minor</TH><TH>Issues</TH>");
      for (UserRole item : getUserRoles(peerArt)) {
         IAtsUser user = item.getUser();
         String name = "";
         if (user != null) {
            name = user.getName();
            if (!Strings.isValid(name)) {
               name = user.getName();
            }
         }
         builder.append("<TR>");
         builder.append("<TD>" + item.getRole().name() + "</TD>");
         builder.append("<TD>" + item.getUser().getName() + "</TD>");
         builder.append("<TD>" + item.getHoursSpentStr() + "</TD>");

         ReviewDefectManager defectMgr = new ReviewDefectManager(peerArt);
         builder.append("<TD>" + defectMgr.getNumMajor(item.getUser()) + "</TD>");
         builder.append("<TD>" + defectMgr.getNumMinor(item.getUser()) + "</TD>");
         builder.append("<TD>" + defectMgr.getNumIssues(item.getUser()) + "</TD>");
         builder.append("</TR>");
      }
      builder.append("</TABLE>");
      return builder.toString();
   }

   private void rollupHoursSpentToReviewState(Artifact artifact) throws OseeCoreException {
      double hoursSpent = 0.0;
      for (UserRole role : getUserRoles()) {
         hoursSpent += role.getHoursSpent() == null ? 0 : role.getHoursSpent();
      }
      AbstractWorkflowArtifact awa = (AbstractWorkflowArtifact) artifact;
      awa.getStateMgr().setMetrics(awa.getStateDefinition(), hoursSpent,
         awa.getStateMgr().getPercentComplete(awa.getCurrentStateName()), true,
         AtsClientService.get().getUserAdmin().getCurrentUser(), new Date());
   }
}

Back to the top