KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > CubeAccess


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/CubeAccess.java#9 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 1999-2002 Kana Software, Inc.
7 // Copyright (C) 2001-2006 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // lkrivopaltsev, 01 November, 1999
12 */

13
14 package mondrian.olap;
15 import mondrian.resource.MondrianResource;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 /**
21  * This class implements object of type GrantCube to apply permissions
22  * on user's MDX query
23  */

24 public class CubeAccess {
25
26     private boolean hasRestrictions;
27     /** array of hierarchies with no access */
28     private Hierarchy[] noAccessHierarchies;
29     /** array of members which have limited access */
30     private Member[] limitedMembers;
31     private final List JavaDoc<Hierarchy> hierarchyList = new ArrayList JavaDoc<Hierarchy>();
32     private final List JavaDoc<Member> memberList = new ArrayList JavaDoc<Member>();
33     private final Cube mdxCube;
34
35     /**
36      * Creates a CubeAccess object.
37      *
38      * <p>User's code should be responsible for
39      * filling cubeAccess with restricted hierarchies and restricted
40      * members by calling addSlicer(). Do NOT forget to call
41      * {@link #normalizeCubeAccess()} after you done filling cubeAccess.
42      */

43     public CubeAccess(Cube mdxCube) {
44         this.mdxCube = mdxCube;
45         noAccessHierarchies = null;
46         limitedMembers = null;
47         hasRestrictions = false;
48     }
49
50     public boolean hasRestrictions() {
51         return hasRestrictions;
52     }
53
54     public Hierarchy[] getNoAccessHierarchies() {
55         return noAccessHierarchies;
56     }
57
58     public Member[] getLimitedMembers() {
59         return limitedMembers;
60     }
61
62     public List JavaDoc<Hierarchy> getNoAccessHierarchyList() {
63         return hierarchyList;
64     }
65
66     public List JavaDoc<Member> getLimitedMemberList() {
67         return memberList;
68     }
69
70     public boolean isHierarchyAllowed(Hierarchy mdxHierarchy) {
71         String JavaDoc hierName = mdxHierarchy.getUniqueName();
72         if(noAccessHierarchies == null || hierName == null) {
73             return true;
74         }
75         for (Hierarchy noAccessHierarchy : noAccessHierarchies) {
76             if (hierName.equalsIgnoreCase(noAccessHierarchy.getUniqueName())) {
77                 return false;
78             }
79         }
80         return true;
81     }
82
83     public Member getLimitedMemberForHierarchy(Hierarchy mdxHierarchy) {
84         String JavaDoc hierName = mdxHierarchy.getUniqueName();
85         if (limitedMembers == null || hierName == null) {
86             return null;
87         }
88         for (Member limitedMember : limitedMembers) {
89             Hierarchy limitedHierarchy = limitedMember.getHierarchy();
90             if (hierName.equalsIgnoreCase(limitedHierarchy.getUniqueName())) {
91                 return limitedMember;
92             }
93         }
94         return null;
95     }
96
97     /**
98      * Adds restricted hierarchy or limited member based on bMember
99      */

100     public void addGrantCubeSlicer(String JavaDoc sHierarchy,
101                                    String JavaDoc sMember,
102                                    boolean bMember) {
103         if (bMember) {
104             boolean fail = false;
105             String JavaDoc[] sMembers = Util.explode(sMember);
106             SchemaReader schemaReader = mdxCube.getSchemaReader(null);
107             Member member = schemaReader.getMemberByUniqueName(sMembers, fail);
108             if (member == null) {
109                 throw MondrianResource.instance().MdxCubeSlicerMemberError.ex(
110                     sMember, sHierarchy, mdxCube.getUniqueName());
111             }
112             // there should be only slicer per hierarchy; ignore the rest
113
if (getLimitedMemberForHierarchy(member.getHierarchy()) == null) {
114                 memberList.add(member);
115             }
116         } else {
117             boolean fail = false;
118             Hierarchy hierarchy = mdxCube.lookupHierarchy(sHierarchy, fail);
119             if (hierarchy == null) {
120                 throw MondrianResource.instance().MdxCubeSlicerHierarchyError.ex(
121                     sHierarchy, mdxCube.getUniqueName());
122             }
123             hierarchyList.add(hierarchy);
124         }
125     }
126
127     /**
128      * Initializes internal arrays of restricted hierarchies and limited
129      * members. It has to be called after all 'addSlicer()' calls.
130      */

131     public void normalizeCubeAccess() {
132         if (memberList.size() > 0) {
133             limitedMembers = memberList.toArray(new Member[memberList.size()]);
134             hasRestrictions = true;
135         }
136         if (hierarchyList.size() > 0) {
137             noAccessHierarchies =
138                 hierarchyList.toArray(
139                     new Hierarchy[ hierarchyList.size()]);
140             hasRestrictions = true;
141         }
142     }
143
144     public boolean equals(Object JavaDoc object) {
145         if (!(object instanceof CubeAccess)) {
146            return false;
147         }
148         CubeAccess cubeAccess = (CubeAccess) object;
149         List JavaDoc<Hierarchy> hierarchyList = cubeAccess.getNoAccessHierarchyList();
150         List JavaDoc<Member> limitedMemberList = cubeAccess.getLimitedMemberList();
151
152         if ((this.hierarchyList.size() != hierarchyList.size()) ||
153             (this.memberList.size() != limitedMemberList.size())) {
154             return false;
155         }
156         for (Hierarchy o : hierarchyList) {
157             if (!this.hierarchyList.contains(o)) {
158                 return false;
159             }
160         }
161         for (Member member : limitedMemberList) {
162             if (!this.memberList.contains(member)) {
163                 return false;
164             }
165         }
166         return true;
167     }
168 }
169
170 // End CubeAccess.java
171
Popular Tags