Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.27 KB | None | 0 0
  1. package db.oracle.trueminer;
  2.  
  3. import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
  4. import org.springframework.beans.BeanUtils;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import org.springframework.jdbc.support.GeneratedKeyHolder;
  7.  
  8. import java.sql.*;
  9. import java.util.List;
  10. import java.util.Objects;
  11.  
  12. import static java.util.Collections.singletonList;
  13. import static java.util.stream.Collectors.toList;
  14. import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
  15.  
  16. /**
  17. * @author Pacific GeoTech System
  18. * <p>
  19. * Migration to move Right fields from Independent Right Type groups to Dependent Right Type groups;
  20. * to delete Independent Right Type groups;
  21. * to delete unnecessary Setting Custom Field Configuration
  22. */
  23. public class V1_2_0_666__MoveRightFieldsFromIndependentRightTypeGroupToDependentLol implements SpringJdbcMigration {
  24.  
  25. private static final String INDEPENDENT_RIGHT_TYPE_GROUPS = "select g.ID_PK, c.JURISDICTION_CODE, g.CUSTOM_GROUP_TYPE from TM_SETTING_CUSTOM_FIELD_CONFIG c " +
  26. "inner join TM_SETTING_CSTM_FLD_GRP g on g.CUSTOM_FIELD_SETTING_CONFIG_ID = c.ID_PK where c.SCREEN_TYPE = 'RIGHT'";
  27.  
  28. private static final String FIELD_TEMPLATES = "select ID_PK, ALIAS, BINDING, DATA_TYPE, DEFAULT_VALUE, DISPLAY, EFFECTIVE_DATE, EXPIRY_DATE, FIELD_LABEL, HAS_CHILD, " +
  29. "ITEM_ORDER, IS_REQUIRED, IS_MULTISELECT, IS_VISIBLE, SCHEMA_CF_TEMP_PARENT_ID, CUSTOM_FIELD_GROUP_ID, NOT_MANAGEABLE from TM_SETTING_CSTM_FLD_TMPLTE " +
  30. "where CUSTOM_FIELD_GROUP_ID = ?";
  31.  
  32. private static final String DEPENDENT_RIGHT_TYPE_GROUPS = "select g.ID_PK from TM_SETTING_CUSTOM_FIELD_CONFIG c " +
  33. "inner join TM_SETTING_CSTM_FLD_GRP g on g.CUSTOM_FIELD_SETTING_CONFIG_ID = c.ID_PK inner join TM_RIGHT_TYPE_CODE t on c.ID_PK = t.CUSTOM_FIELD_SETTING_CONFIG_ID " +
  34. "where c.SCREEN_TYPE = 'RIGHT_TYPE' and g.CUSTOM_GROUP_TYPE = ? and c.JURISDICTION_CODE = ?";
  35.  
  36. private static final String DEPENDENT_DEFAULT_RIGHT_TYPE_GROUPS = "select g.ID_PK from TM_SETTING_CSTM_FLD_GRP g inner join TM_SETTING_CUSTOM_FIELD_CONFIG c " +
  37. "on c.ID_PK = g.CUSTOM_FIELD_SETTING_CONFIG_ID where c.IS_DEFAULT = '1' and c.SCREEN_TYPE = 'RIGHT_TYPE' and g.CUSTOM_GROUP_TYPE = ? and c.JURISDICTION_CODE = ?";
  38.  
  39. private static final String INSERT_TEMPLATE = "Insert into TM_SETTING_CSTM_FLD_TMPLTE (ID_PK, ALIAS, BINDING, DATA_TYPE, DEFAULT_VALUE, DISPLAY, EFFECTIVE_DATE, EXPIRY_DATE, " +
  40. "FIELD_LABEL, HAS_CHILD, ITEM_ORDER, IS_REQUIRED, IS_MULTISELECT, IS_VISIBLE, SCHEMA_CF_TEMP_PARENT_ID, CUSTOM_FIELD_GROUP_ID, NOT_MANAGEABLE)" +
  41. " values (TM_SET_CUS_FIELD_TEMP_SEQ.nextVal,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  42.  
  43. private static final String DELETE_TEMPLATES = "delete from TM_SETTING_CSTM_FLD_TMPLTE where CUSTOM_FIELD_GROUP_ID in (?)";
  44. private static final String DELETE_GROUPS = "delete from TM_SETTING_CSTM_FLD_GRP where ID_PK in (?)";
  45. private static final String DELETE_CONFIG = "delete from TM_SETTING_CUSTOM_FIELD_CONFIG where SCREEN_TYPE = 'RIGHT'";
  46.  
  47. @Override
  48. public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
  49. List<IndependentRightTypeGroup> independentRightTypeGroups = findIndependentRightTypeGroups(jdbcTemplate);
  50.  
  51. for (IndependentRightTypeGroup independentRightTypeGroup : independentRightTypeGroups) {
  52. List<FieldTemplate> relatedTemplates = findRelatedTemplates(jdbcTemplate, independentRightTypeGroup);
  53. List<Long> dependentRightTypeGroupIds = findGroupIdsToAddTemplates(jdbcTemplate, independentRightTypeGroup);
  54.  
  55. addTemplates(jdbcTemplate, dependentRightTypeGroupIds, relatedTemplates);
  56.  
  57. removeTemplates(jdbcTemplate, relatedTemplates);
  58. }
  59.  
  60. removeGroups(jdbcTemplate, independentRightTypeGroups);
  61.  
  62. removeConfig(jdbcTemplate);
  63. }
  64.  
  65. private void removeConfig(JdbcTemplate jdbcTemplate) {
  66. jdbcTemplate.update(DELETE_CONFIG);
  67. }
  68.  
  69. private void removeGroups(JdbcTemplate jdbcTemplate, List<IndependentRightTypeGroup> independentRightTypeGroups) {
  70. List<Long> groupIds = independentRightTypeGroups.stream()
  71. .map(IndependentRightTypeGroup::getId)
  72. .collect(toList());
  73.  
  74. if (isNotEmpty(groupIds)) {
  75. jdbcTemplate.update(DELETE_GROUPS, groupIds);
  76. }
  77. }
  78.  
  79. private void removeTemplates(JdbcTemplate jdbcTemplate, List<FieldTemplate> relatedTemplates) {
  80. List<Long> templateIds = relatedTemplates.stream()
  81. .map(FieldTemplate::getId)
  82. .collect(toList());
  83.  
  84. if (isNotEmpty(templateIds)) {
  85. jdbcTemplate.update(DELETE_TEMPLATES, templateIds);
  86. }
  87. }
  88.  
  89. private void addTemplates(JdbcTemplate jdbcTemplate, List<Long> dependentRightTypeGroupIds, List<FieldTemplate> fieldTemplates) {
  90. for (FieldTemplate template : fieldTemplates) {
  91. addTemplateToGroups(jdbcTemplate, dependentRightTypeGroupIds, template, fieldTemplates);
  92. }
  93. }
  94.  
  95. private void addTemplateToGroups(JdbcTemplate jdbcTemplate, List<Long> dependentRightTypeGroupIds, FieldTemplate template, List<FieldTemplate> fieldTemplates) {
  96. for (Long dependentRightTypeGroupId : dependentRightTypeGroupIds) {
  97. if (hasChildren(template)) {
  98. processTemplateWithChildren(jdbcTemplate, template, dependentRightTypeGroupId, fieldTemplates);
  99. } else {
  100. addTemplate(jdbcTemplate, template, dependentRightTypeGroupId);
  101. }
  102. }
  103. }
  104.  
  105. /**
  106. * Returns inserted template id.
  107. */
  108. private Long addTemplate(JdbcTemplate jdbcTemplate, FieldTemplate template, Long groupId) {
  109. GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
  110.  
  111. jdbcTemplate.update(con -> {
  112. PreparedStatement statement = con.prepareStatement(INSERT_TEMPLATE, new String[]{"ID_PK"});
  113.  
  114. statement.setString(1, template.getAlias());
  115. statement.setString(2, template.getBinding());
  116. statement.setString(3, template.getDataType());
  117. statement.setString(4, template.getDefaultValue());
  118. setNumber(statement, 5, template.getDisplay(), 6);
  119. statement.setDate(6, template.getEffectiveDate());
  120. statement.setDate(7, template.getExpiryDate());
  121. statement.setString(8, template.getFieldLabel());
  122. setNumber(statement, 9, template.getHasChild(), 10);
  123. setNumber(statement, 10, template.getItemOrder(), 11);
  124. setNumber(statement, 11, template.getIsRequired(), 12);
  125. setNumber(statement, 12, template.getIsMultiselect(), 13);
  126. setNumber(statement, 13, template.getIsVisible(), 14);
  127. setNumber(statement, 14, template.getSchemaCfTempParentId(), 15);
  128. statement.setLong(15, groupId);
  129. setNumber(statement, 16, template.getNotManageable(), 17);
  130.  
  131. return statement;
  132. }, keyHolder);
  133.  
  134. return keyHolder.getKey().longValue();
  135. }
  136.  
  137. private <T extends Number> void setNumber(PreparedStatement statement, int targetColumnIndex, T number, int sourceColumnIndex) throws SQLException {
  138. if (number == null) {
  139. statement.setNull(targetColumnIndex, Types.NUMERIC);
  140. } else {
  141. statement.setLong(targetColumnIndex, number.longValue());
  142. }
  143. }
  144.  
  145.  
  146. private void processTemplateWithChildren(JdbcTemplate jdbcTemplate, FieldTemplate template, Long groupId, List<FieldTemplate> fieldTemplates) {
  147. List<FieldTemplate> children = fieldTemplates.stream()
  148. .filter(fieldTemplate -> Objects.equals(fieldTemplate.getSchemaCfTempParentId(), template.getId()))
  149. .collect(toList());
  150.  
  151. Long newTemplateId = addTemplate(jdbcTemplate, template, groupId);
  152.  
  153. for (FieldTemplate child : children) {
  154. FieldTemplate clone = new FieldTemplate();
  155. BeanUtils.copyProperties(child, clone);
  156. clone.setSchemaCfTempParentId(newTemplateId);
  157.  
  158. addTemplateToGroups(jdbcTemplate, singletonList(groupId), clone, fieldTemplates);
  159. }
  160. }
  161.  
  162. private boolean hasChildren(FieldTemplate template) {
  163. return template.getHasChild() == 1;
  164. }
  165.  
  166. private List<Long> findGroupIdsToAddTemplates(JdbcTemplate jdbcTemplate, IndependentRightTypeGroup independentRightTypeGroup) {
  167. List<Long> dependentRightTypeGroupIds = jdbcTemplate.queryForList(DEPENDENT_RIGHT_TYPE_GROUPS, Long.class,
  168. independentRightTypeGroup.getCustomGroupType(), independentRightTypeGroup.getJurisdictionCode());
  169.  
  170. List<Long> dependentDefaultRightTypeGroupIds = jdbcTemplate.queryForList(DEPENDENT_DEFAULT_RIGHT_TYPE_GROUPS, Long.class,
  171. independentRightTypeGroup.getCustomGroupType(), independentRightTypeGroup.getJurisdictionCode());
  172.  
  173. dependentRightTypeGroupIds.addAll(dependentDefaultRightTypeGroupIds);
  174.  
  175. return dependentRightTypeGroupIds;
  176. }
  177.  
  178. private List<FieldTemplate> findRelatedTemplates(JdbcTemplate jdbcTemplate, IndependentRightTypeGroup independentRightTypeGroup) {
  179. return jdbcTemplate.query(FIELD_TEMPLATES, new Long[]{independentRightTypeGroup.getId()}, this::createTemplate);
  180. }
  181.  
  182. private FieldTemplate createTemplate(ResultSet rs, int rowNumber) throws SQLException {
  183. FieldTemplate template = new FieldTemplate();
  184. template.setId(rs.getLong("ID_PK"));
  185. template.setAlias(rs.getString("ALIAS"));
  186. template.setBinding(rs.getString("BINDING"));
  187. template.setDataType(rs.getString("DATA_TYPE"));
  188. template.setDefaultValue(rs.getString("DEFAULT_VALUE"));
  189. template.setDisplay(getNullableInt(rs, "DISPLAY"));
  190. template.setEffectiveDate(rs.getDate("EFFECTIVE_DATE"));
  191. template.setExpiryDate(rs.getDate("EXPIRY_DATE"));
  192. template.setFieldLabel(rs.getString("FIELD_LABEL"));
  193. template.setHasChild(getNullableInt(rs, "HAS_CHILD"));
  194. template.setItemOrder(getNullableInt(rs, "ITEM_ORDER"));
  195. template.setIsRequired(getNullableInt(rs, "IS_REQUIRED"));
  196. template.setIsMultiselect(getNullableInt(rs, "IS_MULTISELECT"));
  197. template.setIsVisible(getNullableInt(rs, "IS_VISIBLE"));
  198. template.setSchemaCfTempParentId(getNullableLong(rs, "SCHEMA_CF_TEMP_PARENT_ID"));
  199. template.setCustomFieldGroupId(getNullableLong(rs, "CUSTOM_FIELD_GROUP_ID"));
  200. template.setNotManageable(getNullableInt(rs, "NOT_MANAGEABLE"));
  201. return template;
  202. }
  203.  
  204. private Integer getNullableInt(ResultSet rs, String columnLabel) throws SQLException {
  205. int intValue = rs.getInt(columnLabel);
  206. return rs.wasNull() ? null : intValue;
  207. }
  208.  
  209. private Long getNullableLong(ResultSet rs, String columnLabel) throws SQLException {
  210. long longValue = rs.getLong(columnLabel);
  211. return rs.wasNull() ? null : longValue;
  212. }
  213.  
  214. private List<IndependentRightTypeGroup> findIndependentRightTypeGroups(JdbcTemplate jdbcTemplate) {
  215. return jdbcTemplate.query(INDEPENDENT_RIGHT_TYPE_GROUPS,
  216. (rs, rowNum) -> new IndependentRightTypeGroup(rs.getLong("ID_PK"), rs.getString("JURISDICTION_CODE"), rs.getString("CUSTOM_GROUP_TYPE")));
  217. }
  218.  
  219.  
  220. private static class IndependentRightTypeGroup {
  221. private Long id;
  222. private String jurisdictionCode;
  223. private String customGroupType;
  224.  
  225. public IndependentRightTypeGroup(Long id, String jurisdictionCode, String customGroupType) {
  226. this.id = id;
  227. this.jurisdictionCode = jurisdictionCode;
  228. this.customGroupType = customGroupType;
  229. }
  230.  
  231. public Long getId() {
  232. return id;
  233. }
  234.  
  235. public void setId(Long id) {
  236. this.id = id;
  237. }
  238.  
  239. public String getJurisdictionCode() {
  240. return jurisdictionCode;
  241. }
  242.  
  243. public void setJurisdictionCode(String jurisdictionCode) {
  244. this.jurisdictionCode = jurisdictionCode;
  245. }
  246.  
  247. public String getCustomGroupType() {
  248. return customGroupType;
  249. }
  250.  
  251. public void setCustomGroupType(String customGroupType) {
  252. this.customGroupType = customGroupType;
  253. }
  254. }
  255.  
  256. private static class FieldTemplate {
  257. private Long id;
  258. private String alias;
  259. private String binding;
  260. private String dataType;
  261. private String defaultValue;
  262. private Integer display;
  263. private Date effectiveDate;
  264. private Date expiryDate;
  265. private String fieldLabel;
  266. private Integer hasChild;
  267. private Integer itemOrder;
  268. private Integer isRequired;
  269. private Integer isMultiselect;
  270. private Integer isVisible;
  271. private Long schemaCfTempParentId;
  272. private Long customFieldGroupId;
  273. private Integer notManageable;
  274.  
  275. public Long getId() {
  276. return id;
  277. }
  278.  
  279. public void setId(Long id) {
  280. this.id = id;
  281. }
  282.  
  283. public String getAlias() {
  284. return alias;
  285. }
  286.  
  287. public void setAlias(String alias) {
  288. this.alias = alias;
  289. }
  290.  
  291. public String getBinding() {
  292. return binding;
  293. }
  294.  
  295. public void setBinding(String binding) {
  296. this.binding = binding;
  297. }
  298.  
  299. public String getDataType() {
  300. return dataType;
  301. }
  302.  
  303. public void setDataType(String dataType) {
  304. this.dataType = dataType;
  305. }
  306.  
  307. public String getDefaultValue() {
  308. return defaultValue;
  309. }
  310.  
  311. public void setDefaultValue(String defaultValue) {
  312. this.defaultValue = defaultValue;
  313. }
  314.  
  315. public Integer getDisplay() {
  316. return display;
  317. }
  318.  
  319. public void setDisplay(Integer display) {
  320. this.display = display;
  321. }
  322.  
  323. public Date getEffectiveDate() {
  324. return effectiveDate;
  325. }
  326.  
  327. public void setEffectiveDate(Date effectiveDate) {
  328. this.effectiveDate = effectiveDate;
  329. }
  330.  
  331. public Date getExpiryDate() {
  332. return expiryDate;
  333. }
  334.  
  335. public void setExpiryDate(Date expiryDate) {
  336. this.expiryDate = expiryDate;
  337. }
  338.  
  339. public String getFieldLabel() {
  340. return fieldLabel;
  341. }
  342.  
  343. public void setFieldLabel(String fieldLabel) {
  344. this.fieldLabel = fieldLabel;
  345. }
  346.  
  347. public Integer getHasChild() {
  348. return hasChild;
  349. }
  350.  
  351. public void setHasChild(Integer hasChild) {
  352. this.hasChild = hasChild;
  353. }
  354.  
  355. public Integer getItemOrder() {
  356. return itemOrder;
  357. }
  358.  
  359. public void setItemOrder(Integer itemOrder) {
  360. this.itemOrder = itemOrder;
  361. }
  362.  
  363. public Integer getIsRequired() {
  364. return isRequired;
  365. }
  366.  
  367. public void setIsRequired(Integer isRequired) {
  368. this.isRequired = isRequired;
  369. }
  370.  
  371. public Integer getIsMultiselect() {
  372. return isMultiselect;
  373. }
  374.  
  375. public void setIsMultiselect(Integer isMultiselect) {
  376. this.isMultiselect = isMultiselect;
  377. }
  378.  
  379. public Integer getIsVisible() {
  380. return isVisible;
  381. }
  382.  
  383. public void setIsVisible(Integer isVisible) {
  384. this.isVisible = isVisible;
  385. }
  386.  
  387. public Long getSchemaCfTempParentId() {
  388. return schemaCfTempParentId;
  389. }
  390.  
  391. public void setSchemaCfTempParentId(Long schemaCfTempParentId) {
  392. this.schemaCfTempParentId = schemaCfTempParentId;
  393. }
  394.  
  395. public Long getCustomFieldGroupId() {
  396. return customFieldGroupId;
  397. }
  398.  
  399. public void setCustomFieldGroupId(Long customFieldGroupId) {
  400. this.customFieldGroupId = customFieldGroupId;
  401. }
  402.  
  403. public Integer getNotManageable() {
  404. return notManageable;
  405. }
  406.  
  407. public void setNotManageable(Integer notManageable) {
  408. this.notManageable = notManageable;
  409. }
  410. }
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement