<commit_before>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.web.controller.visit;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.util.Logger;
import org.openmrs.Encounter;
import org.openmrs.Form;
import org.openmrs.Patient;
import org.openmrs.Provider;
import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.web.controller.PortletControllerUtil;
import org.openmrs.web.controller.bean.DatatableRequest;
import org.openmrs.web.controller.bean.DatatableResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Lists visits.
 */
@Controller
public class VisitListController {
	
	protected final Logger log = Logger.getLogger(getClass());
	
	public static final String VISITS_PATH = "/admin/visits/datatable";
	
	public static final String PATIENT = "patient";
	
	/**
	 * It handles calls from DataTables.
	 * 
	 * @param patient
	 * @param request
	 * @return {@link DatatableResponse}
	 */
	@RequestMapping(VISITS_PATH)
	public @ResponseBody
	DatatableResponse getVisits(@ModelAttribute Patient patient, HttpServletRequest request) {
		DatatableRequest datatable = DatatableRequest.parseRequest(request);
		
		DatatableResponse response = new DatatableResponse(datatable);
		
		Integer totalVisitsCount = Context.getEncounterService().getEncountersByVisitsAndPatientCount(patient, false, null);
		response.setiTotalRecords(totalVisitsCount);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("person", patient);
		PortletControllerUtil.addFormToEditAndViewUrlMaps(model);
		@SuppressWarnings("unchecked")
		Map<Form, String> formToViewUrlMap = (Map<Form, String>) model.get("formToViewUrlMap");
		
		if (!StringUtils.isBlank(datatable.getsSearch())) {
			Integer filteredVisitsCount = Context.getEncounterService().getEncountersByVisitsAndPatientCount(patient, false,
			    datatable.getsSearch());
			response.setiTotalDisplayRecords(filteredVisitsCount);
		} else {
			response.setiTotalDisplayRecords(totalVisitsCount);
		}
		
		List<Encounter> encounters = Context.getEncounterService().getEncountersByVisitsAndPatient(patient, false,
		    datatable.getsSearch(), datatable.getiDisplayStart(), datatable.getiDisplayLength());
		
		response.setsColumns("visitId", "visitActive", "visitType", "visitLocation", "visitFrom", "visitTo",
		    "visitIndication", "firstInVisit", "lastInVisit", "encounterId", "encounterDate", "encounterType",
		    "encounterProviders", "encounterLocation", "encounterEnterer", "formViewURL");
		
		Map<String, String> row = new HashMap<String, String>();
		for (Encounter encounter : encounters) {
			if (encounter.getVisit() != null) {
				Visit visit = encounter.getVisit();
				row.put("visitId", visit.getId().toString());
				row.put("visitActive", Boolean.toString(isActive(visit.getStartDatetime(), visit.getStopDatetime())));
				row.put("visitType", visit.getVisitType().getName());
				row.put("visitLocation", (visit.getLocation() != null) ? visit.getLocation().getName() : "");
				row.put("visitFrom", Context.getDateFormat().format(visit.getStartDatetime()));
				
				if (visit.getStopDatetime() != null) {
					row.put("visitTo", Context.getDateFormat().format(visit.getStopDatetime()));
				}
				
				if (visit.getIndication() != null && visit.getIndication().getName() != null) {
					row.put("visitIndication", visit.getIndication().getName().getName());
				}
				
				Object[] visitEncounters = visit.getEncounters().toArray();
				if (visitEncounters.length > 0) {
					if (encounter.equals(visitEncounters[0])) {
						row.put("firstInVisit", Boolean.TRUE.toString());
					}
					if (encounter.equals(visitEncounters[visitEncounters.length - 1])) {
						row.put("lastInVisit", Boolean.TRUE.toString());
					}
				} else {
					row.put("firstInVisit", Boolean.TRUE.toString());
					row.put("lastInVisit", Boolean.TRUE.toString());
				}
			}
			
			if (encounter.getId() != null) { //If it is not mocked encounter
				row.put("encounterId", encounter.getId().toString());
				row.put("encounterDate", Context.getDateFormat().format(encounter.getEncounterDatetime()));
				row.put("encounterType", encounter.getEncounterType().getName());
				row.put("encounterProviders", getProviders(encounter));
				row.put("encounterLocation", (encounter.getLocation() != null) ? encounter.getLocation().getName() : "");
				row.put("encounterEnterer", (encounter.getCreator() != null) ? encounter.getCreator().getPersonName()
				        .toString() : "");
				row.put("formViewURL", getViewFormURL(request, formToViewUrlMap, encounter));
			}
			
			response.addRow(row);
		}
		
		return response;
	}
	
	private String getViewFormURL(HttpServletRequest request, Map<Form, String> formToViewUrlMap, Encounter encounter) {
		String viewFormURL = formToViewUrlMap.get(encounter.getForm());
		if (viewFormURL != null) {
			viewFormURL = request.getContextPath() + "/" + viewFormURL + "?encounterId=" + encounter.getId();
		} else {
			viewFormURL = request.getContextPath() + "/admin/encounters/encounterDisplay.list?encounterId="
			        + encounter.getId();
		}
		return viewFormURL;
	}
	
	private String getProviders(Encounter encounter) {
		StringBuilder providersBuilder = new StringBuilder();
		for (Set<Provider> providers : encounter.getProvidersByRoles().values()) {
			for (Provider provider : providers) {
				if (provider.getPerson() != null) {
					providersBuilder.append(provider.getPerson().getPersonName().getFullName());
				} else {
					providersBuilder.append(provider.getIdentifier());
				}
				providersBuilder.append(", ");
			}
		}
		
		if (providersBuilder.length() > 1) {
			return providersBuilder.substring(0, providersBuilder.length() - 2);
		} else {
			return "";
		}
	}
	
	@ModelAttribute
	public Patient getPatient(@RequestParam(PATIENT) Integer patientId) {
		return Context.getPatientService().getPatient(patientId);
	}
	
	private boolean isActive(Date start, Date end) {
		Date now = new Date();
		if (OpenmrsUtil.compare(now, start) >= 0) {
			if (OpenmrsUtil.compareWithNullAsLatest(now, end) < 0) {
				return true;
			}
		}
		return false;
	}
}
<commit_msg>CODE REVIEW (POST-COMMIT) - issue TRUNK-2710: Create Encounters by Visit portlet  https://tickets.openmrs.org/browse/TRUNK-2710<commit_after>/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.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://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */
package org.openmrs.web.controller.visit;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.util.Logger;
import org.openmrs.Encounter;
import org.openmrs.Form;
import org.openmrs.Patient;
import org.openmrs.Provider;
import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.web.controller.PortletControllerUtil;
import org.openmrs.web.controller.bean.DatatableRequest;
import org.openmrs.web.controller.bean.DatatableResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Lists visits.
 */
@Controller
public class VisitListController {
	
	protected final Logger log = Logger.getLogger(getClass());
	
	public static final String VISITS_PATH = "/admin/visits/datatable";
	
	public static final String PATIENT = "patient";
	
	/**
	 * It handles calls from DataTables.
	 * 
	 * @param patient
	 * @param request
	 * @return {@link DatatableResponse}
	 */
	@RequestMapping(VISITS_PATH)
	public @ResponseBody
	DatatableResponse getVisits(@ModelAttribute Patient patient, HttpServletRequest request) {
		DatatableRequest datatable = DatatableRequest.parseRequest(request);
		
		DatatableResponse response = new DatatableResponse(datatable);
		
		Integer totalVisitsCount = Context.getEncounterService().getEncountersByVisitsAndPatientCount(patient, false, null);
		response.setiTotalRecords(totalVisitsCount);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("person", patient);
		PortletControllerUtil.addFormToEditAndViewUrlMaps(model);
		@SuppressWarnings("unchecked")
		Map<Form, String> formToViewUrlMap = (Map<Form, String>) model.get("formToViewUrlMap");
		
		if (!StringUtils.isBlank(datatable.getsSearch())) {
			Integer filteredVisitsCount = Context.getEncounterService().getEncountersByVisitsAndPatientCount(patient, false,
			    datatable.getsSearch());
			response.setiTotalDisplayRecords(filteredVisitsCount);
		} else {
			response.setiTotalDisplayRecords(totalVisitsCount);
		}
		
		List<Encounter> encounters = Context.getEncounterService().getEncountersByVisitsAndPatient(patient, false,
		    datatable.getsSearch(), datatable.getiDisplayStart(), datatable.getiDisplayLength());
		
		response.setsColumns("visitId", "visitActive", "visitType", "visitLocation", "visitFrom", "visitTo",
		    "visitIndication", "firstInVisit", "lastInVisit", "encounterId", "encounterDate", "encounterType",
		    "encounterProviders", "encounterLocation", "encounterEnterer", "formViewURL");
		
		for (Encounter encounter : encounters) {
			Map<String, String> row = new HashMap<String, String>();
			
			if (encounter.getVisit() != null) {
				Visit visit = encounter.getVisit();
				row.put("visitId", visit.getId().toString());
				row.put("visitActive", Boolean.toString(isActive(visit.getStartDatetime(), visit.getStopDatetime())));
				row.put("visitType", visit.getVisitType().getName());
				row.put("visitLocation", (visit.getLocation() != null) ? visit.getLocation().getName() : "");
				row.put("visitFrom", Context.getDateFormat().format(visit.getStartDatetime()));
				
				if (visit.getStopDatetime() != null) {
					row.put("visitTo", Context.getDateFormat().format(visit.getStopDatetime()));
				}
				
				if (visit.getIndication() != null && visit.getIndication().getName() != null) {
					row.put("visitIndication", visit.getIndication().getName().getName());
				}
				
				Object[] visitEncounters = visit.getEncounters().toArray();
				if (visitEncounters.length > 0) {
					if (encounter.equals(visitEncounters[0])) {
						row.put("firstInVisit", Boolean.TRUE.toString());
					}
					if (encounter.equals(visitEncounters[visitEncounters.length - 1])) {
						row.put("lastInVisit", Boolean.TRUE.toString());
					}
				} else {
					row.put("firstInVisit", Boolean.TRUE.toString());
					row.put("lastInVisit", Boolean.TRUE.toString());
				}
			}
			
			if (encounter.getId() != null) { //If it is not mocked encounter
				row.put("encounterId", encounter.getId().toString());
				row.put("encounterDate", Context.getDateFormat().format(encounter.getEncounterDatetime()));
				row.put("encounterType", encounter.getEncounterType().getName());
				row.put("encounterProviders", getProviders(encounter));
				row.put("encounterLocation", (encounter.getLocation() != null) ? encounter.getLocation().getName() : "");
				row.put("encounterEnterer", (encounter.getCreator() != null) ? encounter.getCreator().getPersonName()
				        .toString() : "");
				row.put("formViewURL", getViewFormURL(request, formToViewUrlMap, encounter));
			}
			
			response.addRow(row);
		}
		
		return response;
	}
	
	private String getViewFormURL(HttpServletRequest request, Map<Form, String> formToViewUrlMap, Encounter encounter) {
		String viewFormURL = formToViewUrlMap.get(encounter.getForm());
		if (viewFormURL != null) {
			viewFormURL = request.getContextPath() + "/" + viewFormURL + "?encounterId=" + encounter.getId();
		} else {
			viewFormURL = request.getContextPath() + "/admin/encounters/encounterDisplay.list?encounterId="
			        + encounter.getId();
		}
		return viewFormURL;
	}
	
	private String getProviders(Encounter encounter) {
		StringBuilder providersBuilder = new StringBuilder();
		for (Set<Provider> providers : encounter.getProvidersByRoles().values()) {
			for (Provider provider : providers) {
				if (provider.getPerson() != null) {
					providersBuilder.append(provider.getPerson().getPersonName().getFullName());
				} else {
					providersBuilder.append(provider.getIdentifier());
				}
				providersBuilder.append(", ");
			}
		}
		
		if (providersBuilder.length() > 1) {
			return providersBuilder.substring(0, providersBuilder.length() - 2);
		} else {
			return "";
		}
	}
	
	@ModelAttribute
	public Patient getPatient(@RequestParam(PATIENT) Integer patientId) {
		return Context.getPatientService().getPatient(patientId);
	}
	
	private boolean isActive(Date start, Date end) {
		Date now = new Date();
		if (OpenmrsUtil.compare(now, start) >= 0) {
			if (OpenmrsUtil.compareWithNullAsLatest(now, end) < 0) {
				return true;
			}
		}
		return false;
	}
}

{'fraction_non_alphanumeric': 0.13006251698831203, 'fraction_numerical': 0.0019026909486273443, 'mean_word_length': 4.180218233016544, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 1, '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': '535895', 'n_tokens_mistral': 4883, 'n_tokens_neox': 4296, 'n_words': 846}