View Javadoc
1   package ca.uhn.fhir.jpa.config;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.i18n.HapiLocalizer;
24  import ca.uhn.fhir.jpa.entity.ResourceHistoryTable;
25  import ca.uhn.fhir.jpa.entity.ResourceIndexedCompositeStringUnique;
26  import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
27  import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
28  import org.hibernate.HibernateException;
29  import org.hibernate.exception.ConstraintViolationException;
30  import org.springframework.dao.DataAccessException;
31  import org.springframework.data.mapping.PreferredConstructor;
32  import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
33  
34  import static org.apache.commons.lang3.StringUtils.defaultString;
35  
36  public class HapiFhirHibernateJpaDialect extends HibernateJpaDialect {
37  
38  	private HapiLocalizer myLocalizer;
39  
40  	/**
41  	 * Constructor
42  	 */
43  	public HapiFhirHibernateJpaDialect(HapiLocalizer theLocalizer) {
44  		myLocalizer = theLocalizer;
45  	}
46  
47  	@Override
48  	protected DataAccessException convertHibernateAccessException(HibernateException theException) {
49  		if (theException instanceof ConstraintViolationException) {
50  			String constraintName = ((ConstraintViolationException) theException).getConstraintName();
51  			switch (defaultString(constraintName)) {
52  				case ResourceHistoryTable.IDX_RESVER_ID_VER:
53  					throw new ResourceVersionConflictException(myLocalizer.getMessage(HapiFhirHibernateJpaDialect.class, "resourceVersionConstraintFailure"));
54  				case ResourceIndexedCompositeStringUnique.IDX_IDXCMPSTRUNIQ_STRING:
55  					throw new ResourceVersionConflictException(myLocalizer.getMessage(HapiFhirHibernateJpaDialect.class, "resourceIndexedCompositeStringUniqueConstraintFailure"));
56  			}
57  		}
58  
59  		return super.convertHibernateAccessException(theException);
60  	}
61  
62  }