package jp.ac.nii.prl.mape.redundancy.service;

import java.util.Collection;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jp.ac.nii.prl.mape.redundancy.model.Instance;
import jp.ac.nii.prl.mape.redundancy.model.RedundancyView;
import jp.ac.nii.prl.mape.redundancy.repository.RedundancyViewRepository;

@Service("redundancyViewService")
public class RedundancyViewServiceImpl implements RedundancyViewService {

	@Autowired
	private RedundancyViewRepository redundancyViewRepository;
	
	@Autowired
	private InstanceService instanceService;
	
	/* (non-Javadoc)
	 * @see jp.ac.nii.prl.mape.redundancy.service.RedundancyViewService#save(jp.ac.nii.prl.mape.redundancy.model.RedundancyView)
	 */
	@Override
	public void save(RedundancyView redundancyView) {
		redundancyViewRepository.save(redundancyView);
		for (Instance instance:redundancyView.getInstances()) {
			instanceService.save(instance);
		}
	}
	
	/* (non-Javadoc)
	 * @see jp.ac.nii.prl.mape.redundancy.service.RedundancyViewService#findOne(java.lang.Long)
	 */
	@Override
	public Optional<RedundancyView> findOne(Long redundancyViewId) {
		RedundancyView redundancy = redundancyViewRepository.findOne(redundancyViewId);
		if (redundancy == null)
			return Optional.empty();
		Collection<Instance> instances = instanceService.findByRedundancyViewId(redundancyViewId);
		redundancy.setInstances(instances);
		return Optional.of(redundancy);
	}

	@Override
	public void analyseAndPlan(RedundancyView redundancyView) {
		if (redundancyView.getInstances().size() < 2) {// adaptation required
			addInstance(redundancyView);
		}
	}
	
	private void addInstance(RedundancyView redundancyView) {
		Instance instance = instanceService.createNewInstance(redundancyView);
		redundancyView.addInstance(instance);
		redundancyViewRepository.save(redundancyView);
		instanceService.save(instance);
	}

}

@@ -96,16 +96,52 @@
 ptional;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -1609,41 +1609,180 @@
 %7B%0A%09%09
-if (redundancyView.getInstances()
+Collection%3CInstance%3E liveInstances = redundancyView.getInstances()%0A%09%09%09%09.stream()%0A%09%09%09%09.filter(i -%3E i.getStatus() != 2)%0A%09%09%09%09.collect(Collectors.toList());%0A%09%09if (liveInstances
 .siz
@@ -1821,32 +1821,58 @@
 %0A%09%09%09addInstance(
+2 - liveInstances.size(), 
 redundancyView);
@@ -1907,16 +1907,28 @@
 nstance(
+int number, 
 Redundan
@@ -1952,16 +1952,318 @@
 View) %7B%0A
+%09%09while (number %3E 0) %7B%0A%09%09%09Collection%3CInstance%3E terminatingInstances = redundancyView.getInstances()%0A%09%09%09%09%09.stream()%0A%09%09%09%09%09.filter(i -%3E i.getStatus() == 2)%0A%09%09%09%09%09.collect(Collectors.toList());%0A%09%09%09if (!terminatingInstances.isEmpty()) %7B%0A%09%09%09%09terminatingInstances.iterator().next().setStatus(0);%0A%09%09%09%7D else %7B%0A%09%09
 %09%09Instan
@@ -2325,24 +2325,26 @@
 ncyView);%0A%09%09
+%09
+%09
 redundancyVi
@@ -2369,16 +2369,18 @@
 tance);%0A
+%09%09
 %09%09redund
@@ -2422,16 +2422,18 @@
 iew);%0A%09%09
+%09
+%09
 instance
@@ -2456,14 +2456,36 @@
 tance);%0A
+%09%09%09%7D%0A%09%09%09number--;%0A%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Fix bug in analysis and plan