Coverage Report - org.vostokframework.application.services.AssetService
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetService
68%
37/54
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.IList;
 32  
         import org.as3utils.StringUtil;
 33  
         import org.vostokframework.VostokFramework;
 34  
         import org.vostokframework.VostokIdentification;
 35  
         import org.vostokframework.application.AssetsContext;
 36  
         import org.vostokframework.domain.assets.Asset;
 37  
         import org.vostokframework.domain.assets.AssetPackage;
 38  
         import org.vostokframework.domain.assets.AssetType;
 39  
         import org.vostokframework.domain.assets.errors.AssetNotFoundError;
 40  
         import org.vostokframework.domain.assets.errors.DuplicateAssetError;
 41  
         import org.vostokframework.domain.loading.settings.LoadingSettings;
 42  
         import org.vostokframework.application.LoadingContext;
 43  
 
 44  
         /**
 45  
          * description
 46  
          * 
 47  
          * @author Flávio Silva
 48  
          */
 49  
         public class AssetService
 50  
         {
 51  
                 /**
 52  
                  * @private
 53  
                  */
 54  
                 private var _context: AssetsContext;
 55  
                 private var _loadingContext: LoadingContext;
 56  
                 
 57  
                 /**
 58  
                  * description
 59  
                  */
 60  
                 public function AssetService()
 61  1
                 {
 62  1
                         _context = AssetsContext.getInstance();
 63  1
                         _loadingContext = LoadingContext.getInstance();
 64  1
                 }
 65  
                 
 66  
                 /**
 67  
                  * description
 68  
                  * 
 69  
                  * @param         id
 70  
                  * @param         locale
 71  
                  * @throws         ArgumentError         if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>.
 72  
                  * @return
 73  
                  */
 74  
                 public function assetExists(assetId:String, locale:String = null): Boolean
 75  
                 {
 76  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 77  
                         
 78  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 79  
                         
 80  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 81  1
                         return _context.assetRepository.exists(identification);
 82  
                 }
 83  
 
 84  
                 /**
 85  
                  * description
 86  
                  * 
 87  
                  * @param src
 88  
                  * @param assetPackage
 89  
                  * @param configuration
 90  
                  * @param id
 91  
                  * @param type
 92  
                  * @throws         ArgumentError         if the <code>src</code> argument is <code>null</code> or <code>empty</code>.
 93  
                  * @throws         ArgumentError         if the <code>assetPackage</code> argument is <code>null</code>.
 94  
                  * @throws         org.vostokframework.assetmanagement.errors.DuplicateAssetError         if already exists an <code>Asset</code> object stored with the provided <code>assetId</code> and <code>assetPackage.locale</code>.
 95  
                  * @return
 96  
                  */
 97  
                 public function createAsset(src:String, assetPackage:AssetPackage, settings:LoadingSettings = null, assetId:String = null, type:AssetType = null): Asset
 98  
                 {
 99  1
                         var asset:Asset = _context.assetFactory.create(src, assetPackage, assetId, type);
 100  
                         
 101  
                         try
 102  
                         {
 103  1
                                 _context.assetRepository.add(asset);
 104  
                         }
 105  1
                         catch(error:DuplicateAssetError)
 106  
                         {
 107  0
                                 var $assetPackage:AssetPackage = _context.assetPackageRepository.findAssetPackageByAssetId(error.identification);
 108  
                                 
 109  0
                                 var message:String = "There is already an Asset object stored with identification:\n";
 110  0
                                 message += "<" + error.identification + ">\n";
 111  0
                                 message += "It belongs to the AssetPackage:\n";
 112  0
                                 message += "<" + $assetPackage + ">\n";
 113  0
                                 message += "Use the method <AssetService().assetExists()> to check if an Asset object already exists.\n";
 114  0
                                 message += "In addition, make sure you have provided the correct <assetId>, <src> and <AssetPackage> arguments:\n";
 115  0
                                 message += "Provided <assetId>:\n";
 116  0
                                 message += "<" + assetId + ">\n";
 117  0
                                 message += "Provided <src>:\n";
 118  0
                                 message += "<" + src + ">\n";
 119  0
                                 message += "Provided <AssetPackage>:\n";
 120  0
                                 message += "<" + assetPackage + ">\n";
 121  0
                                 message += "Final Asset identification:\n";
 122  0
                                 message += "<" + error.identification + ">\n";
 123  0
                                 message += "For further information please read the documentation section about the Asset object.";
 124  
                                 
 125  0
                                 throw new DuplicateAssetError(error.identification, message);
 126  
                         }
 127  
                         
 128  1
                         assetPackage.addAsset(asset);
 129  
                         
 130  1
                         if (!settings) settings = _loadingContext.loadingSettingsFactory.defaultLoadingSettings;
 131  1
                         _loadingContext.loadingSettingsRepository.add(asset, settings);
 132  
                         
 133  1
                         return asset;
 134  
                 }
 135  
 
 136  
                 /**
 137  
                  * description
 138  
                  * 
 139  
                  * @return
 140  
                   */
 141  
                 public function getAllAssets(): IList
 142  
                 {
 143  1
                         return _context.assetRepository.findAll();
 144  
                 }
 145  
 
 146  
                 /**
 147  
                  * description
 148  
                  * 
 149  
                  * @param id
 150  
                  * @param locale
 151  
                  * @throws         ArgumentError         if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>.
 152  
                  * @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>.
 153  
                  * @return
 154  
                  */
 155  
                 public function getAsset(assetId:String, locale:String = null): Asset
 156  
                 {
 157  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 158  
                         
 159  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 160  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 161  
                         
 162  1
                         if (!assetExists(assetId, locale))
 163  
                         {
 164  1
                                 var message:String = "There is no Asset object stored with identification:\n";
 165  1
                                 message += "<" + identification + ">.\n";
 166  1
                                 message += "Use the method <AssetService().assetExists()> to check if an Asset object exists.\n";
 167  1
                                 message += "In addition, make sure you have provided the correct <assetId> and <locale> arguments:\n";
 168  1
                                 message += "Provided <assetId>: " + assetId + "\n";
 169  1
                                 message += "Provided <locale>: " + locale + "\n";
 170  1
                                 message += "Final Asset identification: " + identification + "\n";
 171  1
                                 message += "For further information please read the documentation section about the Asset object.";
 172  
                                 
 173  1
                                 throw new AssetNotFoundError(identification, message);
 174  
                         }
 175  
                         
 176  1
                         return _context.assetRepository.find(identification);
 177  
                 }
 178  
 
 179  
                 /**
 180  
                  * description
 181  
                  * 
 182  
                  * @return
 183  
                   */
 184  
                 public function removeAllAssets(): void
 185  
                 {
 186  1
                         _context.assetRepository.clear();
 187  1
                 }
 188  
 
 189  
                 /**
 190  
                  * description
 191  
                  * 
 192  
                  * @param id
 193  
                  * @param locale
 194  
                  * @throws         ArgumentError         if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>.
 195  
                  * @return
 196  
                  */
 197  
                 public function removeAsset(assetId:String, locale:String = null): Boolean
 198  
                 {
 199  1
                         if (StringUtil.isBlank(assetId)) throw new ArgumentError("Argument <assetId> must not be null nor an empty String.");
 200  
                         
 201  1
                         if (!locale) locale = VostokFramework.CROSS_LOCALE_ID;
 202  
                         
 203  1
                         var identification:VostokIdentification = new VostokIdentification(assetId, locale);
 204  1
                         return _context.assetRepository.remove(identification);
 205  
                 }
 206  
 
 207  
         }
 208  
 
 209  
 }