001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2026 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 ca.uhn.fhir.i18n.Msg; 023import org.apache.commons.lang3.Validate; 024import org.apache.commons.lang3.builder.EqualsBuilder; 025import org.apache.commons.lang3.builder.HashCodeBuilder; 026 027import java.util.Collections; 028import java.util.HashMap; 029import java.util.Map; 030import java.util.Set; 031import java.util.function.Function; 032 033public abstract class BaseTableColumnTask extends BaseTableTask { 034 035 protected Map<String, Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object>> myCalculators = 036 new HashMap<>(); 037 protected String myColumnName; 038 // If a concrete class decides to, they can define a custom WHERE clause for the task. 039 protected String myWhereClause; 040 041 private final ColumnNameCase myColumnNameCase; 042 043 public BaseTableColumnTask(String theProductVersion, String theSchemaVersion) { 044 this(theProductVersion, theSchemaVersion, ColumnNameCase.ALL_UPPER, Collections.emptySet()); 045 } 046 047 BaseTableColumnTask( 048 String theProductVersion, 049 String theSchemaVersion, 050 ColumnNameCase theColumnNameCase, 051 Set<ColumnDriverMappingOverride> theColumnDriverMappingOverrides) { 052 super(theProductVersion, theSchemaVersion, theColumnDriverMappingOverrides); 053 myColumnNameCase = theColumnNameCase; 054 } 055 056 public String getColumnName() { 057 return myColumnName; 058 } 059 060 public BaseTableColumnTask setColumnName(String theColumnName) { 061 switch (myColumnNameCase) { 062 case ALL_UPPER: 063 myColumnName = theColumnName.toUpperCase(); 064 break; 065 case ALL_LOWER: 066 myColumnName = theColumnName.toLowerCase(); 067 break; 068 default: 069 throw new IllegalArgumentException(Msg.code(2448) 070 + " Unknown ColumnNameCase was passed when setting column name case: " + myColumnNameCase); 071 } 072 return this; 073 } 074 075 protected String getWhereClause() { 076 if (myWhereClause == null) { 077 return getColumnName() + " IS NULL"; 078 } else { 079 return myWhereClause; 080 } 081 } 082 083 protected void setWhereClause(String theWhereClause) { 084 this.myWhereClause = theWhereClause; 085 } 086 087 @Override 088 public void validate() { 089 super.validate(); 090 Validate.notBlank(myColumnName, "Column name not specified"); 091 } 092 093 @Override 094 protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) { 095 BaseTableColumnTask otherObject = (BaseTableColumnTask) theOtherObject; 096 super.generateEquals(theBuilder, otherObject); 097 theBuilder.append(myColumnName, otherObject.myColumnName); 098 } 099 100 @Override 101 protected void generateHashCode(HashCodeBuilder theBuilder) { 102 super.generateHashCode(theBuilder); 103 theBuilder.append(myColumnName); 104 } 105 106 public BaseTableColumnTask addCalculator( 107 String theColumnName, 108 Function<BaseColumnCalculatorTask.MandatoryKeyMap<String, Object>, Object> theConsumer) { 109 Validate.isTrue(myCalculators.containsKey(theColumnName) == false); 110 myCalculators.put(theColumnName, theConsumer); 111 return this; 112 } 113}