001/*-
002 * #%L
003 * HAPI FHIR JPA Model
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.model.dialect;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.jpa.model.entity.StorageSettings;
024import ca.uhn.fhir.jpa.util.ISequenceValueMassager;
025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
026import org.apache.commons.lang3.Validate;
027import org.hibernate.HibernateException;
028import org.hibernate.MappingException;
029import org.hibernate.boot.model.relational.Database;
030import org.hibernate.boot.model.relational.ExportableProducer;
031import org.hibernate.boot.model.relational.SqlStringGenerationContext;
032import org.hibernate.engine.spi.SharedSessionContractImplementor;
033import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
034import org.hibernate.id.IdentifierGenerator;
035import org.hibernate.id.OptimizableGenerator;
036import org.hibernate.id.PersistentIdentifierGenerator;
037import org.hibernate.id.enhanced.Optimizer;
038import org.hibernate.id.enhanced.SequenceStyleGenerator;
039import org.hibernate.id.enhanced.StandardOptimizerDescriptor;
040import org.hibernate.service.ServiceRegistry;
041import org.hibernate.type.Type;
042import org.springframework.beans.factory.annotation.Autowired;
043
044import java.io.Serializable;
045import java.util.Properties;
046
047import static ca.uhn.fhir.jpa.model.util.JpaConstants.NO_MORE_PID;
048
049/**
050 * This is a sequence generator that wraps the Hibernate default sequence generator {@link SequenceStyleGenerator}
051 * and by default will therefore work exactly as the default would, but allows for customization.
052 */
053@SuppressWarnings("unused")
054public class HapiSequenceStyleGenerator
055                implements PersistentIdentifierGenerator, BulkInsertionCapableIdentifierGenerator, ExportableProducer {
056        public static final String ID_MASSAGER_TYPE_KEY = "hapi_fhir.sequence_generator_massager";
057        private final SequenceStyleGenerator myGen = new SequenceStyleGenerator();
058
059        @Autowired
060        private StorageSettings myStorageSettings;
061
062        private ISequenceValueMassager myIdMassager;
063        private boolean myConfigured;
064        private String myGeneratorName;
065
066        @Override
067        public boolean supportsBulkInsertionIdentifierGeneration() {
068                return myGen.supportsBulkInsertionIdentifierGeneration();
069        }
070
071        @Override
072        public String determineBulkInsertionIdentifierGenerationSelectFragment(SqlStringGenerationContext theContext) {
073                return myGen.determineBulkInsertionIdentifierGenerationSelectFragment(theContext);
074        }
075
076        @Override
077        public Serializable generate(SharedSessionContractImplementor theSession, Object theObject)
078                        throws HibernateException {
079                Long retVal = myIdMassager != null ? myIdMassager.generate(myGeneratorName) : null;
080                if (retVal == null) {
081                        Long next = (Long) myGen.generate(theSession, theObject);
082
083                        retVal = myIdMassager.massage(myGeneratorName, next);
084
085                        /*
086                         * This should never happen since the sequence starts at 1, but if someone ever manually messes with sequences
087                         * or the sequence otherwise gets messed up, we don't want to end up with a resource using this PID which has
088                         * a special meaning to HAPI.
089                         */
090                        if (NO_MORE_PID.equals(next) || NO_MORE_PID.equals(retVal)) {
091                                throw new InternalErrorException(
092                                                Msg.code(2791) + "Resource ID generator provided illegal value: " + next + " / " + retVal);
093                        }
094                }
095                return retVal;
096        }
097
098        @Override
099        public void configure(Type theType, Properties theParams, ServiceRegistry theServiceRegistry)
100                        throws MappingException {
101
102                myIdMassager = theServiceRegistry.getService(ISequenceValueMassager.class);
103                if (myIdMassager == null) {
104                        myIdMassager = new ISequenceValueMassager.NoopSequenceValueMassager();
105                }
106
107                // Create a HAPI FHIR sequence style generator
108                myGeneratorName = theParams.getProperty(IdentifierGenerator.GENERATOR_NAME);
109                Validate.notBlank(myGeneratorName, "No generator name found");
110
111                Properties props = new Properties(theParams);
112                props.put(OptimizableGenerator.OPT_PARAM, StandardOptimizerDescriptor.POOLED.getExternalName());
113                props.put(OptimizableGenerator.INITIAL_PARAM, "1");
114                props.put(OptimizableGenerator.INCREMENT_PARAM, "50");
115                props.put(GENERATOR_NAME, myGeneratorName);
116
117                myGen.configure(theType, props, theServiceRegistry);
118
119                myConfigured = true;
120        }
121
122        @Override
123        public void registerExportables(Database database) {
124                myGen.registerExportables(database);
125        }
126
127        @Override
128        public void initialize(SqlStringGenerationContext context) {
129                myGen.initialize(context);
130        }
131
132        @Override
133        public boolean supportsJdbcBatchInserts() {
134                return myGen.supportsJdbcBatchInserts();
135        }
136
137        @Override
138        public Optimizer getOptimizer() {
139                return myGen.getOptimizer();
140        }
141}