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