This project has retired. For details please refer to its Attic page.
DefaultDocumentTypeHandler xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.jcr.impl;
20  
21  import org.apache.chemistry.opencmis.commons.data.ContentStream;
22  import org.apache.chemistry.opencmis.commons.data.Properties;
23  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
24  import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
25  import org.apache.chemistry.opencmis.commons.enums.ContentStreamAllowed;
26  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
27  import org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException;
28  import org.apache.chemistry.opencmis.commons.impl.dataobjects.DocumentTypeDefinitionImpl;
29  import org.apache.chemistry.opencmis.jcr.JcrBinary;
30  import org.apache.chemistry.opencmis.jcr.JcrDocument;
31  import org.apache.chemistry.opencmis.jcr.JcrFolder;
32  import org.apache.chemistry.opencmis.jcr.JcrNode;
33  import org.apache.chemistry.opencmis.jcr.JcrTypeManager;
34  import org.apache.chemistry.opencmis.jcr.JcrVersion;
35  import org.apache.chemistry.opencmis.jcr.JcrVersionBase;
36  import org.apache.chemistry.opencmis.jcr.query.IdentifierMap;
37  import org.apache.chemistry.opencmis.jcr.type.JcrDocumentTypeHandler;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  import javax.jcr.Binary;
42  import javax.jcr.Node;
43  import javax.jcr.Property;
44  import javax.jcr.RepositoryException;
45  import javax.jcr.nodetype.NodeType;
46  import javax.jcr.version.Version;
47  import javax.jcr.version.VersionManager;
48  import java.io.BufferedInputStream;
49  import java.io.IOException;
50  
51  /**
52   * Type handler that provides cmis:document.
53   */
54  public class DefaultDocumentTypeHandler extends AbstractJcrTypeHandler implements JcrDocumentTypeHandler {
55  
56      private static final Log log = LogFactory.getLog(JcrFolder.class);
57  
58      public String getTypeId() {
59          return BaseTypeId.CMIS_DOCUMENT.value();
60      }
61  
62      public TypeDefinition getTypeDefinition() {
63          DocumentTypeDefinitionImpl documentType = new DocumentTypeDefinitionImpl();
64          documentType.setBaseTypeId(BaseTypeId.CMIS_DOCUMENT);
65          documentType.setIsControllableAcl(false);
66          documentType.setIsControllablePolicy(false);
67          documentType.setIsCreatable(true);
68          documentType.setDescription("Document");
69          documentType.setDisplayName("Document");
70          documentType.setIsFileable(true);
71          documentType.setIsFulltextIndexed(false);
72          documentType.setIsIncludedInSupertypeQuery(true);
73          documentType.setLocalName("Document");
74          documentType.setLocalNamespace(JcrTypeManager.NAMESPACE);
75          documentType.setIsQueryable(true);
76          documentType.setQueryName(JcrTypeManager.DOCUMENT_TYPE_ID);
77          documentType.setId(JcrTypeManager.DOCUMENT_TYPE_ID);
78          documentType.setIsVersionable(true);
79          documentType.setContentStreamAllowed(ContentStreamAllowed.ALLOWED);
80  
81          JcrTypeManager.addBasePropertyDefinitions(documentType);
82          JcrTypeManager.addDocumentPropertyDefinitions(documentType);
83  
84          return documentType;
85      }
86  
87      public IdentifierMap getIdentifierMap() {
88          return new DefaultDocumentIdentifierMap(true);
89      }
90  
91      public JcrDocument getJcrNode(Node node) throws RepositoryException {
92          VersionManager versionManager = node.getSession().getWorkspace().getVersionManager();
93          Version version = versionManager.getBaseVersion(node.getPath());
94          return new JcrVersion(node, version, typeManager, pathManager, typeHandlerManager);
95      }
96  
97      public boolean canHandle(Node node) throws RepositoryException {
98          return node.isNodeType(NodeType.NT_FILE) && node.isNodeType(NodeType.MIX_SIMPLE_VERSIONABLE);
99      }
100 
101     public JcrNode createDocument(JcrFolder parentFolder, String name, Properties properties, ContentStream contentStream, VersioningState versioningState) {
102         try {
103             Node fileNode = parentFolder.getNode().addNode(name, NodeType.NT_FILE);
104             if (versioningState != VersioningState.NONE) {
105                 fileNode.addMixin(NodeType.MIX_SIMPLE_VERSIONABLE);
106             }
107 
108             Node contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
109             contentNode.addMixin(NodeType.MIX_CREATED);
110 
111             // compile the properties
112             JcrFolder.setProperties(contentNode, getTypeDefinition(), properties);
113 
114             // write content, if available
115             Binary binary = contentStream == null || contentStream.getStream() == null
116                     ? JcrBinary.EMPTY
117                     : new JcrBinary(new BufferedInputStream(contentStream.getStream()));
118             try {
119                 contentNode.setProperty(Property.JCR_DATA, binary);
120                 if (contentStream != null && contentStream.getMimeType() != null) {
121                     contentNode.setProperty(Property.JCR_MIMETYPE, contentStream.getMimeType());
122                 }
123             }
124             finally {
125                 binary.dispose();
126             }
127 
128             fileNode.getSession().save();
129             JcrNode jcrFileNode = getJcrNode(fileNode);
130             if (versioningState == VersioningState.NONE) {
131                 return jcrFileNode;
132             }
133 
134             JcrVersionBase jcrVersion = jcrFileNode.asVersion();
135             if (versioningState == VersioningState.MINOR || versioningState == VersioningState.MAJOR) {
136                 return jcrVersion.checkin(null, null, "auto checkin");
137             } else {
138                 return jcrVersion.getPwc();
139             }
140         }
141         catch (RepositoryException e) {
142             log.debug(e.getMessage(), e);
143             throw new CmisStorageException(e.getMessage(), e);
144         }
145         catch (IOException e) {
146             log.debug(e.getMessage(), e);
147             throw new CmisStorageException(e.getMessage(), e);
148         }
149     }
150 }