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