Project

General

Profile

Download (4.43 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.api.service.idminter;
10

    
11
import org.hibernate.Query;
12
import org.hibernate.SessionFactory;
13
import org.hibernate.StatelessSession;
14
import org.springframework.beans.factory.annotation.Autowired;
15

    
16
/**
17
 * @author a.kohlbecker
18
 * @since Dec 12, 2017
19
 *
20
 */
21
public class RegistrationIdentifierMinter implements IdentifierMinter<String> {
22

    
23
    enum Method {
24
        naturalNumberIncrement
25
    }
26

    
27
    private SessionFactory factory;
28
    @Autowired
29
    protected void setSessionFactory (SessionFactory  factory){
30
        this.factory = factory;
31
    }
32

    
33
    Integer minLocalId = 1;
34

    
35
    Integer maxLocalId = Integer.MAX_VALUE;
36

    
37
    String identifierFormatString = null;
38

    
39
    Method method = Method.naturalNumberIncrement;
40

    
41
    /**
42
     * {@inheritDoc}
43
     */
44
    @Override
45
    public void setMinLocalId(String min) {
46
        minLocalId = Integer.valueOf(min);
47
    }
48

    
49
    /**
50
     * {@inheritDoc}
51
     */
52
    @Override
53
    public void setMaxLocalId(String max) {
54
        maxLocalId = Integer.valueOf(max);
55
    }
56

    
57
    /**
58
     * {@inheritDoc}
59
     */
60
    @Override
61
    synchronized public Identifier<String> mint() throws OutOfIdentifiersException {
62

    
63
        switch(method){
64
            case naturalNumberIncrement:
65
                return mintByNaturalNumberIncrement();
66
            default: throw new RuntimeException("unsupported genreation method: " + method);
67
        }
68
    }
69

    
70
    /**
71
     *
72
     */
73
    protected Identifier<String> mintByNaturalNumberIncrement() {
74
        Integer localid = null;
75
        Object result = null;
76
        StatelessSession session = factory.openStatelessSession();
77
        try{
78

    
79
            String filter = " where cast(reg.specificIdentifier as int) >= " + minLocalId + " AND cast(reg.specificIdentifier as int) <= " + maxLocalId;
80
            if(identifierFormatString != null){
81
                filter += " AND reg.identifier like '" + identifierFormatString.replaceAll("%s", "%") + "'";
82
            }
83
            String hql = "select max(cast(reg.specificIdentifier as int)) from Registration as reg" + filter;
84

    
85
            // a query with correlated sub-select should allow to filter out registrations which are not matching the formatString
86
            // but the hql below is not working as expected:
87
//          String filter = "";
88
//          if(identifierFormatString != null){
89
//              filter += " WHERE reg.identifier like '" + identifierFormatString.replaceAll("%s", "%") + "'";
90
//          }
91
//          String hql =
92
//                  "SELECT "
93
//                  //+ "max("
94
//                  // + " cast( "
95

    
96
//                  + " ( SELECT max(cast(reg.specificIdentifier as int)) FROM reg WHERE cast(reg.specificIdentifier as int) >= " + minLocalId + " AND cast(reg.specificIdentifier as int) <= " + maxLocalId + ") "
97
//                  //+ " as int)"
98
//                  //+ ")"
99
//                  + " FROM Registration reg " + filter;
100

    
101
            Query query = session.createQuery(hql);
102

    
103
            result = query.uniqueResult();
104
        } finally {
105
            session.close();
106
        }
107
        if(result != null){
108
            localid  = ((Integer)result) + 1;
109
            if(localid > maxLocalId){
110
                throw new OutOfIdentifiersException("No available identifiers left in range [" + minLocalId + ", " + maxLocalId + "]");
111
            }
112
        } else {
113
            localid = minLocalId;
114
        }
115

    
116
        if(localid != null){
117
            Identifier<String> identifier = new Identifier<String>();
118
            identifier.localId = localid.toString();
119
            if(identifierFormatString != null){
120
                identifier.identifier = String.format(identifierFormatString, identifier.localId);
121
            }
122
            return identifier;
123
        }
124
        return null; // should never happen
125
    }
126

    
127
    public void setGenerationMethod(Method method){
128
        this.method = method;
129
    }
130

    
131
    /**
132
     * @return the identifierFormatString
133
     */
134
    public String getIdentifierFormatString() {
135
        return identifierFormatString;
136
    }
137

    
138
    /**
139
     * @param identifierFormatString the identifierFormatString to set
140
     */
141
    public void setIdentifierFormatString(String identifierFormatString) {
142
        this.identifierFormatString = identifierFormatString;
143
    }
144

    
145

    
146
}
(4-4/4)