|
| 1 | +package org.finos.waltz.service.system_version; |
| 2 | + |
| 3 | +import org.finos.waltz.model.system_version.ImmutableSystemVersionInfo; |
| 4 | +import org.finos.waltz.model.system_version.SystemVersionInfo; |
| 5 | +import org.jooq.CommonTableExpression; |
| 6 | +import org.jooq.DSLContext; |
| 7 | +import org.jooq.Field; |
| 8 | +import org.jooq.Record2; |
| 9 | +import org.jooq.impl.DSL; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.finos.waltz.schema.Tables.DATABASECHANGELOG; |
| 16 | + |
| 17 | +@Service |
| 18 | +public class SystemVersionService { |
| 19 | + |
| 20 | + private final DSLContext dsl; |
| 21 | + |
| 22 | + @Autowired |
| 23 | + public SystemVersionService(DSLContext dsl) { |
| 24 | + this.dsl = dsl; |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public SystemVersionInfo getVersionInfo() { |
| 29 | + return ImmutableSystemVersionInfo |
| 30 | + .builder() |
| 31 | + .packageVersion(packageVersion()) |
| 32 | + .databaseVersions(databaseVersions()) |
| 33 | + .build(); |
| 34 | + } |
| 35 | + |
| 36 | + private String packageVersion() { |
| 37 | + String version = getClass().getPackage().getImplementationVersion(); |
| 38 | + String title = getClass().getPackage().getImplementationTitle(); |
| 39 | + String vendor = getClass().getPackage().getImplementationVendor(); |
| 40 | + |
| 41 | + return String.format("%s - %s - %s", version, title, vendor); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + private List<String> databaseVersions() { |
| 46 | + Field<String> filenameField = DSL.field("filename", String.class); |
| 47 | + Field<Integer> rowNumField = DSL.field("rn", Integer.class); |
| 48 | + |
| 49 | + CommonTableExpression<Record2<String, Integer>> versionCTE = DSL |
| 50 | + .name("raw") |
| 51 | + .fields(filenameField.getName(), rowNumField.getName()) |
| 52 | + .as(DSL |
| 53 | + .select( |
| 54 | + DATABASECHANGELOG.FILENAME, |
| 55 | + DSL.rowNumber() |
| 56 | + .over(DSL |
| 57 | + .partitionBy(DATABASECHANGELOG.FILENAME) |
| 58 | + .orderBy(DATABASECHANGELOG.DATEEXECUTED))) |
| 59 | + .from(DATABASECHANGELOG) |
| 60 | + .orderBy(DATABASECHANGELOG.DATEEXECUTED.desc())); |
| 61 | + |
| 62 | + return dsl |
| 63 | + .with(versionCTE) |
| 64 | + .select(filenameField) |
| 65 | + .from(versionCTE) |
| 66 | + .where(rowNumField.eq(1)) |
| 67 | + .fetch(filenameField); |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments