2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.authorization;
26 import org.collectionspace.authentication.AuthN;
29 * CSpaceResourceImpl abstract resource implementation
32 public abstract class CSpaceResourceImpl implements CSpaceResource {
34 final protected static String SEPARATOR_HASH = "#";
35 final protected static String SEPARATOR_COLON = ":";
38 private CSpaceAction action;
39 private String tenantId;
41 private CSpaceResourceImpl() {
45 * constructor that uses logged in user's tenant context to associate resource with
50 public CSpaceResourceImpl(String id, CSpaceAction action, TYPE type) {
51 setup(id, action, type);
52 tenantId = AuthN.get().getCurrentTenantId();
56 * constructor that uses given tenant id to associate the resource with
62 public CSpaceResourceImpl(String tenantId, String id, CSpaceAction action, TYPE type) {
63 setup(id, action, type);
64 if (tenantId == null) {
65 throw new IllegalArgumentException("tenantId cannot be null");
67 this.tenantId = tenantId;
70 private void setup(String id, CSpaceAction action, TYPE type) {
71 if (id == null || id.isEmpty()) {
72 throw new IllegalArgumentException("id cannot be null or empty");
74 this.id = id.toLowerCase();
76 throw new IllegalArgumentException("type cannot be null");
80 throw new IllegalArgumentException("action cannot be null");
86 public String getId() {
88 return tenantId + SEPARATOR_COLON + id;
92 public Long getHashedId() {
93 return Long.valueOf(getId().hashCode());
97 public TYPE getType() {
102 public String getTenantId() {
107 * getAction a convenience method to get action invoked on the resource
110 public CSpaceAction getAction() {
115 public String toString() {
116 StringBuilder builder = new StringBuilder();
117 builder.append("CSpaceResourceImpl [");
118 builder.append("id=");
120 builder.append(", type=");
121 builder.append(type);
122 builder.append(", tenantId=");
123 builder.append(tenantId);
124 builder.append(", action=");
125 builder.append(action.toString());
127 return builder.toString();