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