gt: 		if (admission != null) {
context: /**
   *
   * Copyright (c) 2005-2012. Centre for Research on Inner City Health, St. Michael's Hospital, Toronto. All Rights Reserved.
   * This software is published under the GPL GNU General Public License.
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   * This software was written for
   * Centre for Research on Inner City Health, St. Michael's Hospital,
   * Toronto, Ontario, Canada
   */
  
  package org.oscarehr.casemgmt.web;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.MalformedURLException;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Set;
  import java.util.Vector;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import net.sf.json.JSONObject;
  import net.sf.json.JsonConfig;
  import net.sf.json.processors.JsDateJsonBeanProcessor;
  
  import org.apache.commons.lang.StringUtils;
  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
  import org.apache.log4j.Logger;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.util.LabelValueBean;
  import org.caisi.model.CustomFilter;
  import org.oscarehr.PMmodule.caisi_integrator.CaisiIntegratorManager;
  import org.oscarehr.PMmodule.caisi_integrator.IntegratorFallBackManager;
  import org.oscarehr.PMmodule.dao.SecUserRoleDao;
  import org.oscarehr.PMmodule.model.Admission;
  import org.oscarehr.PMmodule.model.ProgramProvider;
  import org.oscarehr.PMmodule.model.ProgramTeam;
  import org.oscarehr.PMmodule.model.SecUserRole;
  import org.oscarehr.billing.CA.ON.dao.BillingClaimDAO;
  import org.oscarehr.billing.CA.ON.model.BillingClaimHeader1;
  import org.oscarehr.caisi_integrator.ws.CachedDemographicIssue;
  import org.oscarehr.caisi_integrator.ws.CachedDemographicNote;
  import org.oscarehr.caisi_integrator.ws.CachedDemographicNoteCompositePk;
  import org.oscarehr.caisi_integrator.ws.CachedFacility;
  import org.oscarehr.caisi_integrator.ws.DemographicWs;
  import org.oscarehr.caisi_integrator.ws.NoteIssue;
  import org.oscarehr.casemgmt.common.Colour;
  import org.oscarehr.casemgmt.common.EChartNoteEntry;
  import org.oscarehr.casemgmt.dao.CaseManagementNoteDAO;
  import org.oscarehr.casemgmt.dao.IssueDAO;
  import org.oscarehr.casemgmt.model.CaseManagementCPP;
  import org.oscarehr.casemgmt.model.CaseManagementIssue;
  import org.oscarehr.casemgmt.model.CaseManagementNote;
  import org.oscarehr.casemgmt.model.CaseManagementNoteExt;
  import org.oscarehr.casemgmt.model.CaseManagementSearchBean;
  import org.oscarehr.casemgmt.model.CaseManagementTmpSave;
  import org.oscarehr.casemgmt.model.ClientImage;
  import org.oscarehr.casemgmt.model.Issue;
  import org.oscarehr.casemgmt.service.CaseManagementManager;
  import org.oscarehr.casemgmt.web.formbeans.CaseManagementViewFormBean;
  import org.oscarehr.common.dao.CaseManagementIssueNotesDao;
  import org.oscarehr.common.dao.DemographicDao;
  import org.oscarehr.common.dao.EFormDataDao;
  import org.oscarehr.common.dao.EncounterFormDao;
  import org.oscarehr.common.dao.GroupNoteDao;
  import org.oscarehr.common.model.Demographic;
  import org.oscarehr.common.model.Drug;
  import org.oscarehr.common.model.Dxresearch;
  import org.oscarehr.common.model.GroupNoteLink;
  import org.oscarehr.common.model.UserProperty;
  import org.oscarehr.eyeform.EyeformInit;
  import org.oscarehr.eyeform.dao.FollowUpDao;
  import org.oscarehr.eyeform.dao.MacroDao;
  import org.oscarehr.eyeform.dao.TestBookRecordDao;
  import org.oscarehr.eyeform.model.EyeformFollowUp;
  import org.oscarehr.eyeform.model.EyeformTestBook;
  import org.oscarehr.eyeform.model.Macro;
  import org.oscarehr.provider.web.CppPreferencesUIBean;
  import org.oscarehr.util.LoggedInInfo;
  import org.oscarehr.util.MiscUtils;
  import org.oscarehr.util.SpringUtils;
  
  import oscar.OscarProperties;
  import oscar.eform.EFormUtil;
  import oscar.oscarEncounter.data.EctFormData;
  import oscar.oscarEncounter.data.EctFormData.PatientForm;
  import oscar.oscarRx.pageUtil.RxSessionBean;
  import oscar.util.OscarRoleObjectPrivilege;
  
  /*
   * Updated by Eugene Petruhin on 21 jan 2009 while fixing missing "New Note" link
   */
  public class CaseManagementViewAction extends BaseCaseManagementViewAction {
  
  	private static final Integer QUICK_CHART = 20;
  	private static final Integer FULL_CHART = -1;
  	private static final Integer MAX_INVOICES = 20;
  	private static Logger logger = MiscUtils.getLogger();
  	private CaseManagementManager caseManagementManager = (CaseManagementManager) SpringUtils.getBean("caseManagementManager");
  	private IssueDAO issueDao = (IssueDAO) SpringUtils.getBean("IssueDAO");
  	private CaseManagementNoteDAO caseManagementNoteDao = (CaseManagementNoteDAO) SpringUtils.getBean("caseManagementNoteDAO");
  	private SecUserRoleDao secUserRoleDao = (SecUserRoleDao) SpringUtils.getBean("secUserRoleDao");
  	private GroupNoteDao groupNoteDao = (GroupNoteDao) SpringUtils.getBean("groupNoteDao");
  	private DemographicDao demographicDao = (DemographicDao) SpringUtils.getBean("demographicDao");
  	private CaseManagementIssueNotesDao cmeIssueNotesDao = (CaseManagementIssueNotesDao)SpringUtils.getBean("caseManagementIssueNotesDao");
  	private BillingClaimDAO billingClaimDao = (BillingClaimDAO)SpringUtils.getBean("billingClaimDAO");
  	private EFormDataDao eFormDataDao = (EFormDataDao) SpringUtils.getBean("EFormDataDao");
  
  	static {
  		//temporary..need something generic;
  		EyeformInit.init();
  	}
  	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		CaseManagementViewFormBean caseForm = (CaseManagementViewFormBean) form;
  		caseForm.setFilter_provider("");
  		request.setAttribute("patientCppPrintPreview", "false");
  
  		// prevent null pointer errors as both these variables are required in navigation.jsp
  		request.getSession().setAttribute("casemgmt_newFormBeans", new ArrayList<Object>());
  		request.getSession().setAttribute("casemgmt_msgBeans", new ArrayList<Object>());
  
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward setViewType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward setPrescriptViewType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward setHideActiveIssues(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward saveAndExit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		return save(mapping, form, request, response);
  	}
  
  	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  
  		HttpSession session = request.getSession();
  
  		if (session.getAttribute("userrole") != null) {
  			CaseManagementViewFormBean caseForm = (CaseManagementViewFormBean) form;
  			CaseManagementCPP cpp = caseForm.getCpp();
  			cpp.setUpdate_date(new Date());
  
  			LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get();
  			caseManagementMgr.saveCPP(cpp, loggedInInfo.loggedInProvider.getProviderNo());
  
  			// caseManagementMgr.saveEctWin(ectWin);
  		} else response.sendError(HttpServletResponse.SC_FORBIDDEN);
  
  		return null;
  	}
  
  	/* save CPP for patient */
  	public ActionForward patientCPPSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		logger.debug("patientCPPSave");
  		CaseManagementViewFormBean caseForm = (CaseManagementViewFormBean) form;
  		CaseManagementCPP cpp = caseForm.getCpp();
  		cpp.setUpdate_date(new Date());
  		cpp.setDemographic_no(caseForm.getDemographicNo());
  
  		LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get();
  		caseManagementMgr.saveCPP(cpp, loggedInInfo.loggedInProvider.getProviderNo());
  		addMessage(request, "cpp.saved");
  
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward patientCppPrintPreview(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		logger.debug("patientCPPSave");
  
  		request.setAttribute("patientCppPrintPreview", "true");
  		return view(mapping, form, request, response);
  	}
  
  	public ActionForward viewNotes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		// response.setCharacterEncoding("UTF-8");
  		long start = System.currentTimeMillis();
  		long beginning = start;
  		long current = 0;
  		CaseManagementViewFormBean caseForm = (CaseManagementViewFormBean) form;
  		boolean useNewCaseMgmt = false;
  		String useNewCaseMgmtString = (String) request.getSession().getAttribute("newCaseManagement");
  		if (useNewCaseMgmtString != null) useNewCaseMgmt = Boolean.parseBoolean(useNewCaseMgmtString);
  
  		logger.debug("Starting VIEW");
  		String tab = request.getParameter("tab");
  		if (tab == null) {
  			tab = CaseManagementViewFormBean.tabs[0];
  		}
  		HttpSession se = request.getSession();
  		if (se.getAttribute("userrole") == null) return mapping.findForward("expired");
  
  		LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get();
  
  		String demoNo = getDemographicNo(request);
  
  		logger.debug("is client in program");
  		// need to check to see if the client is in our program domain
  		// if not...don't show this screen!
  		String roles = (String) se.getAttribute("userrole");
  		if (OscarProperties.getInstance().isOscarLearning() && roles != null && roles.indexOf("moderator") != -1) {
  			logger.info("skipping domain check..provider is a moderator");
  		} else if (!caseManagementMgr.isClientInProgramDomain(loggedInInfo.loggedInProvider.getProviderNo(), demoNo)) {
  			return mapping.findForward("domain-error");
  		}
  		String programId = (String) request.getSession().getAttribute("case_program_id");
  
  		viewCurrentIssuesTab_newCmeNotes(request, caseForm, demoNo, programId);
  
  		return mapping.findForward("ajaxDisplayNotes");
  	}
  
  	/* show case management view */
  	/*
  	 * Session variables : case_program_id casemgmt_DemoNo casemgmt_VlCountry casemgmt_msgBeans readonly
  	 */
  	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  		// response.setCharacterEncoding("UTF-8");
  		long start = System.currentTimeMillis();
  		long beginning = start;
  		long current = 0;
  		CaseManagementViewFormBean caseForm = (CaseManagementViewFormBean) form;
  		boolean useNewCaseMgmt = false;
  		String useNewCaseMgmtString = (String) request.getSession().getAttribute("newCaseManagement");
  		if (useNewCaseMgmtString != null) useNewCaseMgmt = Boolean.parseBoolean(useNewCaseMgmtString);
  
  		logger.debug("Starting VIEW");
  		String tab = request.getParameter("tab");
  		if (tab == null) {
  			tab = CaseManagementViewFormBean.tabs[0];
  		}
  		HttpSession se = request.getSession();
  		if (se.getAttribute("userrole") == null) return mapping.findForward("expired");
  
  		LoggedInInfo loggedInInfo = LoggedInInfo.loggedInInfo.get();
  
  		String demoNo = getDemographicNo(request);
  
  		logger.debug("is client in program");
  		// need to check to see if the client is in our program domain
  		// if not...don't show this screen!
  		String roles = (String) se.getAttribute("userrole");
  		if (OscarProperties.getInstance().isOscarLearning() && roles != null && roles.indexOf("moderator") != -1) {
  			logger.info("skipping domain check..provider is a moderator");
  		} else if (!caseManagementMgr.isClientInProgramDomain(loggedInInfo.loggedInProvider.getProviderNo(), demoNo)) {
  			return mapping.findForward("domain-error");
  		}
  
  		current = System.currentTimeMillis();
  		logger.debug("client in program " + String.valueOf(current - start));
  		start = current;
  
  		request.setAttribute("casemgmt_demoName", getDemoName(demoNo));
  		request.setAttribute("casemgmt_demoAge", getDemoAge(demoNo));
  		request.setAttribute("casemgmt_demoDOB", getDemoDOB(demoNo));
  		request.setAttribute("demographicNo", demoNo);
  
  		logger.debug("client Image?");
  		// get client image
  		ClientImage img = clientImageMgr.getClientImage(Integer.parseInt(demoNo));
  		if (img != null) {
  			request.setAttribute("image_exists", "true");
  		}
  
  		current = System.currentTimeMillis();
  		logger.debug("client image " + String.valueOf(current - start));
  		start = current;
  
  		String programId = (String) request.getSession().getAttribute("case_program_id");
  
  		if (programId == null || programId.length() == 0) {
  			programId = "0";
  		}
  
  		logger.debug("is there a tmp note?");
  		// check to see if there is an unsaved note
  		// if there is see if casemanagemententry has already handled it
  		// if it has, disregard unsaved note; if it has not then set attribute
  		CaseManagementTmpSave tmpsavenote = this.caseManagementMgr.restoreTmpSave(loggedInInfo.loggedInProvider.getProviderNo(), demoNo, programId);
  		if (tmpsavenote != null) {
  			String restoring = (String) se.getAttribute("restoring");
  			if (restoring == null) request.setAttribute("can_restore", new Boolean(true));
  			else se.setAttribute("restoring", null);
  		}
  
  		current = System.currentTimeMillis();
  		logger.debug("tmp note " + String.valueOf(current - start));
  		start = current;
  
  		logger.debug("Get admission");
  		String teamName = "";
  		Admission admission = admissionMgr.getCurrentAdmission(programId, Integer.valueOf(demoNo));
  		current = System.currentTimeMillis();
  		logger.debug("Get admission " + String.valueOf(current - start));
  		start = current;
  
  