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