1: package com.jonniespratley.snippr.services
2: {
3: import com.jonniespratley.snippr.model.ModelLocator;
4: import com.jonniespratley.snippr.vo.SnippetVO;
5:
6: import flash.net.NetConnection;
7: import flash.net.Responder;
8: import flash.utils.ByteArray;
9:
10: import mx.collections.ArrayCollection;
11: import mx.controls.Alert;
12: import mx.controls.Image;
13: import mx.controls.Text;
14: import mx.core.Window;
15: import mx.rpc.events.ResultEvent;
16:
17: /**
18: * This file is for use without! using the services-config.xml file
19: * @author Jonnie
20: *
21: */
22: public class SnipprService
23: {
24: private static var _service:NetConnection;
25: private var model:ModelLocator = ModelLocator.getInstance();
26:
27: //Here we are creating a new connection to our amfphp service, when this is instantiated, it connects to our service
28: public function SnipprService()
29: {
30: _service = new NetConnection();
31: _service.connect( "http://localhost/snippr/amfphp/gateway.php" );
32: }
33:
34:
35: /* ***************************************
36: *** Service Calls
37: *****************************************/
38:
39: //Here we are calling the getSnippets on our server (amfphp) and setting the result and fault handlers
40: public function getSnippets():void
41: {
42: _service.call( "snippr.SnipprService.getSnippets", new Responder( snippetResultHandler, snipprFaultHandler ) );
43: }
44:
45: //We take one argument here, and that is a snippet, because our server (amfphp) is expecting a snippetVO
46: public function saveSnippet( snippet:SnippetVO ):void
47: {
48: _service.call( "snippr.SnipprService.saveSnippet", new Responder( snippetSavedHandler, snipprFaultHandler ), snippet );
49: }
50:
51: //We take one argument here, and that is the id of the snippet we are wanting to remove
52: public function removeSnippet( snippet_id:uint ):void
53: {
54: _service.call( "snippr.SnipprService.removeSnippet", new Responder( snippetRemoveHandler, snipprFaultHandler ), snippet_id );
55: }
56:
57: //We take two arguments here, one is a byte array and the other is the filename of the file
58: public function takeSnapshot( bytes:ByteArray, filename:String ):void
59: {
60: _service.call( "snippr.SnipprService.takeSnapshot", new Responder( snapshotResultHandler, snipprFaultHandler ), bytes, filename );
61: }
62:
63: /* ***************************************
64: *** Result and Fault Handlers
65: *****************************************/
66:
67: //Here we are handling the result coming back as an array of snippets, then we add our snippets to our model
68: private function snippetResultHandler( data:Array ):void
69: {
70: model.snippetCollection = initVO( data );
71: }
72:
73: private function snapshotResultHandler( data:Object ):void
74: {
75: var result:ResultEvent = data as ResultEvent;
76:
77: /* This is just for some fun */
78: var w:Window = new Window();
79: w.width = 600;
80: w.height = 500;
81: w.layout = "vertical";
82:
83: var i:Image = new Image();
84: i.source = data.result;
85:
86: var t:Text = new Text();
87: t.text = "Nice shot, and nice app, Here is the link for the screenshot.n " + data.result;
88:
89: w.addChild( i );
90: w.addChild( t );
91: w.open();
92:
93: trace( data.result );
94: }
95:
96: //Helper for the result
97: private function initVO( resultArray:Array ):ArrayCollection
98: {
99: var tempArray:ArrayCollection = new ArrayCollection();
100:
101: for ( var s:String in resultArray )
102: {
103: tempArray.addItem( new SnippetVO( resultArray[s] ) );
104:
105: }
106: return tempArray;
107: }
108:
109: //Here we are handling the result and adding it to the value of serviceResponse in our model
110: private function snippetSavedHandler( data:Object ):void
111: {
112: ModelLocator.getInstance().serviceResponse = data.toString();
113: }
114:
115: /*
116: Here we are handling the result that is being returned, which will be the id of the removed snippet, and
117: removing it from our model, at the snippet index
118: */
119: private function snippetRemoveHandler( data:Object ):void
120: {
121: getSnippets();
122: }
123:
124: //Here we are alerting the user that there was an error connection to our server
125: private function snipprFaultHandler( fault:Object ):void
126: {
127:
128: Alert.show( "There was an error connecting to the server.", "Snippr Service Error" );
129: }
130: }
131: }