/**
 * Copyright © 2016-2022 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.rule.engine.util;

import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.DashboardInfo;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.EntityView;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.edge.Edge;
import org.thingsboard.server.common.data.id.EntityId;

import java.util.Optional;

public class EntitiesByNameAndTypeLoader {

    public static EntityId findEntityId(TbContext ctx, EntityType entityType, String entityName) {
        EntityId targetEntityId = null;
        switch (entityType) {
            case DEVICE:
                Device device = ctx.getDeviceService().findDeviceByTenantIdAndName(ctx.getTenantId(), entityName);
                if (device != null) {
                    targetEntityId = device.getId();
                }
                break;
            case ASSET:
                Asset asset = ctx.getAssetService().findAssetByTenantIdAndName(ctx.getTenantId(), entityName);
                if (asset != null) {
                    targetEntityId = asset.getId();
                }
                break;
            case ENTITY_VIEW:
                EntityView entityView = ctx.getEntityViewService().findEntityViewByTenantIdAndName(ctx.getTenantId(), entityName);
                if (entityView != null) {
                    targetEntityId = entityView.getId();
                }
                break;
            case EDGE:
                Edge edge = ctx.getEdgeService().findEdgeByTenantIdAndName(ctx.getTenantId(), entityName);
                if (edge != null) {
                    targetEntityId = edge.getId();
                }
                break;
            case USER:
                User user = ctx.getUserService().findUserByTenantIdAndEmail(ctx.getTenantId(), entityName);
                if (user != null) {
                    targetEntityId = user.getId();
                }
                break;
            default:
                throw new IllegalStateException("Unexpected entity type " + entityType.name());
        }
        if (targetEntityId == null) {
            throw new IllegalStateException("Failed to found " + entityType.name() + "  entity by name: '" + entityName + "'!");
        }
        return targetEntityId;
    }

}

@@ -747,387 +747,95 @@
 ata.
-Customer;%0Aimport org.thingsboard.server.common.data.DashboardInfo;%0Aimport org.thingsboard.server.common.data.Device;%0Aimport org.thingsboard.server.common.data.EntityType;%0Aimport org.thingsboard.server.common.data.EntityView;%0Aimport org.thingsboard.server.common.data.User;%0Aimport org.thingsboard.server.common.data.asset.Asset;%0Aimport org.thingsboard.server.common.data.edge.Edge
+EntityType;%0Aimport org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo
 ;%0Aim
@@ -892,36 +892,8 @@
 d;%0A%0A
-import java.util.Optional;%0A%0A
 publ
@@ -1039,24 +1039,68 @@
         
+SearchTextBasedWithAdditionalInfo%3C? extends 
 EntityId
  targetE
@@ -1091,16 +1091,17 @@
 EntityId
+%3E
  targetE
@@ -1109,17 +1109,8 @@
 tity
-Id = null
 ;%0A  
@@ -1182,21 +1182,20 @@
     
-Device device
+targetEntity
  = c
@@ -1280,117 +1280,8 @@
 e);%0A
-                if (device != null) %7B%0A                    targetEntityId = device.getId();%0A                %7D%0A
     
@@ -1343,19 +1343,20 @@
     
-Asset asset
+targetEntity
  = c
@@ -1439,115 +1439,8 @@
 e);%0A
-                if (asset != null) %7B%0A                    targetEntityId = asset.getId();%0A                %7D%0A
     
@@ -1508,29 +1508,20 @@
     
-EntityView e
+targetE
 ntity
-View
  = c
@@ -1614,125 +1614,8 @@
 e);%0A
-                if (entityView != null) %7B%0A                    targetEntityId = entityView.getId();%0A                %7D%0A
     
@@ -1676,17 +1676,20 @@
     
-Edge edge
+targetEntity
  = c
@@ -1770,113 +1770,8 @@
 e);%0A
-                if (edge != null) %7B%0A                    targetEntityId = edge.getId();%0A                %7D%0A
     
@@ -1832,17 +1832,20 @@
     
-User user
+targetEntity
  = c
@@ -1927,113 +1927,8 @@
 e);%0A
-                if (user != null) %7B%0A                    targetEntityId = user.getId();%0A                %7D%0A
     
@@ -2097,18 +2097,16 @@
 etEntity
-Id
  == null
@@ -2275,18 +2275,24 @@
 etEntity
-Id
+.getId()
 ;%0A    %7D%0A

refactor code