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 java.util.HashMap;
028import java.util.Map;
029import java.util.function.Function;
030
031public abstract class BaseTableColumnTask extends BaseTableTask {
032
033        protected Map<String, Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object>> myCalculators = 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(String theColumnName, Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object> theConsumer) {
083                Validate.isTrue(myCalculators.containsKey(theColumnName) == false);
084                myCalculators.put(theColumnName, theConsumer);
085                return this;
086        }
087}