| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 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 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public class AssetService |
| 50 | |
{ |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private var _context: AssetsContext; |
| 55 | |
private var _loadingContext: LoadingContext; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public function AssetService() |
| 61 | 1 | { |
| 62 | 1 | _context = AssetsContext.getInstance(); |
| 63 | 1 | _loadingContext = LoadingContext.getInstance(); |
| 64 | 1 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 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 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 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 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public function getAllAssets(): IList |
| 142 | |
{ |
| 143 | 1 | return _context.assetRepository.findAll(); |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 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 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public function removeAllAssets(): void |
| 185 | |
{ |
| 186 | 1 | _context.assetRepository.clear(); |
| 187 | 1 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 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 | |
} |