Commit b2ac8f6447ad49e541b98ef5fd088f16282f25ac

Authored by Igor Kulikov
1 parent 8e272b01

Add missing getRelation method.

... ... @@ -97,8 +97,8 @@ public class EntityRelationController extends BaseController {
97 97
98 98 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
99 99 @RequestMapping(value = "/relation", method = RequestMethod.GET, params = {"fromId", "fromType", "relationType", "toId", "toType"})
100   - @ResponseStatus(value = HttpStatus.OK)
101   - public void checkRelation(@RequestParam("fromId") String strFromId,
  100 + @ResponseBody
  101 + public EntityRelation getRelation(@RequestParam("fromId") String strFromId,
102 102 @RequestParam("fromType") String strFromType,
103 103 @RequestParam("relationType") String strRelationType,
104 104 @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup,
... ... @@ -114,10 +114,7 @@ public class EntityRelationController extends BaseController {
114 114 checkEntityId(fromId);
115 115 checkEntityId(toId);
116 116 RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
117   - Boolean found = relationService.checkRelation(fromId, toId, strRelationType, typeGroup).get();
118   - if (!found) {
119   - throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
120   - }
  117 + return checkNotNull(relationService.getRelation(fromId, toId, strRelationType, typeGroup).get());
121 118 } catch (Exception e) {
122 119 throw handleException(e);
123 120 }
... ...
... ... @@ -128,6 +128,19 @@ public class BaseRelationDao extends CassandraAbstractAsyncDao implements Relati
128 128 }
129 129
130 130 @Override
  131 + public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
  132 + BoundStatement stmt = getCheckRelationStmt().bind()
  133 + .setUUID(0, from.getId())
  134 + .setString(1, from.getEntityType().name())
  135 + .setUUID(2, to.getId())
  136 + .setString(3, to.getEntityType().name())
  137 + .set(4, typeGroup, relationTypeGroupCodec)
  138 + .setString(5, relationType);
  139 + return getFuture(executeAsyncRead(stmt), rs -> rs != null ? getEntityRelation(rs.one()) : null);
  140 + }
  141 +
  142 +
  143 + @Override
131 144 public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
132 145 BoundStatement stmt = getSaveStmt().bind()
133 146 .setUUID(0, relation.getFrom().getId())
... ...
... ... @@ -56,6 +56,13 @@ public class BaseRelationService implements RelationService {
56 56 }
57 57
58 58 @Override
  59 + public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
  60 + log.trace("Executing EntityRelation [{}][{}][{}][{}]", from, to, relationType, typeGroup);
  61 + validate(from, to, relationType, typeGroup);
  62 + return relationDao.getRelation(from, to, relationType, typeGroup);
  63 + }
  64 +
  65 + @Override
59 66 public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
60 67 log.trace("Executing saveRelation [{}]", relation);
61 68 validate(relation);
... ...
... ... @@ -39,6 +39,8 @@ public interface RelationDao {
39 39
40 40 ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
41 41
  42 + ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
  43 +
42 44 ListenableFuture<Boolean> saveRelation(EntityRelation relation);
43 45
44 46 ListenableFuture<Boolean> deleteRelation(EntityRelation relation);
... ...
... ... @@ -30,6 +30,8 @@ public interface RelationService {
30 30
31 31 ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
32 32
  33 + ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
  34 +
33 35 ListenableFuture<Boolean> saveRelation(EntityRelation relation);
34 36
35 37 ListenableFuture<Boolean> deleteRelation(EntityRelation relation);
... ...
... ... @@ -109,6 +109,18 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
109 109 }
110 110
111 111 @Override
  112 + public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
  113 + RelationCompositeKey key =
  114 + new RelationCompositeKey(from.getId(),
  115 + from.getEntityType().name(),
  116 + to.getId(),
  117 + to.getEntityType().name(),
  118 + relationType,
  119 + typeGroup.name());
  120 + return service.submit(() -> DaoUtil.getData(relationRepository.findOne(key)));
  121 + }
  122 +
  123 + @Override
112 124 public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
113 125 return service.submit(() -> relationRepository.save(new RelationEntity(relation)) != null);
114 126 }
... ...
... ... @@ -24,6 +24,7 @@ function EntityRelationService($http, $q) {
24 24 saveRelation: saveRelation,
25 25 deleteRelation: deleteRelation,
26 26 deleteRelations: deleteRelations,
  27 + getRelation: getRelation,
27 28 findByFrom: findByFrom,
28 29 findInfoByFrom: findInfoByFrom,
29 30 findByFromAndType: findByFromAndType,
... ... @@ -74,6 +75,20 @@ function EntityRelationService($http, $q) {
74 75 return deferred.promise;
75 76 }
76 77
  78 + function getRelation(fromId, fromType, relationType, toId, toType) {
  79 + var deferred = $q.defer();
  80 + var url = '/api/relation?fromId=' + fromId;
  81 + url += '&fromType=' + fromType;
  82 + url += '&relationType=' + relationType;
  83 + url += '&toId=' + toId;
  84 + url += '&toType=' + toType;
  85 + $http.get(url).then(function success(response) {
  86 + deferred.resolve(response.data);
  87 + }, function fail() {
  88 + deferred.reject();
  89 + });
  90 + return deferred.promise;
  91 + }
77 92
78 93 function findByFrom(fromId, fromType) {
79 94 var deferred = $q.defer();
... ...
... ... @@ -34,9 +34,23 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter
34 34
35 35 scope.fetchEntities = function(searchText) {
36 36 var deferred = $q.defer();
37   - entityService.getEntitiesByNameFilter(scope.entityType, searchText, 50, null, scope.entitySubtype).then(function success(result) {
  37 + var limit = 50;
  38 + if (scope.excludeEntityIds && scope.excludeEntityIds.length) {
  39 + limit += scope.excludeEntityIds.length;
  40 + }
  41 + entityService.getEntitiesByNameFilter(scope.entityType, searchText, limit, null, scope.entitySubtype).then(function success(result) {
38 42 if (result) {
39   - deferred.resolve(result);
  43 + if (scope.excludeEntityIds && scope.excludeEntityIds.length) {
  44 + var entities = [];
  45 + result.forEach(function(entity) {
  46 + if (scope.excludeEntityIds.indexOf(entity.id.id) == -1) {
  47 + entities.push(entity);
  48 + }
  49 + });
  50 + deferred.resolve(entities);
  51 + } else {
  52 + deferred.resolve(result);
  53 + }
40 54 } else {
41 55 deferred.resolve([]);
42 56 }
... ... @@ -165,7 +179,8 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter
165 179 tbRequired: '=?',
166 180 disabled:'=ngDisabled',
167 181 entityType: '=',
168   - entitySubtype: '=?'
  182 + entitySubtype: '=?',
  183 + excludeEntityIds: '=?'
169 184 }
170 185 };
171 186 }
... ...