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.jboss.portal.cms.impl.FileImpl;
025: import org.jboss.portal.cms.impl.jcr.JCRCommand;
026: import org.jboss.portal.cms.model.Content;
027: import org.jboss.portal.cms.model.File;
028: import org.jboss.portal.cms.util.FileUtil;
029:
030: import javax.jcr.Node;
031: import javax.jcr.Session;
032: import java.util.Locale;
033:
034: /**
035: * Retrieves a file and its versioned, localized content.
036: *
037: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
038: */
039: public class FileGetCommand extends JCRCommand {
040: /** The serialVersionUID */
041: private static final long serialVersionUID = 7912459435886700650L;
042: String path;
043: String versionNumber;
044: Locale locale;
045:
046: /**
047: * Retrieves the "LIVE" version of the file. Useful for serving content.
048: *
049: * @param sPath Path of *original* file.
050: */
051: public FileGetCommand(String sPath, Locale locale) {
052: this (sPath, null, locale);
053: }
054:
055: /**
056: * Use this constructor for retrieveing a specific version of the file.
057: *
058: * @param sPath Path of *original* file.
059: * @param sVersionNumber Identifier used to retrieve the specific version.
060: * @param locale Locale of content to be retrieved.
061: */
062: public FileGetCommand(String sPath, String sVersionNumber,
063: Locale locale) {
064: this .path = sPath;
065: this .versionNumber = sVersionNumber;
066: this .locale = locale;
067: }
068:
069: public String getPath() {
070: return path;
071: }
072:
073: public String getVersionNumber() {
074: return versionNumber;
075: }
076:
077: public Locale getLocale() {
078: return locale;
079: }
080:
081: public Object execute() {
082: try {
083: // build File
084: File file = new FileImpl();
085: Session session = context.getSession();
086:
087: boolean exists = session.itemExists(FileUtil
088: .cleanDoubleSlashes(this .path + "/"
089: + this .locale.getLanguage()));
090: if (!exists) {
091: return null;
092: }
093:
094: Node fileNode = (Node) session.getItem(this .path);
095: fileNode = (Node) session.getItem(this .path);
096: file.setCreationDate(fileNode.getProperty("jcr:created")
097: .getDate().getTime());
098: file.setLastModified(fileNode.getProperty(
099: "jcr:lastModified").getDate().getTime());
100: file.setBasePath(this .path);
101: file.setName(fileNode.getName());
102:
103: // get content
104: Content content = null;
105: try {
106: JCRCommand contentGetCMD = (JCRCommand) context
107: .getCommandFactory().createContentGetCommand(
108: this .path, this .versionNumber,
109: this .locale);
110: content = (Content) context.execute(contentGetCMD);
111: file.setContent(this .locale, content);
112: } catch (Exception ignore) {
113: // ignore, we let the streamcommand handle these.
114: }
115:
116: return file;
117: } catch (Exception e) {
118: e.printStackTrace();
119: }
120: return null;
121: }
122:
123: public String toString() {
124: return "FileGetCommand[path=" + path + ", versionNumber="
125: + versionNumber + ", locale=" + locale + "]";
126: }
127: }
|