|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.polaris.core.entity; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | +import org.apache.iceberg.catalog.Namespace; |
| 24 | +import org.apache.iceberg.rest.RESTUtil; |
| 25 | +import org.apache.polaris.core.policy.PolicyType; |
| 26 | + |
| 27 | +public class PolicyEntity extends PolarisEntity { |
| 28 | + |
| 29 | + public static final String POLICY_TYPE_CODE_KEY = "policy-type-code"; |
| 30 | + public static final String POLICY_DESCRIPTION_KEY = "policy-description"; |
| 31 | + public static final String POLICY_VERSION_KEY = "policy-version"; |
| 32 | + public static final String POLICY_CONTENT_KEY = "policy-content"; |
| 33 | + |
| 34 | + PolicyEntity(PolarisBaseEntity sourceEntity) { |
| 35 | + super(sourceEntity); |
| 36 | + } |
| 37 | + |
| 38 | + public static PolicyEntity of(PolarisBaseEntity sourceEntity) { |
| 39 | + if (sourceEntity != null) { |
| 40 | + return new PolicyEntity(sourceEntity); |
| 41 | + } |
| 42 | + |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + @JsonIgnore |
| 47 | + public PolicyType getPolicyType() { |
| 48 | + return PolicyType.fromCode(getPolicyTypeCode()); |
| 49 | + } |
| 50 | + |
| 51 | + @JsonIgnore |
| 52 | + public int getPolicyTypeCode() { |
| 53 | + String policyTypeCode = getPropertiesAsMap().get(POLICY_TYPE_CODE_KEY); |
| 54 | + if (policyTypeCode != null) { |
| 55 | + return Integer.parseInt(policyTypeCode); |
| 56 | + } |
| 57 | + |
| 58 | + return -1; |
| 59 | + } |
| 60 | + |
| 61 | + @JsonIgnore |
| 62 | + public String getDescription() { |
| 63 | + return getPropertiesAsMap().get(POLICY_DESCRIPTION_KEY); |
| 64 | + } |
| 65 | + |
| 66 | + @JsonIgnore |
| 67 | + public String getContent() { |
| 68 | + return getPropertiesAsMap().get(POLICY_CONTENT_KEY); |
| 69 | + } |
| 70 | + |
| 71 | + @JsonIgnore |
| 72 | + public String getPolicyVersion() { |
| 73 | + return getPropertiesAsMap().get(POLICY_VERSION_KEY); |
| 74 | + } |
| 75 | + |
| 76 | + public static class Builder extends PolarisEntity.BaseBuilder<PolicyEntity, Builder> { |
| 77 | + public Builder(Namespace namespace, String policyName) { |
| 78 | + super(); |
| 79 | + setType(PolarisEntityType.POLICY); |
| 80 | + setParentNamespace(namespace); |
| 81 | + setName(policyName); |
| 82 | + setPolicyVersion(0); |
| 83 | + } |
| 84 | + |
| 85 | + public Builder(PolicyEntity original) { |
| 86 | + super(original); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public PolicyEntity build() { |
| 91 | + Preconditions.checkArgument( |
| 92 | + properties.get(POLICY_TYPE_CODE_KEY) != null, "Policy type must be specified"); |
| 93 | + |
| 94 | + return new PolicyEntity(buildBase()); |
| 95 | + } |
| 96 | + |
| 97 | + public Builder setParentNamespace(Namespace namespace) { |
| 98 | + if (namespace != null && !namespace.isEmpty()) { |
| 99 | + internalProperties.put( |
| 100 | + NamespaceEntity.PARENT_NAMESPACE_KEY, RESTUtil.encodeNamespace(namespace)); |
| 101 | + } |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + public Builder setPolicyType(PolicyType policyType) { |
| 106 | + properties.put(POLICY_TYPE_CODE_KEY, Integer.toString(policyType.getCode())); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public Builder setDescription(String description) { |
| 111 | + properties.put(POLICY_DESCRIPTION_KEY, description); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public Builder setPolicyVersion(long version) { |
| 116 | + properties.put(POLICY_VERSION_KEY, Long.toString(version)); |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Builder setContent(String content) { |
| 121 | + properties.put(POLICY_CONTENT_KEY, content); |
| 122 | + return this; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments