001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.commonimpl.variant;
017:
018: import org.outerj.daisy.repository.variant.*;
019: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
020: import org.outerj.daisy.repository.RepositoryException;
021: import org.outerj.daisy.repository.RepositoryListener;
022:
023: public class CommonVariantManager {
024: private VariantStrategy strategy;
025: private VariantCache cache;
026:
027: public CommonVariantManager(VariantStrategy strategy,
028: VariantCache cache) {
029: this .strategy = strategy;
030: this .cache = cache;
031: }
032:
033: public RepositoryListener getCacheListener() {
034: return cache;
035: }
036:
037: public Branch createBranch(String name, AuthenticatedUser user) {
038: return new BranchImpl(strategy, name, user);
039: }
040:
041: public Branch getBranch(long id, boolean updateable,
042: AuthenticatedUser user) throws RepositoryException {
043: if (!updateable)
044: return cache.getBranch(id);
045: return strategy.getBranch(id, user);
046: }
047:
048: public Branch getBranchByName(String name, boolean updateable,
049: AuthenticatedUser user) throws RepositoryException {
050: if (!updateable)
051: return cache.getBranchByName(name);
052: return strategy.getBranchByName(name, user);
053: }
054:
055: public Branch getBranch(String branch, boolean updateable,
056: AuthenticatedUser user) throws RepositoryException {
057: if (branch == null || branch.length() == 0)
058: throw new IllegalArgumentException("name: null or empty");
059:
060: if (Character.isDigit(branch.charAt(0))) {
061: try {
062: long id = Long.parseLong(branch);
063: return getBranch(id, updateable, user);
064: } catch (NumberFormatException e) {
065: throw new BranchNotFoundException(branch);
066: }
067: }
068: return getBranchByName(branch, updateable, user);
069: }
070:
071: public Branches getAllBranches(boolean updateable,
072: AuthenticatedUser user) throws RepositoryException {
073: if (!updateable)
074: return cache.getBranches();
075: return new BranchesImpl(strategy.getAllBranches(user));
076: }
077:
078: public void deleteBranch(long id, AuthenticatedUser user)
079: throws RepositoryException {
080: strategy.deleteBranch(id, user);
081: }
082:
083: public Language createLanguage(String name, AuthenticatedUser user) {
084: return new LanguageImpl(strategy, name, user);
085: }
086:
087: public Language getLanguage(long id, boolean updateable,
088: AuthenticatedUser user) throws RepositoryException {
089: if (!updateable)
090: return cache.getLanguage(id);
091: return strategy.getLanguage(id, user);
092: }
093:
094: public Language getLanguageByName(String name, boolean updateable,
095: AuthenticatedUser user) throws RepositoryException {
096: if (!updateable)
097: return cache.getLanguageByName(name);
098: return strategy.getLanguageByName(name, user);
099: }
100:
101: public Language getLanguage(String language, boolean updateable,
102: AuthenticatedUser user) throws RepositoryException {
103: if (language == null || language.length() == 0)
104: throw new IllegalArgumentException("name: null or empty");
105:
106: if (Character.isDigit(language.charAt(0))) {
107: try {
108: long id = Long.parseLong(language);
109: return getLanguage(id, updateable, user);
110: } catch (NumberFormatException e) {
111: throw new LanguageNotFoundException(language);
112: }
113: }
114: return getLanguageByName(language, updateable, user);
115: }
116:
117: public Languages getAllLanguages(boolean updateable,
118: AuthenticatedUser user) throws RepositoryException {
119: if (!updateable)
120: return cache.getLanguages();
121: return new LanguagesImpl(strategy.getAllLanguages(user));
122: }
123:
124: public void deleteLanguage(long id, AuthenticatedUser user)
125: throws RepositoryException {
126: strategy.deleteLanguage(id, user);
127: }
128: }
|