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.observation;
019:
020: import org.apache.avalon.framework.logger.Logger;
021: import org.apache.avalon.framework.service.ServiceManager;
022: import org.apache.lenya.cms.publication.Document;
023: import org.apache.lenya.cms.publication.DocumentException;
024: import org.apache.lenya.cms.publication.DocumentFactory;
025: import org.apache.lenya.cms.publication.DocumentUtil;
026: import org.apache.lenya.cms.publication.Publication;
027: import org.apache.lenya.cms.repository.Node;
028: import org.apache.lenya.cms.repository.Session;
029:
030: /**
031: * Factory to create repository events.
032: */
033: public class RepositoryEventFactory {
034:
035: /**
036: * Creates a repository event for a node.
037: * @param manager The service manager.
038: * @param session The session.
039: * @param logger The logger.
040: * @param descriptor The descriptor.
041: * @return An event.
042: */
043: public static final RepositoryEvent createEvent(
044: ServiceManager manager, Session session, Logger logger,
045: Object descriptor) {
046: return new RepositoryEvent(session, descriptor);
047: }
048:
049: /**
050: * Creates a repository event for a node.
051: * @param manager The service manager.
052: * @param doc The document.
053: * @param logger The logger.
054: * @param descriptor The descriptor.
055: * @return An event.
056: */
057: public static final RepositoryEvent createEvent(
058: ServiceManager manager, Document doc, Logger logger,
059: Object descriptor) {
060: try {
061: return new DocumentEvent(doc.getRepositoryNode()
062: .getSession(), doc.getPublication().getId(), doc
063: .getArea(), doc.getUUID(), doc.getLanguage(), doc
064: .getResourceType(), descriptor);
065: } catch (DocumentException e) {
066: throw new RuntimeException(e);
067: }
068:
069: }
070:
071: /**
072: * Creates a repository event for a node.
073: * @param manager The service manager.
074: * @param node The node.
075: * @param logger The logger.
076: * @param descriptor The descriptor.
077: * @return An event.
078: */
079: public static final RepositoryEvent createEvent(
080: ServiceManager manager, Node node, Logger logger,
081: Object descriptor) {
082: RepositoryEvent event;
083: Document doc = null;
084: if (!node.getSourceURI().endsWith("meta")) {
085: doc = getDocument(manager, node, logger);
086: }
087: if (doc != null) {
088: event = createEvent(manager, doc, logger, descriptor);
089: } else {
090: event = new RepositoryEvent(node.getSession(), descriptor);
091: }
092: return event;
093: }
094:
095: /**
096: * @param manager The service manager.
097: * @param node The node.
098: * @param logger The logger.
099: * @return The document represented by the node or <code>null</code> if
100: * the node doesn't represent a document.
101: */
102: protected static final Document getDocument(ServiceManager manager,
103: Node node, Logger logger) {
104:
105: final String sourceUri = node.getSourceURI();
106: if (sourceUri.endsWith(".xml")) {
107: return null;
108: }
109:
110: Document doc = null;
111:
112: if (!sourceUri.startsWith("lenya://")) {
113: throw new IllegalStateException("The source URI ["
114: + sourceUri + "] doesn't start with lenya://");
115: }
116:
117: String path = sourceUri.substring("lenya://lenya/pubs/"
118: .length());
119:
120: String[] steps = path.split("/");
121: String pubId = steps[0];
122: String area = steps[2];
123:
124: try {
125:
126: DocumentFactory factory = DocumentUtil
127: .createDocumentFactory(manager, node.getSession());
128: Publication pub = factory.getPublication(pubId);
129: String docPath = path
130: .substring((pubId + "/content/" + area).length());
131:
132: String uuid = docPath.substring(1, docPath.length()
133: - "/en".length());
134: String language = docPath.substring(docPath.length()
135: - "en".length());
136:
137: doc = factory.get(pub, area, uuid, language);
138:
139: if (doc == null) {
140: // this happens if the node was not a document node
141: logger.info("No document found for node [" + sourceUri
142: + "]");
143: }
144:
145: } catch (Exception e) {
146: throw new RuntimeException(e);
147: }
148: return doc;
149: }
150:
151: }
|