package com.imt.intimamedia.commands.acquire { import com.adobe.cairngorm.control.CairngormEvent; import com.imt.flex.cairngormExtended.command.SuperCommand; import com.imt.intimamedia.business.AcquisitionDelegate; import com.imt.intimamedia.events.acquire.GetImagesEvent; import com.imt.intimamedia.helpers.FlashHelper; import com.imt.intimamedia.model.ApplicationModelLocator; import com.imt.intimamedia.views.common.ImtToaster; import com.imt.intimamedia.views.physician.acquire.AcquireView; import com.imt.intimamedia.vo.DragAndDropVo; import flash.display.Bitmap; import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.Event; import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.events.SecurityErrorEvent; import flash.events.StatusEvent; import flash.events.TimerEvent; import flash.net.LocalConnection; import flash.net.URLRequest; import flash.utils.Timer; import mx.collections.ArrayCollection; import mx.controls.Alert; import mx.controls.List; import mx.core.Application; import mx.resources.ResourceManager; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.utils.ObjectUtil; /** * Commande de récupération des images pour un RDV */ public class GetImagesCommand extends SuperCommand { [Bindable] private var _model : ApplicationModelLocator = ApplicationModelLocator.getInstance(); private var _resultArray : Array; private var _dispatcher : *; private var _reportTimer : Timer = new Timer( 100, 1 ); private var _swfTimer : Timer = new Timer(200, 1); private var _swfResetTimer : Timer = new Timer(200, 1); private var _segmentResetTimer : Timer = new Timer (500, 1); private var _leftImgType : ArrayCollection = new ArrayCollection(); private var _rightImgType : ArrayCollection = new ArrayCollection(); public var list : Array; public var currentDragAndDropVo : DragAndDropVo; /** * Excecution de la commande * * @param event Evénement de type CairngormEvent * @return void */ override public function execute( event : CairngormEvent ) : void { super.execute( event ); var delegate : AcquisitionDelegate = new AcquisitionDelegate( this ); var getImagesEvent : GetImagesEvent = GetImagesEvent( event ); delegate.getImages( getImagesEvent.appointmentId ); } /** * Résultat de la commande * * @param result Un objet * @return void */ override public function result( result : Object ) : void { _dispatcher = this.getDispatcher(this); var resultEvent : ResultEvent = ResultEvent( result ); list = resultEvent.result as Array; if (list.length > 0) { _dispatcher.loadingPopUp.message = ResourceManager.getInstance().getString('labels', 'acquire.popUp.get.images'); if (_dispatcher.name == 'report') { if (_model.viewPatient) _model.measuresList.removeAll(); _model.snapshotReportRightSegment.removeAll(); _model.snapshotReportLeftSegment.removeAll(); switch( ( list[ 0 ] as DragAndDropVo ).collection ) { case "CR": case "CL": FlashHelper.resetCarotids(); break; case "AR": case "AL": FlashHelper.resetArms(); break; case "LR": case "LL": FlashHelper.resetLegs(); break; case "BO": FlashHelper.resetBody(); break; } _dispatcher.loadingPopUp.progress.setProgress( 15, 100 ); _segmentResetTimer.start(); _segmentResetTimer.addEventListener(TimerEvent.TIMER_COMPLETE, startDownloadImages); } else _dispatcher.loadingPopUp.progress.setProgress( 60, 100 ); // empty data if measure View if (_dispatcher.name == 'generalMeasure' && _model.viewPatient) _model.measuresList.removeAll(); if (_dispatcher.name != 'report') downloadImages( list[ 0 ] as DragAndDropVo ); } else _dispatcher.loadingPopUp.closeHandler(); super.result( result ); } private function startDownloadImages(event : TimerEvent) : void { _segmentResetTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, startDownloadImages); _segmentResetTimer.stop(); _dispatcher.loadingPopUp.progress.setProgress( 25, 100 ); downloadImages( list[ 0 ] as DragAndDropVo ); } private function downloadImages( dragAndDropVo : DragAndDropVo ) : void { // comment by J. Deppe : no more copy //currentDragAndDropVo = ObjectUtil.copy( dragAndDropVo ) as DragAndDropVo; currentDragAndDropVo = dragAndDropVo as DragAndDropVo; if (_dispatcher.name == 'report') { switch (list.length) { case 6: _dispatcher.loadingPopUp.progress.setProgress( 35, 100 ); break; case 5: _dispatcher.loadingPopUp.progress.setProgress( 45, 100 ); break; case 4: _dispatcher.loadingPopUp.progress.setProgress( 55, 100 ); break; case 3: _dispatcher.loadingPopUp.progress.setProgress( 65, 100 ); break; case 2: _dispatcher.loadingPopUp.progress.setProgress( 75, 100 ); break; case 1: _dispatcher.loadingPopUp.progress.setProgress( 85, 100 ); break; } } var loaderSmall : Loader = new Loader(); var requestSmall : URLRequest = new URLRequest( currentDragAndDropVo.base64Small ); loaderSmall.load( requestSmall ); loaderSmall.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandlerSmall); } private function completeHandlerSmall( event : Event ) : void { var loaderInfo : LoaderInfo = LoaderInfo(event.target); currentDragAndDropVo.imageSmall = loaderInfo.content as Bitmap; var request : URLRequest = new URLRequest( currentDragAndDropVo.base64High ); var loader : Loader = new Loader(); loader.load( request ); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler); loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); } private function completeHandler( event : Event ) : void { var loaderInfo : LoaderInfo = LoaderInfo(event.target); _dispatcher.loadingPopUp.message = ResourceManager.getInstance().getString('labels', 'acquire.popUp.get.image.done'); startImageConversion( loaderInfo.content as Bitmap ); } private function startImageConversion( data : Bitmap ) : void { currentDragAndDropVo.imageHigh = data; switch( currentDragAndDropVo.collection ) { case "CR": _dispatcher.collectionImagesCarotidRight.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForRightCarotid( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForRightCarotid( currentDragAndDropVo.type ); break; case "CL": _dispatcher.collectionImagesCarotidLeft.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForLeftCarotid( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForLeftCarotid( currentDragAndDropVo.type ); break; case "AR": _dispatcher.collectionImagesArmRight.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForRightArm( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForRightArm( currentDragAndDropVo.type ); break; case "AL": _dispatcher.collectionImagesArmLeft.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForLeftArm( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForLeftArm( currentDragAndDropVo.type ); break; case "LR": _dispatcher.collectionImagesLegRight.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForRightLeg( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForRightLeg( currentDragAndDropVo.type ); break; case "LL": _dispatcher.collectionImagesLegLeft.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForLeftLeg( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForLeftLeg( currentDragAndDropVo.type ); break; case "BO": _dispatcher.collectionImagesBody.addItem( currentDragAndDropVo ); if (_dispatcher.name == 'report') { FlashHelper.restoreSelectionForBody( currentDragAndDropVo.type ); startSwfSelectionSnapshotTimer(); } else FlashHelper.restoreSelectionForBody( currentDragAndDropVo.type ); break; } if (_dispatcher.name != 'report') // acquire or generalMeasure dispatcher endImageConversion(); } private function startSwfSelectionSnapshotTimer() : void { _swfTimer.start(); _swfTimer.addEventListener(TimerEvent.TIMER_COMPLETE, swfSelectionSnapshot); } private function swfSelectionSnapshot(event : TimerEvent) : void { _swfTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, swfSelectionSnapshot); _swfTimer.stop(); switch( currentDragAndDropVo.collection ) { case "CR": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomCarotidRight, "CR"); _model.snapshotReportRightSegment.addItem(_model.carotidRight); _rightImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForRightCarotid(currentDragAndDropVo.type); break; case "CL": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomCarotidLeft, "CL"); _model.snapshotReportLeftSegment.addItem(_model.carotidLeft); _leftImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForLeftCarotid(currentDragAndDropVo.type); break; case "AR": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomArmRight, "AR"); _model.snapshotReportRightSegment.addItem(_model.armRight); _rightImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForRightArm(currentDragAndDropVo.type); break; case "AL": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomArmLeft, "AL"); _model.snapshotReportLeftSegment.addItem(_model.armLeft); _leftImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForLeftArm(currentDragAndDropVo.type); break; case "LR": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomLegRight, "LR"); _model.snapshotReportRightSegment.addItem(_model.legRight); _rightImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForRightLeg(currentDragAndDropVo.type); break; case "LL": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomLegLeft, "LL"); _model.snapshotReportLeftSegment.addItem(_model.legLeft); _leftImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForLeftLeg(currentDragAndDropVo.type); break; case "BO": FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomBody, "BO"); _model.snapshotReportRightSegment.addItem(_model.body); _rightImgType.addItem(currentDragAndDropVo.type); FlashHelper.removeSelectionForBody(currentDragAndDropVo.type); break; } _swfResetTimer.start(); _swfResetTimer.addEventListener(TimerEvent.TIMER_COMPLETE, waitForSelectionRemoving); } private function waitForSelectionRemoving(event : TimerEvent) : void { _swfResetTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, waitForSelectionRemoving); _swfResetTimer.stop(); endImageConversion(); } private function endImageConversion() : void { if (_model.viewPatient) { _model.measuresList.addItem( currentDragAndDropVo ); } if( list.length == 1 ) { if (_dispatcher.name == "report") { var type : String; _dispatcher.loadingPopUp.progress.setProgress( 95, 100 ); switch( currentDragAndDropVo.collection ) { case "CR": case "CL": for each( type in _rightImgType ) FlashHelper.restoreSelectionForRightCarotid( type ); for each( type in _leftImgType ) FlashHelper.restoreSelectionForLeftCarotid( type ); break; case "AR": case "AL": for each( type in _rightImgType ) FlashHelper.restoreSelectionForRightArm( type ); for each( type in _leftImgType ) FlashHelper.restoreSelectionForLeftArm( type ); break; case "LR": case "LL": for each( type in _rightImgType ) FlashHelper.restoreSelectionForRightLeg( type ); for each( type in _leftImgType ) FlashHelper.restoreSelectionForLeftLeg( type ); break; case "BO": for each( type in _rightImgType ) FlashHelper.restoreSelectionForBody( type ); break; } startReportTimer(); } else { // images processing ended, start markers processing _dispatcher.setImagesLoaded( (list[ 0 ] as DragAndDropVo).collection ); _dispatcher.getMarkers(); } } else { // images processing continue list.shift(); downloadImages( list[ 0 ] as DragAndDropVo ); } } private function startReportTimer() : void { _reportTimer = new Timer( 300, 1 ); _reportTimer.start(); _reportTimer.addEventListener(TimerEvent.TIMER_COMPLETE, stopReportTimer); } private function stopReportTimer(event : TimerEvent) : void { _reportTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, stopReportTimer); _reportTimer.stop(); // J. Deppe : uncomment later //if (_model.viewPatient) //{ FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomCarotidRight, "CR"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomCarotidLeft, "CL"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomArmRight, "AR"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomArmLeft, "AL"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomLegRight, "LR"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomLegLeft, "LL"); FlashHelper.buildSegmentSelected(Application.application.menuPhysician.acquire.panzoomBody, "BO"); //} _dispatcher.setImagesLoaded( (list[ 0 ] as DragAndDropVo).collection ); _dispatcher.getMarkers(); } private function openHandler( event : Event ) : void { _dispatcher.loadingPopUp.message = ResourceManager.getInstance().getString('labels', 'acquire.open.image'); } private function progressHandler( event : ProgressEvent ) : void { _dispatcher.loadingPopUp.message = ResourceManager.getInstance().getString('labels', 'acquire.popUp.get.images.progress') + " : " + event.bytesLoaded / 1000 + ResourceManager.getInstance().getString('labels', 'acquire.popUp.images.unit') + " / " + event.bytesTotal / 1000 + " " + ResourceManager.getInstance().getString('labels', 'acquire.popUp.images.unit'); } private function httpStatusHandler( event : HTTPStatusEvent ) : void { _dispatcher.loadingPopUp.message = ResourceManager.getInstance().getString('labels', 'acquire.popUp.get.images.complete'); } private function ioErrorHandler( event : IOErrorEvent ) : void { } private function securityErrorHandler( event : SecurityErrorEvent ) : void { } override public function fault( fault : Object ) : void { super.fault( fault ); if (( fault as FaultEvent ).fault.faultString == ApplicationModelLocator.getInstance().ERROR_SESSION ) { ApplicationModelLocator.displaySessionExpired( fault as FaultEvent ); } else { var toastMessage : ImtToaster = new ImtToaster(); toastMessage.titleMessage = ResourceManager.getInstance().getString('labels', 'error.title'); toastMessage.message = ResourceManager.getInstance().getString('labels', 'error.type') + " (" + ResourceManager.getInstance().getString('errors', ( fault as FaultEvent ).fault.faultString) +")"; toastMessage.timeToLive = 7; toastMessage.level = ImtToaster.ERROR; _model.toaster.toast( toastMessage ); } } } }