Add missing getRelation method.

This commit is contained in:
Igor Kulikov 2017-07-03 12:45:27 +03:00
parent 8e272b0162
commit b2ac8f6447
8 changed files with 72 additions and 9 deletions

View File

@ -97,8 +97,8 @@ public class EntityRelationController extends BaseController {
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.GET, params = {"fromId", "fromType", "relationType", "toId", "toType"})
@ResponseStatus(value = HttpStatus.OK)
public void checkRelation(@RequestParam("fromId") String strFromId,
@ResponseBody
public EntityRelation getRelation(@RequestParam("fromId") String strFromId,
@RequestParam("fromType") String strFromType,
@RequestParam("relationType") String strRelationType,
@RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup,
@ -114,10 +114,7 @@ public class EntityRelationController extends BaseController {
checkEntityId(fromId);
checkEntityId(toId);
RelationTypeGroup typeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
Boolean found = relationService.checkRelation(fromId, toId, strRelationType, typeGroup).get();
if (!found) {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
}
return checkNotNull(relationService.getRelation(fromId, toId, strRelationType, typeGroup).get());
} catch (Exception e) {
throw handleException(e);
}

View File

@ -127,6 +127,19 @@ public class BaseRelationDao extends CassandraAbstractAsyncDao implements Relati
return getFuture(executeAsyncRead(stmt), rs -> rs != null ? rs.one() != null : false);
}
@Override
public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
BoundStatement stmt = getCheckRelationStmt().bind()
.setUUID(0, from.getId())
.setString(1, from.getEntityType().name())
.setUUID(2, to.getId())
.setString(3, to.getEntityType().name())
.set(4, typeGroup, relationTypeGroupCodec)
.setString(5, relationType);
return getFuture(executeAsyncRead(stmt), rs -> rs != null ? getEntityRelation(rs.one()) : null);
}
@Override
public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
BoundStatement stmt = getSaveStmt().bind()

View File

@ -55,6 +55,13 @@ public class BaseRelationService implements RelationService {
return relationDao.checkRelation(from, to, relationType, typeGroup);
}
@Override
public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
log.trace("Executing EntityRelation [{}][{}][{}][{}]", from, to, relationType, typeGroup);
validate(from, to, relationType, typeGroup);
return relationDao.getRelation(from, to, relationType, typeGroup);
}
@Override
public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
log.trace("Executing saveRelation [{}]", relation);

View File

@ -39,6 +39,8 @@ public interface RelationDao {
ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
ListenableFuture<Boolean> saveRelation(EntityRelation relation);
ListenableFuture<Boolean> deleteRelation(EntityRelation relation);

View File

@ -30,6 +30,8 @@ public interface RelationService {
ListenableFuture<Boolean> checkRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
ListenableFuture<Boolean> saveRelation(EntityRelation relation);
ListenableFuture<Boolean> deleteRelation(EntityRelation relation);

View File

@ -108,6 +108,18 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
return service.submit(() -> relationRepository.findOne(key) != null);
}
@Override
public ListenableFuture<EntityRelation> getRelation(EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
RelationCompositeKey key =
new RelationCompositeKey(from.getId(),
from.getEntityType().name(),
to.getId(),
to.getEntityType().name(),
relationType,
typeGroup.name());
return service.submit(() -> DaoUtil.getData(relationRepository.findOne(key)));
}
@Override
public ListenableFuture<Boolean> saveRelation(EntityRelation relation) {
return service.submit(() -> relationRepository.save(new RelationEntity(relation)) != null);

View File

@ -24,6 +24,7 @@ function EntityRelationService($http, $q) {
saveRelation: saveRelation,
deleteRelation: deleteRelation,
deleteRelations: deleteRelations,
getRelation: getRelation,
findByFrom: findByFrom,
findInfoByFrom: findInfoByFrom,
findByFromAndType: findByFromAndType,
@ -74,6 +75,20 @@ function EntityRelationService($http, $q) {
return deferred.promise;
}
function getRelation(fromId, fromType, relationType, toId, toType) {
var deferred = $q.defer();
var url = '/api/relation?fromId=' + fromId;
url += '&fromType=' + fromType;
url += '&relationType=' + relationType;
url += '&toId=' + toId;
url += '&toType=' + toType;
$http.get(url).then(function success(response) {
deferred.resolve(response.data);
}, function fail() {
deferred.reject();
});
return deferred.promise;
}
function findByFrom(fromId, fromType) {
var deferred = $q.defer();

View File

@ -34,9 +34,23 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter
scope.fetchEntities = function(searchText) {
var deferred = $q.defer();
entityService.getEntitiesByNameFilter(scope.entityType, searchText, 50, null, scope.entitySubtype).then(function success(result) {
var limit = 50;
if (scope.excludeEntityIds && scope.excludeEntityIds.length) {
limit += scope.excludeEntityIds.length;
}
entityService.getEntitiesByNameFilter(scope.entityType, searchText, limit, null, scope.entitySubtype).then(function success(result) {
if (result) {
if (scope.excludeEntityIds && scope.excludeEntityIds.length) {
var entities = [];
result.forEach(function(entity) {
if (scope.excludeEntityIds.indexOf(entity.id.id) == -1) {
entities.push(entity);
}
});
deferred.resolve(entities);
} else {
deferred.resolve(result);
}
} else {
deferred.resolve([]);
}
@ -165,7 +179,8 @@ export default function EntityAutocomplete($compile, $templateCache, $q, $filter
tbRequired: '=?',
disabled:'=ngDisabled',
entityType: '=',
entitySubtype: '=?'
entitySubtype: '=?',
excludeEntityIds: '=?'
}
};
}