Commit 31b06fefdd17d30307888768ee7bcb29df5cc6da

Authored by Dmytro Shvaika
Committed by Andrew Shvayka
1 parent a9df9df9

added InsertRepository for Relation Dao

  1 +/**
  2 + * Copyright © 2016-2020 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.sql.relation;
  17 +
  18 +import lombok.extern.slf4j.Slf4j;
  19 +import org.springframework.data.jpa.repository.Modifying;
  20 +import org.thingsboard.server.dao.model.sql.RelationEntity;
  21 +
  22 +import javax.persistence.EntityManager;
  23 +import javax.persistence.PersistenceContext;
  24 +import javax.persistence.Query;
  25 +
  26 +@Slf4j
  27 +public abstract class AbstractRelationInsertRepository implements RelationInsertRepository {
  28 +
  29 + @PersistenceContext
  30 + protected EntityManager entityManager;
  31 +
  32 + protected Query getQuery(RelationEntity entity, String query) {
  33 + Query nativeQuery = entityManager.createNativeQuery(query, RelationEntity.class);
  34 + if (entity.getAdditionalInfo().isNull()) {
  35 + nativeQuery.setParameter("additionalInfo", null);
  36 + } else {
  37 + nativeQuery.setParameter("additionalInfo", entity.getAdditionalInfo().asText());
  38 + }
  39 + return nativeQuery
  40 + .setParameter("fromId", entity.getFromId())
  41 + .setParameter("fromType", entity.getFromType())
  42 + .setParameter("toId", entity.getToId())
  43 + .setParameter("toType", entity.getToType())
  44 + .setParameter("relationTypeGroup", entity.getRelationTypeGroup())
  45 + .setParameter("relationType", entity.getRelationType());
  46 + }
  47 +
  48 + @Modifying
  49 + protected abstract RelationEntity processSaveOrUpdate(RelationEntity entity);
  50 +
  51 +}
  1 +/**
  2 + * Copyright © 2016-2020 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.sql.relation;
  17 +
  18 +import org.springframework.stereotype.Repository;
  19 +import org.springframework.transaction.annotation.Transactional;
  20 +import org.thingsboard.server.dao.model.sql.RelationCompositeKey;
  21 +import org.thingsboard.server.dao.model.sql.RelationEntity;
  22 +import org.thingsboard.server.dao.util.HsqlDao;
  23 +import org.thingsboard.server.dao.util.SqlDao;
  24 +
  25 +@HsqlDao
  26 +@SqlDao
  27 +@Repository
  28 +@Transactional
  29 +public class HsqlRelationInsertRepository extends AbstractRelationInsertRepository implements RelationInsertRepository {
  30 +
  31 + private static final String INSERT_ON_CONFLICT_DO_UPDATE = "MERGE INTO relation USING (VALUES :fromId, :fromType, :toId, :toType, :relationTypeGroup, :relationType, :additionalInfo) R " +
  32 + "(from_id, from_type, to_id, to_type, relation_type_group, relation_type, additional_info) " +
  33 + "ON (relation.from_id = R.from_id AND relation.from_type = R.from_type AND relation.relation_type_group = R.relation_type_group AND relation.relation_type = R.relation_type AND relation.to_id = R.to_id AND relation.to_type = R.to_type) " +
  34 + "WHEN MATCHED THEN UPDATE SET relation.additional_info = R.additional_info " +
  35 + "WHEN NOT MATCHED THEN INSERT (from_id, from_type, to_id, to_type, relation_type_group, relation_type, additional_info) VALUES (R.from_id, R.from_type, R.to_id, R.to_type, R.relation_type_group, R.relation_type, R.additional_info)";
  36 +
  37 + @Override
  38 + public RelationEntity saveOrUpdate(RelationEntity entity) {
  39 + return processSaveOrUpdate(entity);
  40 + }
  41 +
  42 + @Override
  43 + protected RelationEntity processSaveOrUpdate(RelationEntity entity) {
  44 + getQuery(entity, INSERT_ON_CONFLICT_DO_UPDATE).executeUpdate();
  45 + return entityManager.find(RelationEntity.class, new RelationCompositeKey(entity.toData()));
  46 + }
  47 +}
@@ -56,6 +56,9 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple @@ -56,6 +56,9 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
56 @Autowired 56 @Autowired
57 private RelationRepository relationRepository; 57 private RelationRepository relationRepository;
58 58
  59 + @Autowired
  60 + private RelationInsertRepository relationInsertRepository;
  61 +
59 @Override 62 @Override
60 public ListenableFuture<List<EntityRelation>> findAllByFrom(TenantId tenantId, EntityId from, RelationTypeGroup typeGroup) { 63 public ListenableFuture<List<EntityRelation>> findAllByFrom(TenantId tenantId, EntityId from, RelationTypeGroup typeGroup) {
61 return service.submit(() -> DaoUtil.convertDataList( 64 return service.submit(() -> DaoUtil.convertDataList(
@@ -117,12 +120,12 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple @@ -117,12 +120,12 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
117 120
118 @Override 121 @Override
119 public boolean saveRelation(TenantId tenantId, EntityRelation relation) { 122 public boolean saveRelation(TenantId tenantId, EntityRelation relation) {
120 - return relationRepository.save(new RelationEntity(relation)) != null; 123 + return relationInsertRepository.saveOrUpdate(new RelationEntity(relation)) != null;
121 } 124 }
122 125
123 @Override 126 @Override
124 public ListenableFuture<Boolean> saveRelationAsync(TenantId tenantId, EntityRelation relation) { 127 public ListenableFuture<Boolean> saveRelationAsync(TenantId tenantId, EntityRelation relation) {
125 - return service.submit(() -> relationRepository.save(new RelationEntity(relation)) != null); 128 + return service.submit(() -> relationInsertRepository.saveOrUpdate(new RelationEntity(relation)) != null);
126 } 129 }
127 130
128 @Override 131 @Override
  1 +/**
  2 + * Copyright © 2016-2020 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.sql.relation;
  17 +
  18 +import org.springframework.stereotype.Repository;
  19 +import org.springframework.transaction.annotation.Transactional;
  20 +import org.thingsboard.server.dao.model.sql.RelationEntity;
  21 +import org.thingsboard.server.dao.util.PsqlDao;
  22 +import org.thingsboard.server.dao.util.SqlDao;
  23 +
  24 +@PsqlDao
  25 +@SqlDao
  26 +@Repository
  27 +@Transactional
  28 +public class PsqlRelationInsertRepository extends AbstractRelationInsertRepository implements RelationInsertRepository {
  29 +
  30 + private static final String INSERT_ON_CONFLICT_DO_UPDATE = "INSERT INTO relation (from_id, from_type, to_id, to_type, relation_type_group, relation_type, additional_info)" +
  31 + " VALUES (:fromId, :fromType, :toId, :toType, :relationTypeGroup, :relationType, :additionalInfo) " +
  32 + "ON CONFLICT (from_id, from_type, relation_type_group, relation_type, to_id, to_type) DO UPDATE SET additional_info = :additionalInfo returning *";
  33 +
  34 + @Override
  35 + public RelationEntity saveOrUpdate(RelationEntity entity) {
  36 + return processSaveOrUpdate(entity);
  37 + }
  38 +
  39 + @Override
  40 + protected RelationEntity processSaveOrUpdate(RelationEntity entity) {
  41 + return (RelationEntity) getQuery(entity, INSERT_ON_CONFLICT_DO_UPDATE).getSingleResult();
  42 + }
  43 +}
  1 +/**
  2 + * Copyright © 2016-2020 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +package org.thingsboard.server.dao.sql.relation;
  17 +
  18 +import org.thingsboard.server.dao.model.sql.RelationEntity;
  19 +
  20 +public interface RelationInsertRepository {
  21 +
  22 + RelationEntity saveOrUpdate(RelationEntity entity);
  23 +
  24 +}