|
| 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.JsonCreator; |
| 22 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | + |
| 30 | +public class PolarisPolicyMappingRecord { |
| 31 | + // to serialize/deserialize properties |
| 32 | + public static final String EMPTY_MAP_STRING = "{}"; |
| 33 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 34 | + |
| 35 | + // id of the catalog where target entity resides |
| 36 | + private long targetCatalogId; |
| 37 | + |
| 38 | + // id of the target entity |
| 39 | + private long targetId; |
| 40 | + |
| 41 | + // id of the catalog where the policy entity resides |
| 42 | + private long policyCatalogId; |
| 43 | + |
| 44 | + // id of the policy |
| 45 | + private long policyId; |
| 46 | + |
| 47 | + // id associated to the policy type |
| 48 | + private int policyTypeCode; |
| 49 | + |
| 50 | + // additional parameters of the mapping |
| 51 | + private String parameters; |
| 52 | + |
| 53 | + public PolarisPolicyMappingRecord() {} |
| 54 | + |
| 55 | + public long getTargetCatalogId() { |
| 56 | + return targetCatalogId; |
| 57 | + } |
| 58 | + |
| 59 | + public void setTargetCatalogId(long targetCatalogId) { |
| 60 | + this.targetCatalogId = targetCatalogId; |
| 61 | + } |
| 62 | + |
| 63 | + public long getTargetId() { |
| 64 | + return targetId; |
| 65 | + } |
| 66 | + |
| 67 | + public void setTargetId(long targetId) { |
| 68 | + this.targetId = targetId; |
| 69 | + } |
| 70 | + |
| 71 | + public long getPolicyId() { |
| 72 | + return policyId; |
| 73 | + } |
| 74 | + |
| 75 | + public void setPolicyId(long policyId) { |
| 76 | + this.policyId = policyId; |
| 77 | + } |
| 78 | + |
| 79 | + public int getPolicyTypeCode() { |
| 80 | + return policyTypeCode; |
| 81 | + } |
| 82 | + |
| 83 | + public void setPolicyTypeCode(int policyTypeCode) { |
| 84 | + this.policyTypeCode = policyTypeCode; |
| 85 | + } |
| 86 | + |
| 87 | + public long getPolicyCatalogId() { |
| 88 | + return policyCatalogId; |
| 89 | + } |
| 90 | + |
| 91 | + public void setPolicyCatalogId(long policyCatalogId) { |
| 92 | + this.policyCatalogId = policyCatalogId; |
| 93 | + } |
| 94 | + |
| 95 | + public String getParameters() { |
| 96 | + return parameters; |
| 97 | + } |
| 98 | + |
| 99 | + public void setParameters(String parameters) { |
| 100 | + this.parameters = parameters; |
| 101 | + } |
| 102 | + |
| 103 | + public Map<String, String> getParametersAsMap() { |
| 104 | + if (parameters == null) { |
| 105 | + return new HashMap<>(); |
| 106 | + } |
| 107 | + try { |
| 108 | + return MAPPER.readValue(parameters, new TypeReference<>() {}); |
| 109 | + } catch (JsonProcessingException ex) { |
| 110 | + throw new IllegalStateException( |
| 111 | + String.format("Failed to deserialize json. parameters %s", parameters), ex); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void setParametersAsMap(Map<String, String> parameters) { |
| 116 | + try { |
| 117 | + this.parameters = |
| 118 | + parameters == null ? EMPTY_MAP_STRING : MAPPER.writeValueAsString(parameters); |
| 119 | + } catch (JsonProcessingException ex) { |
| 120 | + throw new IllegalStateException( |
| 121 | + String.format("Failed to serialize json. properties %s", parameters), ex); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Constructor |
| 127 | + * |
| 128 | + * @param targetCatalogId id of the catalog where target entity resides |
| 129 | + * @param targetId id of the target entity |
| 130 | + * @param policyCatalogId id of the catalog where the policy entity resides |
| 131 | + * @param policyId id of the policy |
| 132 | + * @param policyTypeCode id associated to the policy type |
| 133 | + * @param parameters additional parameters of the mapping |
| 134 | + */ |
| 135 | + @JsonCreator |
| 136 | + public PolarisPolicyMappingRecord( |
| 137 | + @JsonProperty("targetCatalogId") long targetCatalogId, |
| 138 | + @JsonProperty("targetId") long targetId, |
| 139 | + @JsonProperty("policyCatalogId") long policyCatalogId, |
| 140 | + @JsonProperty("policyId") long policyId, |
| 141 | + @JsonProperty("policyTypeCode") int policyTypeCode, |
| 142 | + @JsonProperty("parameters") String parameters) { |
| 143 | + this.targetCatalogId = targetCatalogId; |
| 144 | + this.targetId = targetId; |
| 145 | + this.policyCatalogId = policyCatalogId; |
| 146 | + this.policyId = policyId; |
| 147 | + this.policyTypeCode = policyTypeCode; |
| 148 | + this.parameters = parameters; |
| 149 | + } |
| 150 | + |
| 151 | + public PolarisPolicyMappingRecord( |
| 152 | + long targetCatalogId, |
| 153 | + long targetId, |
| 154 | + long policyCatalogId, |
| 155 | + long policyId, |
| 156 | + int policyTypeCode, |
| 157 | + Map<String, String> parameters) { |
| 158 | + this.targetCatalogId = targetCatalogId; |
| 159 | + this.targetId = targetId; |
| 160 | + this.policyCatalogId = policyCatalogId; |
| 161 | + this.policyId = policyId; |
| 162 | + this.policyTypeCode = policyTypeCode; |
| 163 | + this.setParametersAsMap(parameters); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Copy constructor |
| 168 | + * |
| 169 | + * @param policyMappingRecord policy mapping rec to copy |
| 170 | + */ |
| 171 | + public PolarisPolicyMappingRecord(PolarisPolicyMappingRecord policyMappingRecord) { |
| 172 | + this.targetCatalogId = policyMappingRecord.getTargetCatalogId(); |
| 173 | + this.targetId = policyMappingRecord.getTargetId(); |
| 174 | + this.policyCatalogId = policyMappingRecord.getPolicyCatalogId(); |
| 175 | + this.policyId = policyMappingRecord.getPolicyId(); |
| 176 | + this.policyTypeCode = policyMappingRecord.getPolicyTypeCode(); |
| 177 | + this.parameters = policyMappingRecord.getParameters(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public String toString() { |
| 182 | + return "PolarisPolicyMappingRec{" |
| 183 | + + "targetCatalogId=" |
| 184 | + + targetCatalogId |
| 185 | + + ", targetId=" |
| 186 | + + targetId |
| 187 | + + ", policyCatalogId=" |
| 188 | + + policyCatalogId |
| 189 | + + ", policyId=" |
| 190 | + + policyId |
| 191 | + + ", policyType='" |
| 192 | + + policyTypeCode |
| 193 | + + ", parameters='" |
| 194 | + + parameters |
| 195 | + + "}"; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public boolean equals(Object o) { |
| 200 | + if (this == o) return true; |
| 201 | + if (o == null || getClass() != o.getClass()) return false; |
| 202 | + PolarisPolicyMappingRecord that = (PolarisPolicyMappingRecord) o; |
| 203 | + return targetCatalogId == that.targetCatalogId |
| 204 | + && targetId == that.targetId |
| 205 | + && policyCatalogId == that.policyCatalogId |
| 206 | + && policyId == that.policyId |
| 207 | + && policyTypeCode == that.policyTypeCode |
| 208 | + && Objects.equals(parameters, that.parameters); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public int hashCode() { |
| 213 | + return java.util.Objects.hash(targetId, policyId, policyCatalogId, policyTypeCode, parameters); |
| 214 | + } |
| 215 | +} |
0 commit comments