in src/main/java/com/google/gcs/sdrs/service/impl/RetentionRulesServiceImpl.java [74:109]
public Integer createRetentionRule(RetentionRuleCreateRequest rule, UserInfo user)
throws SQLException, IOException {
String bucketName = RetentionUtil.getBucketName(rule.getDataStorageName());
if (rule.getRetentionRuleType() != RetentionRuleType.GLOBAL) {
if (!GcsHelper.getInstance().doesBucketExist(bucketName, rule.getProjectId())) {
throw new IOException(
String.format("Bucket %s does not exist for project %s", bucketName, rule.getProjectId()));
}
}
String userName = user.getEmail() == null ? DEFAULT_UNKNOWN_USER : user.getEmail();
List<RetentionRule> bucketRules =
ruleDao.findRulesByDataStorageRoot(
rule.getProjectId(),
RetentionUtil.getBucketName(rule.getDataStorageName()),
rule.getRetentionRuleType(),
true);
RetentionRule existingRule = getExistingRule(bucketRules, rule);
RetentionRule newRule = null;
if (existingRule == null) {
// This is a truly new rule
newRule = mapPojoToPersistenceEntity(rule, userName);
newRule.setId(ruleDao.save(newRule));
} else if (!existingRule.getIsActive()) {
// The rule is not new; re-use the previously deactivated rule with updated values
updateUserInputValues(rule, userName, existingRule);
existingRule.setIsActive(true);
existingRule.setVersion(existingRule.getVersion() + 1);
newRule = existingRule;
ruleDao.update(newRule);
}
return newRule.getId();
}