001package ca.uhn.fhir.jpa.migrate.taskdef;
002
003/*-
004 * #%L
005 * HAPI FHIR Server - SQL Migration
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import org.apache.commons.lang3.Validate;
024import org.apache.commons.lang3.builder.EqualsBuilder;
025import org.apache.commons.lang3.builder.HashCodeBuilder;
026
027import javax.annotation.Nullable;
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
038        public BaseTableColumnTypeTask(String theProductVersion, String theSchemaVersion) {
039                super(theProductVersion, theSchemaVersion);
040        }
041
042        public ColumnTypeEnum getColumnType() {
043                return myColumnType;
044        }
045
046        public BaseTableColumnTask setColumnType(ColumnTypeEnum theColumnType) {
047                myColumnType = theColumnType;
048                return this;
049        }
050
051        @Override
052        public void validate() {
053                super.validate();
054                Validate.notNull(myColumnType);
055                Validate.notNull(myNullable);
056
057                if (myColumnType == ColumnTypeEnum.STRING) {
058                        Validate.notNull(myColumnLength, "No length specified for " + ColumnTypeEnum.STRING + " column " + getColumnName());
059                } else {
060                        Validate.isTrue(myColumnLength == null);
061                }
062        }
063
064        protected String getSqlType() {
065                return getSqlType(getColumnLength());
066        }
067
068        protected String getSqlType(Long theColumnLength) {
069                return getSqlType(myColumnType, theColumnLength);
070        }
071
072        public boolean isNullable() {
073                return myNullable;
074        }
075
076        public BaseTableColumnTask setNullable(boolean theNullable) {
077                myNullable = theNullable;
078                return this;
079        }
080
081        protected String getSqlNotNull() {
082                return isNullable() ? " null " : " not null";
083        }
084
085        public Long getColumnLength() {
086                return myColumnLength;
087        }
088
089        public BaseTableColumnTypeTask setColumnLength(long theColumnLength) {
090                myColumnLength = theColumnLength;
091                return this;
092        }
093
094        @Override
095        protected void generateHashCode(HashCodeBuilder theBuilder) {
096                super.generateHashCode(theBuilder);
097                theBuilder.append(getColumnTypeName(myColumnType));
098                theBuilder.append(myNullable);
099                theBuilder.append(myColumnLength);
100        }
101
102        @Override
103        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
104                BaseTableColumnTypeTask otherObject = (BaseTableColumnTypeTask) theOtherObject;
105                super.generateEquals(theBuilder, otherObject);
106                theBuilder.append(getColumnTypeName(myColumnType), getColumnTypeName(otherObject.myColumnType));
107                theBuilder.append(myNullable, otherObject.myNullable);
108                theBuilder.append(myColumnLength, otherObject.myColumnLength);
109        }
110
111        @Nullable
112        private Object getColumnTypeName(ColumnTypeEnum theColumnType) {
113                if (theColumnType == null) {
114                        return null;
115                }
116                return myColumnType.name();
117        }
118
119        public ColumnTypeToDriverTypeToSqlType getColumnTypeToDriverTypeToSqlType() {
120                return myColumnTypeToDriverTypeToSqlType;
121        }
122
123}