This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
BeatCommandHandler.java
106 lines (97 loc) · 4.89 KB
/
BeatCommandHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright 2017 The Mifos Initiative.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mifos.rhythm.service.internal.command.handler;
import io.mifos.core.command.annotation.Aggregate;
import io.mifos.core.command.annotation.CommandHandler;
import io.mifos.core.command.annotation.CommandLogLevel;
import io.mifos.rhythm.api.v1.domain.ClockOffset;
import io.mifos.rhythm.api.v1.events.BeatEvent;
import io.mifos.rhythm.api.v1.events.EventConstants;
import io.mifos.rhythm.service.ServiceConstants;
import io.mifos.rhythm.service.internal.command.CreateBeatCommand;
import io.mifos.rhythm.service.internal.command.DeleteBeatCommand;
import io.mifos.rhythm.service.internal.mapper.BeatMapper;
import io.mifos.rhythm.service.internal.repository.BeatEntity;
import io.mifos.rhythm.service.internal.repository.BeatRepository;
import io.mifos.rhythm.service.internal.service.ClockOffsetService;
import io.mifos.rhythm.service.internal.service.IdentityPermittableGroupService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Myrle Krantz
*/
@SuppressWarnings("unused")
@Aggregate
public class BeatCommandHandler {
private final IdentityPermittableGroupService identityPermittableGroupService;
private final BeatRepository beatRepository;
private final ClockOffsetService clockOffsetService;
private final EventHelper eventHelper;
private final Logger logger;
@Autowired
public BeatCommandHandler(
final IdentityPermittableGroupService identityPermittableGroupService,
final BeatRepository beatRepository,
final ClockOffsetService clockOffsetService,
final EventHelper eventHelper,
@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger) {
super();
this.identityPermittableGroupService = identityPermittableGroupService;
this.beatRepository = beatRepository;
this.clockOffsetService = clockOffsetService;
this.eventHelper = eventHelper;
this.logger = logger;
}
@CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.NONE)
public void process(final CreateBeatCommand createBeatCommand) {
processCreateBeatCommand(createBeatCommand);
final BeatEvent event
= new BeatEvent(createBeatCommand.getApplicationIdentifier(), createBeatCommand.getInstance().getIdentifier());
logger.info("Sending event {}", event);
eventHelper.sendEvent(EventConstants.POST_BEAT, createBeatCommand.getTenantIdentifier(), event);
}
//I want the transaction to close before I send a beat or log it being sent. So I need a separate function for the
//stuff that should happen in the transaction.
@SuppressWarnings("WeakerAccess")
@Transactional
public void processCreateBeatCommand(final CreateBeatCommand createBeatCommand) {
final boolean applicationHasRequestForAccessPermission = identityPermittableGroupService.checkThatApplicationHasRequestForAccessPermission(
createBeatCommand.getTenantIdentifier(), createBeatCommand.getApplicationIdentifier());
if (!applicationHasRequestForAccessPermission) {
logger.info("Rhythm needs permission to publish beats to application, but couldn't request that permission for tenant '{}' and application '{}'.",
createBeatCommand.getTenantIdentifier(), createBeatCommand.getApplicationIdentifier());
}
final ClockOffset clockOffset = clockOffsetService.findByTenantIdentifier(createBeatCommand.getTenantIdentifier());
final BeatEntity entity = BeatMapper.map(
createBeatCommand.getTenantIdentifier(),
createBeatCommand.getApplicationIdentifier(),
createBeatCommand.getInstance(),
clockOffset);
this.beatRepository.save(entity);
}
@CommandHandler(logStart = CommandLogLevel.INFO, logFinish = CommandLogLevel.NONE)
@Transactional
public void process(final DeleteBeatCommand deleteBeatCommand) {
this.beatRepository.deleteByTenantIdentifierAndApplicationIdentifierAndBeatIdentifier(
deleteBeatCommand.getTenantIdentifier(),
deleteBeatCommand.getApplicationIdentifier(),
deleteBeatCommand.getIdentifier());
eventHelper.sendEvent(EventConstants.DELETE_BEAT, deleteBeatCommand.getTenantIdentifier(),
new BeatEvent(deleteBeatCommand.getApplicationIdentifier(), deleteBeatCommand.getIdentifier()));
}
}