/FileExtraction/Java_unseen/IMS-MAXIMS_openMAXIMS/Source Library/openmaxims_workspace/Nursing/src/ims/nursing/forms/secs/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 Peter Martin using IMS Development Environment (version 1.52 build 2508.29436)
// Copyright (C) 1995-2006 IMS MAXIMS plc. All rights reserved.

package ims.nursing.forms.secs;

import ims.clinical.vo.PatientSECSVo;
import ims.configuration.gen.ConfigFlag;
import ims.core.vo.MemberOfStaffLiteVo;
import ims.core.vo.RecordingUserInformationVo;
import ims.domain.exceptions.StaleObjectException;
import ims.framework.enumerations.DialogResult;
import ims.framework.exceptions.PresentationLogicException;
import ims.framework.utils.DateTime;

public class Logic extends BaseLogic
{
	private static final long serialVersionUID = 1L;
	private static final int MAX_LENGHT = 105;

	protected void onFormOpen(Object[] args) throws PresentationLogicException 
	{
		form.customControlAuthoringInfo().initializeComponent();
		form.customControlAuthoringInfo().setIsRequiredPropertyToControls(true);//WDEV-13559
		Object mos = domain.getMosUser();
		if(mos != null)
		{
			form.cmbRecordingHCP().newRow((MemberOfStaffLiteVo)mos, mos.toString());
			form.cmbRecordingHCP().setValue((MemberOfStaffLiteVo)mos);
		}
		form.dtimRecordingDateTime().setValue(new DateTime());
		form.lblOriginOfScore().setValue(formatScoreDetails(form.getGlobalContext().Core.getVitalSignsEscalationText()));
		
		//WDEV-20311
		form.getLocalContext().setSECSVo(domain.getSECSConfig());
		form.lblScore().setValue(form.getLocalContext().getSECSVo()!=null && form.getLocalContext().getSECSVo().getOBSProtocolTypeIsNotNull() ? form.getLocalContext().getSECSVo().getOBSProtocolType().getText() + " Score:": "OBS Score:");
		form.txtScore().setValue(form.getGlobalContext().Core.getVitalSignsEscalationScore());
	}
	
	protected void onBtnCancelClick() throws PresentationLogicException {
		engine.close(DialogResult.CANCEL);
	}

	protected void onBtnSaveClick() throws PresentationLogicException {
		
		PatientSECSVo patientSecs = new PatientSECSVo();
		
		patientSecs.setAuthoringInformation(form.customControlAuthoringInfo().getValue());
		if(patientSecs.getRecordingInformation()==null){
			RecordingUserInformationVo voRecordingUserInformation = new RecordingUserInformationVo();
			voRecordingUserInformation.setRecordingUser(form.cmbRecordingHCP().getValue());
			voRecordingUserInformation.setRecordingDateTime(new DateTime());
			patientSecs.setRecordingInformation(voRecordingUserInformation);
		}
		patientSecs.setCareContext(form.getGlobalContext().Core.getCurrentCareContext());
		patientSecs.setVitalSign(form.getGlobalContext().Core.getVitalSign());
		if(form.GroupEscalated().getValue().equals(GenForm.GroupEscalatedEnumeration.rdoEscYes))
			patientSecs.setEscalated(Boolean.TRUE);
		else if(form.GroupEscalated().getValue().equals(GenForm.GroupEscalatedEnumeration.rdoEscNo))
			patientSecs.setEscalated(Boolean.FALSE);
		if(form.txtScore().getValue()!=null)
			patientSecs.setSECSScore(Integer.valueOf(form.txtScore().getValue()));
		patientSecs.setScoreDetails(form.getGlobalContext().Core.getVitalSignsEscalationText());
		patientSecs.setActionTaken(form.txtActionTaken().getValue());
		
		//WDEV-20311
		patientSecs.setOBSProtocolType(form.getLocalContext().getSECSVo()!=null ? form.getLocalContext().getSECSVo().getOBSProtocolType() : null);
		
		String[] arrErrors = patientSecs.validate();
		if(arrErrors == null)
		{
			try {
				domain.savePatientSECS(patientSecs);
			} catch (StaleObjectException e) {
				engine.showMessage(ConfigFlag.UI.STALE_OBJECT_MESSAGE.getValue());
				return;
			}
		}
		else
		{
			engine.showErrors("Validation Errors", arrErrors);
			return;	
		}
		
		engine.close(DialogResult.OK);
		
	}

	private String formatScoreDetails(String original)
	{
		if(original == null || original.length() == 0)
			return null;
		
		String value = new String(original);
		StringBuffer a = new StringBuffer();
		
		int lastEnter = value.lastIndexOf("\n");
		
		if(lastEnter == -1)
			return original;
		
		a.append(value.substring(0, lastEnter));
		
		String urine = value.substring(lastEnter, value.length());
		
		if(urine != null && urine.length() > 0)
		{
			a.append(wrap(urine));
		}
		
		return a.toString();
	}
	
	private String wrap(String s) 
	{
		if(s == null)
			return "";
		
		StringBuilder sb = new StringBuilder(s);

		int i = 0;
		while (i + MAX_LENGHT < sb.length() && (i = sb.lastIndexOf(" ", i + MAX_LENGHT)) != -1) 
		{
		    sb.replace(i, i + 1, "\n");
		}
		
		return sb.toString();
	}
}
