{"action":"created","comment":{"url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/comments/223682142","pull_request_review_id":162865771,"id":223682142,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIyMzY4MjE0Mg==","diff_hunk":"@@ -1,25 +1,94 @@\n package uk.gov.hmcts.probate.services.persistence.services;\n \n+import com.fasterxml.jackson.databind.JsonNode;\n+import com.fasterxml.jackson.databind.ObjectMapper;\n+import com.fasterxml.jackson.databind.node.ObjectNode;\n+\n+import javax.persistence.EntityManager;\n+import javax.persistence.LockModeType;\n+import javax.transaction.Transactional;\n+\n import org.springframework.beans.factory.annotation.Autowired;\n-import org.springframework.boot.context.config.ResourceNotFoundException;\n import org.springframework.jdbc.support.incrementer.PostgreSQLSequenceMaxValueIncrementer;\n import org.springframework.stereotype.Service;\n+import uk.gov.hmcts.probate.services.persistence.model.Registry;\n import uk.gov.hmcts.probate.services.persistence.model.RegistryNotConfiguredException;\n+import uk.gov.hmcts.probate.services.persistence.repository.RegistryRepository;\n \n+import java.util.List;\n import java.util.Map;\n+import java.util.stream.Collectors;\n \n @Service\n+@Transactional\n public class SequenceNumberService {\n \n-    @Autowired\n     private Map<String, PostgreSQLSequenceMaxValueIncrementer> registrySequenceNumbers;\n+    private RegistryRepository registryRepository;\n+    private EntityManager entityManager;\n+    private final ObjectMapper mapper;\n+\n+    private static final String SUBMISSION_REFERENCE = \"submissionReference\";\n+    private static final String REGISTRY = \"registry\";\n+    private static final String SEQUENCE_NUMBER = \"sequenceNumber\";\n+    private static final String NAME = \"name\";\n+    private static final String ADDRESS = \"address\";\n+    private static final String EMAIL = \"email\";\n \n-    public Long getNext(String registryName) {\n+    @Autowired\n+    public SequenceNumberService(Map<String, PostgreSQLSequenceMaxValueIncrementer> registrySequenceNumbers,\n+                                 RegistryRepository registryRepository, EntityManager entityManager,\n+                                 ObjectMapper mapper) {\n+        this.registrySequenceNumbers = registrySequenceNumbers;\n+        this.registryRepository = registryRepository;\n+        this.entityManager = entityManager;\n+        this.mapper = mapper;\n+    }\n+\n+    public JsonNode getNextRegistry(long submissionReference) {\n+        Registry nextRegistry = identifyNextRegistry();\n+        nextRegistry.incrementCounter();\n+        entityManager.lock(registryRepository.save(nextRegistry), LockModeType.PESSIMISTIC_WRITE);\n+        return populateRegistrySubmitData(submissionReference, nextRegistry);\n+    }\n \n+    public Long getNextSequenceNumber(String registryName) {\n         try {\n             return registrySequenceNumbers.get(registryName).nextLongValue();\n         } catch (NullPointerException e) {\n-                throw new RegistryNotConfiguredException(\"Registry not configured: \" + registryName, e);\n+            throw new RegistryNotConfiguredException(\"Registry not configured: \" + registryName, e);\n         }\n     }\n+\n+    JsonNode populateRegistrySubmitData(long submissionReference, Registry registry) {\n+        ObjectNode registryDataObject = mapper.createObjectNode();\n+        ObjectNode registryMapper = mapper.createObjectNode();\n+\n+        registryDataObject.put(SUBMISSION_REFERENCE, submissionReference);\n+        registryMapper.put(NAME, registry.capitalizeRegistryName());\n+        registryMapper.put(SEQUENCE_NUMBER, getNextSequenceNumber(registry.getId()));\n+        registryMapper.put(EMAIL, registry.getEmail());\n+        registryMapper.put(ADDRESS, registry.getAddress());\n+        registryDataObject.set(REGISTRY, registryMapper);\n+\n+        return registryDataObject;\n+    }\n+\n+    Registry identifyNextRegistry() {\n+        List<Registry> registries = registryRepository.findAll().stream()\n+                .filter(r -> r.getCounter() < r.getRatio())\n+                .collect(Collectors.toList());\n+        return registries.isEmpty() ?\n+                resetRegistryCounters().get(0) : registries.get(0);","path":"src/main/java/uk/gov/hmcts/probate/services/persistence/services/SequenceNumberService.java","position":86,"original_position":86,"commit_id":"e19bf55be651c77b7ab5fc5425c0ad16fd5ae115","original_commit_id":"e19bf55be651c77b7ab5fc5425c0ad16fd5ae115","user":{"login":"tomxelliott","id":20407957,"node_id":"MDQ6VXNlcjIwNDA3OTU3","avatar_url":"https://avatars0.githubusercontent.com/u/20407957?v=4","gravatar_id":"","url":"https://api.github.com/users/tomxelliott","html_url":"https://github.com/tomxelliott","followers_url":"https://api.github.com/users/tomxelliott/followers","following_url":"https://api.github.com/users/tomxelliott/following{/other_user}","gists_url":"https://api.github.com/users/tomxelliott/gists{/gist_id}","starred_url":"https://api.github.com/users/tomxelliott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomxelliott/subscriptions","organizations_url":"https://api.github.com/users/tomxelliott/orgs","repos_url":"https://api.github.com/users/tomxelliott/repos","events_url":"https://api.github.com/users/tomxelliott/events{/privacy}","received_events_url":"https://api.github.com/users/tomxelliott/received_events","type":"User","site_admin":false},"body":"I see where you are coming from and would be nicer. However the amount of code required to implement would bloat that function.","created_at":"2018-10-09T12:40:47Z","updated_at":"2018-10-09T12:40:47Z","html_url":"https://github.com/hmcts/probate-persistence-service/pull/69#discussion_r223682142","pull_request_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/comments/223682142"},"html":{"href":"https://github.com/hmcts/probate-persistence-service/pull/69#discussion_r223682142"},"pull_request":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69"}},"in_reply_to_id":223648948},"pull_request":{"url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69","id":218908083,"node_id":"MDExOlB1bGxSZXF1ZXN0MjE4OTA4MDgz","html_url":"https://github.com/hmcts/probate-persistence-service/pull/69","diff_url":"https://github.com/hmcts/probate-persistence-service/pull/69.diff","patch_url":"https://github.com/hmcts/probate-persistence-service/pull/69.patch","issue_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/69","number":69,"state":"open","locked":false,"title":"Pro 3554 store registry variables db","user":{"login":"tomxelliott","id":20407957,"node_id":"MDQ6VXNlcjIwNDA3OTU3","avatar_url":"https://avatars0.githubusercontent.com/u/20407957?v=4","gravatar_id":"","url":"https://api.github.com/users/tomxelliott","html_url":"https://github.com/tomxelliott","followers_url":"https://api.github.com/users/tomxelliott/followers","following_url":"https://api.github.com/users/tomxelliott/following{/other_user}","gists_url":"https://api.github.com/users/tomxelliott/gists{/gist_id}","starred_url":"https://api.github.com/users/tomxelliott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomxelliott/subscriptions","organizations_url":"https://api.github.com/users/tomxelliott/orgs","repos_url":"https://api.github.com/users/tomxelliott/repos","events_url":"https://api.github.com/users/tomxelliott/events{/privacy}","received_events_url":"https://api.github.com/users/tomxelliott/received_events","type":"User","site_admin":false},"body":"**Before creating a pull request make sure that:**\r\n\r\n- [ ] commit messages are meaningful and follow good commit message guidelines\r\n- [ ] README and other documentation has been updated / added (if needed)\r\n- [ ] tests have been updated / new tests has been added (if needed)\r\n\r\nPlease remove this line and everything above and fill the following sections:\r\n\r\n\r\n### JIRA link (if applicable) ###\r\n\r\n\r\n\r\n### Change description ###\r\n\r\n\r\n\r\n**Does this PR introduce a breaking change?** (check one with \"x\")\r\n\r\n```\r\n[ ] Yes\r\n[ ] No\r\n```\r\n","created_at":"2018-09-28T09:33:22Z","updated_at":"2018-10-09T12:40:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69/commits","review_comments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69/comments","review_comment_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/69/comments","statuses_url":"https://api.github.com/repos/hmcts/probate-persistence-service/statuses/e19bf55be651c77b7ab5fc5425c0ad16fd5ae115","head":{"label":"hmcts:PRO-3554-Store-Registry-Variables-DB","ref":"PRO-3554-Store-Registry-Variables-DB","sha":"e19bf55be651c77b7ab5fc5425c0ad16fd5ae115","user":{"login":"hmcts","id":22817111,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIyODE3MTEx","avatar_url":"https://avatars1.githubusercontent.com/u/22817111?v=4","gravatar_id":"","url":"https://api.github.com/users/hmcts","html_url":"https://github.com/hmcts","followers_url":"https://api.github.com/users/hmcts/followers","following_url":"https://api.github.com/users/hmcts/following{/other_user}","gists_url":"https://api.github.com/users/hmcts/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcts/subscriptions","organizations_url":"https://api.github.com/users/hmcts/orgs","repos_url":"https://api.github.com/users/hmcts/repos","events_url":"https://api.github.com/users/hmcts/events{/privacy}","received_events_url":"https://api.github.com/users/hmcts/received_events","type":"Organization","site_admin":false},"repo":{"id":129219613,"node_id":"MDEwOlJlcG9zaXRvcnkxMjkyMTk2MTM=","name":"probate-persistence-service","full_name":"hmcts/probate-persistence-service","private":false,"owner":{"login":"hmcts","id":22817111,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIyODE3MTEx","avatar_url":"https://avatars1.githubusercontent.com/u/22817111?v=4","gravatar_id":"","url":"https://api.github.com/users/hmcts","html_url":"https://github.com/hmcts","followers_url":"https://api.github.com/users/hmcts/followers","following_url":"https://api.github.com/users/hmcts/following{/other_user}","gists_url":"https://api.github.com/users/hmcts/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcts/subscriptions","organizations_url":"https://api.github.com/users/hmcts/orgs","repos_url":"https://api.github.com/users/hmcts/repos","events_url":"https://api.github.com/users/hmcts/events{/privacy}","received_events_url":"https://api.github.com/users/hmcts/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/hmcts/probate-persistence-service","description":null,"fork":false,"url":"https://api.github.com/repos/hmcts/probate-persistence-service","forks_url":"https://api.github.com/repos/hmcts/probate-persistence-service/forks","keys_url":"https://api.github.com/repos/hmcts/probate-persistence-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hmcts/probate-persistence-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hmcts/probate-persistence-service/teams","hooks_url":"https://api.github.com/repos/hmcts/probate-persistence-service/hooks","issue_events_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/events{/number}","events_url":"https://api.github.com/repos/hmcts/probate-persistence-service/events","assignees_url":"https://api.github.com/repos/hmcts/probate-persistence-service/assignees{/user}","branches_url":"https://api.github.com/repos/hmcts/probate-persistence-service/branches{/branch}","tags_url":"https://api.github.com/repos/hmcts/probate-persistence-service/tags","blobs_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hmcts/probate-persistence-service/statuses/{sha}","languages_url":"https://api.github.com/repos/hmcts/probate-persistence-service/languages","stargazers_url":"https://api.github.com/repos/hmcts/probate-persistence-service/stargazers","contributors_url":"https://api.github.com/repos/hmcts/probate-persistence-service/contributors","subscribers_url":"https://api.github.com/repos/hmcts/probate-persistence-service/subscribers","subscription_url":"https://api.github.com/repos/hmcts/probate-persistence-service/subscription","commits_url":"https://api.github.com/repos/hmcts/probate-persistence-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/comments{/number}","contents_url":"https://api.github.com/repos/hmcts/probate-persistence-service/contents/{+path}","compare_url":"https://api.github.com/repos/hmcts/probate-persistence-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hmcts/probate-persistence-service/merges","archive_url":"https://api.github.com/repos/hmcts/probate-persistence-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hmcts/probate-persistence-service/downloads","issues_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues{/number}","pulls_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls{/number}","milestones_url":"https://api.github.com/repos/hmcts/probate-persistence-service/milestones{/number}","notifications_url":"https://api.github.com/repos/hmcts/probate-persistence-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hmcts/probate-persistence-service/labels{/name}","releases_url":"https://api.github.com/repos/hmcts/probate-persistence-service/releases{/id}","deployments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/deployments","created_at":"2018-04-12T08:35:44Z","updated_at":"2018-09-27T13:07:50Z","pushed_at":"2018-09-30T08:09:30Z","git_url":"git://github.com/hmcts/probate-persistence-service.git","ssh_url":"git@github.com:hmcts/probate-persistence-service.git","clone_url":"https://github.com/hmcts/probate-persistence-service.git","svn_url":"https://github.com/hmcts/probate-persistence-service","homepage":null,"size":311,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":3,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"hmcts:master","ref":"master","sha":"6fad8322cb765a29619758e822c8495389abe076","user":{"login":"hmcts","id":22817111,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIyODE3MTEx","avatar_url":"https://avatars1.githubusercontent.com/u/22817111?v=4","gravatar_id":"","url":"https://api.github.com/users/hmcts","html_url":"https://github.com/hmcts","followers_url":"https://api.github.com/users/hmcts/followers","following_url":"https://api.github.com/users/hmcts/following{/other_user}","gists_url":"https://api.github.com/users/hmcts/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcts/subscriptions","organizations_url":"https://api.github.com/users/hmcts/orgs","repos_url":"https://api.github.com/users/hmcts/repos","events_url":"https://api.github.com/users/hmcts/events{/privacy}","received_events_url":"https://api.github.com/users/hmcts/received_events","type":"Organization","site_admin":false},"repo":{"id":129219613,"node_id":"MDEwOlJlcG9zaXRvcnkxMjkyMTk2MTM=","name":"probate-persistence-service","full_name":"hmcts/probate-persistence-service","private":false,"owner":{"login":"hmcts","id":22817111,"node_id":"MDEyOk9yZ2FuaXphdGlvbjIyODE3MTEx","avatar_url":"https://avatars1.githubusercontent.com/u/22817111?v=4","gravatar_id":"","url":"https://api.github.com/users/hmcts","html_url":"https://github.com/hmcts","followers_url":"https://api.github.com/users/hmcts/followers","following_url":"https://api.github.com/users/hmcts/following{/other_user}","gists_url":"https://api.github.com/users/hmcts/gists{/gist_id}","starred_url":"https://api.github.com/users/hmcts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmcts/subscriptions","organizations_url":"https://api.github.com/users/hmcts/orgs","repos_url":"https://api.github.com/users/hmcts/repos","events_url":"https://api.github.com/users/hmcts/events{/privacy}","received_events_url":"https://api.github.com/users/hmcts/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/hmcts/probate-persistence-service","description":null,"fork":false,"url":"https://api.github.com/repos/hmcts/probate-persistence-service","forks_url":"https://api.github.com/repos/hmcts/probate-persistence-service/forks","keys_url":"https://api.github.com/repos/hmcts/probate-persistence-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hmcts/probate-persistence-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hmcts/probate-persistence-service/teams","hooks_url":"https://api.github.com/repos/hmcts/probate-persistence-service/hooks","issue_events_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/events{/number}","events_url":"https://api.github.com/repos/hmcts/probate-persistence-service/events","assignees_url":"https://api.github.com/repos/hmcts/probate-persistence-service/assignees{/user}","branches_url":"https://api.github.com/repos/hmcts/probate-persistence-service/branches{/branch}","tags_url":"https://api.github.com/repos/hmcts/probate-persistence-service/tags","blobs_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hmcts/probate-persistence-service/statuses/{sha}","languages_url":"https://api.github.com/repos/hmcts/probate-persistence-service/languages","stargazers_url":"https://api.github.com/repos/hmcts/probate-persistence-service/stargazers","contributors_url":"https://api.github.com/repos/hmcts/probate-persistence-service/contributors","subscribers_url":"https://api.github.com/repos/hmcts/probate-persistence-service/subscribers","subscription_url":"https://api.github.com/repos/hmcts/probate-persistence-service/subscription","commits_url":"https://api.github.com/repos/hmcts/probate-persistence-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/hmcts/probate-persistence-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/comments{/number}","contents_url":"https://api.github.com/repos/hmcts/probate-persistence-service/contents/{+path}","compare_url":"https://api.github.com/repos/hmcts/probate-persistence-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hmcts/probate-persistence-service/merges","archive_url":"https://api.github.com/repos/hmcts/probate-persistence-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hmcts/probate-persistence-service/downloads","issues_url":"https://api.github.com/repos/hmcts/probate-persistence-service/issues{/number}","pulls_url":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls{/number}","milestones_url":"https://api.github.com/repos/hmcts/probate-persistence-service/milestones{/number}","notifications_url":"https://api.github.com/repos/hmcts/probate-persistence-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hmcts/probate-persistence-service/labels{/name}","releases_url":"https://api.github.com/repos/hmcts/probate-persistence-service/releases{/id}","deployments_url":"https://api.github.com/repos/hmcts/probate-persistence-service/deployments","created_at":"2018-04-12T08:35:44Z","updated_at":"2018-09-27T13:07:50Z","pushed_at":"2018-09-30T08:09:30Z","git_url":"git://github.com/hmcts/probate-persistence-service.git","ssh_url":"git@github.com:hmcts/probate-persistence-service.git","clone_url":"https://github.com/hmcts/probate-persistence-service.git","svn_url":"https://github.com/hmcts/probate-persistence-service","homepage":null,"size":311,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":3,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit","node_id":"MDc6TGljZW5zZTEz"},"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69"},"html":{"href":"https://github.com/hmcts/probate-persistence-service/pull/69"},"issue":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/69"},"comments":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/issues/69/comments"},"review_comments":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69/comments"},"review_comment":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/pulls/69/commits"},"statuses":{"href":"https://api.github.com/repos/hmcts/probate-persistence-service/statuses/e19bf55be651c77b7ab5fc5425c0ad16fd5ae115"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"tomxelliott"}}