001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.cms.workflow;
023:
024: import org.apache.log4j.Logger;
025: import org.jboss.portal.cms.CMS;
026: import org.jboss.portal.cms.Command;
027: import org.jboss.portal.cms.impl.interceptors.ACLInterceptor;
028: import org.jboss.portal.cms.impl.jcr.JCRCMS;
029: import org.jboss.portal.cms.model.File;
030: import org.jbpm.graph.def.ActionHandler;
031: import org.jbpm.graph.exe.ExecutionContext;
032:
033: import javax.naming.InitialContext;
034: import java.util.List;
035: import java.util.Locale;
036:
037: /**
038: * Created on : Dec 20, 2006
039: *
040: * @author Sohil Shah - sohil.shah@jboss.com
041: */
042: public class FinalizePublish implements ActionHandler {
043: /**
044: *
045: */
046: private static Logger log = Logger.getLogger(FinalizePublish.class);
047:
048: /**
049: *
050: */
051: public void execute(ExecutionContext executionContext) {
052: Content content = (Content) executionContext
053: .getContextInstance().getVariable("content");
054: boolean approved = ((Boolean) executionContext
055: .getContextInstance().getVariable("approved"))
056: .booleanValue();
057: String modifiedContent = (String) executionContext
058: .getContextInstance().getVariable("modifiedContent");
059: long processId = executionContext.getProcessInstance().getId();
060: CMS cms = this .getCMSService();
061:
062: if (approved) {
063: ACLInterceptor.turnOff();
064: JCRCMS.turnOffWorkflow();
065:
066: //create this content in the CMS and make it live
067: int lastIndex = content.getPath().lastIndexOf('/');
068: String path = content.getPath().substring(0, lastIndex);
069: String language = content.getPath()
070: .substring(lastIndex + 1);
071:
072: Command command = cms.getCommandFactory()
073: .createContentGetVersionsCommand(content.getPath());
074: List versions = (List) cms.execute(command);
075: org.jboss.portal.cms.model.Content processContent = this
076: .findProcessContent(processId, versions);
077:
078: /**
079: * if processContent is null, a pending version of the content was not found
080: * this could be due to an outdated link. let the process finish, but nothing
081: * new will be published
082: */
083: if (processContent != null) {
084: //get the file to be updated and published live
085: command = cms.getCommandFactory().createFileGetCommand(
086: path, processContent.getVersionNumber(),
087: new Locale(language));
088: File file = (File) cms.execute(command);
089:
090: if (modifiedContent != null
091: && modifiedContent.trim().length() > 0) {
092: file.getContent().setBytes(
093: modifiedContent.getBytes());
094: }
095:
096: //now publish this
097: file.getContent().setApprovalProcessId(null);
098: file.getContent().setWaitingForPublishApproval(false);
099: command = cms.getCommandFactory()
100: .createUpdateFileCommand(file,
101: file.getContent(), true);
102: cms.execute(command);
103: }
104:
105: JCRCMS.turnOnWorkflow();
106: ACLInterceptor.turnOn();
107: }
108: }
109:
110: /**
111: * @param processId
112: * @param versions
113: * @return
114: */
115: private org.jboss.portal.cms.model.Content findProcessContent(
116: long processId, List versions) {
117: org.jboss.portal.cms.model.Content content = null;
118:
119: if (versions != null) {
120: for (int i = 0; i < versions.size(); i++) {
121: org.jboss.portal.cms.model.Content cour = (org.jboss.portal.cms.model.Content) versions
122: .get(i);
123: if (cour.isWaitingForPublishApproval()) {
124: if (cour.getApprovalProcessId() != null
125: && cour.getApprovalProcessId().trim()
126: .equals(String.valueOf(processId))) {
127: //not if found...the content of this version should be published to go live
128: return cour;
129: }
130: }
131: }
132: }
133:
134: return content;
135: }
136:
137: /** @return */
138: private CMS getCMSService() {
139: try {
140: return (CMS) new InitialContext()
141: .lookup("java:/portal/CMS");
142: } catch (Exception e) {
143: throw new RuntimeException(e);
144: }
145: }
146: }
|