ed2ac531b9ba2890f8ad55c9610f0ffd78792a15
[ccsdk/apps.git] / ms / neng / src / main / java / org / onap / ccsdk / apps / ms / neng / persistence / repository / GeneratedNameRespository.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.apps.ms.neng.persistence.repository;
22
23 import java.util.List;
24 import org.onap.ccsdk.apps.ms.neng.persistence.entity.GeneratedName;
25 import org.springframework.data.jpa.repository.Query;
26 import org.springframework.data.repository.CrudRepository;
27 import org.springframework.data.repository.query.Param;
28
29 /**
30  * Repository for the GeneratedName entity.
31  */
32 public interface GeneratedNameRespository extends CrudRepository<GeneratedName, Integer> {
33     
34     /*
35      * Finds an entity by element type, name and the 'isReleased' flag.
36      */
37     public GeneratedName findByElementTypeAndNameAndIsReleased(String elementType, String name, String isReleased);
38
39     /*
40      * Finds entities for a given external system ID.
41      */
42     public List<GeneratedName> findByExternalId(String externalId);
43
44     /*
45      * Finds entities for a given external system ID and element type, ignoring any special characters in names.
46      */
47     @Query(value = "select * from Generated_Name g where g.external_Id=:externalId and "
48                  + "REPLACE(REPLACE(REPLACE(element_type,'NAME',''),'-',''),'_','')=:elementType", 
49                  nativeQuery = true)
50     public GeneratedName findByExternalIdAndRelaxedElementType(@Param("externalId")String externalId,  
51                                                                @Param("elementType")String elementType);
52
53     /*
54      * Finds the maximum sequence number used for a given prefix and suffix.
55      */
56     @Query("select max(sequenceNumber) from GeneratedName where prefix=:prefix "
57                     + "and ((suffix is null and :suffix is null) or suffix=:suffix)")
58     public String findMaxByPrefixAndSuffix(@Param("prefix") String prefix, @Param("suffix") String suffix);
59
60     /*
61      * Finds the maximum sequence number used that is greater than the given sequence number,
62      * for a given prefix and suffix. 
63      */
64     @Query("select max(sequenceNumber) from GeneratedName g where g.sequenceNumber > :seqNum "
65                     + "and g.prefix=:prefix and ((:suffix is null and g.suffix is null) or g.suffix=:suffix) "
66                     + "and g.isReleased='Y'")
67     public Long findNextReleasedSeq(@Param("seqNum") Long seqNum, @Param("prefix") String prefix,
68                     @Param("suffix") String suffix);
69
70     /*
71      * Finds the entity for given type and name, that is NOT already released. 
72      */
73     @Query("select g from GeneratedName g where g.elementType=:elementType "
74                     + "and g.name=:name and (g.isReleased is null or g.isReleased ='N')")
75     public GeneratedName findUnReleased(@Param("elementType") String elementType, @Param("name") String name);
76
77
78 }