PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/FTTSH_G/src/com/fttsh/apps/mobile/g/service/SqliteService.as

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