27 changed files with 419 additions and 85 deletions
@ -0,0 +1,80 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.query; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonIgnore; |
|||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
|||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
|||
import io.swagger.v3.oas.annotations.media.Schema; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.id.UserId; |
|||
|
|||
@JsonDeserialize(using = AliasEntityIdDeserializer.class) |
|||
@JsonSerialize(using = AliasEntityIdSerializer.class) |
|||
@Schema |
|||
public interface AliasEntityId extends EntityId { |
|||
|
|||
AliasEntityType getAliasEntityType(); |
|||
|
|||
EntityId defaultEntityId(); |
|||
|
|||
EntityId toEntityId(); |
|||
|
|||
@JsonIgnore |
|||
default boolean isAliasEntityId() { |
|||
return getAliasEntityType() != null; |
|||
} |
|||
|
|||
static AliasEntityId fromEntityId(EntityId entityId) { |
|||
if (entityId != null) { |
|||
return new AliasEntityIdImpl(entityId); |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
static AliasEntityId resolveAliasEntityId(AliasEntityId aliasEntityId, TenantId tenantId, UserId userId, EntityId userOwnerId) { |
|||
if (aliasEntityId != null) { |
|||
if (aliasEntityId.isAliasEntityId()) { |
|||
AliasEntityType aliasEntityType = aliasEntityId.getAliasEntityType(); |
|||
switch (aliasEntityType) { |
|||
case CURRENT_CUSTOMER -> { |
|||
if (EntityType.CUSTOMER.equals(userOwnerId.getEntityType())) { |
|||
return fromEntityId(userOwnerId); |
|||
} else { |
|||
return fromEntityId(aliasEntityId.defaultEntityId()); |
|||
} |
|||
} |
|||
case CURRENT_TENANT -> { |
|||
return fromEntityId(tenantId); |
|||
} |
|||
case CURRENT_USER -> { |
|||
return fromEntityId(userId); |
|||
} |
|||
case CURRENT_USER_OWNER -> { |
|||
return fromEntityId(userOwnerId); |
|||
} |
|||
} |
|||
} else { |
|||
return aliasEntityId; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,58 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.query; |
|||
|
|||
import com.fasterxml.jackson.core.JacksonException; |
|||
import com.fasterxml.jackson.core.JsonParser; |
|||
import com.fasterxml.jackson.core.ObjectCodec; |
|||
import com.fasterxml.jackson.databind.DeserializationContext; |
|||
import com.fasterxml.jackson.databind.JsonDeserializer; |
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.UUID; |
|||
|
|||
public class AliasEntityIdDeserializer extends JsonDeserializer<AliasEntityId> { |
|||
|
|||
@Override |
|||
public AliasEntityId deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException, JacksonException { |
|||
ObjectCodec oc = jsonParser.getCodec(); |
|||
ObjectNode node = oc.readTree(jsonParser); |
|||
if (node.has("entityType")) { |
|||
String entityType = node.get("entityType").asText(); |
|||
try { |
|||
EntityType.valueOf(entityType); |
|||
if (!node.has("id")) { |
|||
throw new IOException("Missing entityType or id!"); |
|||
} |
|||
EntityId entityId = EntityIdFactory.getByTypeAndId(node.get("entityType").asText(), node.get("id").asText()); |
|||
return new AliasEntityIdImpl(entityId); |
|||
} catch (IllegalArgumentException e) { |
|||
AliasEntityType aliasEntityType = AliasEntityType.valueOf(entityType); |
|||
UUID id = null; |
|||
if (node.has("id")) { |
|||
id = UUID.fromString(node.get("id").asText()); |
|||
} |
|||
return new AliasEntityIdImpl(aliasEntityType, id); |
|||
} |
|||
} else { |
|||
throw new IOException("Missing entityType!"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,104 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.query; |
|||
|
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.id.CustomerId; |
|||
import org.thingsboard.server.common.data.id.EntityId; |
|||
import org.thingsboard.server.common.data.id.EntityIdFactory; |
|||
|
|||
import java.util.UUID; |
|||
|
|||
class AliasEntityIdImpl implements AliasEntityId { |
|||
|
|||
private UUID id; |
|||
private EntityType entityType; |
|||
private AliasEntityType aliasEntityType; |
|||
private EntityId defaultEntityId; |
|||
|
|||
protected AliasEntityIdImpl(EntityId entityId) { |
|||
this.id = entityId.getId(); |
|||
this.entityType = entityId.getEntityType(); |
|||
} |
|||
|
|||
protected AliasEntityIdImpl(AliasEntityType aliasEntityType, UUID id) { |
|||
this.aliasEntityType = aliasEntityType; |
|||
if (id != null) { |
|||
switch (this.aliasEntityType) { |
|||
case CURRENT_CUSTOMER: |
|||
this.defaultEntityId = new CustomerId(id); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public AliasEntityType getAliasEntityType() { |
|||
return aliasEntityType; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId defaultEntityId() { |
|||
return defaultEntityId; |
|||
} |
|||
|
|||
@Override |
|||
public EntityId toEntityId() { |
|||
return EntityIdFactory.getByTypeAndUuid(entityType, id); |
|||
} |
|||
|
|||
@Override |
|||
public UUID getId() { |
|||
return id; |
|||
} |
|||
|
|||
@Override |
|||
public EntityType getEntityType() { |
|||
return entityType; |
|||
} |
|||
|
|||
@Override |
|||
public boolean equals(Object obj) { |
|||
if (this == obj) |
|||
return true; |
|||
if (obj == null) |
|||
return false; |
|||
if (!(obj instanceof EntityId otherEntityId)) |
|||
return false; |
|||
if (obj instanceof AliasEntityId otherAliasEntityId) { |
|||
if (otherAliasEntityId.isAliasEntityId()) { |
|||
if (!this.isAliasEntityId()) { |
|||
return false; |
|||
} |
|||
if (this.aliasEntityType != otherAliasEntityId.getAliasEntityType()) { |
|||
return false; |
|||
} |
|||
if (this.defaultEntityId != null && !this.defaultEntityId.equals(otherAliasEntityId.defaultEntityId())) { |
|||
return false; |
|||
} |
|||
if (this.defaultEntityId == null && otherAliasEntityId.defaultEntityId() != null) { |
|||
return false; |
|||
} |
|||
} |
|||
} |
|||
if (this.isAliasEntityId()) { |
|||
return false; |
|||
} |
|||
if (id == null) { |
|||
return otherEntityId.getId() == null; |
|||
} else return id.equals(otherEntityId.getId()); |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.query; |
|||
|
|||
import com.fasterxml.jackson.core.JsonGenerator; |
|||
import com.fasterxml.jackson.databind.JsonSerializer; |
|||
import com.fasterxml.jackson.databind.SerializerProvider; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.UUID; |
|||
|
|||
public class AliasEntityIdSerializer extends JsonSerializer<AliasEntityId> { |
|||
@Override |
|||
public void serialize(AliasEntityId value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
|||
gen.writeStartObject(); |
|||
String entityType; |
|||
if (value.isAliasEntityId()) { |
|||
entityType = value.getAliasEntityType().name(); |
|||
} else { |
|||
entityType = value.getEntityType().name(); |
|||
} |
|||
gen.writeStringField("entityType", entityType); |
|||
UUID id = null; |
|||
if (value.getId() != null) { |
|||
id = value.getId(); |
|||
} else if (value.defaultEntityId() != null) { |
|||
id = value.defaultEntityId().getId(); |
|||
} |
|||
if (id != null) { |
|||
gen.writeStringField("id", id.toString()); |
|||
} |
|||
gen.writeEndObject(); |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
/** |
|||
* Copyright © 2016-2025 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.query; |
|||
|
|||
public enum AliasEntityType { |
|||
CURRENT_CUSTOMER, |
|||
CURRENT_TENANT, |
|||
CURRENT_USER, |
|||
CURRENT_USER_OWNER |
|||
} |
|||
Loading…
Reference in new issue