001package ca.uhn.fhir.jpa.migrate.entity; 002 003/*- 004 * #%L 005 * HAPI FHIR Server - SQL Migration 006 * %% 007 * Copyright (C) 2014 - 2023 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 ca.uhn.fhir.jpa.migrate.taskdef.BaseTask; 024import ca.uhn.fhir.util.VersionEnum; 025import org.hibernate.annotations.GenericGenerator; 026import org.springframework.jdbc.core.PreparedStatementSetter; 027import org.springframework.jdbc.core.RowMapper; 028 029import javax.persistence.Column; 030import javax.persistence.Entity; 031import javax.persistence.GeneratedValue; 032import javax.persistence.GenerationType; 033import javax.persistence.Id; 034import java.util.Date; 035 036// Note even though we are using javax.persistence annotations here, we are managing these records outside of jpa 037// so these annotations are for informational purposes only 038@Entity 039public class HapiMigrationEntity { 040 public static final int VERSION_MAX_SIZE = 50; 041 public static final int DESCRIPTION_MAX_SIZE = 200; 042 public static final int TYPE_MAX_SIZE = 20; 043 public static final int SCRIPT_MAX_SIZE = 1000; 044 public static final int INSTALLED_BY_MAX_SIZE = 100; 045 public static final int CREATE_TABLE_PID = -1; 046 public static final String INITIAL_RECORD_DESCRIPTION = "<< HAPI FHIR Schema History table created >>"; 047 public static final String INITIAL_RECORD_SCRIPT = "HAPI FHIR"; 048 @Id 049 @GenericGenerator(name = "SEQ_FLY_HFJ_MIGRATION", strategy = "ca.uhn.fhir.jpa.model.dialect.HapiSequenceStyleGenerator") 050 @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_FLY_HFJ_MIGRATION") 051 @Column(name = "INSTALLED_RANK") 052 private Integer myPid; 053 054 @Column(name = "VERSION", length = VERSION_MAX_SIZE) 055 private String myVersion; 056 057 @Column(name = "DESCRIPTION", length = DESCRIPTION_MAX_SIZE) 058 private String myDescription; 059 060 @Column(name = "TYPE", length = TYPE_MAX_SIZE) 061 private String myType; 062 063 @Column(name = "SCRIPT", length = SCRIPT_MAX_SIZE) 064 private String myScript; 065 066 @Column(name = "CHECKSUM") 067 private Integer myChecksum; 068 069 @Column(name = "INSTALLED_BY", length = INSTALLED_BY_MAX_SIZE) 070 private String myInstalledBy; 071 072 @Column(name = "INSTALLED_ON") 073 private Date myInstalledOn; 074 075 @Column(name = "EXECUTION_TIME") 076 private Integer myExecutionTime; 077 078 @Column(name = "SUCCESS") 079 private Boolean mySuccess; 080 081 public static HapiMigrationEntity tableCreatedRecord() { 082 HapiMigrationEntity retVal = new HapiMigrationEntity(); 083 retVal.setPid(CREATE_TABLE_PID); 084 retVal.setDescription(INITIAL_RECORD_DESCRIPTION); 085 retVal.setType("TABLE"); 086 retVal.setScript(INITIAL_RECORD_SCRIPT); 087 retVal.setInstalledBy(VersionEnum.latestVersion().name()); 088 retVal.setInstalledOn(new Date()); 089 retVal.setExecutionTime(0); 090 retVal.setSuccess(true); 091 return retVal; 092 } 093 094 public Integer getPid() { 095 return myPid; 096 } 097 098 public void setPid(Integer thePid) { 099 myPid = thePid; 100 } 101 102 public String getVersion() { 103 return myVersion; 104 } 105 106 public void setVersion(String theVersion) { 107 myVersion = theVersion; 108 } 109 110 public String getDescription() { 111 return myDescription; 112 } 113 114 public void setDescription(String theDescription) { 115 myDescription = theDescription; 116 } 117 118 public String getType() { 119 return myType; 120 } 121 122 public void setType(String theType) { 123 myType = theType; 124 } 125 126 public String getScript() { 127 return myScript; 128 } 129 130 public void setScript(String theScript) { 131 myScript = theScript; 132 } 133 134 public Integer getChecksum() { 135 return myChecksum; 136 } 137 138 public void setChecksum(Integer theChecksum) { 139 myChecksum = theChecksum; 140 } 141 142 public String getInstalledBy() { 143 return myInstalledBy; 144 } 145 146 public void setInstalledBy(String theInstalledBy) { 147 myInstalledBy = theInstalledBy; 148 } 149 150 public Date getInstalledOn() { 151 return myInstalledOn; 152 } 153 154 public void setInstalledOn(Date theInstalledOn) { 155 myInstalledOn = theInstalledOn; 156 } 157 158 public Integer getExecutionTime() { 159 return myExecutionTime; 160 } 161 162 public void setExecutionTime(Integer theExecutionTime) { 163 myExecutionTime = theExecutionTime; 164 } 165 166 public Boolean getSuccess() { 167 return mySuccess; 168 } 169 170 public void setSuccess(Boolean theSuccess) { 171 mySuccess = theSuccess; 172 } 173 174 public static HapiMigrationEntity fromBaseTask(BaseTask theTask) { 175 HapiMigrationEntity retval = new HapiMigrationEntity(); 176 retval.setVersion(theTask.getMigrationVersion()); 177 retval.setDescription(theTask.getDescription()); 178 retval.setChecksum(theTask.hashCode()); 179 retval.setType("JDBC"); 180 return retval; 181 } 182 183 public static RowMapper<HapiMigrationEntity> rowMapper() { 184 return (rs, rowNum) -> { 185 HapiMigrationEntity entity = new HapiMigrationEntity(); 186 entity.setPid(rs.getInt(1)); 187 entity.setVersion(rs.getString(2)); 188 entity.setDescription(rs.getString(3)); 189 entity.setType(rs.getString(4)); 190 entity.setScript(rs.getString(5)); 191 entity.setChecksum(rs.getInt(6)); 192 entity.setInstalledBy(rs.getString(7)); 193 entity.setInstalledOn(rs.getDate(8)); 194 entity.setExecutionTime(rs.getInt(9)); 195 entity.setSuccess(rs.getBoolean(10)); 196 return entity; 197 }; 198 } 199 200 public PreparedStatementSetter asPreparedStatementSetter() { 201 return ps -> { 202 ps.setInt(1, getPid()); 203 ps.setString(2, getVersion()); 204 ps.setString(3, getDescription()); 205 ps.setString(4, getType()); 206 ps.setString(5, getScript()); 207 if (getChecksum() == null) { 208 ps.setNull(6, java.sql.Types.INTEGER); 209 } else { 210 ps.setInt(6, getChecksum()); 211 } 212 ps.setString(7, getInstalledBy()); 213 ps.setDate(8, getInstalledOn() != null ? new java.sql.Date(getInstalledOn().getTime()) : null); 214 ps.setInt(9, getExecutionTime()); 215 ps.setBoolean(10, getSuccess()); 216 }; 217 } 218}