Coverage Report - org.vostokframework.application.services.LoadingService
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadingService
93%
227/243
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.application.services
 30  
 {
 31  
         import org.as3collections.ICollection;
 32  
         import org.as3collections.IIterator;
 33  
         import org.as3collections.IList;
 34  
         import org.as3collections.IListMap;
 35  
         import org.as3collections.lists.ArrayList;
 36  
         import org.as3collections.lists.TypedList;
 37  
         import org.as3collections.maps.ArrayListMap;
 38  
         import org.as3collections.utils.CollectionUtil;
 39  
         import org.as3utils.StringUtil;
 40  
         import org.vostokframework.VostokFramework;
 41  
         import org.vostokframework.VostokIdentification;
 42  
         import org.vostokframework.application.LoadingContext;
 43  
         import org.vostokframework.application.cache.CachedAssetData;
 44  
         import org.vostokframework.application.cache.CachedAssetDataRepository;
 45  
         import org.vostokframework.application.cache.errors.AssetDataAlreadyCachedError;
 46  
         import org.vostokframework.application.cache.errors.CachedAssetDataNotFoundError;
 47  
         import org.vostokframework.application.monitoring.ILoadingMonitor;
 48  
         import org.vostokframework.application.monitoring.monitors.CompositeLoadingMonitor;
 49  
         import org.vostokframework.application.monitoring.monitors.LoadingMonitor;
 50  
         import org.vostokframework.application.monitoring.monitors.LoadingMonitorDispatcher;
 51  
         import org.vostokframework.application.monitoring.monitors.dispatchers.AssetLoadingMonitorDispatcher;
 52  
         import org.vostokframework.application.monitoring.monitors.dispatchers.QueueLoadingMonitorDispatcher;
 53  
         import org.vostokframework.domain.assets.Asset;
 54  
         import org.vostokframework.domain.loading.GlobalLoadingSettings;
 55  
         import org.vostokframework.domain.loading.ILoader;
 56  
         import org.vostokframework.domain.loading.ILoaderFactory;
 57  
         import org.vostokframework.domain.loading.LoadPriority;
 58  
         import org.vostokframework.domain.loading.LoaderRepository;
 59  
         import org.vostokframework.domain.loading.errors.DuplicateLoaderError;
 60  
         import org.vostokframework.domain.loading.errors.DuplicateLoadingMonitorError;
 61  
         import org.vostokframework.domain.loading.errors.LoaderNotFoundError;
 62  
         import org.vostokframework.domain.loading.errors.LoadingMonitorNotFoundError;
 63  
         import org.vostokframework.domain.loading.settings.LoadingSettings;
 64  
 
 65  
         /**
 66  
          * description
 67  
          * 
 68  
          * @author Flávio Silva
 69  
          */
 70  
         public class LoadingService
 71  
         {
 72  
                 /**
 73  
                  * @private
 74  
                  */
 75  
                 private var _context:LoadingContext;
 76  
                 
 77  
                 private function get globalMonitor():ILoadingMonitor { return _context.globalQueueLoadingMonitor; }
 78  
                 
 79  
                 private function get globalQueueLoader():ILoader { return _context.globalQueueLoader; }
 80  
                 
 81  
                 private function get cachedAssetDataRepository():CachedAssetDataRepository { return _context.cachedAssetDataRepository; }
 82  
                 
 83  
                 private function get loaderRepository():LoaderRepository { return _context.loaderRepository; }
 84  
                 
 85  
                 //private function get loadingMonitorRepository():LoadingMonitorRepository { return _context.loadingMonitorRepository; }
 86  
                 
 87  
                 private function get loaderFactory():ILoaderFactory { return _context.loaderFactory; }
 88  
                 
 89  
                 /**
 90  
                  * description
 91  
                  */
 92  
                 public function LoadingService()
 93  1
                 {
 94  1
                         _context = LoadingContext.getInstance();
 95  1
                 }
 96  
                 
 97  
                 /**
 98  
                  * description
 99  
                  * 
 100  
                  * @param queueId
 101  
                  */
 102  
                 public function cancel(loaderId:String, locale:String = null): void
 103  
                 {
 104  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 105  
                         
 106  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 107  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 108  
                         
 109  1
                         if (!exists(loaderId, locale))
 110  
                         {
 111  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 112  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 113  1
                                 message += "<locale>: <" + locale + ">\n";
 114  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 115  
                                 
 116  1
                                 throw new LoaderNotFoundError(identification, message);
 117  
                         }
 118  
                         
 119  1
                         globalQueueLoader.cancelChild(identification);
 120  1
                 }
 121  
                 
 122  
                 /**
 123  
                  * description
 124  
                  * 
 125  
                  * @param loaderId
 126  
                  * @param priority
 127  
                  * @param locale
 128  
                  * @throws         ArgumentError         if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>.
 129  
                  * @throws         ArgumentError         if the <code>priority</code> argument is <code>null</code>.
 130  
                  * @throws         org.vostokframework.assetmanagement.errors.AssetNotFoundError         if do not exist an <code>Asset</code> object stored with the provided <code>assetId</code> and <code>locale</code>.
 131  
                  */
 132  
                 public function changePriority(loaderId:String, priority:LoadPriority, locale:String = null): void
 133  
                 {
 134  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 135  1
                         if (!priority) throw new ArgumentError("Argument <priority> must not be null.");
 136  
                         
 137  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 138  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 139  
                         
 140  1
                         var assetService:AssetService = new AssetService();
 141  
                         
 142  1
                         if (!exists(loaderId, locale) && !assetService.assetExists(loaderId, locale))
 143  
                         {
 144  1
                                 var message:String = "There is no ILoader object NOR Asset object stored with specified arguments:\n";
 145  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 146  1
                                 message += "<locale>: <" + locale + ">\n";
 147  1
                                 message += "If you are trying to change the priority of an ongoing loading, is a queue or an asset, use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 148  1
                                 message += "But if you are trying to change the priority of a previously created Asset object that is not loading, use method <AssetService().assetExists()> to check if an Asset object exists.\n";
 149  
                                 
 150  1
                                 throw new LoaderNotFoundError(identification, message);
 151  
                         }
 152  
                         
 153  1
                         if (exists(loaderId, locale))
 154  
                         {
 155  1
                                 var loader:ILoader = globalQueueLoader.getChild(identification);
 156  1
                                 loader.priority = priority.ordinal;
 157  
                         }
 158  
                         
 159  1
                         if (assetService.assetExists(loaderId, locale))
 160  
                         {
 161  1
                                 var asset:Asset = assetService.getAsset(loaderId, locale);
 162  
                                 
 163  1
                                 var settings:LoadingSettings = _context.loadingSettingsRepository.find(asset);
 164  1
                                 settings.policy.priority = priority;
 165  
                         }
 166  
                         
 167  
                         /*
 168  
                         if (!exists(loaderId, locale))
 169  
                         {
 170  
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 171  
                                 message += "<loaderId>: <" + loaderId + ">\n";
 172  
                                 message += "<locale>: <" + locale + ">\n";
 173  
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 174  
                                 
 175  
                                 throw new LoaderNotFoundError(identification, message);
 176  
                         }
 177  
                         */
 178  1
                 }
 179  
                 
 180  
                 /**
 181  
                  * description
 182  
                  * 
 183  
                  * @param assetId
 184  
                  * @param locale
 185  
                  * @return
 186  
                  */
 187  
                 public function containsAssetData(assetId:String, locale:String = null): Boolean
 188  
                 {
 189  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 190  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 191  
                         
 192  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 193  1
                         return cachedAssetDataRepository.exists(identification);
 194  
                 }
 195  
                 
 196  
                 /**
 197  
                  * description
 198  
                  * 
 199  
                  * @param queueId
 200  
                  * @return
 201  
                  */
 202  
                 public function exists(loaderId:String, locale:String = null): Boolean
 203  
                 {
 204  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 205  
                         
 206  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 207  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 208  
                         
 209  
                         //return loaderRepository.exists(queueId);
 210  1
                         return globalQueueLoader.containsChild(identification);
 211  
                 }
 212  
                 
 213  
                 /**
 214  
                  * description
 215  
                  * 
 216  
                  * @param assetId
 217  
                  * @param locale
 218  
                  */
 219  
                 public function getAssetData(assetId:String, locale:String = null): *
 220  
                 {
 221  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 222  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 223  
                         
 224  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 225  1
                         if(!cachedAssetDataRepository.exists(identification))
 226  
                         {
 227  1
                                 var message:String = "There is no data cached for an Asset object with identification:\n";
 228  1
                                 message += "<" + identification + ">\n";
 229  1
                                 message += "Use method <LoadingService().containsAssetData()> to check if an Asset object was loaded and cached.\n";
 230  
                                 
 231  1
                                 throw new CachedAssetDataNotFoundError(identification, message);
 232  
                         }
 233  
                         
 234  1
                         var cachedAssetData:CachedAssetData = cachedAssetDataRepository.find(identification);
 235  1
                         return cachedAssetData.data;
 236  
                 }
 237  
                 
 238  
                 /**
 239  
                  * description
 240  
                  * 
 241  
                  * @param queueId
 242  
                  * @return
 243  
                  */
 244  
                 public function getMonitor(loaderId:String, locale:String = null): ILoadingMonitor
 245  
                 {
 246  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 247  
                         
 248  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 249  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 250  
                         
 251  1
                         if (!globalMonitor.containsChild(identification))
 252  
                         {
 253  1
                                 var message:String = "There is no ILoadingMonitor object stored with specified arguments:\n";
 254  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 255  1
                                 message += "<locale>: <" + locale + ">\n";
 256  1
                                 message += "Use method <LoadingService().exists()> to check if an ILoadingMonitor object is mapped for a ILoader object.\n";
 257  
                                 
 258  1
                                 throw new LoadingMonitorNotFoundError(message);
 259  
                         }
 260  
                         
 261  1
                         return globalMonitor.getChild(identification);;
 262  
                 }
 263  
                 
 264  
                 /**
 265  
                  * description
 266  
                  * 
 267  
                  * @param queueId
 268  
                  * @return
 269  
                  */
 270  
                 public function isLoading(loaderId:String, locale:String = null): Boolean
 271  
                 {
 272  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 273  
                         
 274  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 275  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 276  
                         
 277  1
                         if (!exists(loaderId, locale))
 278  
                         {
 279  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 280  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 281  1
                                 message += "<locale>: <" + locale + ">\n";
 282  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 283  
                                 
 284  1
                                 throw new LoaderNotFoundError(identification, message);
 285  
                         }
 286  
                         
 287  
                         //var state:ILoaderState = globalQueueLoader.getLoaderState(identification);
 288  
                         //return state.equals(LoaderConnecting.INSTANCE) || state.equals(LoaderLoading.INSTANCE);
 289  
                         
 290  1
                         var loader:ILoader = globalQueueLoader.getChild(identification);
 291  1
                         return loader.isLoading;
 292  
                 }
 293  
                 
 294  
                 /**
 295  
                  * description
 296  
                  * 
 297  
                  * @param queueId
 298  
                  * @return
 299  
                  */
 300  
                 public function isQueued(loaderId:String, locale:String = null): Boolean
 301  
                 {
 302  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 303  
                         
 304  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 305  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 306  
                         
 307  1
                         if (!exists(loaderId, locale))
 308  
                         {
 309  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 310  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 311  1
                                 message += "<locale>: <" + locale + ">\n";
 312  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 313  
                                 
 314  1
                                 throw new LoaderNotFoundError(identification, message);
 315  
                         }
 316  
                         
 317  
                         //var state:ILoaderState = globalQueueLoader.getLoaderState(identification);
 318  
                         //return state.equals(LoaderQueued.INSTANCE);
 319  
                         
 320  1
                         var loader:ILoader = globalQueueLoader.getChild(identification);
 321  1
                         return loader.isQueued;
 322  
                 }
 323  
                 
 324  
                 /**
 325  
                  * description
 326  
                  * 
 327  
                  * @param queueId
 328  
                  * @return
 329  
                  */
 330  
                 public function isStopped(loaderId:String, locale:String = null): Boolean
 331  
                 {
 332  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 333  
                         
 334  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 335  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 336  
                         
 337  1
                         if (!exists(loaderId, locale))
 338  
                         {
 339  0
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 340  0
                                 message += "<loaderId>: <" + loaderId + ">\n";
 341  0
                                 message += "<locale>: <" + locale + ">\n";
 342  0
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 343  
                                 
 344  0
                                 throw new LoaderNotFoundError(identification, message);
 345  
                         }
 346  
                         
 347  
                         //var state:ILoaderState = globalQueueLoader.getLoaderState(identification);
 348  
                         //return state.equals(LoaderQueued.INSTANCE);
 349  
                         
 350  1
                         var loader:ILoader = globalQueueLoader.getChild(identification);
 351  1
                         return loader.isStopped;
 352  
                 }
 353  
 
 354  
                 /**
 355  
                  * description
 356  
                  * 
 357  
                  * @param queueId
 358  
                  * @param assets
 359  
                  * @param priority
 360  
                  * @param concurrentConnections
 361  
                  * @return
 362  
                  */
 363  
                 public function load(loaderId:String, assets:IList, priority:LoadPriority = null, concurrentConnections:int = 1): ILoadingMonitor
 364  
                 {
 365  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 366  1
                         if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty.");
 367  1
                         if (concurrentConnections < 1) throw new ArgumentError("Argument <concurrentConnections> must be greater than zero. Received: <" + concurrentConnections + ">");
 368  
                         
 369  
                         //throws org.as3coreaddendum.ClassCastError
 370  
                         //if there's any type other than Asset in <assets>
 371  1
                         assets = new TypedList(assets, Asset);
 372  
                         
 373  
                         //throws ArgumentError
 374  
                         //if there's any duplicated Asset object
 375  1
                         validateDuplicateAsset(assets);
 376  
                         
 377  1
                         if (!priority) priority = LoadPriority.MEDIUM;
 378  
                         
 379  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, VostokFramework.CROSS_LOCALE_ID);
 380  
                         var errorMessage:String;
 381  
                         
 382  1
                         if (globalQueueLoader.containsChild(identification))
 383  
                         {
 384  1
                                 errorMessage = "There is already a ILoader object stored with specified arguments:\n";
 385  1
                                 errorMessage += "<loaderId>: <" + loaderId + ">\n";
 386  1
                                 errorMessage += "utilized locale: <" + VostokFramework.CROSS_LOCALE_ID + ">\n";
 387  1
                                 errorMessage += "Use method <LoadingService().exists()> to check if a ILoader object already exists.\n";
 388  1
                                 errorMessage += "For further information please read the documentation section about ILoader object.";
 389  
                                 
 390  1
                                 throw new DuplicateLoaderError(identification, errorMessage);
 391  
                         }
 392  
                         
 393  1
                         if (globalMonitor.containsChild(identification))
 394  
                         {
 395  0
                                 errorMessage = "There is already a ILoadingMonitor object stored for a ILoader with identification:\n";
 396  0
                                 errorMessage += "<identification>: <" + identification + ">\n";
 397  0
                                 errorMessage += "Use method <LoadingService().exists()> to check if a ILoadingMonitor object already exists for the specified ILoader.\n";
 398  0
                                 errorMessage += "For further information please read the documentation section about ILoadingMonitor object.";
 399  
                                 
 400  0
                                 throw new DuplicateLoadingMonitorError(errorMessage);
 401  
                         }
 402  
                         
 403  1
                         var globalLoadingSettings:GlobalLoadingSettings = LoadingContext.getInstance().globalLoadingSettings;
 404  1
                         var queueLoader:ILoader = loaderFactory.createComposite(identification, loaderRepository, globalLoadingSettings, priority, concurrentConnections);
 405  
                         
 406  
                         //throws org.vostokframework.application.cache.errors.DuplicateLoadedAssetError
 407  
                         //if some Asset object is already loaded and cached internally
 408  1
                         checkIfSomeAssetIsAlreadyLoadedAndCached(assets);
 409  
                         
 410  
                         //throws org.vostokframework.domain.loading.errors.DuplicateLoaderError
 411  
                         //if there is a ILoader object with the identification of any Asset inside <assets>
 412  1
                         var loaders:IList = createLeafLoaders(assets);
 413  
                         
 414  1
                         queueLoader.addChildren(loaders);
 415  
                         
 416  1
                         var assetsAndLoadersMap:IListMap = createAssetsAndLoadersMap(assets, loaders);
 417  1
                         var assetLoadingMonitors:IList = createAssetLoadingMonitors(assetsAndLoadersMap);
 418  
                         
 419  1
                         var loadingMonitorDispatcher:LoadingMonitorDispatcher = new QueueLoadingMonitorDispatcher(identification.id, identification.locale);
 420  1
                         var monitor:ILoadingMonitor = new CompositeLoadingMonitor(queueLoader, loadingMonitorDispatcher);
 421  1
                         monitor.addChildren(assetLoadingMonitors);
 422  
                         //loadingMonitorRepository.add(monitor);
 423  1
                         globalMonitor.addChild(monitor);
 424  
                         
 425  1
                         globalQueueLoader.addChild(queueLoader);
 426  1
                         globalQueueLoader.load();
 427  
                         
 428  1
                         return monitor;
 429  
                 }
 430  
                 
 431  
                 /**
 432  
                  * description
 433  
                  * 
 434  
                  * @param queueId
 435  
                  * @param asset
 436  
                  * @param priority
 437  
                  * @param concurrentConnections
 438  
                  * @return
 439  
                  */
 440  
                 public function loadSingle(loaderId:String, asset:Asset, priority:LoadPriority = null, concurrentConnections:int = 1): ILoadingMonitor
 441  
                 {
 442  1
                         return load(loaderId, new ArrayList([asset]), priority, concurrentConnections);
 443  
                 }
 444  
                 
 445  
                 /**
 446  
                  * description
 447  
                  * 
 448  
                  * @param queueId
 449  
                  * @param assets
 450  
                  * @return
 451  
                  */
 452  
                 public function mergeAssets(loaderId:String, assets:IList, locale:String = null): ILoadingMonitor
 453  
                 {
 454  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 455  1
                         if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty.");
 456  
                         
 457  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 458  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 459  
                         
 460  1
                         if (!exists(loaderId, locale))
 461  
                         {
 462  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 463  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 464  1
                                 message += "<locale>: <" + locale + ">\n";
 465  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 466  
                                 
 467  1
                                 throw new LoaderNotFoundError(identification, message);
 468  
                         }
 469  
                         
 470  
                         //throws org.as3coreaddendum.ClassCastError
 471  
                         //if there's any type other than Asset in <assets>
 472  1
                         assets = new TypedList(assets, Asset);
 473  
                         
 474  
                         //throws ArgumentError
 475  
                         //if there's any duplicate Asset object
 476  1
                         validateDuplicateAsset(assets);
 477  
                         
 478  
                         //throws org.vostokframework.application.cache.errors.DuplicateLoadedAssetError
 479  
                         //if some Asset object is already loaded and cached internally
 480  1
                         checkIfSomeAssetIsAlreadyLoadedAndCached(assets);
 481  
                         
 482  
                         //throws org.vostokframework.domain.loading.errors.DuplicateLoaderError
 483  
                         //if there is a ILoader object with the identification of any Asset inside <assets>
 484  1
                         var loaders:IList = createLeafLoaders(assets);
 485  
                         
 486  1
                         var assetsAndLoadersMap:IListMap = createAssetsAndLoadersMap(assets, loaders);
 487  1
                         var assetLoadingMonitors:IList = createAssetLoadingMonitors(assetsAndLoadersMap);
 488  
                         
 489  1
                         var queueLoader:ILoader = globalQueueLoader.getChild(identification);
 490  1
                         queueLoader.addChildren(loaders);
 491  
                         
 492  1
                         var monitor:ILoadingMonitor = globalMonitor.getChild(identification);
 493  1
                         monitor.addChildren(assetLoadingMonitors);
 494  
                         
 495  1
                         return monitor;
 496  
                 }
 497  
                 
 498  
                 /**
 499  
                  * description
 500  
                  * 
 501  
                  * @param assetId
 502  
                  * @param locale
 503  
                  * @return
 504  
                  */
 505  
                 public function removeAssetData(assetId:String, locale:String = null): void
 506  
                 {
 507  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 508  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID; 
 509  
                         
 510  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 511  
                         
 512  1
                         if (!containsAssetData(assetId, locale))
 513  
                         {
 514  1
                                 var message:String = "There is no data cached for an Asset object with identification:\n";
 515  1
                                 message += "<" + identification + ">\n";
 516  1
                                 message += "Use method <LoadingService().containsAssetData()> to check if an Asset object was loaded and cached.\n";
 517  
                                 
 518  1
                                 throw new CachedAssetDataNotFoundError(identification, message);
 519  
                         }
 520  
                         
 521  0
                         cachedAssetDataRepository.remove(identification);
 522  0
                 }
 523  
                 
 524  
                 /**
 525  
                  * description
 526  
                  * 
 527  
                  * @param requestId
 528  
                  * @return
 529  
                  */
 530  
                 public function resume(loaderId:String, locale:String = null): void
 531  
                 {
 532  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 533  
                         
 534  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 535  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 536  
                         
 537  1
                         if (!exists(loaderId, locale))
 538  
                         {
 539  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 540  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 541  1
                                 message += "<locale>: <" + locale + ">\n";
 542  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 543  
                                 
 544  1
                                 throw new LoaderNotFoundError(identification, message);
 545  
                         }
 546  
                         
 547  1
                         globalQueueLoader.resumeChild(identification);
 548  1
                 }
 549  
 
 550  
                 /**
 551  
                  * description
 552  
                  * 
 553  
                  * @param requestId
 554  
                  * @return
 555  
                  */
 556  
                 public function stop(loaderId:String, locale:String = null): void
 557  
                 {
 558  1
                         if (StringUtil.isBlank(loaderId)) throw new ArgumentError("Argument <loaderId> must not be null nor an empty String.");
 559  
                         
 560  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 561  1
                         var identification:VostokIdentification = new VostokIdentification(loaderId, locale);
 562  
                         
 563  1
                         if (!exists(loaderId, locale))
 564  
                         {
 565  1
                                 var message:String = "There is no ILoader object stored with specified arguments:\n";
 566  1
                                 message += "<loaderId>: <" + loaderId + ">\n";
 567  1
                                 message += "<locale>: <" + locale + ">\n";
 568  1
                                 message += "Use method <LoadingService().exists()> to check if a ILoader object exists.\n";
 569  
                                 
 570  1
                                 throw new LoaderNotFoundError(identification, message);
 571  
                         }
 572  
                         
 573  1
                         globalQueueLoader.stopChild(identification);
 574  1
                 }
 575  
                 
 576  
                 private function checkIfSomeAssetIsAlreadyLoadedAndCached(assets:IList):void
 577  
                 {
 578  
                         var asset:Asset;
 579  1
                         var it:IIterator = assets.iterator();
 580  
                         
 581  1
                         while (it.hasNext())
 582  
                         {
 583  1
                                 asset = it.next();
 584  
                                 
 585  1
                                 if (cachedAssetDataRepository.exists(asset.identification))
 586  
                                 {
 587  1
                                         var cachedAssetData:CachedAssetData = cachedAssetDataRepository.find(asset.identification);
 588  
                                         
 589  1
                                         var errorMessage:String = "The Asset object with identification:\n";
 590  1
                                         errorMessage += "<" + asset.identification + ">\n";
 591  1
                                         errorMessage += "Is already loaded and cached internally.\n";
 592  1
                                         errorMessage += "It belonged to a queue loader object with identification:\n";
 593  1
                                         errorMessage += "<" + cachedAssetData.queueIdentification + ">\n";
 594  1
                                         errorMessage += "Use the method <LoadingService().containsAssetData()> to find it out.\n";
 595  1
                                         errorMessage += "Also, cached asset data can be retrieved using <LoadingService().getAssetData()>.";
 596  
                                         
 597  1
                                         throw new AssetDataAlreadyCachedError(asset.identification, errorMessage);
 598  
                                 }
 599  
                         }
 600  1
                 }
 601  
                 
 602  
                 private function createAssetsAndLoadersMap(assets:IList, loaders:IList):IListMap
 603  
                 {
 604  1
                         if (!assets || assets.isEmpty()) throw new ArgumentError("Argument <assets> must not be null nor empty.");
 605  1
                         if (!loaders || loaders.isEmpty()) throw new ArgumentError("Argument <loaders> must not be null nor empty.");
 606  1
                         if (assets.size() != loaders.size())
 607  
                         {
 608  0
                                 var errorMessage:String = "Both arguments must have same size.\n";
 609  0
                                 errorMessage += "<assets.size()>: " + assets.size();
 610  0
                                 errorMessage += "<loaders.size()>: " + loaders.size();
 611  
                                 
 612  0
                                 throw new ArgumentError(errorMessage);
 613  
                         }
 614  
                         
 615  1
                         var map:IListMap = new ArrayListMap();
 616  1
                         var itAssets:IIterator = assets.iterator();
 617  1
                         var itLoaders:IIterator = loaders.iterator();
 618  
                         
 619  1
                         while (itAssets.hasNext())
 620  
                         {
 621  1
                                 map.put(itAssets.next(), itLoaders.next());
 622  
                         }
 623  
                         
 624  1
                         return map;
 625  
                 }
 626  
                 
 627  
                 private function createAssetLoadingMonitors(assetsAndLoaders:IListMap):IList
 628  
                 {
 629  
                         var asset:Asset;
 630  
                         var loader:ILoader;
 631  
                         var assetLoadingMonitor:ILoadingMonitor;
 632  1
                         var assetLoadingMonitors:IList = new ArrayList();
 633  
                         var loadingMonitorDispatcher:LoadingMonitorDispatcher;
 634  1
                         var it:IIterator = assetsAndLoaders.iterator();
 635  
                         
 636  1
                         while (it.hasNext())
 637  
                         {
 638  1
                                 loader = it.next();
 639  1
                                 asset = it.pointer();
 640  
                                 
 641  1
                                 loadingMonitorDispatcher = new AssetLoadingMonitorDispatcher(asset.identification.id, asset.identification.locale, asset.type);
 642  1
                                 assetLoadingMonitor = new LoadingMonitor(loader, loadingMonitorDispatcher);
 643  1
                                 assetLoadingMonitors.add(assetLoadingMonitor);
 644  
                         }
 645  
                         
 646  1
                         return assetLoadingMonitors;
 647  
                 }
 648  
                 
 649  
                 private function createLeafLoaders(assets:IList):IList
 650  
                 {
 651  
                         var asset:Asset;
 652  
                         var loadingSettings:LoadingSettings;
 653  
                         var loader:ILoader;
 654  1
                         var loaders:IList = new ArrayList();
 655  1
                         var it:IIterator = assets.iterator();
 656  
                         
 657  1
                         while (it.hasNext())
 658  
                         {
 659  1
                                 asset = it.next();
 660  1
                                 loadingSettings = _context.loadingSettingsRepository.find(asset);
 661  1
                                 loader = loaderFactory.createLeaf(asset.identification, asset.src, asset.type, loadingSettings);
 662  
                                 
 663  1
                                 if (globalQueueLoader.containsChild(loader.identification))
 664  
                                 {
 665  
                                         //var childLoaderState:ILoaderState = globalQueueLoader.getLoaderState(loader.identification);
 666  1
                                         var parent:ILoader = globalQueueLoader.getParent(loader.identification);
 667  
                                         
 668  1
                                         var errorMessage:String = "There is already a ILoader object stored with identification:\n";
 669  1
                                         errorMessage += "<" + loader.identification + ">\n";
 670  
                                         //errorMessage += "Its current state is: <" + childLoaderState + ">\n";
 671  1
                                         errorMessage += "And it belongs to a parent ILoader object with identification: <" + parent.identification + ">\n";
 672  1
                                         errorMessage += "Use method <LoadingService().exists()> to check if a ILoader object already exists for some Asset.\n";
 673  1
                                         errorMessage += "For further information please read the documentation section about the ILoader object.";
 674  
                                         
 675  1
                                         throw new DuplicateLoaderError(loader.identification, errorMessage);
 676  
                                 }
 677  
                                 
 678  1
                                 loaders.add(loader);
 679  
                         }
 680  
                         
 681  1
                         return loaders;
 682  
                 }
 683  
                 
 684  
                 private function validateDuplicateAsset(assets:IList):void
 685  
                 {
 686  1
                         var duplicate:ICollection = CollectionUtil.getDuplicate(assets);
 687  
                         
 688  1
                         if (!duplicate.isEmpty())
 689  
                         {
 690  1
                                 var errorMessage:String = "Argument <assets> must not have duplicate elements.\n";
 691  1
                                 errorMessage += "The following duplicates were found:\n";
 692  1
                                 errorMessage += duplicate;
 693  
                                 
 694  1
                                 throw new ArgumentError(errorMessage);
 695  
                         }
 696  1
                 }
 697  
                 
 698  
         }
 699  
 
 700  
 }