Skip to main content
summaryrefslogtreecommitdiffstats
blob: 38ca3b3f883a4bed91978f4910c4a17fd30f9ced (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
/*******************************************************************************
 * Copyright (c) 2016 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.define.report.internal.wordupdate;

import java.util.Arrays;
import java.util.Date;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsArtifactToken;
import org.eclipse.osee.ats.api.data.AtsArtifactTypes;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.util.IAtsChangeSet;
import org.eclipse.osee.ats.api.workflow.IAtsAction;
import org.eclipse.osee.ats.api.workflow.IAtsTeamWorkflow;
import org.eclipse.osee.ats.api.workflow.NewActionAdapter;
import org.eclipse.osee.ats.core.users.AtsCoreUsers;
import org.eclipse.osee.ats.rest.IAtsServer;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
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.Conditions;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

/**
 * @author David W. Miller
 */
public class SafetyWorkflowEventHandler implements EventHandler {
   public final static String SAFETY_EVENT_TOPIC = "org/eclipse/osee/define/safetyworkflow/modified";
   public final static String SAFETY_EVENT_BRANCH_ID = "org/eclipse/osee/define/report/internal/BRANCH_ID";
   public final static String SAFETY_EVENT_USER_ART = "org/eclipse/osee/define/report/internal/USER_ART";

   private QueryFactory queryFactory;
   private IAtsServer atsServer;
   private Log logger;

   public SafetyWorkflowEventHandler() {
      /**
       * this is placed here to remind developers that the osgi component this class implements needs a no argument
       * constructor
       */
   }

   public void setOrcsApi(OrcsApi orcsApi) {
      this.queryFactory = orcsApi.getQueryFactory();
   }

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   public void setAtsServer(IAtsServer atsServer) {
      this.atsServer = atsServer;
   }

   @Override
   public void handleEvent(Event event) {
      try {
         Object branchId = event.getProperty(SAFETY_EVENT_BRANCH_ID);
         Object userArt = event.getProperty(SAFETY_EVENT_USER_ART);

         checkEventObjects(branchId, userArt); // throws exception if incorrect
         ArtifactReadable assocArt = getAssociatedWorkflowArt((BranchId) branchId);
         if (assocArt != null) {
            IAtsTeamWorkflow safetyWf = getSafetyWorkflow(assocArt);
            if (safetyWf == null) {
               IAtsTeamWorkflow teamWf = atsServer.getWorkItemFactory().getTeamWf(assocArt);
               safetyWf = createSafetyAction(teamWf, (ArtifactReadable) userArt);
            }
         }
      } catch (Exception ex) {
         logger.error(ex, "Could not create safety workflow");
      }
   }

   private void checkEventObjects(Object branchId, Object userArt) throws OseeArgumentException {
      if (!(branchId instanceof BranchId)) {
         throw new OseeArgumentException("BranchID provided to safety workflow creation event incorrect type: %s",
            branchId.getClass());
      }
      if (!(userArt instanceof ArtifactReadable)) {
         throw new OseeArgumentException("User Artifact provided to safety workflow creation event incorrect type: %s",
            userArt.toString());
      }
   }

   private ArtifactReadable getAssociatedWorkflowArt(BranchId branchId) {
      ArtifactReadable toReturn = null;
      try {
         BranchReadable branch = queryFactory.branchQuery().andIds(branchId).getResults().getOneOrNull();
         if (branch != null) {
            long workflowUuid = branch.getAssociatedArtifactId();
            toReturn = atsServer.getQuery().andUuid(workflowUuid).andIsOfType(
               AtsArtifactTypes.TeamWorkflow).getResults().getOneOrNull();
         }
      } catch (Exception ex) {
         // do not throw exception when associated artifact is osee system or doesn't exist
      }
      return toReturn;
   }

   private IAtsTeamWorkflow getSafetyWorkflow(ArtifactReadable workflowArt) {
      Conditions.checkNotNull(workflowArt, "work flow artifact");
      IAtsTeamWorkflow safetyWorkflow = null;
      ArtifactReadable safetyActionableItemArt = atsServer.getArtifact(AtsArtifactToken.SafetyActionableItem.getUuid());
      IAtsTeamWorkflow teamWf = atsServer.getWorkItemFactory().getTeamWf(workflowArt);
      IAtsActionableItem actionableItem = atsServer.getConfigItemFactory().getActionableItem(safetyActionableItemArt);
      for (IAtsTeamWorkflow sibling : atsServer.getActionFactory().getSiblingTeamWorkflows(teamWf)) {
         if (sibling.getActionableItems().contains(actionableItem)) {
            safetyWorkflow = sibling;
            break;
         }
      }
      return safetyWorkflow;
   }

   private IAtsTeamWorkflow createSafetyAction(IAtsTeamWorkflow teamWf, ArtifactReadable userArt) {
      IAtsTeamWorkflow teamWorkflow = null;
      try {
         IAtsActionableItem ai =
            atsServer.getCache().getByUuid(AtsArtifactToken.SafetyActionableItem.getUuid(), IAtsActionableItem.class);
         if (ai == null) {
            throw new OseeCoreException("Safety Actionable Item not configured");
         }
         IAtsTeamDefinition teamDef =
            atsServer.getCache().getByUuid(AtsArtifactToken.SafetyTeamDefinition.getUuid(), IAtsTeamDefinition.class);
         if (teamDef == null) {
            throw new OseeCoreException("Safety Team Definition not configured");
         }
         IAtsUser createdBy = AtsCoreUsers.SYSTEM_USER;
         IAtsChangeSet changes = atsServer.getStoreService().createAtsChangeSet("Create System Safety Workflow",
            atsServer.getUserService().getUserById(userArt.getSoleAttributeAsString(CoreAttributeTypes.UserId)));
         IAtsAction action = atsServer.getActionFactory().getAction(teamWf);
         teamWorkflow =
            atsServer.getActionFactory().createTeamWorkflow(action, teamDef, java.util.Collections.singleton(ai),
               Arrays.asList(AtsCoreUsers.UNASSIGNED_USER), changes, new Date(), createdBy, new NewActionAdapter() {

                  @Override
                  public void teamCreated(IAtsAction action, IAtsTeamWorkflow teamWf, IAtsChangeSet changes) throws OseeCoreException {
                     changes.setSoleAttributeValue(teamWf, AtsAttributeTypes.Description,
                        "Review System Safety Changes for the associated RPCR to Complete the Workflow");
                  }

               });
         changes.setSoleAttributeValue(teamWorkflow, CoreAttributeTypes.Name,
            "Safety Workflow for " + teamWf.getAtsId());
         changes.execute();
      } catch (Exception ex) {
         logger.error(ex, "WordUpdateData Safety Action creation");
      }
      return teamWorkflow;
   }

}

Back to the top