1 16 package org.outerj.daisy.repository.commonimpl.variant; 17 18 import org.outerj.daisy.repository.variant.*; 19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 20 import org.outerj.daisy.repository.RepositoryException; 21 import org.outerj.daisy.repository.RepositoryListener; 22 23 public class CommonVariantManager { 24 private VariantStrategy strategy; 25 private VariantCache cache; 26 27 public CommonVariantManager(VariantStrategy strategy, VariantCache cache) { 28 this.strategy = strategy; 29 this.cache = cache; 30 } 31 32 public RepositoryListener getCacheListener() { 33 return cache; 34 } 35 36 public Branch createBranch(String name, AuthenticatedUser user) { 37 return new BranchImpl(strategy, name, user); 38 } 39 40 public Branch getBranch(long id, boolean updateable, AuthenticatedUser user) throws RepositoryException { 41 if (!updateable) 42 return cache.getBranch(id); 43 return strategy.getBranch(id, user); 44 } 45 46 public Branch getBranchByName(String name, boolean updateable, AuthenticatedUser user) throws RepositoryException { 47 if (!updateable) 48 return cache.getBranchByName(name); 49 return strategy.getBranchByName(name, user); 50 } 51 52 public Branch getBranch(String branch, boolean updateable, AuthenticatedUser user) throws RepositoryException { 53 if (branch == null || branch.length() == 0) 54 throw new IllegalArgumentException ("name: null or empty"); 55 56 if (Character.isDigit(branch.charAt(0))) { 57 try { 58 long id = Long.parseLong(branch); 59 return getBranch(id, updateable, user); 60 } catch (NumberFormatException e) { 61 throw new RepositoryException("Invalid branch name or ID: " + branch); 62 } 63 } 64 return getBranchByName(branch, updateable, user); 65 } 66 67 public Branches getAllBranches(boolean updateable, AuthenticatedUser user) throws RepositoryException { 68 if (!updateable) 69 return cache.getBranches(); 70 return new BranchesImpl(strategy.getAllBranches(user)); 71 } 72 73 public void deleteBranch(long id, AuthenticatedUser user) throws RepositoryException { 74 strategy.deleteBranch(id, user); 75 } 76 77 public Language createLanguage(String name, AuthenticatedUser user) { 78 return new LanguageImpl(strategy, name, user); 79 } 80 81 public Language getLanguage(long id, boolean updateable, AuthenticatedUser user) throws RepositoryException { 82 if (!updateable) 83 return cache.getLanguage(id); 84 return strategy.getLanguage(id, user); 85 } 86 87 public Language getLanguageByName(String name, boolean updateable, AuthenticatedUser user) throws RepositoryException { 88 if (!updateable) 89 return cache.getLanguageByName(name); 90 return strategy.getLanguageByName(name, user); 91 } 92 93 public Language getLanguage(String language, boolean updateable, AuthenticatedUser user) throws RepositoryException { 94 if (language == null || language.length() == 0) 95 throw new IllegalArgumentException ("name: null or empty"); 96 97 if (Character.isDigit(language.charAt(0))) { 98 try { 99 long id = Long.parseLong(language); 100 return getLanguage(id, updateable, user); 101 } catch (NumberFormatException e) { 102 throw new RepositoryException("Invalid language name or ID: " + language); 103 } 104 } 105 return getLanguageByName(language, updateable, user); 106 } 107 108 public Languages getAllLanguages(boolean updateable, AuthenticatedUser user) throws RepositoryException { 109 if (!updateable) 110 return cache.getLanguages(); 111 return new LanguagesImpl(strategy.getAllLanguages(user)); 112 } 113 114 public void deleteLanguage(long id, AuthenticatedUser user) throws RepositoryException { 115 strategy.deleteLanguage(id, user); 116 } 117 } 118 | Popular Tags |