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.impl.jcr.command;
023:
024: import org.apache.jackrabbit.value.StringValue;
025: import org.jboss.portal.cms.impl.jcr.JCRCommand;
026: import org.jboss.portal.cms.impl.jcr.util.VersionUtil;
027: import org.jboss.portal.cms.model.Content;
028: import org.jboss.portal.common.invocation.Scope;
029: import org.jboss.portal.identity.User;
030:
031: import javax.jcr.Node;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * Creates a new version of the content and labels it "LIVE".
037: *
038: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
039: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
040: * @author Sohil Shah - sohil.shah@jboss.com
041: */
042: public class ContentCreateNewVersionCommand extends JCRCommand {
043:
044: /** The serialVersionUID */
045: private static final long serialVersionUID = -4055346144913229565L;
046:
047: List mContents = new ArrayList();
048:
049: boolean bMakeLive;
050:
051: /**
052: * @param content
053: * @param bMakeLive Whether this updated version should be made "LIVE"
054: */
055: public ContentCreateNewVersionCommand(Content content,
056: boolean bMakeLive) {
057: this .mContents.add(content);
058: this .bMakeLive = bMakeLive;
059: }
060:
061: public ContentCreateNewVersionCommand(List contents,
062: boolean bMakeLive) {
063: this .mContents = contents;
064: this .bMakeLive = bMakeLive;
065: }
066:
067: /**
068: *
069: */
070: public Object execute() {
071: try {
072: Scope scope = this .context.scope;
073:
074: // create versions
075: for (int i = 0; i < mContents.size(); i++) {
076: Content content = (Content) mContents.get(i);
077: Node contentNode = (Node) context.getSession().getItem(
078: content.getBasePath());
079:
080: // if the creator of this content is known, record it
081: User user = (User) this .context.getAttribute(scope,
082: "user");
083: if (user != null) {
084: contentNode.setProperty("portalcms:user", user
085: .getUserName());
086: }
087:
088: contentNode.setProperty("portalcms:size",
089: new StringValue(String.valueOf(content
090: .getBytes().length)));
091: context.getSession().save();
092:
093: String processId = (String) context.getAttribute(scope,
094: "processid");
095:
096: // integration of publish/approval workflow
097: if (this .bMakeLive && processId != null
098: && processId.trim().length() != 0) {
099: // now save workflow related meta data on this version
100: // so that this version can be processed later in the execution
101: // of the workflow
102: contentNode.setProperty("portalcms:processid",
103: processId);
104:
105: // save
106: context.getSession().save();
107:
108: // create a new version, but dont make it live
109: VersionUtil.createVersion(contentNode, false);
110: } else {
111: // remove any processid if they are present on the node
112: contentNode.setProperty("portalcms:processid",
113: (String) null);
114: contentNode.save();
115:
116: // not asking to publish the content...no workflow needed
117: // even if its activated
118: VersionUtil.createVersion(contentNode,
119: this .bMakeLive);
120: }
121: }
122:
123: context.getSession().save();
124: } catch (Exception e) {
125: e.printStackTrace();
126: throw new RuntimeException(e);
127: }
128: return null;
129: }
130: }
|