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.blog.cms.usecases;
019:
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.ArrayList;
023: import java.util.Date;
024: import java.util.List;
025:
026: import org.apache.lenya.cms.publication.Area;
027: import org.apache.lenya.cms.publication.Document;
028: import org.apache.lenya.cms.publication.DocumentManager;
029: import org.apache.lenya.cms.publication.Publication;
030: import org.apache.lenya.cms.publication.util.DocumentSet;
031: import org.apache.lenya.cms.repository.Node;
032: import org.apache.lenya.cms.site.NodeSet;
033: import org.apache.lenya.cms.site.SiteUtil;
034: import org.apache.lenya.cms.usecase.DocumentUsecase;
035: import org.apache.lenya.cms.usecase.UsecaseException;
036: import org.apache.lenya.cms.workflow.WorkflowUtil;
037: import org.apache.lenya.cms.workflow.usecases.UsecaseWorkflowHelper;
038: import org.apache.lenya.workflow.Version;
039: import org.apache.lenya.workflow.Workflowable;
040: import org.apache.lenya.xml.DocumentHelper;
041: import org.apache.xpath.XPathAPI;
042: import org.w3c.dom.Element;
043:
044: /**
045: * Publish usecase handler.
046: *
047: * @version $Id: Publish.java 209612 2005-07-07 16:52:44Z chestnut $
048: */
049: public class Publish extends DocumentUsecase {
050:
051: protected static final String MISSING_DOCUMENTS = "missingDocuments";
052:
053: /**
054: * @see org.apache.lenya.cms.usecase.AbstractUsecase#getNodesToLock()
055: */
056: protected Node[] getNodesToLock() throws UsecaseException {
057: try {
058: List nodes = new ArrayList();
059: DocumentSet set = new DocumentSet();
060:
061: Document doc = getSourceDocument();
062: NodeSet subsite = SiteUtil.getSubSite(this .manager, doc
063: .getLink().getNode());
064: set.addAll(new DocumentSet(subsite.getDocuments()));
065:
066: Document[] documents = set.getDocuments();
067: for (int i = 0; i < documents.length; i++) {
068: nodes.add(documents[i].getRepositoryNode());
069: }
070:
071: Area live = doc.getPublication().getArea(
072: Publication.LIVE_AREA);
073: nodes.add(live.getSite().getRepositoryNode());
074: return (Node[]) nodes.toArray(new Node[nodes.size()]);
075:
076: } catch (Exception e) {
077: throw new UsecaseException(e);
078: }
079: }
080:
081: /**
082: * Checks if the workflow event is supported and the parent of the document
083: * exists in the live area.
084: *
085: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doCheckPreconditions()
086: */
087: protected void doCheckPreconditions() throws Exception {
088: super .doCheckPreconditions();
089: if (!hasErrors()) {
090:
091: String event = getEvent();
092: Document document = getSourceDocument();
093:
094: if (!document.getArea().equals(Publication.AUTHORING_AREA)) {
095: addErrorMessage("This usecase can only be invoked from the authoring area.");
096: return;
097: }
098:
099: UsecaseWorkflowHelper.checkWorkflow(this .manager, this ,
100: event, document, getLogger());
101: }
102: }
103:
104: /**
105: * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
106: */
107: protected void doExecute() throws Exception {
108: DocumentManager documentManager = null;
109: try {
110: Document authoringDocument = getSourceDocument();
111: if (authoringDocument.getResourceType().getName().equals(
112: "entry")) {
113: updateBlogEntry(authoringDocument);
114: }
115: updateFeed();
116: documentManager = (DocumentManager) this .manager
117: .lookup(DocumentManager.ROLE);
118: documentManager.copyToArea(authoringDocument,
119: Publication.LIVE_AREA);
120: WorkflowUtil.invoke(this .manager, getSession(),
121: getLogger(), authoringDocument, getEvent());
122: } catch (Exception e) {
123: throw new RuntimeException(e);
124: } finally {
125: if (documentManager != null) {
126: this .manager.release(documentManager);
127: }
128: }
129: }
130:
131: protected void updateFeed() throws Exception {
132:
133: Document[] docs = new Document[2];
134: org.w3c.dom.Document[] doms = new org.w3c.dom.Document[2];
135:
136: Publication pub = getSourceDocument().getPublication();
137: Area authoring = pub.getArea(Publication.AUTHORING_AREA);
138: Area live = pub.getArea(Publication.LIVE_AREA);
139: String path = "/feeds/all/index";
140: String language = pub.getDefaultLanguage();
141:
142: docs[0] = live.getSite().getNode(path).getLink(language)
143: .getDocument();
144: docs[1] = authoring.getSite().getNode(path).getLink(language)
145: .getDocument();
146:
147: DateFormat datefmt = new SimpleDateFormat(
148: "yyyy-MM-dd'T'HH:mm:ss");
149: DateFormat ofsfmt = new SimpleDateFormat("Z");
150: Date date = new Date();
151:
152: String dateofs = ofsfmt.format(date);
153: String datestr = datefmt.format(date) + dateofs.substring(0, 3)
154: + ":" + dateofs.substring(3, 5);
155:
156: for (int i = 0; i < 2; i++) {
157: doms[i] = DocumentHelper.readDocument(docs[i]
158: .getInputStream());
159: Element parent = doms[i].getDocumentElement();
160: // set modified date on publish
161: Element element = (Element) XPathAPI
162: .selectSingleNode(parent,
163: "/*[local-name() = 'feed']/*[local-name() = 'modified']");
164: DocumentHelper.setSimpleElementText(element, datestr);
165: DocumentHelper.writeDocument(doms[i], docs[i]
166: .getOutputStream());
167: }
168: }
169:
170: protected void updateBlogEntry(Document doc) throws Exception {
171: org.w3c.dom.Document dom = DocumentHelper.readDocument(doc
172: .getInputStream());
173: Element parent = dom.getDocumentElement();
174:
175: DateFormat datefmt = new SimpleDateFormat(
176: "yyyy-MM-dd'T'HH:mm:ss");
177: DateFormat ofsfmt = new SimpleDateFormat("Z");
178: Date date = new Date();
179:
180: String dateofs = ofsfmt.format(date);
181: String datestr = datefmt.format(date) + dateofs.substring(0, 3)
182: + ":" + dateofs.substring(3, 5);
183:
184: // set modified date on re-publish
185: Element element = (Element) XPathAPI
186: .selectSingleNode(parent,
187: "/*[local-name() = 'entry']/*[local-name() = 'modified']");
188: DocumentHelper.setSimpleElementText(element, datestr);
189:
190: // set issued date on first time publish
191: Workflowable dw = WorkflowUtil.getWorkflowable(this .manager,
192: this .getSession(), this .getLogger(), doc);
193: Version versions[] = dw.getVersions();
194: boolean wasLive = false;
195: for (int i = 0; i < versions.length; i++) {
196: if (versions[i].getValue("is_live")) {
197: wasLive = true;
198: break;
199: }
200: }
201: if (!wasLive) {
202: element = (Element) XPathAPI
203: .selectSingleNode(parent,
204: "/*[local-name() = 'entry']/*[local-name() = 'issued']");
205: DocumentHelper.setSimpleElementText(element, datestr);
206: }
207:
208: DocumentHelper.writeDocument(dom, doc.getOutputStream());
209: }
210:
211: /**
212: * @return The event to invoke.
213: */
214: private String getEvent() {
215: return "publish";
216: }
217:
218: }
|