001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.site.simple;
019:
020: import org.apache.avalon.framework.container.ContainerUtil;
021: import org.apache.avalon.framework.logger.AbstractLogEnabled;
022: import org.apache.avalon.framework.logger.Logger;
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.lenya.cms.publication.Area;
026: import org.apache.lenya.cms.publication.Document;
027: import org.apache.lenya.cms.publication.DocumentFactory;
028: import org.apache.lenya.cms.publication.DocumentManager;
029: import org.apache.lenya.cms.publication.DocumentUtil;
030: import org.apache.lenya.cms.publication.Publication;
031: import org.apache.lenya.cms.publication.PublicationException;
032: import org.apache.lenya.cms.repository.RepositoryException;
033: import org.apache.lenya.cms.repository.RepositoryItem;
034: import org.apache.lenya.cms.repository.RepositoryItemFactory;
035: import org.apache.lenya.cms.repository.Session;
036: import org.apache.lenya.util.Assert;
037:
038: /**
039: * Factory for sitetree objects.
040: *
041: * @version $Id: DocumentStoreFactory.java 527520 2007-04-11 15:22:11Z andreas $
042: */
043: public class DocumentStoreFactory extends AbstractLogEnabled implements
044: RepositoryItemFactory {
045:
046: protected ServiceManager manager;
047:
048: /**
049: * Ctor.
050: * @param manager The service manager.
051: * @param logger The logger.
052: */
053: public DocumentStoreFactory(ServiceManager manager, Logger logger) {
054: this .manager = manager;
055: ContainerUtil.enableLogging(this , logger);
056: }
057:
058: /**
059: * @see org.apache.lenya.cms.repository.RepositoryItemFactory#getItemType()
060: */
061: public String getItemType() {
062: return DocumentStore.IDENTIFIABLE_TYPE;
063: }
064:
065: /**
066: * @see org.apache.lenya.cms.repository.RepositoryItemFactory#buildItem(org.apache.lenya.cms.repository.Session,
067: * java.lang.String)
068: */
069: public RepositoryItem buildItem(Session session, String key)
070: throws RepositoryException {
071: String[] snippets = key.split(":");
072:
073: Assert.isTrue("key [" + key + "] is invalid!",
074: snippets.length == 3);
075:
076: String publicationId = snippets[0];
077: String areaName = snippets[1];
078: String uuid = snippets[2];
079: DocumentStore store;
080: try {
081: DocumentFactory factory = DocumentUtil
082: .createDocumentFactory(this .manager, session);
083: Publication publication = factory
084: .getPublication(publicationId);
085: Area area = publication.getArea(areaName);
086: String lang = publication.getDefaultLanguage();
087:
088: if (!area.contains(uuid, lang)) {
089: createAreaVersion(publication, areaName, uuid, lang);
090: }
091:
092: Document doc = area.getDocument(uuid, lang);
093:
094: store = new DocumentStore(doc, getLogger());
095: } catch (Exception e) {
096: throw new RepositoryException(e);
097: }
098: return store;
099: }
100:
101: protected void createAreaVersion(Publication publication,
102: String areaName, String uuid, String lang)
103: throws PublicationException, ServiceException {
104: DocumentManager docManager = null;
105: try {
106: Area authoring = publication
107: .getArea(Publication.AUTHORING_AREA);
108: Document authoringDoc = authoring.getDocument(uuid, lang);
109: docManager = (DocumentManager) this .manager
110: .lookup(DocumentManager.ROLE);
111: docManager.copyToArea(authoringDoc, areaName);
112: } finally {
113: if (docManager != null) {
114: this .manager.release(docManager);
115: }
116: }
117: }
118:
119: public boolean isSharable() {
120: return false;
121: }
122:
123: }
|