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.BinaryValue;
025: import org.apache.jackrabbit.value.DateValue;
026: import org.jboss.portal.cms.CMSMimeMappings;
027: import org.jboss.portal.cms.impl.jcr.JCRCommand;
028: import org.jboss.portal.cms.model.File;
029:
030: import javax.jcr.Node;
031:
032: import java.util.Calendar;
033:
034: /** @author <a href="mailto:roy@jboss.org">Roy Russo</a> */
035: public class FileUpdateCommand extends JCRCommand {
036: /** The serialVersionUID */
037: private static final long serialVersionUID = 882238623005109537L;
038: File mFile;
039: boolean bMakeLive;
040:
041: /**
042: * Updates a given file content in the repo, creating a new version.
043: *
044: * @param file File obj to update
045: */
046: public FileUpdateCommand(File file) {
047: this .mFile = file;
048: }
049:
050: public Object execute() {
051: try {
052: Node contentNode = (Node) context.getSession().getItem(
053: mFile.getContent().getBasePath());
054: contentNode.checkout();
055:
056: DateValue timestamp = new DateValue(Calendar.getInstance());
057:
058: contentNode.setProperty("portalcms:description", mFile
059: .getContent().getDescription());
060: contentNode.setProperty("portalcms:title", mFile
061: .getContent().getTitle());
062: contentNode.setProperty("portalcms:language", mFile
063: .getContent().getLocale().getLanguage());
064: contentNode.setProperty("jcr:encoding", "UTF-8");
065: contentNode.setProperty("jcr:data", new BinaryValue(mFile
066: .getContent().getBytes()));
067: contentNode.setProperty("jcr:lastModified", timestamp);
068:
069: if (mFile.getContent().getMimeType() != null) {
070: contentNode.setProperty("jcr:mimeType", mFile
071: .getContent().getMimeType());
072: } else {
073: String fileExt = mFile.getBasePath().substring(
074: mFile.getBasePath().lastIndexOf(".") + 1,
075: mFile.getBasePath().length());
076: CMSMimeMappings mapper = new CMSMimeMappings();
077: if (mapper.getMimeType(fileExt) != null) {
078: contentNode.setProperty("jcr:mimeType", mapper
079: .getMimeType(fileExt));
080: } else {
081: contentNode.setProperty("jcr:mimeType",
082: "application/octet-stream");
083: }
084: }
085:
086: //Update the lastModified Property of the FileNode of this content
087: Node fileNode = (Node) context.getSession().getItem(
088: mFile.getBasePath());
089: fileNode.setProperty("jcr:lastModified", timestamp);
090:
091: // Update the folder modified date
092: fileNode.getParent().setProperty("portalcms:lastmodified",
093: timestamp);
094:
095: contentNode.getSession().save();
096: } catch (Exception e) {
097: e.printStackTrace();
098: }
099: return null;
100: }
101:
102: }
|