This project has retired. For details please refer to its Attic page.
PropertyUtil 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.inmemory.types;
20  
21  import org.apache.chemistry.opencmis.commons.PropertyIds;
22  import org.apache.chemistry.opencmis.commons.data.ContentStream;
23  import org.apache.chemistry.opencmis.commons.data.PropertyData;
24  import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
25  import org.apache.chemistry.opencmis.commons.enums.Cardinality;
26  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Content;
27  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Document;
28  import org.apache.chemistry.opencmis.inmemory.storedobj.api.DocumentVersion;
29  import org.apache.chemistry.opencmis.inmemory.storedobj.api.Folder;
30  import org.apache.chemistry.opencmis.inmemory.storedobj.api.StoredObject;
31  import org.apache.chemistry.opencmis.inmemory.storedobj.api.VersionedDocument;
32  
33  public class PropertyUtil {
34      
35      public static Object getProperty(StoredObject so, String propertyId, PropertyDefinition<?> pd) {
36          ContentStream content = null;
37          DocumentVersion ver = null;
38          VersionedDocument verDoc = null;
39          Folder folder = null;
40          Document doc = null;
41  
42          if (so instanceof Content)
43              content = ((Content) so).getContent(0, 0);
44          if (so instanceof DocumentVersion)
45              ver = (DocumentVersion) so;
46          if (so instanceof VersionedDocument)
47              verDoc = (VersionedDocument) so;
48          if (so instanceof Folder)
49              folder = (Folder) so;
50          if (so instanceof Document)
51              doc = (Document) so;
52  
53          // generic properties:
54          if (propertyId.equals(PropertyIds.NAME)) {
55              return so.getName();
56          }
57          if (propertyId.equals(PropertyIds.OBJECT_ID)) {
58              return so.getId();
59          }
60          if (propertyId.equals(PropertyIds.OBJECT_TYPE_ID)) {
61              return so.getTypeId();
62          }
63          if (propertyId.equals(PropertyIds.BASE_TYPE_ID)) {
64              return null; // TOODO: return so.getBaseTypeId());
65          }
66          if (propertyId.equals(PropertyIds.CREATED_BY)) {
67              return so.getCreatedBy();
68          }
69          if (propertyId.equals(PropertyIds.CREATION_DATE)) {
70              return so.getCreatedAt();
71          }
72          if (propertyId.equals(PropertyIds.LAST_MODIFIED_BY)) {
73              return so.getModifiedBy();
74          }
75          if (propertyId.equals(PropertyIds.LAST_MODIFICATION_DATE)) {
76              return so.getModifiedAt();
77          }
78          if (propertyId.equals(PropertyIds.CHANGE_TOKEN)) {
79              return so.getChangeToken();
80          }
81  
82          if (ver != null) {
83              // get version related properties
84              // not support on a version, only on a versioned document:
85              // VERSION_SERIES_ID, IS_VERSION_SERIES_CHECKED_OUT,
86              // VERSION_SERIES_CHECKED_OUT_BY,
87              // VERSION_SERIES_CHECKED_OUT_ID, IS_LATEST_MAJOR_VERSION,
88              // IS_LATEST_VERSION
89              if (propertyId.equals(PropertyIds.IS_MAJOR_VERSION)) {
90                  return ver.isMajor();
91              }
92  
93              if (propertyId.equals(PropertyIds.CHECKIN_COMMENT)) {
94                  return ver.getCheckinComment();
95              }
96              if (propertyId.equals(PropertyIds.VERSION_LABEL)) {
97                  return ver.getVersionLabel();
98              }
99              if (propertyId.equals(PropertyIds.VERSION_SERIES_CHECKED_OUT_ID)) {
100                 return ver.isPwc() ? ver.getId() : null;
101             }
102         }
103 
104         // get versioned document related properties
105         if (verDoc != null) {
106             if (propertyId.equals(PropertyIds.VERSION_SERIES_ID)) {
107                 return verDoc.getId();
108             }
109             if (propertyId.equals(PropertyIds.IS_VERSION_SERIES_CHECKED_OUT)) {
110                 return verDoc.isCheckedOut();
111             }
112             if (propertyId.equals(PropertyIds.VERSION_SERIES_CHECKED_OUT_BY)) {
113                 return verDoc.getCheckedOutBy();
114             }
115         }
116 
117         // Set the content related properties
118         if (null != content) {
119             // omit: PropertyIds.CMIS_CONTENT_STREAM_ID
120             if (propertyId.equals(PropertyIds.CONTENT_STREAM_FILE_NAME)) {
121                 return content.getFileName();
122             }
123 
124             if (propertyId.equals(PropertyIds.CONTENT_STREAM_LENGTH)) {
125                 return content.getBigLength();
126             }
127             if (propertyId.equals(PropertyIds.CONTENT_STREAM_MIME_TYPE)) {
128                 return content.getMimeType();
129             }
130         }
131 
132         if (folder != null) {
133             // not supported: ALLOWED_CHILD_OBJECT_TYPE_IDS
134             if (propertyId.equals(PropertyIds.PARENT_ID)) {
135                 return folder.getParent() == null ? null : folder.getParent().getId();
136             }
137 
138             if (propertyId.equals(PropertyIds.PATH)) {
139                 return folder.getPath();
140             }
141         }
142 
143         if (doc != null) {
144             if (propertyId.equals(PropertyIds.IS_IMMUTABLE)) {
145                 return false;
146             }
147         }
148 
149        // try custom property:
150        PropertyData<?> lVal = so.getProperties().get(propertyId);
151        if (null == lVal)
152            return null;
153        else if (pd.getCardinality() == Cardinality.SINGLE) {
154            return lVal.getFirstValue();
155        } else {
156            return lVal.getValues();
157        }
158     }
159 
160 }