Showing
2 changed files
with
181 additions
and
0 deletions
1 | +/** | |
2 | + * Copyright © 2016-2018 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.entityview; | |
17 | + | |
18 | +import org.springframework.data.domain.Pageable; | |
19 | +import org.springframework.data.jpa.repository.Query; | |
20 | +import org.springframework.data.repository.CrudRepository; | |
21 | +import org.springframework.data.repository.query.Param; | |
22 | +import org.thingsboard.server.common.data.id.EntityId; | |
23 | +import org.thingsboard.server.dao.model.sql.EntityViewEntity; | |
24 | +import org.thingsboard.server.dao.util.SqlDao; | |
25 | + | |
26 | +import java.util.List; | |
27 | + | |
28 | +/** | |
29 | + * Created by Victor Basanets on 8/31/2017. | |
30 | + */ | |
31 | +@SqlDao | |
32 | +public interface EntityViewRepository extends CrudRepository<EntityViewEntity, String> { | |
33 | + | |
34 | + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + | |
35 | + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + | |
36 | + "AND e.id > :idOffset ORDER BY e.id") | |
37 | + List<EntityViewEntity> findByTenantId(@Param("tenantId") String tenantId, | |
38 | + @Param("textSearch") String textSearch, | |
39 | + @Param("idOffset") String idOffset, | |
40 | + Pageable pageable); | |
41 | + | |
42 | + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + | |
43 | + "AND e.entityId = :entityId " + | |
44 | + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + | |
45 | + "AND e.id > :idOffset ORDER BY e.id") | |
46 | + List<EntityViewEntity> findByTenantIdAndEntityId(@Param("tenantId") String tenantId, | |
47 | + @Param("entityId") String entityId, | |
48 | + @Param("textSearch") String textSearch, | |
49 | + @Param("idOffset") String idOffset, | |
50 | + Pageable pageable); | |
51 | + | |
52 | + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + | |
53 | + "AND e.customerId = :customerId " + | |
54 | + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:searchText, '%')) " + | |
55 | + "AND e.id > :idOffset ORDER BY e.id") | |
56 | + List<EntityViewEntity> findByTenantIdAndCustomerId(@Param("tenantId") String tenantId, | |
57 | + @Param("customerId") String customerId, | |
58 | + @Param("searchText") String searchText, | |
59 | + @Param("idOffset") String idOffset, | |
60 | + Pageable pageable); | |
61 | + | |
62 | + @Query("SELECT e FROM EntityViewEntity e WHERE e.tenantId = :tenantId " + | |
63 | + "AND e.customerId = :customerId " + | |
64 | + "AND e.entityId = :entityId " + | |
65 | + "AND LOWER(e.searchText) LIKE LOWER(CONCAT(:textSearch, '%')) " + | |
66 | + "AND e.id > :idOffset ORDER BY e.id") | |
67 | + List<EntityViewEntity> findByTenantIdAndCustomerIdAndEntityId(@Param("tenantId") String tenantId, | |
68 | + @Param("customerId") String customerId, | |
69 | + @Param("entityId") String entityId, | |
70 | + @Param("textSearch") String textSearch, | |
71 | + @Param("idOffset") String idOffset, | |
72 | + Pageable pageable); | |
73 | + | |
74 | + EntityViewEntity findByTenantIdAndName(String tenantId, String name); | |
75 | + | |
76 | + List<EntityViewEntity> findAllByTenantIdAndCustomerIdAndIdIn(String tenantId, | |
77 | + String customerId, | |
78 | + List<String> entityViewsIds); | |
79 | + | |
80 | + List<EntityViewEntity> findAllByTenantIdAndIdIn(String tenantId, List<String> entityViewsIds); | |
81 | +} | ... | ... |
1 | +package org.thingsboard.server.dao.sql.entityview; | |
2 | + | |
3 | +import org.springframework.beans.factory.annotation.Autowired; | |
4 | +import org.springframework.data.domain.PageRequest; | |
5 | +import org.springframework.data.repository.CrudRepository; | |
6 | +import org.springframework.stereotype.Component; | |
7 | +import org.thingsboard.server.common.data.EntityView; | |
8 | +import org.thingsboard.server.common.data.id.EntityId; | |
9 | +import org.thingsboard.server.common.data.page.TextPageLink; | |
10 | +import org.thingsboard.server.dao.DaoUtil; | |
11 | +import org.thingsboard.server.dao.entityview.EntityViewDao; | |
12 | +import org.thingsboard.server.dao.model.sql.EntityViewEntity; | |
13 | +import org.thingsboard.server.dao.sql.JpaAbstractSearchTextDao; | |
14 | +import org.thingsboard.server.dao.util.SqlDao; | |
15 | + | |
16 | +import java.util.List; | |
17 | +import java.util.Objects; | |
18 | +import java.util.Optional; | |
19 | +import java.util.UUID; | |
20 | + | |
21 | +import static org.thingsboard.server.common.data.UUIDConverter.fromTimeUUID; | |
22 | +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID_STR; | |
23 | + | |
24 | +@Component | |
25 | +@SqlDao | |
26 | +public class JpaEntityViewDao extends JpaAbstractSearchTextDao<EntityViewEntity, EntityView> | |
27 | + implements EntityViewDao { | |
28 | + | |
29 | + @Autowired | |
30 | + EntityViewRepository entityViewRepository; | |
31 | + | |
32 | + @Override | |
33 | + protected Class<EntityViewEntity> getEntityClass() { | |
34 | + return EntityViewEntity.class; | |
35 | + } | |
36 | + | |
37 | + @Override | |
38 | + protected CrudRepository<EntityViewEntity, String> getCrudRepository() { | |
39 | + return entityViewRepository; | |
40 | + } | |
41 | + | |
42 | + @Override | |
43 | + public List<EntityView> findEntityViewByTenantId(UUID tenantId, TextPageLink pageLink) { | |
44 | + return DaoUtil.convertDataList( | |
45 | + entityViewRepository.findByTenantId( | |
46 | + fromTimeUUID(tenantId), | |
47 | + Objects.toString(pageLink.getTextSearch(), ""), | |
48 | + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), | |
49 | + new PageRequest(0, pageLink.getLimit()))); | |
50 | + } | |
51 | + | |
52 | + @Override | |
53 | + public Optional<EntityView> findEntityViewByTenantIdAndName(UUID tenantId, String name) { | |
54 | + return Optional.ofNullable( | |
55 | + DaoUtil.getData(entityViewRepository.findByTenantIdAndName(fromTimeUUID(tenantId), name))); | |
56 | + } | |
57 | + | |
58 | + @Override | |
59 | + public List<EntityView> findEntityViewByTenantIdAndEntityId(UUID tenantId, | |
60 | + UUID entityId, | |
61 | + TextPageLink pageLink) { | |
62 | + return DaoUtil.convertDataList( | |
63 | + entityViewRepository.findByTenantIdAndEntityId( | |
64 | + fromTimeUUID(tenantId), | |
65 | + fromTimeUUID(entityId), | |
66 | + Objects.toString(pageLink.getTextSearch(), ""), | |
67 | + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), | |
68 | + new PageRequest(0, pageLink.getLimit()))); | |
69 | + } | |
70 | + | |
71 | + @Override | |
72 | + public List<EntityView> findEntityViewsByTenantIdAndCustomerId(UUID tenantId, | |
73 | + UUID customerId, | |
74 | + TextPageLink pageLink) { | |
75 | + return DaoUtil.convertDataList( | |
76 | + entityViewRepository.findByTenantIdAndCustomerId( | |
77 | + fromTimeUUID(tenantId), | |
78 | + fromTimeUUID(customerId), | |
79 | + Objects.toString(pageLink, ""), | |
80 | + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), | |
81 | + new PageRequest(0, pageLink.getLimit()) | |
82 | + )); | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + public List<EntityView> findEntityViewsByTenantIdAndCustomerIdAndEntityId(UUID tenantId, | |
87 | + UUID customerId, | |
88 | + UUID entityId, | |
89 | + TextPageLink pageLink) { | |
90 | + return DaoUtil.convertDataList( | |
91 | + entityViewRepository.findByTenantIdAndCustomerIdAndEntityId( | |
92 | + fromTimeUUID(tenantId), | |
93 | + fromTimeUUID(customerId), | |
94 | + fromTimeUUID(entityId), | |
95 | + Objects.toString(pageLink, ""), | |
96 | + pageLink.getIdOffset() == null ? NULL_UUID_STR : fromTimeUUID(pageLink.getIdOffset()), | |
97 | + new PageRequest(0, pageLink.getLimit()) | |
98 | + )); | |
99 | + } | |
100 | +} | ... | ... |