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 java.util.List;
025: import java.util.Locale;
026:
027: import javax.naming.InitialContext;
028:
029: import org.jboss.portal.cms.CMS;
030: import org.jboss.portal.cms.Command;
031: import org.jboss.portal.cms.model.Content;
032: import org.jboss.portal.cms.model.File;
033: import org.jboss.portal.cms.impl.interceptors.ACLInterceptor;
034:
035: /**
036: * @author Sohil Shah - sshah@redhat.com
037: */
038: public class CMSWorkflowUtil {
039: /**
040: *
041: * @return
042: */
043: public static Content getPendingContent(long processId,
044: String contentPath) {
045: Content content = null;
046:
047: int lastIndex = contentPath.lastIndexOf('/');
048: String path = contentPath.substring(0, lastIndex);
049: String language = contentPath.substring(lastIndex + 1);
050:
051: ACLInterceptor.turnOff();
052:
053: CMS cms = CMSWorkflowUtil.getCMSService();
054: Command command = cms.getCommandFactory()
055: .createContentGetVersionsCommand(contentPath);
056: List versions = (List) cms.execute(command);
057:
058: content = CMSWorkflowUtil.findProcessContent(processId,
059: versions);
060: if (content != null) {
061: //fetch the content
062: command = cms.getCommandFactory().createFileGetCommand(
063: path, content.getVersionNumber(),
064: new Locale(language));
065: File file = (File) cms.execute(command);
066:
067: content = file.getContent();
068: }
069:
070: ACLInterceptor.turnOn();
071:
072: return content;
073: }
074:
075: /**
076: * @param processId
077: * @param versions
078: * @return
079: */
080: private static Content findProcessContent(long processId,
081: List versions) {
082: Content content = null;
083:
084: if (versions != null) {
085: for (int i = 0; i < versions.size(); i++) {
086: Content cour = (org.jboss.portal.cms.model.Content) versions
087: .get(i);
088: if (cour.isWaitingForPublishApproval()) {
089: if (cour.getApprovalProcessId() != null
090: && cour.getApprovalProcessId().trim()
091: .equals(String.valueOf(processId))) {
092: //not if found...the content of this version should be published to go live
093: return cour;
094: }
095: }
096: }
097: }
098:
099: return content;
100: }
101:
102: /**
103: *
104: * @return
105: */
106: private static CMS getCMSService() {
107: try {
108: return (CMS) new InitialContext()
109: .lookup("java:/portal/CMS");
110: } catch (Exception e) {
111: throw new RuntimeException(e);
112: }
113: }
114: }
|