/FTTSH_G/src/com/fttsh/apps/mobile/g/service/SqliteService.as
ActionScript | 197 lines | 107 code | 14 blank | 76 comment | 0 complexity | 1df160b1888434a7f8629de69b04b0bc MD5 | raw file
1package com.fttsh.apps.mobile.g.service 2{ 3 import com.fttsh.apps.mobile.g.Constants; 4 import com.fttsh.apps.mobile.g.events.SqliteLoadEvent; 5 import com.fttsh.apps.mobile.g.model.SqliteModel; 6 import com.fttsh.apps.mobile.g.model.vo.NeighborhoodInfo; 7 import com.fttsh.apps.mobile.g.model.vo.POICategory; 8 import com.fttsh.apps.mobile.g.model.vo.POIImages; 9 import com.fttsh.apps.mobile.g.model.vo.POIInfo; 10 import com.fttsh.apps.mobile.g.model.vo.POINeighborhood; 11 import com.fttsh.apps.mobile.g.model.vo.UserInfo; 12 import com.fttsh.apps.mobile.g.utils.ArrayCollectionUtils; 13 import com.fttsh.apps.mobile.g.utils.LogUtil; 14 15 import flash.data.SQLConnection; 16 import flash.data.SQLResult; 17 import flash.data.SQLSchemaResult; 18 import flash.data.SQLStatement; 19 import flash.errors.SQLError; 20 import flash.events.SQLErrorEvent; 21 import flash.events.SQLEvent; 22 import flash.filesystem.File; 23 24 import mx.collections.ArrayCollection; 25 import mx.logging.ILogger; 26 import mx.rpc.Responder; 27 28 import nz.co.codec.flexorm.EntityManager; 29 import nz.co.codec.flexorm.EntityManagerAsync; 30 31 import org.robotlegs.mvcs.Actor; 32 33 //-------------------------------------------------------------------------- 34 // 35 // Imports 36 // 37 //-------------------------------------------------------------------------- 38 39 /** 40 * SqliteService.as class. 41 * @author yangboz 42 * @langVersion 3.0 43 * @playerVersion 9.0 44 * Created Nov 23, 2010 5:01:10 PM 45 */ 46 public class SqliteService extends Actor implements ISqliteService 47 { 48 //-------------------------------------------------------------------------- 49 // 50 // Variables 51 // 52 //-------------------------------------------------------------------------- 53 [Inject] 54 public var model:SqliteModel; 55 // 56// protected var em:EntityManager = EntityManager.instance; 57// protected var emAsyc:EntityManagerAsync = EntityManagerAsync.instance; 58 private var sqlConnection:SQLConnection; 59 //---------------------------------- 60 // CONSTANTS 61 //---------------------------------- 62 private static const LOG:ILogger = LogUtil.getLogger(SqliteService); 63 //-------------------------------------------------------------------------- 64 // 65 // Public properties 66 // 67 //-------------------------------------------------------------------------- 68 69 //-------------------------------------------------------------------------- 70 // 71 // Protected properties 72 // 73 //-------------------------------------------------------------------------- 74 75 //-------------------------------------------------------------------------- 76 // 77 // Constructor 78 // 79 //-------------------------------------------------------------------------- 80 public function SqliteService() 81 { 82 //TODO: implement function 83 } 84 85 //-------------------------------------------------------------------------- 86 // 87 // Public methods 88 // 89 //-------------------------------------------------------------------------- 90 public function load():void 91 { 92 var dbFile:File = File.applicationDirectory.resolvePath(Constants.DB_FILE_PATH); 93 this.sqlConnection = new SQLConnection(); 94 // 95 try 96 { 97// this.sqlConnection.open(dbFile); 98 this.sqlConnection.openAsync(dbFile); 99 this.sqlConnection.addEventListener(SQLEvent.OPEN,function(event:SQLEvent):void 100 { 101 findAll(Constants.DB_TABLE_NEIGHBORHOOD_INFO); 102 findAll(Constants.DB_TABLE_POI_CATEGORY); 103 findAll(Constants.DB_TABLE_POI_IMAGES); 104 findAll(Constants.DB_TABLE_POI_INFO); 105 findAll(Constants.DB_TABLE_POI_NEIGHBORHOOD); 106 findAll(Constants.DB_TABLE_USER_INFO); 107 //loaded sqlite data entities to model. 108 // this.model.loadedNeighborhoodInfos = em.findAll(NeighborhoodInfo); 109 // var neighborhoodInfo:NeighborhoodInfo = new NeighborhoodInfo(); 110 // neighborhoodInfo.NeighborhoodImage = "asdfa"; 111 // neighborhoodInfo.NeighborhoodInfoID = 0; 112 // neighborhoodInfo.NeighborhoodName = "name00"; 113 // this.em.save(neighborhoodInfo); 114 // var result:Object = this.em.loadItem(NeighborhoodInfo,0); 115 // 116// model.loadedPOICategorys = em.findAll(POICategory); 117 // this.model.loadedPOIImages = em.findAll(POIImages); 118 // this.model.loadedPOIInfos = em.findAll(POIInfo); 119 // this.model.loadedPOINeighborhoods = em.findAll(POINeighborhood); 120 // this.model.loadedUserInfos = em.findAll(UserInfo); 121 //dispatch robotlegs sytem events; 122 }); 123// em.sqlConnection = sqlConnection; 124// emAsyc.sqlConnection = this.sqlConnection; 125 }catch(error:SQLError) 126 { 127 LOG.fatal(Constants.MSG_ERROR_DB_CONNECTION,error.details); 128 } 129 } 130 131 virtual public function save(value:Object):void 132 { 133 //TODO: implement function 134 } 135 136 public function remove(value:Object):void 137 { 138 this.sqlConnection.close(); 139 } 140 //-------------------------------------------------------------------------- 141 // 142 // Protected methods 143 // 144 //-------------------------------------------------------------------------- 145 146 //-------------------------------------------------------------------------- 147 // 148 // Private methods 149 // 150 //-------------------------------------------------------------------------- 151 152 private function findAll(tabelName:String):void 153 { 154 var _tableName:String = tabelName; 155 var sql:String = String("SELECT * FROM ").concat(_tableName); 156 var sqlStateMent:SQLStatement = new SQLStatement(); 157 sqlStateMent.sqlConnection = sqlConnection; 158 sqlStateMent.text = sql; 159 sqlStateMent.execute(); 160 sqlStateMent.addEventListener(SQLErrorEvent.ERROR,function(event:SQLErrorEvent):void 161 { 162 LOG.error(event.text); 163 }); 164 sqlStateMent.addEventListener(SQLEvent.RESULT,function(event:SQLEvent):void 165 { 166 var sqlResult:SQLResult = sqlStateMent.getResult(); 167 // 168 switch(_tableName) 169 { 170 case Constants.DB_TABLE_NEIGHBORHOOD_INFO: 171 model.loadedNeighborhoodInfos = new ArrayCollection(sqlResult.data); 172 break; 173 case Constants.DB_TABLE_POI_CATEGORY: 174 model.loadedPOICategorys = new ArrayCollection(sqlResult.data); 175 break; 176 case Constants.DB_TABLE_POI_IMAGES: 177 model.loadedPOIImages = new ArrayCollection(sqlResult.data); 178 break; 179 case Constants.DB_TABLE_POI_INFO: 180 model.loadedPOIInfos = new ArrayCollection(sqlResult.data); 181 break; 182 case Constants.DB_TABLE_POI_NEIGHBORHOOD: 183 model.loadedPOINeighborhoods = new ArrayCollection(sqlResult.data); 184 break; 185 case Constants.DB_TABLE_USER_INFO: 186 model.loadedUserInfos = new ArrayCollection(sqlResult.data); 187 // 188 dispatch(new SqliteLoadEvent(SqliteLoadEvent.SQLITE_LOAD_COMPLETE)); 189 break; 190 default: 191 break; 192 } 193 }); 194 } 195 } 196 197}