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 org.apache.commons.lang3.Validate;
023import org.apache.commons.lang3.builder.EqualsBuilder;
024import org.apache.commons.lang3.builder.HashCodeBuilder;
025
026import java.util.HashMap;
027import java.util.Map;
028import java.util.function.Function;
029
030public abstract class BaseTableColumnTask extends BaseTableTask {
031
032        protected Map<String, Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object>> myCalculators =
033                        new HashMap<>();
034        protected String myColumnName;
035        // If a concrete class decides to, they can define a custom WHERE clause for the task.
036        protected String myWhereClause;
037
038        public BaseTableColumnTask(String theProductVersion, String theSchemaVersion) {
039                super(theProductVersion, theSchemaVersion);
040        }
041
042        public String getColumnName() {
043                return myColumnName;
044        }
045
046        public BaseTableColumnTask setColumnName(String theColumnName) {
047                myColumnName = theColumnName.toUpperCase();
048                return this;
049        }
050
051        protected String getWhereClause() {
052                if (myWhereClause == null) {
053                        return getColumnName() + " IS NULL";
054                } else {
055                        return myWhereClause;
056                }
057        }
058
059        protected void setWhereClause(String theWhereClause) {
060                this.myWhereClause = theWhereClause;
061        }
062
063        @Override
064        public void validate() {
065                super.validate();
066                Validate.notBlank(myColumnName, "Column name not specified");
067        }
068
069        @Override
070        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
071                BaseTableColumnTask otherObject = (BaseTableColumnTask) theOtherObject;
072                super.generateEquals(theBuilder, otherObject);
073                theBuilder.append(myColumnName, otherObject.myColumnName);
074        }
075
076        @Override
077        protected void generateHashCode(HashCodeBuilder theBuilder) {
078                super.generateHashCode(theBuilder);
079                theBuilder.append(myColumnName);
080        }
081
082        public BaseTableColumnTask addCalculator(
083                        String theColumnName,
084                        Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object> theConsumer) {
085                Validate.isTrue(myCalculators.containsKey(theColumnName) == false);
086                myCalculators.put(theColumnName, theConsumer);
087                return this;
088        }
089}