<commit_before>package qa.qcri.aidr.manager.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import qa.qcri.aidr.manager.dto.AidrCollectionTotalDTO;
import qa.qcri.aidr.manager.hibernateEntities.AidrCollectionLog;
import qa.qcri.aidr.manager.service.CollectionLogService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import qa.qcri.aidr.manager.dto.CollectionDataResponse;
import qa.qcri.aidr.manager.hibernateEntities.AidrCollection;
import qa.qcri.aidr.manager.hibernateEntities.UserEntity;
import qa.qcri.aidr.manager.service.CollectionService;
import qa.qcri.aidr.manager.util.CollectionStatus;

@Controller
@RequestMapping("protected/collection")
public class CollectionController extends BaseController{

	private Logger logger = Logger.getLogger(CollectionController.class);
	
	@Autowired
	private CollectionService collectionService;

    @Autowired
    private CollectionLogService collectionLogService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@RequestMapping(value = "/save.action", method={RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> save(AidrCollection collection) throws Exception {
		logger.info("Save AidrCollection to Database having code : "+collection.getCode());
		try{
			UserEntity entity = getAuthenticatedUser();
			collection.setUser(entity);
			collection.setStatus(CollectionStatus.NOT_RUNNING);
			Calendar now = Calendar.getInstance();
			collection.setCreatedDate(now.getTime());
			collectionService.create(collection);
			return getUIWrapper(true);  
		}catch(Exception e){
			logger.error("Error while saving AidrCollection Info to database", e);
			return getUIWrapper(false); 
		}
	}
	
	@RequestMapping(value = "/delete.action", method = { RequestMethod.POST ,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> delete( AidrCollection collection) throws Exception {
		logger.info("Deleting AidrCollection Info from Database having id "+collection.getId());
		try{
			collectionService.delete(collection);
			return getUIWrapper(true);  
		}catch(Exception e){
			logger.error("Error while deleting AIDR Collection Info from database", e);
			return getUIWrapper(false); 
		}
	}
	
	@RequestMapping(value = "/update.action", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update( AidrCollection collection) throws Exception {
        Integer collectionId = collection.getId();
		logger.info("Updating AidrCollection into Database having id " + collectionId);
		try{
            CollectionStatus status = collection.getStatus();
            if (CollectionStatus.RUNNING_WARNING.equals(status) || CollectionStatus.RUNNING.equals(status)) {
                AidrCollection dbCollection = collectionService.findById(collectionId);
//              stop collection
                collectionService.stopAidrFetcher(dbCollection);

//              save current state of the collection to collectionLog
                AidrCollectionLog collectionLog = new AidrCollectionLog();
                collectionLog.setCount(dbCollection.getCount());
                collectionLog.setEndDate(dbCollection.getEndDate());
                collectionLog.setFollow(dbCollection.getFollow());
                collectionLog.setGeo(dbCollection.getGeo());
                collectionLog.setLangFilters(dbCollection.getLangFilters());
                collectionLog.setStartDate(dbCollection.getStartDate());
                collectionLog.setTrack(dbCollection.getTrack());
                collectionLog.setCollectionID(collectionId);
                collectionLogService.create(collectionLog);

//              set some fields from old collection and update collection
                collection.setStartDate(dbCollection.getStartDate());
                collection.setEndDate(dbCollection.getEndDate());
                collection.setUser(dbCollection.getUser());
                collection.setCreatedDate(dbCollection.getCreatedDate());
                collectionService.update(collection);

//              start collection
                collectionService.startFetcher(collectionService.prepareFetcherRequest(collection), collection);
            } else {
                AidrCollection dbCollection = collectionService.findById(collection.getId());
                collection.setStartDate(dbCollection.getStartDate());
                collection.setUser(dbCollection.getUser());
                collection.setCreatedDate(dbCollection.getCreatedDate());
                collectionService.update(collection);
            }
			return getUIWrapper(true);
		}catch(Exception e){<commit_msg>aidr-manager: Edit collection - change end time while edit running collection (bug).<commit_after>package qa.qcri.aidr.manager.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import qa.qcri.aidr.manager.dto.AidrCollectionTotalDTO;
import qa.qcri.aidr.manager.hibernateEntities.AidrCollectionLog;
import qa.qcri.aidr.manager.service.CollectionLogService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import qa.qcri.aidr.manager.dto.CollectionDataResponse;
import qa.qcri.aidr.manager.hibernateEntities.AidrCollection;
import qa.qcri.aidr.manager.hibernateEntities.UserEntity;
import qa.qcri.aidr.manager.service.CollectionService;
import qa.qcri.aidr.manager.util.CollectionStatus;

@Controller
@RequestMapping("protected/collection")
public class CollectionController extends BaseController{

	private Logger logger = Logger.getLogger(CollectionController.class);
	
	@Autowired
	private CollectionService collectionService;

    @Autowired
    private CollectionLogService collectionLogService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	@RequestMapping(value = "/save.action", method={RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> save(AidrCollection collection) throws Exception {
		logger.info("Save AidrCollection to Database having code : "+collection.getCode());
		try{
			UserEntity entity = getAuthenticatedUser();
			collection.setUser(entity);
			collection.setStatus(CollectionStatus.NOT_RUNNING);
			Calendar now = Calendar.getInstance();
			collection.setCreatedDate(now.getTime());
			collectionService.create(collection);
			return getUIWrapper(true);  
		}catch(Exception e){
			logger.error("Error while saving AidrCollection Info to database", e);
			return getUIWrapper(false); 
		}
	}
	
	@RequestMapping(value = "/delete.action", method = { RequestMethod.POST ,RequestMethod.GET })
	@ResponseBody
	public Map<String,Object> delete( AidrCollection collection) throws Exception {
		logger.info("Deleting AidrCollection Info from Database having id "+collection.getId());
		try{
			collectionService.delete(collection);
			return getUIWrapper(true);  
		}catch(Exception e){
			logger.error("Error while deleting AIDR Collection Info from database", e);
			return getUIWrapper(false); 
		}
	}
	
	@RequestMapping(value = "/update.action", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> update( AidrCollection collection) throws Exception {
        Integer collectionId = collection.getId();
		logger.info("Updating AidrCollection into Database having id " + collectionId);
		try{
            CollectionStatus status = collection.getStatus();
            if (CollectionStatus.RUNNING_WARNING.equals(status) || CollectionStatus.RUNNING.equals(status)) {
                AidrCollection dbCollection = collectionService.findById(collectionId);
//              stop collection
                AidrCollection collectionAfterStop = collectionService.stopAidrFetcher(dbCollection);

//              save current state of the collection to collectionLog
                AidrCollectionLog collectionLog = new AidrCollectionLog();
                collectionLog.setCount(dbCollection.getCount());
                collectionLog.setEndDate(collectionAfterStop.getEndDate());
                collectionLog.setFollow(dbCollection.getFollow());
                collectionLog.setGeo(dbCollection.getGeo());
                collectionLog.setLangFilters(dbCollection.getLangFilters());
                collectionLog.setStartDate(dbCollection.getStartDate());
                collectionLog.setTrack(dbCollection.getTrack());
                collectionLog.setCollectionID(collectionId);
                collectionLogService.create(collectionLog);

//              set some fields from old collection and update collection
                collection.setStartDate(dbCollection.getStartDate());
                collection.setEndDate(collectionAfterStop.getEndDate());
                collection.setUser(dbCollection.getUser());
                collection.setCreatedDate(dbCollection.getCreatedDate());
                collectionService.update(collection);

//              start collection
                collectionService.startFetcher(collectionService.prepareFetcherRequest(collection), collection);
            } else {
                AidrCollection dbCollection = collectionService.findById(collection.getId());
                collection.setStartDate(dbCollection.getStartDate());
                collection.setUser(dbCollection.getUser());
                collection.setCreatedDate(dbCollection.getCreatedDate());
                collectionService.update(collection);
            }
			return getUIWrapper(true);
		}catch(Exception e){
{'fraction_non_alphanumeric': 0.09516366311143803, 'fraction_numerical': 0.00018389113644722325, 'mean_word_length': 4.082710280373832, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3406891', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2771, 'n_words': 507}