Project

General

Profile

Task #1464 » ChmiContractSliceSaveRecalculateProcessor.java

Alena Peterová, 01/22/2019 09:50 AM

 
1
package eu.bcvsolutions.idm.chmi.event.processor.contracts;
2

    
3
import java.io.Serializable;
4
import java.util.HashMap;
5
import java.util.Map;
6

    
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.context.annotation.Description;
9
import org.springframework.stereotype.Component;
10

    
11
import com.google.common.collect.ImmutableMap;
12

    
13
import eu.bcvsolutions.idm.core.api.domain.CoreResultCode;
14
import eu.bcvsolutions.idm.core.api.domain.OperationState;
15
import eu.bcvsolutions.idm.core.api.dto.DefaultResultModel;
16
import eu.bcvsolutions.idm.core.api.dto.IdmContractSliceDto;
17
import eu.bcvsolutions.idm.core.api.dto.IdmEntityStateDto;
18
import eu.bcvsolutions.idm.core.api.dto.OperationResultDto;
19
import eu.bcvsolutions.idm.core.api.event.CoreEventProcessor;
20
import eu.bcvsolutions.idm.core.api.event.DefaultEventResult;
21
import eu.bcvsolutions.idm.core.api.event.EntityEvent;
22
import eu.bcvsolutions.idm.core.api.event.EventResult;
23
import eu.bcvsolutions.idm.core.api.event.processor.ContractSliceProcessor;
24
import eu.bcvsolutions.idm.core.api.service.ContractSliceManager;
25
import eu.bcvsolutions.idm.core.api.service.EntityStateManager;
26
import eu.bcvsolutions.idm.core.api.service.IdmContractSliceService;
27
import eu.bcvsolutions.idm.core.model.event.ContractSliceEvent;
28
import eu.bcvsolutions.idm.core.model.event.ContractSliceEvent.ContractSliceEventType;
29
import eu.bcvsolutions.idm.core.scheduler.task.impl.ClearDirtyStateForContractSliceTaskExecutor;
30

    
31
/**
32
 * Project specific recalculation of contract slices.
33
 *
34
 * @author Ondrej Kopr
35
 *
36
 */
37

    
38
@Component
39
@Description("Update/recalculate contract by slice")
40
public class ChmiContractSliceSaveRecalculateProcessor extends CoreEventProcessor<IdmContractSliceDto>
41
implements ContractSliceProcessor {
42

    
43
	public static final String PROCESSOR_NAME = "chmi-contract-slice-save-recalculate-processor";
44
	//
45
	@Autowired
46
	private IdmContractSliceService service;
47
	@Autowired
48
	private ContractSliceManager sliceManager;
49
	@Autowired
50
	private EntityStateManager entityStateManager;
51

    
52
	public ChmiContractSliceSaveRecalculateProcessor() {
53
		super(ContractSliceEventType.UPDATE, ContractSliceEventType.CREATE, ContractSliceEventType.EAV_SAVE);
54
	}
55

    
56
	@Override
57
	public String getName() {
58
		return PROCESSOR_NAME;
59
	}
60

    
61
	@Override
62
	public EventResult<IdmContractSliceDto> process(EntityEvent<IdmContractSliceDto> event) {
63
		IdmContractSliceDto slice = event.getContent();
64
		IdmContractSliceDto originalSlice = event.getOriginalSource();
65

    
66
		// There is conditional for executing this processor
67
		if (this.getBooleanProperty(IdmContractSliceService.SET_DIRTY_STATE_CONTRACT_SLICE, event.getProperties())) {
68
			/*
69
			 * PROJSPEC EDIT: if is set slice as using as contract, set this flag to false
70
			 */
71
			if (slice.isUsingAsContract()) {
72
				slice.setUsingAsContract(false);
73
				service.publish(
74
						new ContractSliceEvent(ContractSliceEventType.UPDATE, slice, ImmutableMap
75
								.of(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, Boolean.TRUE)));
76
			}
77

    
78
			Map<String, Serializable> properties = new HashMap<>(event.getProperties());
79
			// save original slice into parameters, executor ClearDirtyFlagForContractSliceTaskExecutor need him for recalculation
80
			properties.put(ClearDirtyStateForContractSliceTaskExecutor.ORIGINAL_SLICE, originalSlice);
81
			// Creates new dirty states, dirty states must be process by executor
82
			createDirtyState(slice, properties);
83
			return new DefaultEventResult<>(event, this);
84
		}
85

    
86
		// recalculation
87
		Map<String, Serializable> eventProperties = event.getProperties();
88
		sliceManager.recalculateContractSlice(slice, originalSlice, eventProperties);
89

    
90
		event.setContent(service.get(slice.getId()));
91
		return new DefaultEventResult<>(event, this);
92
	}
93

    
94
	@Override
95
	public boolean conditional(EntityEvent<IdmContractSliceDto> event) {
96
		// Skip recalculation totally skip the processor, set dirty state just set dirty for current slice and previous state
97
		return !this.getBooleanProperty(IdmContractSliceService.SKIP_RECALCULATE_CONTRACT_SLICE, event.getProperties());
98
	}
99

    
100
	@Override
101
	public int getOrder() {
102
		// Execute after save
103
		return super.getOrder() + 1;
104
	}
105

    
106
	/**
107
	 * Create new dirty state for contract slice
108
	 *
109
	 * @param slice
110
	 * @param parameters
111
	 * @return
112
	 */
113
	private IdmEntityStateDto createDirtyState(IdmContractSliceDto slice, Map<String, Serializable> parameters) {
114
		Map<String, Object> transformedMarameters = new HashMap<String, Object>();
115
		transformedMarameters.put("entityId", slice.getId());
116
		transformedMarameters.putAll(parameters);
117
		
118
		DefaultResultModel resultModel = new DefaultResultModel(CoreResultCode.DIRTY_STATE, transformedMarameters);
119
		IdmEntityStateDto dirtyState = new IdmEntityStateDto();
120
		dirtyState.setResult(
121
				new OperationResultDto
122
					.Builder(OperationState.BLOCKED)
123
					.setModel(resultModel)
124
					.build());
125
		return entityStateManager.saveState(slice, dirtyState);
126
	}
127
}
(1-1/8)