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.schema;
017:
018: import org.outerj.daisy.repository.schema.*;
019: import org.outerj.daisy.repository.ValueType;
020: import org.outerj.daisy.repository.commonimpl.AuthenticatedUser;
021: import org.outerj.daisy.repository.commonimpl.CommonRepository;
022: import org.outerj.daisy.repository.commonimpl.RepositoryImpl;
023: import org.outerj.daisy.repository.RepositoryException;
024: import org.outerj.daisy.repository.LinkExtractorInfos;
025: import org.outerj.daisy.repository.Repository;
026:
027: import java.util.List;
028: import java.util.ArrayList;
029:
030: public class CommonRepositorySchema {
031: private CommonRepository repository;
032: private SchemaStrategy schemaStrategy;
033: private List<RepositorySchemaListener> changeListeners = new ArrayList<RepositorySchemaListener>();
034: private RepositorySchemaCache cache;
035:
036: public CommonRepositorySchema(SchemaStrategy schemaStrategy,
037: CommonRepository repository, AuthenticatedUser systemUser) {
038: this .schemaStrategy = schemaStrategy;
039: this .cache = new RepositorySchemaCache(schemaStrategy,
040: systemUser);
041: this .repository = repository;
042: addListener(cache);
043: }
044:
045: public RepositorySchemaListener getCacheListener() {
046: return cache;
047: }
048:
049: public RepositorySchemaCache getCache() {
050: return cache;
051: }
052:
053: public DocumentType createDocumentType(String name,
054: AuthenticatedUser user) {
055: return new DocumentTypeImpl(name, schemaStrategy, repository,
056: user);
057: }
058:
059: public void deleteDocumentType(long documentTypeId,
060: AuthenticatedUser user) throws RepositoryException {
061: schemaStrategy.deleteDocumentType(documentTypeId, user);
062: }
063:
064: public FieldType createFieldType(String name, ValueType valueType,
065: boolean multiValue, boolean hierarchical,
066: AuthenticatedUser user) {
067: return wrap(new FieldTypeImpl(name, valueType, multiValue,
068: hierarchical, schemaStrategy, user), user,
069: new RepositoryImpl(this .repository, user));
070: }
071:
072: public void deleteFieldType(long fieldTypeId, AuthenticatedUser user)
073: throws RepositoryException {
074: schemaStrategy.deleteFieldType(fieldTypeId, user);
075: }
076:
077: public PartType createPartType(String name, String mimeTypes,
078: AuthenticatedUser user) {
079: return new PartTypeImpl(name, mimeTypes, schemaStrategy, user);
080: }
081:
082: public void deletePartType(long partTypeId, AuthenticatedUser user)
083: throws RepositoryException {
084: schemaStrategy.deletePartType(partTypeId, user);
085: }
086:
087: public DocumentTypes getAllDocumentTypes(boolean updateable,
088: AuthenticatedUser user) throws RepositoryException {
089: DocumentTypeImpl[] documentTypes;
090: if (updateable)
091: documentTypes = schemaStrategy.getAllDocumentTypes(user)
092: .toArray(new DocumentTypeImpl[0]);
093: else
094: documentTypes = cache.getAllDocumentTypes(user);
095:
096: DocumentType[] wrappedDocumentTypes = new DocumentType[documentTypes.length];
097: for (int i = 0; i < documentTypes.length; i++) {
098: wrappedDocumentTypes[i] = wrap(documentTypes[i], user);
099: }
100: return new DocumentTypesImpl(wrappedDocumentTypes);
101: }
102:
103: public FieldTypes getAllFieldTypes(boolean updateable,
104: AuthenticatedUser user) throws RepositoryException {
105: FieldTypeImpl[] fieldTypes;
106: if (updateable)
107: fieldTypes = schemaStrategy.getAllFieldTypes(user).toArray(
108: new FieldTypeImpl[0]);
109: else
110: fieldTypes = cache.getAllFieldTypes(user);
111:
112: FieldType[] wrappedFieldTypes = new FieldType[fieldTypes.length];
113: Repository repository = new RepositoryImpl(this .repository,
114: user);
115: for (int i = 0; i < fieldTypes.length; i++) {
116: wrappedFieldTypes[i] = wrap(fieldTypes[i], user, repository);
117: }
118: return new FieldTypesImpl(wrappedFieldTypes);
119: }
120:
121: public PartTypes getAllPartTypes(boolean updateable,
122: AuthenticatedUser user) throws RepositoryException {
123: if (updateable)
124: return new PartTypesImpl(schemaStrategy.getAllPartTypes(
125: user).toArray(new PartTypeImpl[0]));
126: else
127: return cache.getAllPartTypes();
128: }
129:
130: public PartType getPartTypeById(long id, boolean updateable,
131: AuthenticatedUser user) throws RepositoryException {
132: if (updateable)
133: return schemaStrategy.getPartTypeById(id, user);
134: else
135: return cache.getPartTypeById(id);
136: }
137:
138: public PartType getPartTypeByName(String name, boolean updateable,
139: AuthenticatedUser user) throws RepositoryException {
140: if (name == null || name.length() == 0)
141: throw new IllegalArgumentException("name: null or empty");
142:
143: if (updateable)
144: return schemaStrategy.getPartTypeByName(name, user);
145: else
146: return cache.getPartTypeByName(name);
147: }
148:
149: public PartType getPartType(String nameOrId, boolean updateable,
150: AuthenticatedUser user) throws RepositoryException {
151: if (nameOrId == null || nameOrId.length() == 0)
152: throw new IllegalArgumentException(
153: "nameOrId: null or empty");
154:
155: if (Character.isDigit(nameOrId.charAt(0))) {
156: try {
157: long id = Long.parseLong(nameOrId);
158: return getPartTypeById(id, updateable, user);
159: } catch (NumberFormatException e) {
160: throw new RepositoryException(
161: "Invalid part type name or ID: " + nameOrId);
162: }
163: }
164: return getPartTypeByName(nameOrId, updateable, user);
165: }
166:
167: public FieldType getFieldTypeById(long id, boolean updateable,
168: AuthenticatedUser user) throws RepositoryException {
169: FieldTypeImpl fieldTypeImpl;
170: if (updateable)
171: fieldTypeImpl = schemaStrategy.getFieldTypeById(id, user);
172: else
173: fieldTypeImpl = cache.getFieldTypeById(id, user);
174: return wrap(fieldTypeImpl, user, new RepositoryImpl(repository,
175: user));
176: }
177:
178: private FieldType wrap(FieldTypeImpl fieldTypeImpl,
179: AuthenticatedUser user, Repository repository) {
180: return new FieldTypeWrapper(fieldTypeImpl, user,
181: schemaStrategy, repository);
182: }
183:
184: public FieldType getFieldTypeByName(String name,
185: boolean updateable, AuthenticatedUser user)
186: throws RepositoryException {
187: if (name == null || name.length() == 0)
188: throw new IllegalArgumentException("name: null or empty");
189:
190: FieldTypeImpl fieldTypeImpl;
191: if (updateable)
192: fieldTypeImpl = schemaStrategy.getFieldTypeByName(name,
193: user);
194: else
195: fieldTypeImpl = cache.getFieldTypeByName(name, user);
196:
197: return wrap(fieldTypeImpl, user, new RepositoryImpl(repository,
198: user));
199: }
200:
201: public FieldType getFieldType(String nameOrId, boolean updateable,
202: AuthenticatedUser user) throws RepositoryException {
203: if (nameOrId == null || nameOrId.length() == 0)
204: throw new IllegalArgumentException(
205: "nameOrId: null or empty");
206:
207: if (Character.isDigit(nameOrId.charAt(0))) {
208: try {
209: long id = Long.parseLong(nameOrId);
210: return getFieldTypeById(id, updateable, user);
211: } catch (NumberFormatException e) {
212: throw new RepositoryException(
213: "Invalid field type name or ID: " + nameOrId);
214: }
215: }
216: return getFieldTypeByName(nameOrId, updateable, user);
217: }
218:
219: public DocumentType getDocumentTypeById(long id,
220: boolean updateable, AuthenticatedUser user)
221: throws RepositoryException {
222: DocumentTypeImpl documentType;
223: if (updateable)
224: documentType = schemaStrategy.getDocumentTypeById(id, user);
225: else
226: documentType = cache.getDocumentTypeById(id, user);
227:
228: return wrap(documentType, user);
229: }
230:
231: public DocumentType getDocumentTypeByName(String name,
232: boolean updateable, AuthenticatedUser user)
233: throws RepositoryException {
234: if (name == null || name.length() == 0)
235: throw new IllegalArgumentException("name: null or empty");
236:
237: DocumentTypeImpl documentType;
238: if (updateable)
239: documentType = schemaStrategy.getDocumentTypeByName(name,
240: user);
241: else
242: documentType = cache.getDocumentTypeByName(name, user);
243:
244: return documentType.isReadOnly() ? wrap(documentType, user)
245: : documentType;
246: }
247:
248: public DocumentType getDocumentType(String nameOrId,
249: boolean updateable, AuthenticatedUser user)
250: throws RepositoryException {
251: if (nameOrId == null || nameOrId.length() == 0)
252: throw new IllegalArgumentException(
253: "nameOrId: null or empty");
254:
255: if (Character.isDigit(nameOrId.charAt(0))) {
256: try {
257: long id = Long.parseLong(nameOrId);
258: return getDocumentTypeById(id, updateable, user);
259: } catch (NumberFormatException e) {
260: throw new RepositoryException(
261: "Invalid document type name or ID: " + nameOrId);
262: }
263: }
264: return getDocumentTypeByName(nameOrId, updateable, user);
265: }
266:
267: private DocumentTypeImpl wrap(DocumentTypeImpl documentTypeImpl,
268: AuthenticatedUser user) {
269: return documentTypeImpl.isReadOnly() ? (DocumentTypeImpl) documentTypeImpl
270: .clone(user)
271: : documentTypeImpl;
272: }
273:
274: public LinkExtractorInfos getLinkExtractors(AuthenticatedUser user)
275: throws RepositoryException {
276: return schemaStrategy.getLinkExtractors(user);
277: }
278:
279: public void removeListener(RepositorySchemaListener listener) {
280: changeListeners.remove(listener);
281: }
282:
283: public void addListener(RepositorySchemaListener listener) {
284: changeListeners.add(listener);
285: }
286:
287: public void fireSchemaEvent(RepositorySchemaEventType type,
288: long id, long updateCount) {
289: for (RepositorySchemaListener changeListener : changeListeners) {
290: changeListener.modelChange(type, id, updateCount);
291: }
292: }
293:
294: protected SchemaStrategy getSchemaStrategy() {
295: return schemaStrategy;
296: }
297: }
|