01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.cms.impl.jcr.command;
23:
24: import org.apache.jackrabbit.value.BinaryValue;
25: import org.apache.jackrabbit.value.DateValue;
26: import org.apache.jackrabbit.value.StringValue;
27: import org.jboss.portal.cms.CMSMimeMappings;
28: import org.jboss.portal.cms.impl.jcr.JCRCommand;
29: import org.jboss.portal.cms.impl.jcr.JCRCommandContext;
30: import org.jboss.portal.cms.model.File;
31:
32: import javax.jcr.Node;
33: import java.util.Calendar;
34:
35: /**
36: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
37: * @author <a href="mailto:theute@jboss.org">Thomas Heute</a>
38: */
39: public class ContentCreateCommand extends JCRCommand {
40: /** The serialVersionUID */
41: private static final long serialVersionUID = -2843288770902185840L;
42: File mFile;
43:
44: public ContentCreateCommand(File file) {
45: this .mFile = file;
46: }
47:
48: public Object execute() {
49: try {
50: JCRCommandContext context = (JCRCommandContext) getContext();
51: Node fileNode = (Node) context.getSession().getItem(
52: mFile.getBasePath());
53:
54: Node contentNode = fileNode.addNode(mFile.getContent()
55: .getLocale().getLanguage(), "portalcms:content");
56: contentNode.setProperty("jcr:encoding", "UTF-8");
57: contentNode.setProperty("jcr:data", new BinaryValue(mFile
58: .getContent().getBytes()));
59: contentNode.setProperty("jcr:lastModified", new DateValue(
60: Calendar.getInstance()));
61: contentNode
62: .setProperty("portalcms:description",
63: new StringValue(mFile.getContent()
64: .getDescription()));
65: contentNode.setProperty("portalcms:title", new StringValue(
66: mFile.getContent().getTitle()));
67: contentNode.setProperty("portalcms:language",
68: new StringValue(mFile.getContent().getLocale()
69: .getLanguage()));
70:
71: if (mFile.getContent().getMimeType() != null) {
72: contentNode.setProperty("jcr:mimeType", mFile
73: .getContent().getMimeType());
74: } else {
75: String fileExt = mFile.getBasePath().substring(
76: mFile.getBasePath().lastIndexOf(".") + 1,
77: mFile.getBasePath().length());
78: CMSMimeMappings mapper = new CMSMimeMappings();
79: if (mapper.getMimeType(fileExt) != null) {
80: contentNode.setProperty("jcr:mimeType", mapper
81: .getMimeType(fileExt));
82: } else {
83: contentNode.setProperty("jcr:mimeType",
84: "application/octet-stream");
85: }
86: }
87: contentNode.addMixin("mix:versionable");
88:
89: // Save this for other sessions.
90: context.getSession().save();
91: } catch (Exception e) {
92: e.printStackTrace();
93: }
94: return null;
95: }
96: }
|