|
| 1 | +package org.finos.waltz.jobs.example.demodata; |
| 2 | + |
| 3 | +import org.apache.poi.ss.usermodel.Workbook; |
| 4 | +import org.apache.poi.ss.usermodel.WorkbookFactory; |
| 5 | +import org.finos.waltz.common.DateTimeUtilities; |
| 6 | +import org.finos.waltz.common.IOUtilities; |
| 7 | +import org.finos.waltz.common.LoggingUtilities; |
| 8 | +import org.finos.waltz.jobs.example.demodata.generators.AllocationAndPrimaryRatingGenerator; |
| 9 | +import org.finos.waltz.jobs.example.demodata.loaders.*; |
| 10 | +import org.finos.waltz.model.EntityKind; |
| 11 | +import org.finos.waltz.schema.tables.records.MeasurableCategoryRecord; |
| 12 | +import org.finos.waltz.schema.tables.records.RatingSchemeItemRecord; |
| 13 | +import org.finos.waltz.schema.tables.records.RatingSchemeRecord; |
| 14 | +import org.finos.waltz.service.DIConfiguration; |
| 15 | +import org.finos.waltz.service.entity_hierarchy.EntityHierarchyService; |
| 16 | +import org.jooq.DSLContext; |
| 17 | +import org.jooq.lambda.tuple.Tuple2; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | +import org.springframework.context.ApplicationContext; |
| 21 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 22 | +import org.springframework.stereotype.Service; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.sql.Timestamp; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import static java.lang.String.format; |
| 31 | +import static java.util.stream.Collectors.toSet; |
| 32 | +import static org.finos.waltz.common.MapUtilities.indexBy; |
| 33 | +import static org.finos.waltz.data.JooqUtilities.summarizeResults; |
| 34 | +import static org.finos.waltz.jobs.example.demodata.DemoUtils.*; |
| 35 | +import static org.jooq.lambda.tuple.Tuple.tuple; |
| 36 | + |
| 37 | + |
| 38 | +@Service |
| 39 | +public class DemoDataPopulator { |
| 40 | + |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(DemoDataPopulator.class); |
| 42 | + |
| 43 | + |
| 44 | + private final DSLContext dsl; |
| 45 | + private final EntityHierarchyService hierarchyService; |
| 46 | + |
| 47 | + |
| 48 | + public DemoDataPopulator(DSLContext dsl, EntityHierarchyService hierarchyService) { |
| 49 | + this.dsl = dsl; |
| 50 | + this.hierarchyService = hierarchyService; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + private void go() throws IOException { |
| 56 | + Workbook workbook = WorkbookFactory.create(IOUtilities.getFileResource("sample-data/v1.xlsx").getInputStream()); |
| 57 | + dsl.transaction(ctx -> { |
| 58 | + DSLContext waltz = ctx.dsl(); |
| 59 | + Timestamp now = DateTimeUtilities.nowUtcTimestamp(); |
| 60 | + |
| 61 | + blat(waltz); |
| 62 | + |
| 63 | + Tuple2<Long, Map<InvestmentRating, Long>> categoryAndScheme = createCategoryAndScheme(waltz, now); |
| 64 | + |
| 65 | + OrgUnitLoader.process(waltz, workbook.getSheet("org"), now); |
| 66 | + ApplicationLoader.process(waltz, workbook.getSheet("apps"), now); |
| 67 | + DataTypeLoader.process(waltz, workbook.getSheet("data-types"), now); |
| 68 | +// DataFlowLoader.process(waltz, workbook.getSheet("flows"), now); |
| 69 | + CapabilityLoader.process(waltz, workbook.getSheet("capabilities"), categoryAndScheme.v1, now); |
| 70 | + AppToCapabilityLoader.process(waltz, workbook.getSheet("app-cap"), now); |
| 71 | + AppToAssessmentLoader.process(waltz, workbook.getSheet("app-assessments"), now); |
| 72 | + AllocationAndPrimaryRatingGenerator.generate(waltz, categoryAndScheme.v1); |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + rebuildHierarchies(); |
| 77 | + |
| 78 | + |
| 79 | + throw new RuntimeException("rolling back, comment this line if you really want to do this!"); |
| 80 | + }); |
| 81 | + |
| 82 | + LOG.debug("Done"); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + private void rebuildHierarchies() { |
| 87 | + LOG.debug("Rebuilding hierarchies"); |
| 88 | + hierarchyService.buildFor(EntityKind.ORG_UNIT); |
| 89 | + hierarchyService.buildFor(EntityKind.MEASURABLE); |
| 90 | + hierarchyService.buildFor(EntityKind.PERSON); |
| 91 | + hierarchyService.buildFor(EntityKind.DATA_TYPE); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + private Tuple2<Long, Map<InvestmentRating, Long>> createCategoryAndScheme(DSLContext waltz, |
| 102 | + Timestamp now) { |
| 103 | + long rsId = createRatingScheme(waltz); |
| 104 | + Map<InvestmentRating, Long> ratingToIdMap = createRatingSchemeItems(waltz, rsId); |
| 105 | + long mcId = createCategory(waltz, rsId, now); |
| 106 | + return tuple(mcId, ratingToIdMap); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + private Map<InvestmentRating, Long> createRatingSchemeItems(DSLContext waltz, long rsId) { |
| 111 | + Set<RatingSchemeItemRecord> items = Stream |
| 112 | + .of(InvestmentRating.values()) |
| 113 | + .map(d -> { |
| 114 | + RatingSchemeItemRecord r = waltz.newRecord(rsi); |
| 115 | + r.setName(d.displayName); |
| 116 | + r.setColor(d.color); |
| 117 | + r.setDescription(format( |
| 118 | + "Investment Rating: %s", |
| 119 | + d.displayName)); |
| 120 | + r.setExternalId(d.name()); |
| 121 | + r.setSchemeId(rsId); |
| 122 | + r.setCode(d.code); |
| 123 | + return r; |
| 124 | + }) |
| 125 | + .collect(toSet()); |
| 126 | + |
| 127 | + int insertCount = summarizeResults(waltz.batchInsert(items).execute()); |
| 128 | + |
| 129 | + LOG.debug("Created {} new ratings for scheme {}", insertCount, rsId); |
| 130 | + |
| 131 | + return indexBy( |
| 132 | + items, |
| 133 | + d -> InvestmentRating.valueOf(d.getExternalId()), |
| 134 | + RatingSchemeItemRecord::getId); |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + private static long createRatingScheme(DSLContext waltz) { |
| 140 | + RatingSchemeRecord r = waltz.newRecord(rs); |
| 141 | + r.setName("Investment Status"); |
| 142 | + r.setDescription("Scheme reflecting Invest/Disinvest/Maintain ratings"); |
| 143 | + r.setExternalId("INVEST"); |
| 144 | + |
| 145 | + r.store(); |
| 146 | + |
| 147 | + LOG.debug("Created new scheme {}/{}", r.getExternalId(), r.getId()); |
| 148 | + |
| 149 | + return r.getId(); |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + private static long createCategory(DSLContext waltz, |
| 155 | + long rsId, |
| 156 | + Timestamp now) { |
| 157 | + MeasurableCategoryRecord r = waltz.newRecord(mc); |
| 158 | + r.setName("Capability"); |
| 159 | + r.setDescription("Capability Taxonomy"); |
| 160 | + r.setAllowPrimaryRatings(true); |
| 161 | + r.setExternalId("CAPABILITY"); |
| 162 | + r.setIconName("puzzle-piece"); |
| 163 | + r.setRatingSchemeId(rsId); |
| 164 | + r.setLastUpdatedAt(now); |
| 165 | + r.setLastUpdatedBy(USER_ID); |
| 166 | + r.store(); |
| 167 | + |
| 168 | + LOG.debug("Created new category {}/{}", r.getExternalId(), r.getId()); |
| 169 | + |
| 170 | + return r.getId(); |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + private void blat(DSLContext waltz) { |
| 175 | + LOG.warn("Deleting lots of data"); |
| 176 | + |
| 177 | + waltz.deleteFrom(alloc).execute(); |
| 178 | + waltz.deleteFrom(allocScheme).execute(); |
| 179 | + |
| 180 | + waltz.deleteFrom(mr).execute(); |
| 181 | + waltz.deleteFrom(ar).execute(); |
| 182 | + waltz.deleteFrom(m).execute(); |
| 183 | + waltz.deleteFrom(mc).execute(); |
| 184 | + waltz.deleteFrom(ad).execute(); |
| 185 | + waltz.deleteFrom(rsi).execute(); |
| 186 | + waltz.deleteFrom(rs).execute(); |
| 187 | + |
| 188 | + waltz.deleteFrom(lf).execute(); |
| 189 | + waltz.deleteFrom(lfd).execute(); |
| 190 | + waltz.deleteFrom(dt).execute(); |
| 191 | + |
| 192 | + waltz.deleteFrom(app).execute(); |
| 193 | + waltz.deleteFrom(ou).execute(); |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + public static void main(String[] args) throws Exception { |
| 198 | + LoggingUtilities.configureLogging(); |
| 199 | + |
| 200 | + ApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class); |
| 201 | + |
| 202 | + DemoDataPopulator loader = ctx.getBean(DemoDataPopulator.class); |
| 203 | + |
| 204 | + loader.go(); |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | +} |
0 commit comments