Project

General

Profile

Download (4.66 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.apache.log4j.Logger;
12
import org.hibernate.Query;
13
import org.hibernate.SessionFactory;
14
import org.hibernate.StatelessSession;
15
import org.springframework.beans.factory.annotation.Autowired;
16

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

    
24
    private static final Logger logger = Logger.getLogger(RegistrationIdentifierMinter.class);
25

    
26
    enum Method {
27
        naturalNumberIncrement
28
    }
29

    
30
    private SessionFactory factory;
31
    @Autowired
32
    protected void setSessionFactory (SessionFactory  factory){
33
        this.factory = factory;
34
    }
35

    
36
    Integer minLocalId = 1;
37

    
38
    Integer maxLocalId = Integer.MAX_VALUE;
39

    
40
    String identifierFormatString = null;
41

    
42
    Method method = Method.naturalNumberIncrement;
43

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

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

    
60
    /**
61
     * {@inheritDoc}
62
     */
63
    @Override
64
    synchronized public Identifier<String> mint() throws OutOfIdentifiersException {
65

    
66
        if(identifierFormatString == null){
67
            logger.warn("identifierFormatString missing");
68
        }
69

    
70
        switch(method){
71
            case naturalNumberIncrement:
72
                return mintByNaturalNumberIncrement();
73
            default: throw new RuntimeException("unsupported genreation method: " + method);
74
        }
75
    }
76

    
77
    /**
78
     *
79
     */
80
    protected Identifier<String> mintByNaturalNumberIncrement() {
81
        Integer localid = null;
82
        Object result = null;
83
        StatelessSession session = factory.openStatelessSession();
84
        try{
85

    
86
            String filter = " where cast(reg.specificIdentifier as int) >= " + minLocalId + " AND cast(reg.specificIdentifier as int) <= " + maxLocalId;
87
            if(identifierFormatString != null){
88
                filter += " AND reg.identifier like '" + identifierFormatString.replaceAll("%s", "%") + "'";
89
            }
90
            String hql = "select max(cast(reg.specificIdentifier as int)) from Registration as reg" + filter;
91

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

    
103
//                  + " ( SELECT max(cast(reg.specificIdentifier as int)) FROM reg WHERE cast(reg.specificIdentifier as int) >= " + minLocalId + " AND cast(reg.specificIdentifier as int) <= " + maxLocalId + ") "
104
//                  //+ " as int)"
105
//                  //+ ")"
106
//                  + " FROM Registration reg " + filter;
107

    
108
            Query query = session.createQuery(hql);
109

    
110
            result = query.uniqueResult();
111
        } finally {
112
            session.close();
113
        }
114
        if(result != null){
115
            localid  = ((Integer)result) + 1;
116
            if(localid > maxLocalId){
117
                throw new OutOfIdentifiersException("No available identifiers left in range [" + minLocalId + ", " + maxLocalId + "]");
118
            }
119
        } else {
120
            localid = minLocalId;
121
        }
122

    
123
        if(localid != null){
124
            Identifier<String> identifier = new Identifier<String>();
125
            identifier.localId = localid.toString();
126
            if(identifierFormatString != null){
127
                identifier.identifier = String.format(identifierFormatString, identifier.localId);
128
            }
129
            return identifier;
130
        }
131
        return null; // should never happen
132
    }
133

    
134
    public void setGenerationMethod(Method method){
135
        this.method = method;
136
    }
137

    
138
    /**
139
     * @return the identifierFormatString
140
     */
141
    public String getIdentifierFormatString() {
142
        return identifierFormatString;
143
    }
144

    
145
    /**
146
     * @param identifierFormatString the identifierFormatString to set
147
     */
148
    public void setIdentifierFormatString(String identifierFormatString) {
149
        this.identifierFormatString = identifierFormatString;
150
    }
151

    
152

    
153
}
(4-4/4)