/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/admissionassessoverview/Logic.java
//#############################################################################
//#                                                                           #
//#  Copyright (C) <2015>  <IMS MAXIMS>                                       #
//#                                                                           #
//#  This program is free software: you can redistribute it and/or modify     #
//#  it under the terms of the GNU Affero General Public License as           #
//#  published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.                      #
//#                                                                           #
//#  You should have received a copy of the GNU Affero General Public License #
//#  along with this program.  If not, see <http://www.gnu.org/licenses/>.    #
//#                                                                           #
//#  IMS MAXIMS provides absolutely NO GUARANTEE OF THE CLINICAL SAFTEY of    #
//#  this program.  Users of this software do so entirely at their own risk.  #
//#  IMS MAXIMS only ensures the Clinical Safety of unaltered run-time        #
//#  software that it builds, deploys and maintains.                          #
//#                                                                           #
//#############################################################################
//#EOH
// This code was generated by Barbara Worwood using IMS Development Environment (version 1.17 build 40517.1630)
// Copyright (C) 1995-2004 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.admissionassessoverview;

import ims.core.vo.HcpLiteVo;
import ims.core.vo.MemberOfStaffShortVo;
import ims.framework.FormName;
import ims.framework.enumerations.FormMode;
import ims.nursing.vo.lookups.LookupHelper;
import ims.nursing.vo.lookups.AssessmentComponentTypeCollection;
import ims.nursing.vo.lookups.AssessmentComponentType;
import ims.nursing.vo.lookups.AssessmentStatus;
import ims.nursing.vo.lookups.AssessmentType;
import ims.nursing.vo.AssessmentComponentLiteVo;
import ims.nursing.vo.AssessmentComponentLiteVoCollection;
import ims.nursing.vo.AssessmentOverview;
import ims.nursing.vo.AssessmentHeaderInfo;
import ims.nursing.vo.AssessmentInfo;
import ims.nursing.vo.AssessmentInfoCollection;
import ims.framework.enumerations.SortOrder;

public class Logic extends BaseLogic
{
	protected void onFormOpen()
	{
		open();
	}
	private void open() 
	{
		clearContext();
		form.gAssessments().getRows().clear();
		
		form.getGlobalContext().Core.setAssessmentIsReadOnly(form.isReadOnly());
		
		form.setMode(FormMode.VIEW);
		// Clear the global context of assessment as if go to another
		// clinical contact, and then select this again, when we go into
		// the component screens, the assessment is still there!
		form.dtInitiated().setEnabled(false);
		form.txtHcpInitiated().setEnabled(false);
		form.txtTimeInitiated().setEnabled(false);
		form.getGlobalContext().Nursing.setAssessmentHeaderInfo(null);
		form.getLocalContext().setassmnt(null);
		
		AssessmentOverview assmnt = null;
		assmnt = domain.getAssessmentDetailsByCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		
		form.getLocalContext().setassmnt(assmnt);
		

		AssessmentComponentLiteVo activity = null;
		AssessmentComponentLiteVoCollection coll;
		AssessmentInfoCollection infoColl;
		AssessmentInfo info;
		AssessmentComponentTypeCollection cCol = LookupHelper.getAssessmentComponentType(domain.getLookupService());
		GenForm.gAssessmentsRow row;
		for (int i = 0; i < cCol.size(); i++)
		{
			AssessmentComponentType comp = cCol.get(i);
						
			FormName componentEditor = getFormToOpen(comp);
			if(componentEditor == null || !engine.isFormRegistered(componentEditor))
								continue;
			
			row = form.gAssessments().getRows().newRow();
			
			row.setValue(comp);
			row.setColAssessment(comp.getText());
			row.setComponent(null);
			//if (form.isReadOnly())
			//	row.setSelectable(false);

			// while here, check through for assessment components
			// to set the other attributes if required
			if (assmnt != null)
			{
				coll = assmnt.getComponents();
				if (coll == null)
					continue;
				// Loop through components until the same one as this lookup is
				// found
				for (int j = 0; j < coll.size(); j++)
				{
					activity = coll.get(j);					
					if (activity.getComponentType() != null && activity.getComponentType().getID() == comp.getID())
					{	
						row.setComponent(activity);
						if (activity.getIsComplete() != null)
						{
							if (activity.getIsComplete().booleanValue() == true)
								row.setColStatus("Complete");
							else if (activity.getIsComplete().booleanValue() == false)
								row.setColStatus("Incomplete");

							// if the logged-in role is readonly then only the
							// completed assessments can be viewed
							//if (form.isReadOnly() && activity.getIsComplete().booleanValue() == false)
							//	row.setSelectable(false);
							//else
							//	row.setSelectable(true);

						}
						if (activity.getCopy() != null)
						{
							if (activity.getCopy().booleanValue())
								row.setColCopy("True");
						}
						infoColl = activity.getAssessmentInfo();
						infoColl.sort(SortOrder.DESCENDING);// RF 5/10/04 Sort
															// Reverse
															// Chronologically.

						if (infoColl != null && infoColl.size() > 0)
						{
							info = infoColl.get(0); // Get the latest assessment
							row.setColDate(info.getDateTime().toString());

							if (info.getHCP() != null)
								row.setColBy(info.getHCP().getName().toString());
						}
					}
				}

			}
		}

		form.dateCompleted().setVisible(false);
		form.timeCompleted().setVisible(false);
		form.textBoxCompletedBy().setVisible(false);
		form.lblBy().setVisible(false);
		form.lblDateTime().setVisible(false);
		if (assmnt != null)
		{
			form.textBoxAss().setValue(assmnt.getAssessmentType().getText());
			form.textBoxStatus().setValue(assmnt.getAssessmentStatus().getText());

			if (assmnt.getDateTimeInitiated() != null)
				form.dtInitiated().setValue(assmnt.getDateTimeInitiated().getDate());
			if (assmnt.getDateTimeComplete() != null)
			{
				form.dateCompleted().setValue(assmnt.getDateTimeComplete().getDate());
				form.timeCompleted().setValue(assmnt.getDateTimeComplete().getTime());
				if (assmnt.getHCPComplete() != null)
					form.textBoxCompletedBy().setValue(assmnt.getHCPComplete().getName().toString());
				form.dateCompleted().setVisible(true);
				form.timeCompleted().setVisible(true);
				form.textBoxCompletedBy().setVisible(true);
				form.dateCompleted().setEnabled(false);
				form.timeCompleted().setEnabled(false);
				form.textBoxCompletedBy().setEnabled(false);
				form.lblBy().setVisible(true);
				form.lblDateTime().setVisible(true);
			}
			form.txtTimeInitiated().setValue(assmnt.getDateTimeInitiated().getTime());
			if (assmnt.getHcpInitiated() != null)
				form.txtHcpInitiated().setValue(assmnt.getHcpInitiated().getName().toString());
		}
		else
		{
			form.textBoxAss().setValue("");
			form.textBoxStatus().setValue("");
			form.dtInitiated().setValue(new ims.framework.utils.Date());
			form.txtTimeInitiated().setValue(new ims.framework.utils.Time());
			if (domain.getMosUser() != null)
				form.txtHcpInitiated().setValue(((MemberOfStaffShortVo) domain.getMosUser()).toString());
		}
	}
	protected void onGridSelectionChanged()
	{
		clearContext();
		AssessmentComponentType component = form.gAssessments().getValue();
		if (component == null)
		{
			engine.showMessage("Internal error: Invalid Component Selected");
			return;
		}
		//	AssessmentHeaderInfo assmntHead = new AssessmentHeaderInfo();
		//	AssessmentOverview assmnt = form.getLocalContext().getassmnt();
		AssessmentHeaderInfo assmntHead = null;
		
		assmntHead = domain.getAssessmentHeaderInfoByCareContext(form.getGlobalContext().Core.getCurrentCareContext());

		if (assmntHead == null)
		{
			// Create a new instance and set the initial values
			assmntHead = new AssessmentHeaderInfo();
			assmntHead.setAssessmentType(AssessmentType.INITIAL);
			assmntHead.setAssessmentStatus(AssessmentStatus.ACTIVE);
			assmntHead.setDateTimeInitiated(new ims.framework.utils.DateTime());
			
			Object hcpLite = domain.getHcpLiteUser();
			assmntHead.setHcpInitiated(hcpLite != null ? ((HcpLiteVo)hcpLite) : null);
			
		}
		/*else
		{
			assmntHead.setID_Assessment(assmnt.getID_Assessment());
			assmntHead.setAssessmentStatus(assmnt.getAssessmentStatus());
			assmntHead.setAssessmentType(assmnt.getAssessmentType());
			assmntHead.setDateTimeInitiated(assmnt.getDateTimeInitiated());
			assmntHead.setDateTimeComplete(assmnt.getDateTimeComplete());
			assmntHead.setHCPComplete(assmnt.getHCPComplete());
			assmntHead.setHcpInitiated(assmnt.getHcpInitiated());
		}*/

		GenForm.gAssessmentsRow row = form.gAssessments().getRowByValue(component);
		AssessmentComponentLiteVo comp = row.getComponent();
		if (comp != null)
		{
			
			assmntHead.setCurrentComponent(domain.getComponent(comp));
		}
			
		if (assmntHead.getClinicalContact() == null)
			assmntHead.setClinicalContact(form.getGlobalContext().Core.getCurrentClinicalContact());
		
		if (assmntHead.getCareContext() == null)
			assmntHead.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());

		form.getGlobalContext().Nursing.setAssessmentHeaderInfo(assmntHead);
		
		FormName formToOpen = getFormToOpen(component);
		if(formToOpen == null)
		{
			engine.showMessage("Component type not supported");
		}
		else if(!engine.isFormRegistered(formToOpen))
		{
			engine.showMessage("Component editor is not available");
		}
		else
		{
			engine.open(formToOpen);
		}
	}
	private FormName getFormToOpen(AssessmentComponentType component) 
	{
		if (component.equals(AssessmentComponentType.ACCOMMODATION))
			return form.getForms().COE.AssessAccommodation;
		else if (component.equals(AssessmentComponentType.ACTIVITY_LEVEL))
			return form.getForms().COE.AssessActivityLevel;
		else if (component.equals(AssessmentComponentType.BREATHING))
			return form.getForms().COE.AssessBreathing;
		else if (component.equals(AssessmentComponentType.SLEEPING))
			return form.getForms().COE.AssessSleeping;
		else if (component.equals(AssessmentComponentType.COMMUNICATION))
			return form.getForms().COE.AssessCommunication;
		else if (component.equals(AssessmentComponentType.MOBILITY))
			return form.getForms().COE.AssessMobility;
		else if (component.equals(AssessmentComponentType.PERSONAL_HYGIENE))
			return form.getForms().COE.AssessPersonalHygiene;
		else if (component.equals(AssessmentComponentType.DRESSING))
			return form.getForms().COE.AssessDressingGrooming;
		else if (component.equals(AssessmentComponentType.SKIN))
			return form.getForms().COE.AssessSkin;
		else if (component.equals(AssessmentComponentType.EATING))
			return form.getForms().COE.AssessEating;
		else if (component.equals(AssessmentComponentType.BOWELS))
			return form.getForms().COE.AssessBowels;
		else if (component.equals(AssessmentComponentType.BLADDER))
			return form.getForms().COE.AssessBladder;
		else if (component.equals(AssessmentComponentType.PAIN))
			return form.getForms().COE.AssessPain;
		else if (component.equals(AssessmentComponentType.LEISURE))
			return form.getForms().COE.AssessLeisure;
		else if (component.equals(AssessmentComponentType.EMOTIONAL_STATE))
			return form.getForms().COE.AssessEmotionalState;
		else if (component.equals(AssessmentComponentType.SPIRITUALITY))
			return form.getForms().COE.AssessSpirituality;
		else if (component.equals(AssessmentComponentType.INFECTION_CONTROL))
			return form.getForms().COE.AssessInfectionControl;
		else if (component.equals(AssessmentComponentType.SPINAL_ACCOMODATION))
			return form.getForms().SpinalInjuries.NurAssessmentAccommodationProperty;
		else if (component.equals(AssessmentComponentType.SPINAL_BREATHING))
			return form.getForms().SpinalInjuries.NurAssessmentBreathing;
		else if (component.equals(AssessmentComponentType.SPINAL_COMMUNICATION))
			return form.getForms().SpinalInjuries.NurAssessmentCommunication;
		else if (component.equals(AssessmentComponentType.SPINAL_MOBILITY))
			return form.getForms().SpinalInjuries.NurAssessmentMobility;
		else if (component.equals(AssessmentComponentType.SPINAL_PERSONAL_DETAILS))
			return form.getForms().SpinalInjuries.NurAssessmentPersonalDetails;
		else if (component.equals(AssessmentComponentType.SPINAL_SAFE_ENVIRONMENT))
			return form.getForms().SpinalInjuries.NurAssessmentSafeEnvironement;
		else if (component.equals(AssessmentComponentType.SPINAL_BOWELS))
			return form.getForms().SpinalInjuries.NurAssessmentBowels;
		else if (component.equals(AssessmentComponentType.SPINAL_SKIN))
			return form.getForms().SpinalInjuries.NurAssessmentSkin;
		else if (component.equals(AssessmentComponentType.SPINAL_BLADDER))
			return form.getForms().SpinalInjuries.NurAssessmentBladder;
		else if (component.equals(AssessmentComponentType.SPINAL_CLEANSING))
			return form.getForms().SpinalInjuries.NurAssessmentCleansing;
		else if (component.equals(AssessmentComponentType.SPINAL_EATING))
			return form.getForms().SpinalInjuries.NurAssessmentEating;
		
		return null;
	}

	private void clearContext()
	{
		// Blank the if Global Context Assessment Component vo
		form.getGlobalContext().Nursing.setAssessmentComponent(null);
	}
}
