001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2023 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.migrate.taskdef; 021 022import org.apache.commons.lang3.Validate; 023import org.apache.commons.lang3.builder.EqualsBuilder; 024import org.apache.commons.lang3.builder.HashCodeBuilder; 025 026import javax.annotation.Nullable; 027 028public abstract class BaseTableColumnTypeTask extends BaseTableColumnTask { 029 private ColumnTypeEnum myColumnType; 030 private Boolean myNullable; 031 private Long myColumnLength; 032 033 /** 034 * Constructor 035 */ 036 public BaseTableColumnTypeTask(String theProductVersion, String theSchemaVersion) { 037 super(theProductVersion, theSchemaVersion); 038 } 039 040 public ColumnTypeEnum getColumnType() { 041 return myColumnType; 042 } 043 044 public BaseTableColumnTask setColumnType(ColumnTypeEnum theColumnType) { 045 myColumnType = theColumnType; 046 return this; 047 } 048 049 @Override 050 public void validate() { 051 super.validate(); 052 Validate.notNull(myColumnType); 053 Validate.notNull(myNullable); 054 055 if (myColumnType == ColumnTypeEnum.STRING) { 056 Validate.notNull( 057 myColumnLength, "No length specified for " + ColumnTypeEnum.STRING + " column " + getColumnName()); 058 } else { 059 Validate.isTrue(myColumnLength == null); 060 } 061 } 062 063 protected String getSqlType() { 064 return getSqlType(getColumnLength()); 065 } 066 067 protected String getSqlType(Long theColumnLength) { 068 return getSqlType(myColumnType, theColumnLength); 069 } 070 071 public boolean isNullable() { 072 return myNullable; 073 } 074 075 public BaseTableColumnTask setNullable(boolean theNullable) { 076 myNullable = theNullable; 077 return this; 078 } 079 080 protected String getSqlNotNull() { 081 return isNullable() ? " null " : " not null"; 082 } 083 084 public Long getColumnLength() { 085 return myColumnLength; 086 } 087 088 public BaseTableColumnTypeTask setColumnLength(long theColumnLength) { 089 myColumnLength = theColumnLength; 090 return this; 091 } 092 093 @Override 094 protected void generateHashCode(HashCodeBuilder theBuilder) { 095 super.generateHashCode(theBuilder); 096 theBuilder.append(getColumnTypeName(myColumnType)); 097 theBuilder.append(myNullable); 098 theBuilder.append(myColumnLength); 099 } 100 101 @Override 102 protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { 103 BaseTableColumnTypeTask otherObject = (BaseTableColumnTypeTask) theOtherObject; 104 super.generateEquals(theBuilder, otherObject); 105 theBuilder.append(getColumnTypeName(myColumnType), getColumnTypeName(otherObject.myColumnType)); 106 theBuilder.append(myNullable, otherObject.myNullable); 107 theBuilder.append(myColumnLength, otherObject.myColumnLength); 108 } 109 110 @Nullable 111 private Object getColumnTypeName(ColumnTypeEnum theColumnType) { 112 if (theColumnType == null) { 113 return null; 114 } 115 return myColumnType.name(); 116 } 117}