Skip to main content
summaryrefslogtreecommitdiffstats
blob: 428f90e963dd955c99471852b009a1ce610740de (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
/*******************************************************************************
 * 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.access;

import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.osee.ats.artifact.ActionableItemArtifact;
import org.eclipse.osee.ats.artifact.TeamWorkFlowArtifact;
import org.eclipse.osee.ats.internal.AtsPlugin;
import org.eclipse.osee.ats.util.AtsArtifactTypes;
import org.eclipse.osee.framework.core.data.AccessContextId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * This class will return access context ids for artifacts stored on a team workflow's related branch
 */
public class AtsBranchObjectManager implements AtsAccessContextIdResolver {

   private final Collection<IAtsAccessControlService> atsAccessServices;

   private final IOseeBranch atsBranch;

   public AtsBranchObjectManager(IOseeBranch atsBranch, Collection<IAtsAccessControlService> atsAccessServices) {
      this.atsBranch = atsBranch;
      this.atsAccessServices = atsAccessServices;
   }

   private ArtifactType getAssociatedArtifactType(Branch objectBranch) throws OseeCoreException {
      Artifact assocArtifact = ArtifactQuery.getArtifactFromId(objectBranch.getAssociatedArtifactId(), atsBranch);
      return assocArtifact.getArtifactType();
   }

   public boolean isApplicable(Branch objectBranch) throws OseeCoreException {
      boolean result = false;
      if (!atsBranch.equals(objectBranch)) {
         ArtifactType assocArtType = getAssociatedArtifactType(objectBranch);
         if (assocArtType != null) {
            result = assocArtType.inheritsFrom(AtsArtifactTypes.TeamWorkflow);
         }
      }
      return result;
   }

   public AccessContextId getContextId(Artifact artifact) {
      AccessContextId contextId = null;
      try {
         // If artifact has a context id on it, use that
         contextId = getFromArtifact(artifact);
         if (contextId == null) {
            Artifact assocArtifact =
               ArtifactQuery.getArtifactFromId(artifact.getBranch().getAssociatedArtifactId(), atsBranch);
            ArtifactType assocArtType = assocArtifact.getArtifactType();
            if (assocArtType.inheritsFrom(AtsArtifactTypes.TeamWorkflow)) {
               contextId = getFromWorkflow((TeamWorkFlowArtifact) assocArtifact);
            } else if (assocArtifact.isOfType(AtsArtifactTypes.AtsArtifact)) {
               contextId = AtsBranchObjectContextId.DENY_CONTEXT;
            } else {
               contextId = AtsBranchObjectContextId.DEFAULT_BRANCH_CONTEXT;
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(AtsPlugin.class, Level.SEVERE, "Exception obtaining Branch Access Context Id; Deny returned", ex);
         contextId = AtsBranchObjectContextId.DENY_CONTEXT;
      }
      return contextId;
   }

   private AccessContextId getFromWorkflow(TeamWorkFlowArtifact teamArt) {
      try {
         if (atsAccessServices != null) {
            for (IAtsAccessControlService service : atsAccessServices) {
               IAtsAccessControlService accessService = service;
               AccessContextId contextId = accessService.getBranchAccessContextIdFromWorkflow(teamArt);
               if (contextId != null) {
                  return contextId;
               }
            }
         }
         for (ActionableItemArtifact aia : teamArt.getActionableItemsDam().getActionableItems()) {
            AccessContextId contextId = getFromArtifact(aia);
            if (contextId != null) {
               return contextId;
            }
         }
         AccessContextId contextId = getFromArtifact(teamArt.getTeamDefinition());
         if (contextId != null) {
            return contextId;
         }
      } catch (Exception ex) {
         OseeLog.log(AtsPlugin.class, Level.SEVERE, "Exception obtaining Branch Access Context Id; Deny returned", ex);
         return AtsBranchObjectContextId.DENY_CONTEXT;
      }
      return null;
   }

   private AccessContextId getFromArtifact(Artifact artifact) {
      if (artifact.isOfType(CoreArtifactTypes.AbstractAccessControlled)) {
         try {
            List<String> attributes = artifact.getAttributesToStringList(CoreAttributeTypes.AccessContextId);
            if (!attributes.isEmpty()) {
               if (attributes.size() > 1) {
                  OseeLog.log(
                     AtsPlugin.class,
                     Level.SEVERE,
                     String.format("Unexpected multiple context ids [%s] for artifact [%s][%s]", attributes,
                        artifact.getHumanReadableId(), artifact));

               }
               String guid = attributes.iterator().next();
               return AtsAccessContextIdFactory.getOrCreate(guid);
            }
         } catch (OseeCoreException ex) {
            OseeLog.log(AtsPlugin.class, Level.SEVERE, ex);
         }
      }
      return null;
   }
}

Back to the top