Showing
8 changed files
with
72 additions
and
9 deletions
@@ -97,8 +97,8 @@ public class EntityRelationController extends BaseController { | @@ -97,8 +97,8 @@ public class EntityRelationController extends BaseController { | ||
97 | 97 | ||
98 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") | 98 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") |
99 | @RequestMapping(value = "/relation", method = RequestMethod.GET, params = {"fromId", "fromType", "relationType", "toId", "toType"}) | 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 | @RequestParam("fromType") String strFromType, | 102 | @RequestParam("fromType") String strFromType, |
103 | @RequestParam("relationType") String strRelationType, | 103 | @RequestParam("relationType") String strRelationType, |
104 | @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup, | 104 | @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup, |
@@ -114,10 +114,7 @@ public class EntityRelationController extends BaseController { | @@ -114,10 +114,7 @@ public class EntityRelationController extends BaseController { | ||
114 | checkEntityId(fromId); | 114 | checkEntityId(fromId); |
115 | checkEntityId(toId); | 115 | checkEntityId(toId); |
116 | RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON); | 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 | } catch (Exception e) { | 118 | } catch (Exception e) { |
122 | throw handleException(e); | 119 | throw handleException(e); |
123 | } | 120 | } |
@@ -128,6 +128,19 @@ public class BaseRelationDao extends CassandraAbstractAsyncDao implements Relati | @@ -128,6 +128,19 @@ public class BaseRelationDao extends CassandraAbstractAsyncDao implements Relati | ||
128 | } | 128 | } |
129 | 129 | ||
130 | @Override | 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 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { | 144 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { |
132 | BoundStatement stmt = getSaveStmt().bind() | 145 | BoundStatement stmt = getSaveStmt().bind() |
133 | .setUUID(0, relation.getFrom().getId()) | 146 | .setUUID(0, relation.getFrom().getId()) |
@@ -56,6 +56,13 @@ public class BaseRelationService implements RelationService { | @@ -56,6 +56,13 @@ public class BaseRelationService implements RelationService { | ||
56 | } | 56 | } |
57 | 57 | ||
58 | @Override | 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 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { | 66 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { |
60 | log.trace("Executing saveRelation [{}]", relation); | 67 | log.trace("Executing saveRelation [{}]", relation); |
61 | validate(relation); | 68 | validate(relation); |
@@ -39,6 +39,8 @@ public interface RelationDao { | @@ -39,6 +39,8 @@ public interface RelationDao { | ||
39 | 39 | ||
40 | ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup); | 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 | ListenableFuture<Boolean> saveRelation(EntityRelation relation); | 44 | ListenableFuture<Boolean> saveRelation(EntityRelation relation); |
43 | 45 | ||
44 | ListenableFuture<Boolean> deleteRelation(EntityRelation relation); | 46 | ListenableFuture<Boolean> deleteRelation(EntityRelation relation); |
@@ -30,6 +30,8 @@ public interface RelationService { | @@ -30,6 +30,8 @@ public interface RelationService { | ||
30 | 30 | ||
31 | ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup); | 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 | ListenableFuture<Boolean> saveRelation(EntityRelation relation); | 35 | ListenableFuture<Boolean> saveRelation(EntityRelation relation); |
34 | 36 | ||
35 | ListenableFuture<Boolean> deleteRelation(EntityRelation relation); | 37 | ListenableFuture<Boolean> deleteRelation(EntityRelation relation); |
@@ -109,6 +109,18 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple | @@ -109,6 +109,18 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple | ||
109 | } | 109 | } |
110 | 110 | ||
111 | @Override | 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 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { | 124 | public ListenableFuture<Boolean> saveRelation(EntityRelation relation) { |
113 | return service.submit(() -> relationRepository.save(new RelationEntity(relation)) != null); | 125 | return service.submit(() -> relationRepository.save(new RelationEntity(relation)) != null); |
114 | } | 126 | } |
@@ -24,6 +24,7 @@ function EntityRelationService($http, $q) { | @@ -24,6 +24,7 @@ function EntityRelationService($http, $q) { | ||
24 | saveRelation: saveRelation, | 24 | saveRelation: saveRelation, |
25 | deleteRelation: deleteRelation, | 25 | deleteRelation: deleteRelation, |
26 | deleteRelations: deleteRelations, | 26 | deleteRelations: deleteRelations, |
27 | + getRelation: getRelation, | ||
27 | findByFrom: findByFrom, | 28 | findByFrom: findByFrom, |
28 | findInfoByFrom: findInfoByFrom, | 29 | findInfoByFrom: findInfoByFrom, |
29 | findByFromAndType: findByFromAndType, | 30 | findByFromAndType: findByFromAndType, |
@@ -74,6 +75,20 @@ function EntityRelationService($http, $q) { | @@ -74,6 +75,20 @@ function EntityRelationService($http, $q) { | ||
74 | return deferred.promise; | 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 | function findByFrom(fromId, fromType) { | 93 | function findByFrom(fromId, fromType) { |
79 | var deferred = $q.defer(); | 94 | var deferred = $q.defer(); |
@@ -34,9 +34,23 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter | @@ -34,9 +34,23 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter | ||
34 | 34 | ||
35 | scope.fetchEntities = function(searchText) { | 35 | scope.fetchEntities = function(searchText) { |
36 | var deferred = $q.defer(); | 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 | if (result) { | 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 | } else { | 54 | } else { |
41 | deferred.resolve([]); | 55 | deferred.resolve([]); |
42 | } | 56 | } |
@@ -165,7 +179,8 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter | @@ -165,7 +179,8 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter | ||
165 | tbRequired: '=?', | 179 | tbRequired: '=?', |
166 | disabled:'=ngDisabled', | 180 | disabled:'=ngDisabled', |
167 | entityType: '=', | 181 | entityType: '=', |
168 | - entitySubtype: '=?' | 182 | + entitySubtype: '=?', |
183 | + excludeEntityIds: '=?' | ||
169 | } | 184 | } |
170 | }; | 185 | }; |
171 | } | 186 | } |