Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68c395d090df3ae93ad0d52b3eb6aa56bcb7e881 (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
/*******************************************************************************
 * 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.framework.core.dsl.integration.internal;

import java.util.Collection;
import java.util.HashSet;
import org.eclipse.osee.framework.core.data.IAccessContextId;
import org.eclipse.osee.framework.core.dsl.integration.AccessModelInterpreter;
import org.eclipse.osee.framework.core.dsl.integration.ArtifactDataProvider;
import org.eclipse.osee.framework.core.dsl.integration.ArtifactDataProvider.ArtifactProxy;
import org.eclipse.osee.framework.core.dsl.integration.RestrictionHandler;
import org.eclipse.osee.framework.core.dsl.oseeDsl.AccessContext;
import org.eclipse.osee.framework.core.dsl.oseeDsl.HierarchyRestriction;
import org.eclipse.osee.framework.core.dsl.oseeDsl.ObjectRestriction;
import org.eclipse.osee.framework.core.dsl.oseeDsl.XArtifactMatcher;
import org.eclipse.osee.framework.core.model.access.AccessDetailCollector;
import org.eclipse.osee.framework.core.model.access.Scope;
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;

/**
 * @author Roberto E. Escobar
 */
public class AccessModelInterpreterImpl implements AccessModelInterpreter {

   private final ArtifactDataProvider provider;
   private final ArtifactMatchInterpreter matcher;
   private final Collection<RestrictionHandler<?>> restrictionHandlers;

   public AccessModelInterpreterImpl(ArtifactDataProvider provider, ArtifactMatchInterpreter matcher, RestrictionHandler<?>... restricitionHandlers) {
      this.provider = provider;
      this.matcher = matcher;
      this.restrictionHandlers = new HashSet<RestrictionHandler<?>>();
      for (RestrictionHandler<?> handler : restricitionHandlers) {
         restrictionHandlers.add(handler);
      }
   }

   @Override
   public AccessContext getContext(Collection<AccessContext> contexts, IAccessContextId contextId) throws OseeCoreException {
      Conditions.checkNotNull(contexts, "accessContext collection");
      Conditions.checkNotNull(contextId, "accessContextId");
      AccessContext toReturn = null;
      for (AccessContext accessContext : contexts) {
         if (contextId.getGuid().equals(Strings.unquote(accessContext.getGuid()))) {
            toReturn = accessContext;
         }
      }
      return toReturn;
   }

   @Override
   public void computeAccessDetails(AccessDetailCollector collector, AccessContext context, Object objectToCheck) throws OseeCoreException {
      Conditions.checkNotNull(collector, "accessDetailCollector");
      Conditions.checkNotNull(context, "accessContext");
      Conditions.checkNotNull(objectToCheck, "objectToCheck");

      if (provider.isApplicable(objectToCheck)) {
         ArtifactProxy data = provider.asCastedObject(objectToCheck);
         Conditions.checkNotNull(data, "artifactData",
            "artifact data provider returned null - provider has an isApplicable error");

         collectApplicable(collector, context, data);
      }
   }

   private void collectApplicable(AccessDetailCollector collector, AccessContext context, ArtifactProxy artifactData) throws OseeCoreException {
      Scope scope = getScope(context);
      processContext(collector, context, artifactData, scope);

      for (AccessContext superContext : context.getSuperAccessContexts()) {
         collectApplicable(collector, superContext, artifactData);
      }
   }

   private Scope getScope(AccessContext context) {
      Scope scope = new Scope();
      scopeHelper(scope, context);
      return scope;
   }

   private void scopeHelper(Scope scope, AccessContext context) {
      for (AccessContext parent : context.getSuperAccessContexts()) {
         scopeHelper(scope, parent);
      }
      scope.add(context.getName());
   }

   private void processContext(AccessDetailCollector collector, AccessContext context, ArtifactProxy artifactData, Scope scope) throws OseeCoreException {
      collectRestrictions(collector, artifactData, context.getAccessRules(), scope);

      Collection<HierarchyRestriction> restrictions = context.getHierarchyRestrictions();
      Collection<ArtifactProxy> proxyHierarchy = artifactData.getHierarchy();

      for (HierarchyRestriction hierarchy : restrictions) {
         XArtifactMatcher artifactRef = hierarchy.getArtifactMatcherRef();
         if (matcher.matches(artifactRef, proxyHierarchy)) {
            String tag = String.format("childOf-%s", artifactRef.getName());
            Scope child = scope.clone().addSubPath(tag);
            collectRestrictions(collector, artifactData, hierarchy.getAccessRules(), child);
         }
      }
   }

   private void collectRestrictions(AccessDetailCollector collector, ArtifactProxy artifactData, Collection<ObjectRestriction> restrictions, Scope scope) throws OseeCoreException {
      for (ObjectRestriction objectRestriction : restrictions) {
         for (RestrictionHandler<?> restrictionHandler : restrictionHandlers) {
            restrictionHandler.process(objectRestriction, artifactData, collector, scope);
         }
      }
   }

}

Back to the top